我想寫一個函數,創建動畫圖形(不使用動畫包),用戶可以控制輸入(樣本大小和分佈等.. )證明了中心極限定理。這在理論上是我想要的,但是在編寫用戶可以實際控制輸入的功能時遇到問題,正如我上面提到的那樣。如何寫一個函數,演示中心極限定理與圖形
msample <- NA # set up empty vector
ns <-3 # sample size
for(i in 1:500){
sam <- runif(ns) * 10 # draw sample
msample[i] <- mean(sam) # save mean of sample
h <- hist(msample, breaks=seq(0,10, len=50), # histogram of all means
xlim=c(0,10), col=grey(.9),
xlab="", main="Central Limit Theorem", border="blue", las=1)
points(sam, rep(max(h$count), length(sam)),
pch=16, col=grey(.2)) # add sampled values
points(msample[i], max(h$count), # add sample mean value
col="red", pch=15)
text(10, max(h$count), paste("sample no", i))
hist(msample[i], breaks=seq(0,10, len=50), # ovelay sample mean
xlim=c(0,10), col="red", add=T, # in histogram
xlab="", border="white", las=1)
Sys.sleep(.05)
}
什麼不適合你?期望的結果是什麼? – 2013-04-18 05:27:43
+1爲好的教育例子。我想你正在尋找'readline()'函數。只是不要忘記做一些輸入驗證。 – 2013-04-18 06:01:12
@RomanLuštrik期望的結果就是具有用戶可以控制輸入的整體功能。 – 2013-04-19 17:18:23