繁体   English   中英

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

[英]How to replicate a figure describing standard error of the mean in R?

链接中的第一个图显示了一个很好的示例,它展示了如何可视化标准错误,我想在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符号绘制原始数据。 谢谢。

使用基本绘图时,您需要使用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 ,绘制所有点,然后?matplot?matpoints会有所帮助吗? 就像是:

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

这是您所追求的效果吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM