Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, how to find the standard error of the mean?

Is there any command to find the standard error of the mean in R?

like image 547
alex Avatar asked Apr 20 '10 15:04

alex


2 Answers

The standard error is just the standard deviation divided by the square root of the sample size. So you can easily make your own function:

> std <- function(x) sd(x)/sqrt(length(x)) > std(c(1,2,3,4)) [1] 0.6454972 
like image 171
Ian Fellows Avatar answered Sep 29 '22 14:09

Ian Fellows


The standard error (SE) is just the standard deviation of the sampling distribution. The variance of the sampling distribution is the variance of the data divided by N and the SE is the square root of that. Going from that understanding one can see that it is more efficient to use variance in the SE calculation. The sd function in R already does one square root (code for sd is in R and revealed by just typing "sd"). Therefore, the following is most efficient.

se <- function(x) sqrt(var(x)/length(x)) 

in order to make the function only a bit more complex and handle all of the options that you could pass to var, you could make this modification.

se <- function(x, ...) sqrt(var(x, ...)/length(x)) 

Using this syntax one can take advantage of things like how var deals with missing values. Anything that can be passed to var as a named argument can be used in this se call.

like image 32
John Avatar answered Sep 29 '22 14:09

John