jeudi 16 avril 2020

Error code Missing value where TRUE/FALSE needed in R

I keep getting this error message when I run my code, and I'm not sure what I need to do to fix it.

My code is as follows:

gwmh<-function(target,N,x,sigmasq){  
  p<-add.var()
  samples<-c(x,p)
  for(i in 2:N){
    prop<-rnorm(1,0,sqrt(sigmasq))
    if(runif(1)<min(1,(target(x+abs(prop)*p))/target(x))){
      x<-x+prop
    samples<-rbind(samples,c(x,p))} else{
      p<--p
    samples<-rbind(samples,c(x,p))
    }
  }
  samples[(1:N)]  ##delete after testing
} 

The error says: Error in if (runif(1) < min(1, (target(x + abs(prop) * p))/target(x))) { : missing value where TRUE/FALSE needed

(add.var is a function i created to generate p in {-1,1} randomly)

Aucun commentaire:

Enregistrer un commentaire