Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:32:28+00:00 2026-06-10T01:32:28+00:00

To help my understanding of Bayesian updating I have been working with the code

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:32:30+00:00Added an answer on June 10, 2026 at 1:32 am

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help understanding what's happening here. This code is from a models/log.py
I need some help understanding this bit of code pre { white-space: pre; white-space:
Kind of need help understanding what this code actually outputs. Does it out put
I need some help understanding some of the points from Paul Graham’s What Made
I'd like some help understanding the use of pheanstalk (php beanstalk client). I have
I'm looking for some help understanding how to generate pages from a database to
I need help understanding the easiest way to create an array which carries 4
I need some help understanding what I'm doing wrong with my js code. Basically
I need help understanding this type signature which is from the Thrist package. import
a big noob needs help understanding things. I have three UIViews stored inside a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.