I am using R2jags to fit a model in R using JAGS. Here is my code:
predictorNames <- c("BMIX", "AGE", "TEXPWK", "FRUITS", "VEGTABLS",
"FISH", "REDMEAT", "POULTRY", "SOY", "NUTS", "GRAINS",
"WHLGRNS", "MILKS", "DAIRY", "RACE.BLACK", "REGION.NE",
"REGION.MW", "REGION.S", "SMOKING.PAST", "SMOKING.CURRENT",
"ALCOHOL.PAST", "ALCOHOL.CURRENT", "TOTHSTAT.LOW",
"TOTHSTAT.HIGH")
nPredictors <- length(predictorNames)
y <- as.vector(dummyData.iT2D$iT2D)
x <- as.matrix(dummyData.iT2D[, predictorNames])
jagsData <- list(x = x, y = y, N = nrow(dummyData.iT2D), nP = nPredictors)
jagsParams <- character(nPredictors+1)
jagsParams[1] <- "b0"
for(eachParam in 1:(length(jagsParams)-1)) {
jagsParams[eachParam+1] <- paste("b", eachParam, sep="")
}
jagsModel <- function() {
for(i in 1:N) {
y[i] ~ dbern(mu[i])
mu[i] <- 1/1+exp(-(b0+inprod(b[], x[i, ])))
}
b0 ~ dnorm(0, 1.0e-6)
for(j in 1:nP) {
b[j] ~ dnorm(0, 1.0e-6)
}
}
jagsFit1 <- jags(data=jagsData, parameters.to.save=jagsParams, n.iter=110000,
n.thin=10, n.burnin=10000, model.file=jagsModel)
However, when I try to fit the model, I get
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph Size: 286683
Initializing model
Deleting model
Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, :
Error in node y[9247]
Invalid parent values
9247 is the number of observations I have in my dataset. When I remove this observation, the same thing occurs, but at 9246 (which would be the last observation in the dataset again). Does anyone have any idea why this is?
Best Answer
The error message means that mu[9247] is evaluating outside the range [0,1] at initialisation, because there is a mistake on the regression line. You could correct the parentheses as instructed by Stéphane Laurent, but it would be more usual to write this type of GLM as:
Also remember to load the glm module which will give you block updating of your parameters and therefore (hopefully) better convergence.
Matt