Reputation: 1
I am working with a dataframe with 234 rows of 4 variables: Fish Age, Mercury Concentration (Hg), Distance from Mouth, and Site #. I am trying to run a multi linear regression to see how Age and Distance from Mouth affect Mercury Concentrations of fish. When I run the code in R, I get the error message:
Error in jags.model(file = model.file, data = data, inits = inits, n.chains =n.chains, :
RUNTIME ERROR:
Compilation error on line 19.
Index out of range taking subset of pred.x2
Any help is greatly appreciated, and my code is below.
library(jagsUI)
#library(coda)
##### READ AND PREPARE DATA #####
dat = read.csv("Exercise_1_3_data-1.csv")
#plot the data
plot(Hg ~ Age, data = dat)
plot(Hg ~ Dist, data = dat)
# x1=Age
# x2=Dist
# compile data into a list to pass to BUGS
pred.x1 = seq(1, 10, 0.5) # predicted x1 values
pred.x2 = seq(0, 90, 10) # predicted x2 values
jags.dat = list(n.obs = nrow(dat), y = dat$Hg, x1 = dat$Age, x2 = dat$Dist,
pred.x1 = pred.x1, pred.x2 = pred.x2, n.pred=length(pred.x1))
##### SPECIFY MODEL CODE #####
cat(file="model.txt", "
model {
# PRIORS
b0 ~ dnorm(0,1E-6)
b1 ~ dnorm(0, 1E-6)
b2 ~ dnorm(0, 1E-6)
sig ~ dunif(0,100)
tau <- 1/pow(sig, 2)
# LIKELIHOOD
for(i in 1:n.obs){
y[i] ~ dnorm(y.hat[i], tau)
y.hat[i] <- b0 + b1 * x1[i] + b2 * x2[i]
}
#DERIVED QUANTITIES
for(i in 1:n.pred){
pred.y[i] <- b0 + b1 * pred.x1[i] + b2 * pred.x2[i]
}
}
")
##### INITIAL VALUES #####
inits1 = list(b0 = rnorm(1), b1 = rnorm(1), b2 = rnorm(1), sig = rlnorm(1))
inits2 = list(b0 = rnorm(1), b1 = rnorm(1), b2 = rnorm(1), sig = rlnorm(1))
inits = list(inits1, inits2)
##### PARAMETERS TO MONITOR #####
params = c("b0", "b1", "b2", "sig", "pred.y")
##### MCMC DIMENSIONS #####
ni = 5000
nb = 1000
nt = 1
nc = 2
##### RUN THE MODEL IN jags #####
# should take about 10 seconds
starttime = Sys.time()
jags.fit = jags(data = jags.dat, inits = inits, parameters.to.save = params, model.file = "model.txt",
n.thin = nt, n.chains = nc, n.burnin = nb, n.iter = ni, parallel = F)
Upvotes: 0
Views: 44
Reputation: 4417
I am neither familiar with JAGS nor that package but I can see, that length(pred.x1) == 19
and length(pred.x2)== 10
so that might explain why pred.x2[i]
goes out of bounds with i
in 1:length(pred.x1)
.
Upvotes: 1