Hi all,
The explanatory variables in my
code consist of 4 continuous variables and two categorical variables;
each one has five levels. I converted the categorical variables to
dummy variables where the baseline in both cases is level 1.
So
I ended up with four continuous covariates, four dummy variables related to
the first categorical variable and four dummy variables related to the
second categorical variable in the model.
So b1,b2,b3, and b4 are the coefficients related to the continuous covariates.
b5,b6,b7,b8 related to the first categorical variable
b9,b10,b11,b12 related to the second categorical variable
I
would like to have a local regression model for each region $i$ where
the draws will be computed for the continuous and non-zero dummy variable coefficients.
I wrote the attached code, but I got an error.
Defining model
Error in eval(code[[2]], constantsEnv) : object 'hope' not found
Error in codeProcessIfThenElse(code[[i]], constants, envir) :
Cannot evaluate condition of 'if' statement: hope[1] != 0.
Condition must be able to be evaluated based on values in 'constants'.
HOPE <- nimbleFunction(
setup =
function(i=double(0),x5=
double(1),x6=double(1),x7=
double(1),x8=double(1),x9=
double(1),x10=double(1),x11=
double(1),x12=double(1)){
expr_beta <- c()
if(x5[i]!=0){
expr_beta <- c(expr_beta,5)
}
if(x6[i]!=0){
expr_beta <- c(expr_beta,6)
}
if(x7[i]!=0){
expr_beta <- c(expr_beta,7)
}
if(x8[i]!=0){
expr_beta <- c(expr_beta,8)
}
if(x9[i]!=0){
expr_beta <- c(expr_beta,9)
}
if(x10[i]!=0){
expr_beta <- c(expr_beta,10)
}
if(x11[i]!=0){
expr_beta <- c(expr_beta,11)
}
if(x12[i]!=0){
expr_beta <- c(expr_beta,12)
}
# Three cases:
# (a) first cat=1 & second cat=1
if(length(expr_beta)==0){
expr_beta <- c(0,0)
}
# (b) One non-baseline
if(length(expr_beta)==1){
expr_beta <- c(expr_beta,0)
}
# (c) Both non-baseline
expr_beta
},
run = function(P = double(1)){
return(P)
returnType(double(1))
})
GWRCode <- nimbleCode({
for (i in 1:S){
dn_inp <-
b[i, 1] * x1[1:N] +
b[i, 2] * x2[1:N] +
b[i, 3] * x3[1:N] +
b[i, 4] * x4[1:N]
hope <- HOPE(i,x5,x6,x7,x8,x9,x10,x11,
x12)
if(hope[1] != 0){
for(k in hope){
if(k == 5){
dn_inp <- dn_inp + b[i,k] * x5[1:N]
} else if(k == 6){
dn_inp <- dn_inp + b[i,k] * x6[1:N]
} else if(k == 7){
dn_inp <- dn_inp + b[i,k] * x7[1:N]
} else if(k == 8){
dn_inp <- dn_inp + b[i,k] * x8[1:N]
}
if(k == 9){
dn_inp <- dn_inp + b[i,k] * x9[1:N]
} else if(k == 10){
dn_inp <- dn_inp + b[i,k] * x10[1:N]
} else if(k == 11){
dn_inp <- dn_inp + b[i,k] * x11[1:N]
} else if(k == 12){
dn_inp <- dn_inp + b[i,k] * x12[1:N]
}
}
}
y[1:N,i] ~ dmnorm(dn_inp, prec[1:N,1:N,i])
for(j in 1:12){
b[i, j] ~ dnorm(0, tau)
}
b0[i] ~ dnorm(0, 1)
}
tau ~ dgamma(0.001, 0.001)
})
Could you please advise in this regard?
Thanks,
Wala