简体   繁体   English

统计思维:将for循环转换为重复函数并使用R存储输出

[英]Statistical Thinking: Convert a for loop to a repeat function and store output using R

I'm attempting to do a simple simulation in order to find the sample distribution of my sample average. 我试图做一个简单的模拟,以便找到样本平均值的样本分布。 I'd like to be able to write it strictly using the repeat function and removing the for loop altogether. 我希望能够严格使用repeat函数并完全删除for循环来编写它。 This is the code thus far (Sorry in advance for the spacing, stackoverflow is formatting it strangely for some reason): 到目前为止,这是代码(对不起,对于空格,由于某些原因,stackoverflow对其进行了奇怪的格式化):

X.samp <- sample(pop.1$height,100)
X.bar <- mean(X.samp)
X.bar <- rep(0,10^5)
for (i in 1:10^5) {
   X.samp <- sample(pop.1$height,100)
   X.bar[i] <- mean(X.samp)
}
hist(X.bar)

Something like this? 像这样吗

 dat <- rnorm(1000) # some data
 res <- replicate(1000, mean(sample(dat, 100)))
 hist(res)

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

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