I'm trying to make a bayesian mixture model using rjags. This is an attempt to map a dose-response relationship for experiments conducted in 19 labs. As such, the model that I produced has intercepts for all the labs. I want to cluster the lab effects using mixture modelling but my code does not work. Here is a copy of my model followed by the error :-
mod_string2 <- "
model{
# Likelihood
for(i in 1:n){
Y[i] ~ dnorm(mu[i],inv.var)
mu[i] <- a[lab[i]] + b[1]*ld1[i] + b[2]*ld2[i] + b[3]*sqld1[i] + b[4]*sqld2[i] + b[5]*lbody[i] + b[6]*B[i]*ld1[i] + b[7]*C[i]*ld1[i] + b[8]*D[i]*ld1[i] + b[9]*B[i]*ld2[i] + b[10]*C[i]*ld2[i] + b[11]*D[i]*ld2[i]
a[lab[i]] ~ dnorm(muOfClust[clust[lab[i]]], tau)
clust[i] ~ dcat( pClust[1:Nclust] )
}
# Prior for labs (intercepts)
for (clustIdx in 1: Nclust) {
muOfClust[clustIdx] ~ dnorm( 0 , 1/100000 )
}
pClust[1:Nclust] ~ ddirch(onesRepNclust) # so (pi1,pi2) follow Dir(1,1) which implies pi1 follows Beta(1,1)
tau ~ dgamma(0.01 , 0.01)
# Prior for beta
for(j in 1:11){
b[j] ~ dnorm(0,0.0001)
}
# Prior for the inverse variance
inv.var ~ dgamma(0.01, 0.01)
sigma <- 1/sqrt(inv.var)
}
"
My error is :-
Error in jags.model(textConnection(mod_string2), data = d2) :
RUNTIME ERROR:
Compilation error on line 7.
Attempt to redefine node a[3]
What am I doing wrong?
Related
I want to perform a mixed effect regression in rjags, with a random slope and intercept. I define the following toy dataset:
library(ggplot2)
library(data.table)
global_slope <- 1
global_int <- 1
Npoints_per_group <- 50
N_groups <- 10
pentes <- rnorm(N_groups,-1,.5)
centers_x <- seq(0,10,length = N_groups)
center_y <- global_slope*centers_x + global_int
group_spread <- 2
group_names <- sample(LETTERS,N_groups)
df <- lapply(1:N_groups,function(i){
x <- seq(centers_x[i]-group_spread/2,centers_x[i]+group_spread/2,length = Npoints_per_group)
y <- pentes[i]*(x- centers_x[i])+center_y[i]+rnorm(Npoints_per_group)
data.table(x = x,y = y,ID = group_names[i])
}) %>% rbindlist()
ggplot(df,aes(x,y,color = as.factor(ID)))+
geom_point()
This is a typical situation of Simpson paradox: an overall increasing trend when you have a decreasing trend within each group (given by the ID variable).
I define the following model:
library(rjags)
model_code_simpson <-
" model
{
# first level
for (i in 1:n) {
y[i] ~ dnorm(alpha[i] + beta[i] * x[i], tau)
alpha[i] = alpha[group[i]] # random intercept
beta[i] = beta[group[i]] # random slope
}
# second level
for(j in 1:J){
alpha[j] ~ dnorm(mu.alpha, tau.alpha)
beta[j] ~ dnorm(mu.beta, tau.beta)
}
# Priors
mu.alpha ~ dnorm(0,0.001)
mu.beta ~ dnorm(0,0.001)
sigma ~ dunif(0,10)
sigma.alpha ~ dunif(0,10)
sigma.beta ~ dunif(0,10)
# Derived quantities
tau <- pow(sigma,-2)
tau.alpha <- pow(sigma.alpha,-2)
tau.beta <- pow(sigma.beta,-2)
}
"
# Choose the parameters to watch
model_parameters <- c("mu.alpha","tau.alpha","tau.beta","tau")
# define numeric grouping variable
df[,ID2 := .GRP,by = ID]
model_data <- list(n = nrow(df),
y = df$y,
x = df$x,
group = df$ID2,
J = df[,uniqueN(ID)])
model <- jags.model(textConnection(model_code_simpson),
data = model_data,
n.chains = 2)
I get the following error:
Compiling model graph
Resolving undeclared variables
Allocating nodes
Deleting model
Error in jags.model(textConnection(model_code_simpson), data = model_data, :
RUNTIME ERROR:
Compilation error on line 8.
Attempt to redefine node beta[1]
I do not understand what is happening, and related questions did not help me much.
You defined beta twice. First, beta is a vector of length n when you are looping through the data. Second, beta is a vector of length J when you are creating the random effects. This "redefining" is causing this issue, but it is an easy fix. You just need to remove that first instance of beta in your model and it will compile (i.e., just move your nested indexing inside of dnorm() and you are good to go).
model_code_simpson <-
" model
{
# first level
for (i in 1:n) {
y[i] ~ dnorm(
alpha[group[i]] + beta[group[i]] * x[i],
tau
)
}
# second level
for(j in 1:J){
alpha[j] ~ dnorm(mu.alpha, tau.alpha)
beta[j] ~ dnorm(mu.beta, tau.beta)
}
# Priors
mu.alpha ~ dnorm(0,0.001)
mu.beta ~ dnorm(0,0.001)
sigma ~ dunif(0,10)
sigma.alpha ~ dunif(0,10)
sigma.beta ~ dunif(0,10)
# Derived quantities
tau <- pow(sigma,-2)
tau.alpha <- pow(sigma.alpha,-2)
tau.beta <- pow(sigma.beta,-2)
}
"
I have fitted following simple linear regression Bayesian model using rjags.
I was able to run the model by specifying all the predictors separately(like for a lm object). Now I want to learn how to specify the predictors by introducing them as a matrix instead of specifying them separately.
So I ran the following code, but it gave some errors.
I used tobbaco data set in rrr package to provide a reproducible example.
library(rrr)
require(dplyr)
library(rjags)
tobacco <- as_data_frame(tobacco)
N1 = length(tobacco$Y1.BurnRate)
x1 = model.matrix(Y1.BurnRate~X2.PercentChlorine+X3.PercentPotassium ,data = tobacco)
bayes_model_mul1=
"model {
for(i in 1:N1){
Y1.BurnRate[i]~dnorm(mu1[i],tau1)
for(j in 1:3){
mu1[i]=beta1[j]*x1[i,j]
}
}
for (l in 1:3) { beta1[l] ~dnorm(0, 0.001) }
tau1 ~ dgamma(.01,.01)
sigma_tau1 = 1/tau1
}"
model3 <- jags.model(textConnection(bayes_model_mul1),
data = list(Y1.BurnRate=tobacco$Y1.BurnRate, x1=x1, N1=N1),
n.chains=1)
After I run model3 , I got following error.
Error in jags.model(textConnection(bayes_model_mul1), data = list(Y1.BurnRate = tobacco$Y1.BurnRate, :
RUNTIME ERROR:
Compilation error on line 6.
Attempt to redefine node mu1[1]
Can anyone help me figure this out ?
Does this due to introducing predictors as a matrix ?
There are a few ways to do this, here are two:
Use matrix multiplication outside of the likelihood loop
m1 =
"model {
mu1 = x1 %*% beta1 # ---> this
for(i in 1:N1){
Y1.BurnRate[i] ~ dnorm(mu1[i], tau1)
}
for (l in 1:3) { beta1[l] ~ dnorm(0, 0.001) }
tau1 ~ dgamma(.01,.01)
sigma_tau1 = 1/tau1
}"
Use inprod to multiply the parameters with the design matrix
m2 =
"model {
for(i in 1:N1){
mu1[i] = inprod(beta1, x1[i,]) #----> this
Y1.BurnRate[i] ~ dnorm(mu1[i], tau1)
}
for (l in 1:3) { beta1[l] ~ dnorm(0, 0.001) }
tau1 ~ dgamma(.01,.01)
sigma_tau1 = 1/tau1
}"
You received an error with for(j in 1:3){ mu1[i] = beta1[j]* x1[i,j] } as every time you loop though the parameter index j you overwrite mu1[i]. It also doesn't sum up the individual terms. You may be able to index mu1 with j as well and then sum but untested ...
I'm currently trying to develop a model in JAGS, but I unfortunately keep getting the following error:
Error in jags.model("ref_model.txt", data = ref.data.jags, inits = inits3, :
RUNTIME ERROR:
Compilation error on line 26.
Unknown variable mu.fine
Either supply values for this variable with the data
or define it on the left hand side of a relation.
This happens when I run the following code:
# Function that generates the initial values for MCMC:
inits <- function()
{
list(beta0=rnorm(1),
beta1=rnorm(1),
beta2=rnorm(1),
beta3=rnorm(1),
beta4=rnorm(1),
beta5=rnorm(1),
beta6=rnorm(1))
}
inits3 <- list(inits(), inits(), inits())
# Parameters that will be monitored:
params <- c("beta0", # intercept
"beta1", "beta2", "beta3", # slopes
"beta4", "beta5", "beta6",
"pred.fine") # fine-grain predictions
# Model compilation:
jm <- jags.model("ref_model.txt",
data = ref.data.jags,
inits = inits3,
n.chains = 3,
n.adapt = 1000)
The following is what is in the file titled "ref_model.txt", I run this in R using Rjags in R
# Model definition for JAGS:
cat("
model
{
# priors
beta0 ~ dnorm(0, 0.01)
beta1 ~ dnorm(0, 0.1)
beta2 ~ dnorm(0, 0.1)
beta3 ~ dnorm(0, 0.1)
beta4 ~ dnorm(0, 0.1)
beta5 ~ dnorm(0, 0.1)
beta6 ~ dnorm(0, 0.1)
# fitting the model to the fine-grain reference dataset
# (600 well surveyed cells)
for (j in 1:N.ref)
{
# Eq. 1 (see Methods):
log(lambda.fine[j]) <- beta0 +
beta1 * NPP.ref[j] +
beta2 * LC.ref[j] +
beta3 * PW.ref[j] +
beta4 * HFP.ref[j] +
beta5 * PS.ref[j] +
beta6 * T.ref[j]
# Eq. 2 (see Methods):
S.ref[j] ~ dpois(mu.fine[j])
}
# predicting in the complete fine-grain dataset
# (all 6238 fine-grain grid cells)
for (i in 1:N.fine)
{
log(pred.fine[i]) <- beta0 +
beta1 * NPP[i] +
beta2 * LC[i] +
beta3 * PW[i] +
beta4 * HFP[i] +
beta5 * PS[i] +
beta6 * T[i]
}
}
", file="ref_model.txt")
I'm a little confused as to why the error is occurring, If anyone can advise how I can address this problem, I would greatly appreciate it.
This error is occurring because mu.fine only occurs on the right hand of an equation and not the left. In another way, it looks like S.ref depends on mu.fine, but you have not told JAGS what mu.fine is (there are no values for it). Assuming that lambda.fine is the linear predictor and S.ref is your dependent variable you could change
S.ref[j] ~ dpois(mu.fine[j])
to
S.ref[j] ~ dpois(lambda.fine[j])
and then this error would not occur.
I'm trying to fit a simplex model with poisson trick, the likelihood is Likelihood Simplex. The code is below
model{
for (i in 1:n){
y[i] ~ dpois(lambda[i])
lambda[i] <- 0.5*log(phi[i]*(y[i]*(1-y[i]))^3) + 0.5*(1/phi[i])*d[i]
d[i] <- ((y[i]-mu[i])^2)/(y[i]*(1-y[i])*mu[i]^2*(1-mu[i])^2)
mu[i] <- beta0+beta1*income[i] + beta2*person[i]
log(phi[i]) <- -delta0
}
beta0 ~ dnorm(0,.001)
beta1 ~ dnorm(0,.001)
beta2 ~ dnorm(0,.001)
delta0 ~ dnorm(0,.001)
}"
When I try to run the code with JAGS in R, I get the following error
RUNTIME ERROR:
Possible directed cycle involving some or all
of the following nodes:
Then it shows all d[], y[] and lambda[]
I found that someone have a similar problem JAGS error, but looks like that I'm not doing the same mistake.
Any help?
EDIT:
Second attempt
regmodel = "
data{
for(i in 1:n) {
zeros[i] <- 0
}
}
model{
C <- 1000
for (i in 1:n){
zeros[i] ~ dpois(lambda[i])
lambda[i] <- -l[i] + C
l[i] <-
0.5*log(phi[i]*(y[i]*(1-y[i]))^3) +
0.5*(1/phi[i])*((y[i]-mu[i])^2)/(y[i]*(1-y[i])*mu[i]^2*(1-mu[i])^2)
mu[i]<- beta0 + beta1*income[i] + beta2*person[i]
log(phi[i]) <- -delta0
}
beta0 ~ dnorm(0,.001)
beta1 ~ dnorm(0,.001)
beta2 ~ dnorm(0,.001)
delta0 ~ dnorm(0,.001)
}"
But the error now is
Error in jags.model(file = "ModeloSimplex.txt", data = reg.dat, n.chains = 3, :
Error in node (a(a0.5*(a1/phi[1])*(a(ay[1]-mu[1])^2))/(ay[1]*(a1-y[1])*(amu[1]^2)*(a(a1-mu[1])^2)))
Invalid parent values
Using JAGS I am trying to estimate a model including a unit-specific time trend.
However, the problem is that I don't know how to model this and so far I have been unable to find a solution.
As an example, consider we have the following data:
rain<-rnorm(200) # Explanatory variable
n1<-rnorm(200) # Some noise
gdp<-rain+n1 # Outcome variable
ccode<-rep(1:10,20) # Unit codes
year<-rep(1:20,10) # Years
Using normal linear regression, we would estimate the model as:
m1<-lm(gdp~rain+factor(ccode)*year)
Where factor(ccode)*year is the unit-specific time trend. Now I want to estimate the model using JAGS. So I create parameters for the indexing:
N<-200
J<-max(ccode)
T<-max(year)
And estimate the model,
library(R2jags)
library(rjags)
set.seed(42); runif(1)
dat<-list(gdp=gdp,
rain=rain,
ccode=ccode,
year=year,
N=N,J=J,T=T)
parameters<-c("b1","b0")
model.file <- "~/model.txt"
system.time(m1<-jags(data=dat,inits=NULL,parameters.to.save=parameters,
model.file=model.file,
n.chains=4,n.iter=500,n.burnin=125,n.thin=2))
with the following model file, and this is where the error is at the moment:
# Simple model
model {
# For N observations
for(i in 1:N) {
gdp[i] ~ dnorm(yhat[i], tau)
yhat[i] <- b1*rain[i] + b0[ccode[i]*year[i]]
}
for(t in 1:T) {
for(j in 1:J) {
b0[t,j] ~ dnorm(0, .01)
}
}
# Priors
b1 ~ dnorm(0, .01)
# Hyperpriors
tau <- pow(sd, -2)
sd ~ dunif(0,20)
}
I am fairly sure that the way in which I define b0 and the indexing is incorrect given the error I get when using the code: Compilation error on line 7. Dimension mismatch taking subset of b0.
However, I don't know how to solve this so I wondered whether someone here has suggestions about this?
Your lm example can also be written:
m1 <- lm(gdp ~ -1 + rain + factor(ccode) + factor(ccode):year)
The equivalent JAGS model would be:
M <- function() {
for(i in 1:N) {
gdp[i] ~ dnorm(yhat[i], sd^-2)
yhat[i] <- b0[ccode[i]] + b1*rain[i] + b2[ccode[i]]*year[i]
}
b1 ~ dnorm(0, 0.001)
for (j in 1:J) {
b0[j] ~ dnorm(0, 0.001)
b2[j] ~ dnorm(0, 0.001)
}
sd ~ dunif(0, 100)
}
parameters<-c('b0', 'b1', 'b2')
mj <- jags(dat, NULL, parameters, M, 3)
Comparing coefficients:
par(mfrow=c(1, 2), mar=c(5, 5, 1, 1))
plot(mj$BUGSoutput$summary[grep('^b0', row.names(mj$BUGSoutput$summary)), '50%'],
coef(m1)[grep('^factor\\(ccode\\)\\d+$', names(coef(m1)))],
xlab='JAGS estimate', ylab='lm estimate', pch=20, las=1,
main='b0')
abline(0, 1)
plot(mj$BUGSoutput$summary[grep('^b2', row.names(mj$BUGSoutput$summary)), '50%'],
coef(m1)[grep('^factor\\(ccode\\)\\d+:', names(coef(m1)))],
xlab='JAGS estimate', ylab='lm estimate', pch=20, las=1,
main='b2')
abline(0, 1)