is there any command to find the standard error of the mean in R
+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
2010-04-20 16:18:41
+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
2010-04-20 19:03:07
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
2010-04-20 22:52:18
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
2010-04-21 07:26:57
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
2010-04-29 21:34:23
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
2010-04-21 06:51:15