简体   繁体   中英

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. This is the code thus far (Sorry in advance for the spacing, stackoverflow is formatting it strangely for some reason):

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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