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.(adsbygoogle = window.adsbygoogle || []).push({});

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.

I know this is wrong but I don't think I'm too far off, could anyone point me in the right direction? :)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))

Thanks

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Metropolis-hastings in R

Can you offer guidance or do you also need help?

Draft saved
Draft deleted

**Physics Forums | Science Articles, Homework Help, Discussion**