Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Metropolis-hastings in R

  1. Mar 5, 2012 #1
    Hi everyone I'm in my final year of university and doing a project on MCMC mainly for applications to bayesian statistics, I think I understand the concept of it so far however I'm struggling to actually make examples.

    I'm trying to model Poisson(2) with a random walk metropolis hastings algorithm in R but really don't know where to even start as I've never used R and just confusing myself.

    Code (Text):
    theta <- 0
    set.seed(1)
    k<-1
    lambda<-2
    lik<-function(lambda)(k^(lambda)*exp(-lambda))/factorial(k)
    alpha<-function(theta,phi) min(lik(phi)/lik(theta),1)
    THETA<-NULL
    b<-runif(1, min=0, max=1)
    for(i in 1: 100)
    {

        if( alpha(theta,phi) < b)
        {theta.star<-theta
       }
        else{theta.star<-theta.star}
        theta = c(theta, theta.star)
    }    
    plot(density(theta))
    I know this is wrong but I don't think I'm too far off, could anyone point me in the right direction? :)

    Thanks
     
    Last edited: Mar 5, 2012
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted



Similar Discussions: Metropolis-hastings in R
Loading...