如何复制描述R中均值标准误差的图形?

时间:2017-01-11 11:10:28

标签: r plot standard-error

link中的第一个数字显示了一个非常好的例子,说明如何可视化标准错误,我想在R中复制它。

我带着以下

到达那里
set.seed(1)
pop<-rnorm(1000,175,10)
mean(pop)
hist(pop)
#-------------------------------------------
# Plotting Standard Error for small Samples 
#-------------------------------------------
smallSample <- replicate(10,sample(pop,3,replace=TRUE)) ; smallSample
smallMeans<-colMeans(smallSample)
par(mfrow=c(1,2))
x<-c(1:10)
plot(x,smallMeans,ylab="",xlab = "",pch=16,ylim = c(150,200))
abline(h=mean(pop))
#-------------------------------------------
# Plotting Standard Error for Large Samples 
#-------------------------------------------
largeSample <- replicate(10,sample(pop,20,replace=TRUE)) 
largeMeans<-colMeans(largeSample)
x<-c(1:10)
plot(x,largeMeans,ylab="",xlab = "",pch=16,ylim = c(150,200))
abline(h=mean(pop))

但我不确定如何使用X符号绘制原始数据。谢谢。

1 个答案:

答案 0 :(得分:1)

使用基础绘图,您需要使用arrows功能。 在R中没有计算标准错误的函数(ASAIK),所以试试这个

sem <- function(x){
     sd(x) / sqrt(length(x))
}

绘图(对于x符号使用pch = 4)

plot(x, largeMeans, ylab = "", xlab = "", pch = 4, ylim = c(150,200))
abline(h = mean(pop))
arrows(x0 = 1:10, x1 = 1:10, y0 = largeMeans - sem(largeSample) * 5, largeMeans + sem(largeSample) * 5, code = 0)

注意:您提供的数据中的SE非常小,所以我将它们乘以5以使它们更明显

修改

啊,为了绘制所有点,那么?matplot?matpoints可能会有所帮助吗?类似的东西:

matplot(t(largeSample), ylab = "", xlab = "", pch = 4, cex = 0.6, col = 1)
abline(h = mean(pop))
points(largeMeans, pch = 19, col = 2)

这更像是你追求的效果吗?