シミュレーションのあたり
library(ggplot2)
## Need help? Try the ggplot2 mailing list:
## http://groups.google.com/group/ggplot2.
library(compiler)
age <- data.frame(age = c(23, 25, 27, 27, 29, 26, 31, 31, 28, 29, 23, 26, 29,
30, 30, 27, 26))
getMean <- cmpfun(function(times, s = 10) {
sample_mean <- numeric(times)
for (i in 1:times) {
res <- sample(age$age, size = s, replace = TRUE)
sample_mean[i] <- mean(res)
}
return(sample_mean)
})
res1 <- getMean(1e+06)
ggplot(data.frame(res1), aes(x = res1)) + geom_histogram(binwidth = 0.1, colour = "white") +
geom_vline(x = mean(res1), color = "red") + annotate(x = 26, y = 40000,
geom = "text", label = paste(sep = ":", "mean", round(mean(res1), 1))) +
annotate(x = 26, y = 30000, geom = "text", label = paste(sep = ":", "sd",
round(sd(res1), 3))) + xlim(24, 32)
res2 <- getMean(1e+06, s = 1000)
ggplot(data.frame(res2), aes(x = res2)) + geom_histogram(binwidth = 0.1, colour = "white") +
geom_vline(x = mean(res2), color = "red") + annotate(x = 26, y = 4e+05,
geom = "text", label = paste(sep = ":", "mean", round(mean(res2), 1))) +
annotate(x = 26, y = 3e+05, geom = "text", label = paste(sep = ":", "sd",
round(sd(res2), 3))) + xlim(24, 32)