2017-01-11 29 views
0

link第一張圖顯示了這裏的如何可視化標準錯誤,我想複製一個非常好的例子,在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符號一樣。謝謝。

+0

嘗試使用'ggplot2'軟件包。基本的是'ggplot(data.frame(x,smallMeans),aes(x = x,y = smallMeans))+ geom_point()+ geom_line()+ geom_hline(yintercept = mean(pop))' – Sotos

+0

感謝你的迴應。這接近我迄今爲止所做的(請參閱更新的代碼),但我希望能夠覆蓋原始數據點 –

回答

1

使用基底繪圖,您需要使用arrows函數。 就R沒有功能(ASAIK),計算標準錯誤,以便嘗試這種

圖(用PCH = 4的X符號)

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) 

這是您效仿之後的效果嗎?

+0

謝謝。這與我之後的情況非常接近,但我寧願繪製原始數據點而不是標準誤差線。 –

相關問題