Reputation: 181
To help my understanding of Bayesian updating I have been working with the code from bayesianbiologist. As I have to learn how to create animated plots, I thought it would be a fun exercise to create an animated plot of the updating. This has proved tougher than expected. Taking inspiration from Rob Hyndman's blog on this issue I tried to create the following:
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)
This however only produced the final plot. So I thought I would try to output all of the PDFs of the plots.
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()
}
However this gives me the following error
Error in plot.xy(xy.coords(x, y), type = type, ...) :
plot.new has not been called yet
I tried inserting plot.new() at certain points but I think this conflicts with the additive nature of the plots.
Has anyone got any idea how I could make one or both of these methods work properly? While this is a bit of a toy example for me, I have some more interesting animations I need to plot where understanding how to animate plots is going to be useful/necessary.
Thanks for any help!
Upvotes: 3
Views: 732
Reputation: 47541
You are only making one plot here. If you use add=TRUE
you add to the current plot, not make a new one. Hence removing that should work:
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
}
Test:
sim_bayes(p=0.6,N=90,prior_a=1,prior_b=1)
Gives multiple plots
Upvotes: 1
Reputation: 3894
Like MatthewK said, you've misplaced the call to pdf
. However, this should get you up and running:
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()
Upvotes: 1