Here's my code
library(R2jags) #library(rjags)
library(bayesplot)
library(coda)
# set working directory
setwd("/Users/isa/Desktop/logreg")
# BUGS model code
cat("model {
for( i in 1 : 8 ) {
y[i] ~ dbin(theta[i],n[i])
logit(theta[i]) <- beta0 + beta1 * x[i]
}
beta0 ~ dunif(-100, 100)
beta1 ~ dunif(-100, 100)
}",
file = "model_log.txt")
data <- read.delim("data.txt",
sep = "",
header = TRUE,
check.names = "FALSE",
stringsAsFactors = FALSE)
initsone <- list(beta0 = -100, beta1 = 100)
initstwo <- list(beta0 = 100, beta1 = -100)
initslog <- list(initsone, initstwo)
paramslog <- c("beta0", "beta1", "theta[6]")
outputlog <-
jags(data = data,
inits = initslog,
parameters.to.save = paramslog,
model.file = "model_log.txt",
n.chains = 2,
n.iter = 1000,
n.burnin = 1000,
n.thin = 1,
DIC = TRUE#,
# bugs.directory = getwd(),
# working.directory = getwd()
)
Everything works fine until I try and compile the output. I get an error:
Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, :
Error in node y[1]
Node inconsistent with parents
I believe this has something to do with my data, which was in an OpenBugs format:
list(y = c(1, 3, 6, 8, 11, 15, 17, 19),
n = c(20, 20, 20, 20, 20, 20, 20, 20),
x = c(30, 32, 34, 36, 38, 40, 42, 44),
N = 8 )
but I converted it into an R format:
y n x
1 20 30
3 20 32
6 20 34
8 20 36
11 20 38
15 20 40
17 20 42
19 20 44
Did I convert the data incorrectly? Where is it going wrong in the data? Everything works fine until I try and compile the output. I get an error stating: Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, : Error in node y[1] Node inconsistent with parents
You have started your initial values for the parameters at the boundaries of your priors. This is not a problem, per se, but those logit scaled values are likely way to extreme and therefore will create initial estimates that are Pr == 0 or Pr == 1.
Just working through your data, let's assume we initial the model with
beta0 = -100andbeta1 = 100.For your first data point
x = 30so your logit-linear predictor starts out as:So we are starting out with a success probability of 1, but
y=1forn=20trials so the success probability cannot be 1. You can try a couple things to encourage the model to start sampling.xcovariate to have a mean = 0 and sd = 1 (i.e.,, use thescalefunction inR.JAGSis not a fan of the direct output fromscaleso you would end up doingx = as.numeric(scale(c(30, 32, 34, 36, 38, 40, 42, 44))). This is helpful as it means you can use standard priors for your regression, but means you need to interpret your coefficients differently. There are a lot of resources online to look at related to mean-centering variables.Another kind of way to get initial values somewhere in the correct region (assuming you are using vague priors) is to just fit a frequentist logistic regression model and use those estimates as the mean of some random normal distribution for each parameter. After all, with vague priors the frequentist estimate should be really close to the bayesian estimate as the likelihood is going to greatly outweigh the prior.
You can see here that values around
abs(100)are pretty far away from the parameter estimates of this specific model.So, if you wanted, you could set some initial values like so:
This will, of course, only really work if you have no prior information and for very simple models such as this one.