[R] Metropolis-Hastings in R
Michael Williams
mikeyy_lol at hotmail.co.uk
Wed Mar 14 23:01:57 CET 2012
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.
More information about the R-help
mailing list