[R] MH algorithm syntax help

ekwaters ekwaters at unimelb.edu.au
Mon Jan 19 00:54:16 CET 2009


Hi all,

I am trying to write a random walk metropolis hastings algorithm, I using
the latest debian distribution of R.

Can anyone tell me what I need to insert in the below code? I have tried
putting various combinations of curly brackets and punctuation between the
"acc=1" and "if" statements, all to no avail. Any clues?

R CODE....SET UP OF PARAMETERS ETC, FYI

 m=2000
> x=as.matrix(read.table("/media/DataTravelerMini/KINGSTON/Honours/R/IPR/m.txt"))
> y=as.matrix(read.table("/media/DataTravelerMini/KINGSTON/Honours/R/IPR/mcr.txt"))
> # establish parameters
> s2ey=matrix(1,m)
> vx=sd(x)
> vy=sd(y)
> s2xscale=sqrt(diag(vcov(lm(y~x-1))*.5))
> s2yscale=sqrt(diag(vcov(lm(x~y-1))*.5))
> s2eyscale=sqrt(var(residuals(lm(x~y-1))*.5))
> s2exscale=sqrt(var(residuals(lm(y~x-1))*.5))
> accrates2x=matrix(0,m)
> accrates2y=matrix(0,m)
> accrates2ey=matrix(0,m)
> accrates2ex=matrix(0,m)
> #specification of posterior
> post<-function(y,x,s2ey,s2x,s2y){return((-4.333*log(s2ey)+(-.5/(s2ey))*(t(y-x*s2y/s2x))*(y-x*s2y/s2x)))}

THIS IS WHERE THE SYNTAX ERROR IS:
> #begin MH sampling
> for(i in 2:m){
+ s2y[i,]=s2y[i-1]
+ for(j in vy)
+ {
+ s2y[i,j]=s2y[b[i-1,j]+rnorm(1,mean=0,sd=s2yscale[j]),
+ acc=1
+ if((post(y,x,s2ey[i-1],s2x,s2y[i])-post(y,x,s2ey[i-1],s2x,s2y[i-1]))
Error: syntax error in:
"acc=1
if"
THE REMAINDER OF THE IF STATEMENT:
<log(runif(1,min=0,max=1))){s2y[i,j]=s2y[i-1,j],
acc=0}accrate[i,j]=(accrate[i-1,j]*(i-1)+acc)/i}

Cheers

Ned
-- 
View this message in context: http://www.nabble.com/MH-algorithm-syntax-help-tp21534889p21534889.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list