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.
Related
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?
I am looking to run a hierarchical poisson model to hockey goal tending data. here is the model as set up in bugs:
modelString <- "model {
for(i in 1:n_obs){
hockey_goals[i] ~ dpois(p[i])
log(p[i]) <- p_inter + p_age * age[i] + p_sv_pct * sv_pct[i] + p_team * team[i] + p_win_pct * win_pct[i] + log(n_mins[i])
}
p_inter ~ dnorm(0,0.00001)
p_age ~ dnorm(0, 0.00001)
p_sv_pct ~ dnorm(0, 0.00001)
p_team ~ dnorm(0, 0.00001)
p_win_pct ~ dnorm(0, 0.00001)
}"
I then compile and load the data:
season_goals <- data$GA
n_mins <- data$MIN
age <- data$Age
sv_pct <- data$SV.
team <- data$Tm
win_pct <- data$W/data$GP
data <- list(n_obs=length(season_goals),n_mins=n_mins,hockey_goals=season_goals,age=age,
sv_pct=sv_pct,team=team,win_pct=win_pct)
# Get the data into BUGS:
modelData( bugsData( data ) )
#------------------------------------------------------------------------------
# INTIALIZE THE CHAINS.
nchain = 1
modelCompile( numChains=nchain )
modelGenInits()
#------------------------------------------------------------------------------
# RUN THE CHAINS.
samplesSet( c("p_age","p_sv_pct","p_team","p_win_pct") )
# R command defines a new variable that specifies an arbitrary chain length:
chainLength = 10000
# BRugs tells BUGS to generate a MCMC chain:
modelUpdate( chainLength )
At this point I get an error:
Error in handleRes(res) : NA
Any ideas on where I went wrong...?
I wish I could just comment but I don't have enough reputation. Anyway, I made up some data and ran a simplified version of your model (i.e. with only age and minutes played). I ran it in the GUI version of OpenBUGS and it didn't work. I then changed the priors on you coefficients to dnorm(0,0.01) and it updated.
So I would suggest changing the priors. The ones you have are very, very vague. Changing them as suggested will not effect your inference and you may get the model to run.
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
I am trying to fit a logistic regression model in JAGS, but I have data in the form of (# success y, # attempts n), rather than a binary variable. In R, one can fit a model to data such as these by using glm(y/n ~ ) with the "weights" argument, but I am not sure how to fit this in JAGS.
Here is a simple example that I hope addresses what I am trying to ask. Note that I am using the rjags package. Thanks for any help!
y <- rbinom(10, 500, 0.2)
n <- sample(500:600, 10)
p <- y/n
x <- sample(0:100, 10) # some covariate
data <- data.frame(y, n, p, x)
model <- "model{
# Specify likelihood
for(i in 1:10){
y[i] ~ dbin(p[i], n[i])
logit(p[i]) <- b0 + b1*x
}
# Specify priors
b0 ~ dnorm(0, 0.0001)
b1 ~ dnorm(0, 0.0001)
}"
You don't need to compute p in your data set at all. Just let it be a logical node in your model. I prefer the R2jags interface, which allows you to specify a BUGS model in the form of an R function ...
jagsdata <- data.frame(y=rbinom(10, 500, 0.2),
n=sample(500:600, 10),
x=sample(0:100, 10))
model <- function() {
## Specify likelihood
for(i in 1:10){
y[i] ~ dbin(p[i], n[i])
logit(p[i]) <- b0 + b1*x[i]
}
## Specify priors
b0 ~ dnorm(0, 0.0001)
b1 ~ dnorm(0, 0.0001)
}
Now run it:
library("R2jags")
jags(model.file=model,data=jagsdata,
parameters.to.save=c("b0","b1"))
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)