【问题标题】:How to replicate a figure describing standard error of the mean in R?如何复制描述R中平均值标准误差的图形?
【发布时间】:2017-01-11 11:10:28
【问题描述】:

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 符号的原始数据。谢谢。

【问题讨论】:

  • 试试ggplot2 包。一个基本的应该是ggplot(data.frame(x, smallMeans), aes(x=x, y = smallMeans))+geom_point()+geom_line()+geom_hline(yintercept = mean(pop))
  • 感谢您的回复。这与我到目前为止所做的很接近(请参阅更新的代码),但我希望能够覆盖原始数据点

标签: r plot standard-error


【解决方案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)

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

【讨论】:

  • 谢谢。这与我所追求的非常接近,但我更愿意绘制原始数据点而不是标准误差线。
猜你喜欢
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 2020-12-26
相关资源
最近更新 更多