[R] Random-walk Metropolis-Hasting

Uwe Ligges ligges at statistik.tu-dortmund.de
Fri Nov 11 19:29:06 CET 2011



On 11.11.2011 18:48, Gyanendra Pokharel wrote:
> Following is my code, can some one help on the error at the bottom?
>
>> mh<-function(iterations,alpha,beta){
> + data<-read.table("epidemic.txt",header = TRUE)
> + attach(data, warn.conflicts = F)
> +     k<-97
> +     d<- (sqrt((x-x[k])^2 + (y-y[k])^2))
> +     p<- 1-exp(-alpha*d^(-beta))
> +     p.alpha<-1 - exp(-3*d^(-beta))
> +     p.beta<- 1 - exp(alpha*d^(-2))
> +     iterations<-1000
> +     mu.lambda<- c(0,0);s.lambda<- c(100,100)
> +     prop.s<- c(0.1,0.1)
> +     lambda<- matrix(nrow=iterations, ncol=2)
> +     acc.prob<-0
> +     current.lambda<- c(0,0)
> +     for(t in 1:iterations){
> +         prop.lambda<- rnorm(2,current.lambda,prop.s)
> +         a<- p.beta/p.alpha
> *(dnorm(prop.lambda,mu.lambda,s.lambda))*dnorm(current.lambda,mu.lambda,s.lambda)
> +         accept<- min(1,a)
> +         u<-runif(1)
> +         if(u[t]<=accept[t]){
> +             current.lambda<- prop.lambda
> +             acc.prob<- acc.prob +1
> +                 }
> +         lambda[t,]<- current.lambda
> +     }
> +     lambda
> + }
>> mh(1000,0,0)
> Error in if (u[t]<= accept[t]) { : missing value where TRUE/FALSE needed

As the error message tells you:

accept[t] is NA t > 1 since

accept<- min(1,a)

Uwe Ligges




>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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