R语言 想要用gganimate创建采样动画

9wbgstp7  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(150)

我想创建一个动画来显示样本大小对样本均值和标准误差的影响。我得到了这段代码。但是,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”。任何帮助都将不胜感激

ru9i0ody

ru9i0ody1#

也许你可以从这个开始,它从2个for循环中的DF开始,3个样本大小分别是10、50和100。i是重复的次数。meansd取自你的代码。
在ggplot部分,DF被分组为ni,并计算这些组的平均值-这将在稍后创建平均值的垂直线。
gganimate部分是最后一行transition_states(i, state_length =.2),它将i定义为在动画中应该改变的状态。然后,用animate(...)ggplot对象进行动画,其中,对帧数nframes和每秒帧数fps的定义与对绘图大小的一些定义一起进行。

df <- data.frame(i = as.numeric(),
                  x = as.numeric(),
                  sd = as.numeric(),
                  mu = as.numeric(), 
                  n = as.numeric())

mu <- 10
sd <- 1
df <- rbind(df, data.frame(i =i, x = x,n = n,
                           sd = sd, mu = mu))  

for (n in c(10, 50, 100)){
   for (i in seq(1,100)){
     #Generating a sample from the normal distribution
     x <- rnorm(n,mu,sd)
     i <- i
     # i <- rep(i, 10)
     df <- rbind(df, data.frame(i =i, x = x,n = n,
                                sd = sd, mu = mu))  
   }
}

p <- df |> 
   group_by(n, i) |> 
   mutate(m = mean(x)) |>
   mutate(n = as.factor(case_when(
     n == "10" ~ "sample size = 10", 
     n == "50" ~ "sample size = 50", 
     n == "100" ~ "sample size = 100"
   ))) |> 
   mutate(n = fct_relevel(n, "sample size = 100", after = Inf)) |> 
   ungroup() |> 
   ggplot() +
     geom_histogram(aes(x, fill = n), 
                    color = 'white', binwidth = .5) +
     geom_vline(aes(xintercept = m, color ='red')) +
     theme_bw() +
     theme(legend.position = 'none') +
     facet_wrap(~n, nrow=1) +
     geom_text(aes(x = 12, y = 30, label = paste('Iter = ',i)), alpha = 0.2,
              col = "gray", size = 5) +
     geom_text(aes(x = 12, y = 28, label = paste('Mean = ', round(m, 2))), alpha = 0.2,
               col = "gray", size = 5) +
    transition_states(i, state_length =.2)

animate(p, fps = 1, nframes=100, height = 10, width = 20, units = "cm", res = 150)

相关问题