Thanks, this isn't actually homework though. I'm researching MCMC methods but
I've never really used R before so it's proved quite troublesome!

my code is now:

n=10000
mu=0
sigma=1
lik<-function(theta) exp((-(theta-mu)^2)/2)
alpha<-function(theta,phi) min(lik(phi)/lik(theta),1)
theta1<-c(0,n)
theta1[1]<-mu
for(i in 2:n){
theta<-theta1[i-1]
phi<-theta+runif(1,-0.5,0.5)
k<-rbinom(1,1,alpha(theta,phi))
k1<-k1+k
theta1[i]<-theta+k*(phi-theta)
}
plot(density(theta1))

which seems to be working correctly now!
Thanks alot for your help I really appreciate you giving me the time!

--
View this message in context: 
http://r.789695.n4.nabble.com/Metropolis-Hastings-in-R-tp4472547p4473264.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to