在R中输出多个图(用于动画)

时间:2012-08-19 06:38:01

标签: r animation plot bayesian

为了帮助我理解贝叶斯更新,我一直在使用bayesianbiologist中的代码。由于我必须学习如何创建动画图,我认为创建动画的更新动画将是一个有趣的练习。事实证明,这比预期的要困难得多。从Rob Hyndman's blog on this issue获取灵感,我尝试创建以下内容:

library(animation)
setwd("~/Dropbox/PriorUpdating") #set working directory

## Simulate Bayesian Binomial updating

sim_bayes<-function(p=0.5,N=10,y_lim=15,prior_a=1,prior_b=1)
{
   print(paste("The prior expectation of p is ",prior_a/(prior_a+prior_b)))
   success<-0
   curve(dbeta(x,prior_a,prior_b),xlim=c(0,1),ylim=c(0,y_lim),xlab='p',ylab='Posterior Density',lty=2)
   legend('topright',legend=c('Prior','Updated Posteriors','Final Posterior'),lty=c(2,1,1),col=c('black','black','red'))

  for(i in 1:N)
     {

        if(runif(1,0,1)<=p) success<-success+1 #this is where we see if there is a "success"

      curve(dbeta(x,success+prior_a,(i-success)+prior_b),add=TRUE) #plot updated
      }
curve(dbeta(x,success+prior_a,(i-success)+prior_b),add=TRUE,col='red',lwd=1.5) #plot final posterior
}

oopt = ani.options(interval = 0)
saveMovie(sim_bayes(p=0.6,N=90,prior_a=1,prior_b=1),interval=0.1,width=580,height=400)
ani.options(oopt)

然而,这只产生了最终的情节。所以我想我会尝试输出这些图的所有PDF。

library(animation)
setwd("~/Dropbox/PriorUpdating") #set working directory

## Simulate Bayesian Binomial updating

sim_bayes<-function(p=0.5,N=10,y_lim=15,prior_a=1,prior_b=1)
{
  print(paste("The prior expectation of p is ",prior_a/(prior_a+prior_b)))
  success<-0



  curve(dbeta(x,prior_a,prior_b),xlim=c(0,1),ylim=c(0,y_lim),xlab='p',ylab='Posterior Density',lty=2)
  legend('topright',legend=c('Prior','Updated Posteriors','Final Posterior'),lty=c(2,1,1),col=c('black','black','red'))



  for(i in 1:N)
  {
    pdf(paste("posterior",i,".pdf",sep=""),height=4,width=6.5)

    if(runif(1,0,1)<=p) success<-success+1 #this is where we see if there is a "success"

    curve(dbeta(x,success+prior_a,(i-success)+prior_b),add=TRUE) #plot updated

    #print(paste(success,"successes and ",i-success," failures"))
    dev.off()
  }
  pdf(paste("posterior_final",".pdf",sep=""),height=4,width=6.5)
  curve(dbeta(x,success+prior_a,(i-success)+prior_b),add=TRUE,col='red',lwd=1.5) #plot final posterior
  dev.off()
}

然而,这给了我以下错误

Error in plot.xy(xy.coords(x, y), type = type, ...) : 
  plot.new has not been called yet

我尝试在某些点插入plot.new(),但我认为这与图的附加性质相冲突。

有谁知道如何让这些方法中的一种或两种正常工作?虽然这对我来说是一个玩具的例子,但是我需要绘制一些更有趣的动画来描绘理解如何动画的情节将是有用/必要的。

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

就像马修说的那样,你把这个号召错了pdf。但是,这应该可以让您启动并运行:

sim_bayes <- function(p=0.5, N=10, y_lim=15, prior_a=1, prior_b=1) {
    success <- 0
    for (i in 1:N) {
        pdf(paste("posterior",i,".pdf",sep=""), height=4, width=6.5)

        if (runif(1,0,1) <= p)
            success<-success + 1

        # Start a new plot.
        curve(dbeta(x,prior_a,prior_b), lty=2,
              xlim=c(0,1), ylim=c(0,y_lim), xlab='p', ylab='Posterior Density')
        # Update plot.
        curve(dbeta(x,success+prior_a, (i-success) + prior_b), add=TRUE)

        legend('topright',
               legend=c('Prior','Updated Posteriors','Final Posterior'),
               lty=c(2,1,1), col=c('black','black','red'))

        dev.off()
    }
}

# `x` had no visible binding in your implementation, so I took the following
# from the `dbeta` documentation example.
x <- seq(0, 1, length=21)
sim_bayes()

答案 1 :(得分:1)

你这里只制作一个情节。如果您使用add=TRUE,则添加到当前图表,而不是新图表。因此删除它应该工作:

sim_bayes<-function(p=0.5,N=10,y_lim=15,prior_a=1,prior_b=1)
{
   print(paste("The prior expectation of p is ",prior_a/(prior_a+prior_b)))
   success<-0
   curve(dbeta(x,prior_a,prior_b),xlim=c(0,1),ylim=c(0,y_lim),xlab='p',ylab='Posterior Density',lty=2)
   legend('topright',legend=c('Prior','Updated Posteriors','Final Posterior'),lty=c(2,1,1),col=c('black','black','red'))

  for(i in 1:N)
     {

        if(runif(1,0,1)<=p) success<-success+1 #this is where we see if there is a "success"

      curve(dbeta(x,success+prior_a,(i-success)+prior_b)) #plot updated
      }
curve(dbeta(x,success+prior_a,(i-success)+prior_b),col='red',lwd=1.5) #plot final posterior
}

测试:

sim_bayes(p=0.6,N=90,prior_a=1,prior_b=1)

提供多个图