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)
if(test_expression)tests an expressions that is neither TRUE or FALSE but missing. Have you checked that the data input into the function has no missing values? - Fnguyentargetis another one of your functions that you haven't provided? It's impossible to help unless you provide a lot more information. Obviously line 5 is the problem child. - Edwardrunif(1)<min(1,(target(x+abs(prop)*p))/target(x))this particular code is not evaluating to True or False. Ideally if conditions must have expression that evaluates to either T or F. Just debug the code written as condition. usebrowser()method before if condition to debug. - nikn8