我想创建一个动画来显示样本大小对样本均值和标准误差的影响。我得到了这段代码。但是,R在运行这段代码时遇到了致命错误
https://aiexplorations.in/2015/09/05/animated-mean-and-sample-size/
library(animation)
#setting GIF options
ani.options(interval = 0.12, ani.width = 480, ani.height = 320)
#package containing saveGIF function
library(animation)
#setting GIF options
ani.options(interval = 0.12, ani.width = 480, ani.height = 320)
mu = 10.0
sd = 1.0
#a function to help us call GIF plots easily
plo <- function(samplesize, iter = 100){
for (i in seq(1,iter)){
#Generating a sample from the normal distribution
x <- rnorm(samplesize,mean=mu,sd=sd)
#Histogram of samples as they're generated
hist(x, main = paste("N = ",samplesize,", xbar = ",round(mean(x), digits = 2),
", s = ",round(sd(x),digits = 2)), xlim = c(5,15),
ylim = c(0,floor(samplesize/3)), breaks = seq(4,16,0.5), col = rgb(0.1,0.9,0.1,0.2),
border = "grey", xlab = "x (Gaussian sample)")
#Adding the estimate of the mean line to the histogram
abline(v = mean(x), col = "red", lw = 2 )
}
}
#Setting the parameters for the distribution
for (i in c(10,50,100,500,1000,10000)){
saveGIF({plo(i,mu,sd)},movie.name = paste("N=",i,", mu=",mu,", sd=",sd,".gif"))
}
但是我不知道怎么用“gganimate”。任何帮助都将不胜感激
1条答案
按热度按时间ru9i0ody1#
也许你可以从这个开始,它从2个for循环中的DF开始,3个样本大小分别是10、50和100。
i
是重复的次数。mean
和sd
取自你的代码。在ggplot部分,DF被分组为
n
和i
,并计算这些组的平均值-这将在稍后创建平均值的垂直线。gganimate
部分是最后一行transition_states(i, state_length =.2)
,它将i
定义为在动画中应该改变的状态。然后,用animate(...)
对ggplot
对象进行动画,其中,对帧数nframes
和每秒帧数fps
的定义与对绘图大小的一些定义一起进行。