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