[R] MH algorithm syntax help

David Winsemius dwinsemius at comcast.net
Mon Jan 19 03:03:02 CET 2009


The comma *before*  acc=1 ?

I also wondered whether (further up)  this should work:
s2y[i,]=s2y[i-1]  # would think this to result in a dimension mismatch

This looks sketchy as well:
s2y[i,j] = s2y[b[i-1,j] + rnorm(1,mean=0, sd=s2yscale[j])
               ^ ^     ^  # unmatched sqr-brackets

It would be easier to run through a paren matching editor if you gave  
the original loop code as well as the error output.
-- 
David Winsemius
On Jan 18, 2009, at 6:54 PM, ekwaters wrote:

>
> 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.
>
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list