tags:

views:

155

answers:

4

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

+1  A: 

There's the plotrix package with has a built-in function for this: std.error

Matt Ball
+7  A: 

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
Ian Fellows
+4  A: 

probably more efficient to use var... since you actually sqrt twice in your code, once to get the sd (code for sd is in r and revealed by just typing "sd")...

stderr <- function(x) sqrt(var(x)/length(x))
John
Interestingly, your function and Ian's are nearly identically fast. I tested them both 1000 times against 10^6 million rnorm draws (not enough power to push them harder than that). Conversely, plotrix's function was always slower than even the slowest runs of those two functions - but it also has a lot more going on under the hood.
Matt Parker
That is likely because the advantage of John's function avoids one function call and calculating the square root of a single number. If there is any advantage at all, it is at very small n.
Ian Fellows
Not just likely but true... I did timing runs of rnorm(5) but did 5e4 executions of the se command. My version takes about 2.5s while the one with two sqrts takes about 2.8. A small speed up indeed.(I did several runs and am reporting a rounded average)
John
A: 

more generally, for standard errors on any other parameter, you can use the boot package for bootstrap simulations (or write them on your own)

owen