Problem adjusting a linear model with JAGS - r

I am trying to adjust a linear model with JAGS but I'm having trouble with the code. I'm writing:
library(R2jags)
library(BEST)
base<-data.table::data.table(read.csv("/Users/franco/Documents/Todo/UNAM/Facultad\ de\ Ciencias/Asignaturas\ Actuaría/Análisis\ Bayesiano\ de\ Datos/Tareas/Tarea-Examen\ 2/FootballLeague.csv"))
X <- cbind(1,as.matrix(base[,-c(1,2,12)]))
y <-as.matrix(base[,2])
n <- length(y)
m <- ncol(X)
model.jags <- function(){
tau ~ dgamma(0.01, 0.01)
for(i in 1:m){
beta[i] ~ dnorm(0,0.001)
}
for (i in 1:n){
y[i] ~ dnorm(x[i,]%*%beta,tau)
}
sigma <- pow(tau,-1)
}
jags.params <- c("beta","sigma")
jags.modelo <- jags(model.file=model.jags,parameters.to.save=jags.params,
data = list('n' = n,
'y' = y,
'x' = X,
'm'=m),
n.chains = 2,
n.thin=1,
DIC=FALSE,
n.burnin = 10000,
n.iter = 20000)
And R throws this error:
Error in jags.model(model.file, data = data, inits = init.values,
n.chains = n.chains, : RUNTIME ERROR: Compilation error on line 8.
Dimension mismatch in subset expression of y.
I don't know which is the error :/ Can someone help me, please.

Related

Compilation Error in jags.model: Runtime Error: Index out of range while running linear mixed model

I am trying to run linear mixed model in JAGS in R.
I am trying to see influence of year as a random factor on speciesTotal.
My code is:
data_glmm<-read.csv("data_bbs_11_19.csv")
Nyear<-length(levels(as.factor(data_glmm$Year)))
Nyear<-9
jagsData_glmm<-with(data_glmm,
list(SpeciesTotal=SpeciesTotal,
StopTotal=StopTotal,
Stratum=Stratum,
Year=Year, N=length(SpeciesTotal), Nyear=Nyear))
lm1_jags <- function() {
for (i in 1:N){
SpeciesTotal[i] ~ dnorm(mu[i], tau) # tau is precision (1 / variance)
mu[i] <- alpha + a[Year[i]] + beta * StopTotal[i] # Random intercept for year
}
# Priors:
alpha ~ dnorm(0, 0.01) # intercept
sigma_a ~ dunif(0, 100) # standard deviation of random effect (variance between year)
tau_a <- 1 / (sigma_a * sigma_a) # convert to precision
for (j in 1:9){
a[j] ~ dnorm(0, tau_a) # random intercept for each year
}
beta ~ dnorm(0, 0.01) # slope
sigma ~ dunif(0, 100) # standard deviation of fixed effect (variance within year)
tau <- 1 / (sigma * sigma) # convert to precision
}
init_values <- function(){
list(alpha = rnorm(1), sigma_a = runif(1), beta = rnorm(1), sigma = runif(1))
}
params <- c("alpha", "beta", "sigma", "sigma_a")
fit_lm3 <- jags(data = jagsData_glmm, inits = init_values, parameters.to.save = params, model.file = lm1_jags, n.chains = 3, n.iter = 20000, n.burnin = 5000, n.thin = 10, DIC = F)
While running this code, I am getting following error:
Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, :
RUNTIME ERROR:
Compilation error on line 5.
Index out of range taking subset of a
Any suggestion would be really appreciated.

How could I solve Dimension mismatch in Jags model.?

I'm super new in bayesian analysis and I'm trying to practice with an example for Classic Capture-recapture models: Mh2
This is my code
nind <- dim(venados)[1]
K <- 43
ntraps <- 13
M <- 150
nz <- M - nind
Yaug <- array(0, dim = c(M, ntraps, K))
Yaug[1:nind,,] <- venados
y <- apply(Yaug, c(1,3), sum)
y[y > 1] <- 1
Bundle data
data1 <- list(y = y, nz = nz, nind = nind, K = K, sup = Buffer)
# Model JAGS
sink("Mh2_jags.txt")
cat("
model{
# Priors
p0 ~ dunif(0,1)
mup <- log(p0/(1-p0))
sigmap ~ dunif(0,10)
taup <- 1/(sigmap*sigmap)
psi ~ dunif(0,1)
# Likelihood
for (i in 1:(nind+nz)) {
z[i] ~ dbern(psi)
lp[i] ~ dnorm(mup,taup)
logit(p[i]) <- lp[i]
y[i] ~ dbin(mu[i],K)
} # i
N <- sum(z[1:(nind+nz)])
D <- N/sup*100
} # modelo
",fill = TRUE)
sink()
# Inicial values
inits <- function(){list(z = as.numeric(y >= 1), psi = 0.6, p0 = runif(1), sigmap = runif(1, 0.7, 1.2), lp = rnorm(M, -0.2))}
params1 <- c("p0","sigmap","psi","N","D")
# MCMC
ni <- 10000; nt <- 1; nb <- 1000; nc <- 3
# JAGS and posteriors
fM2 <- jags(data1, inits, params1, "Mh2_jags.txt", n.chains = nc, n.thin = nt, n.iter = ni, n.burnin = nb)
I received this error message
Processing function input.......
Done.
Compiling model graph
Resolving undeclared variables
Deleting model
Error in jags.model(file = model.file, data = data, inits = inits, n.chains = n.chains, :
RUNTIME ERROR:
Compilation error on line 16.
Dimension mismatch in subset expression of y
I have read that some letters as s and n have to be changed. However,
I do not know what to do. Please if you could give an advice.
Thank you very much
The issue is because y is two dimensional but the model assumes it is one dimensional. If you are assuming that the secondary surveys are i.i.d. Bernoulli trials (and each session had K trials)n then you would just need to take the sum of the rows of the y matrix. Assuming this is the case then you just need to modify a couple lines at the top of this script.
nind <- dim(venados)[1]
K <- 43
ntraps <- 13
M <- 150
nz <- M - nind
Yaug <- array(0, dim = c(M, ntraps, K))
Yaug[1:nind,,] <- venados
y <- apply(Yaug, c(1,3), sum)
y[y > 1] <- 1
# Take the rowSum
y_vector <- rowSums(y)
# Use y_vector instead of y
data1 <- list(y = y_vector, nz = nz, nind = nind, K = K, sup = Buffer)
Conversely, if you wanted to include covariates for the observational process (and those covariates vary by survey) you would use the matrix y and modify the model.
sink("Mh2_jags_Kloop.txt")
cat("
model{
# Priors
p0 ~ dunif(0,1)
mup <- log(p0/(1-p0))
sigmap ~ dunif(0,10)
taup <- 1/(sigmap*sigmap)
psi ~ dunif(0,1)
# Likelihood
for (i in 1:(nind+nz)) {
z[i] ~ dbern(psi)
lp[i] ~ dnorm(mup,taup)
logit(p[i]) <- lp[i]
# Loop over K surveys
for(j in 1:K){
y[i,j] ~ dbern(p[i]*z[i])
}
} # i
N <- sum(z[1:(nind+nz)])
D <- N/sup*100
} # modelo
",fill = TRUE)
sink()
Finally, you don't specify what mu is within the model. I think you want it to be p, but you also need to link the latent state model to the observational state model (if z=0 then that individual cannot be sampled. In this case you would interpret psi as the probability that nind+nz individuals are at your site.
# Model JAGS
sink("Mh2_jags.txt")
cat("
model{
# Priors
p0 ~ dunif(0,1)
mup <- log(p0/(1-p0))
sigmap ~ dunif(0,10)
taup <- 1/(sigmap*sigmap)
psi ~ dunif(0,1)
# Likelihood
for (i in 1:(nind+nz)) {
z[i] ~ dbern(psi)
lp[i] ~ dnorm(mup,taup)
logit(p[i]) <- lp[i]
y[i] ~ dbin(p[i] * z[i],K)
} # i
N <- sum(z[1:(nind+nz)])
D <- N/sup*100
} # modelo
",fill = TRUE)
sink()

R JAGS: Dimension mismatch

I am trying to run a model in JAGS but I got the following error:
Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, :
RUNTIME ERROR:
Cannot insert node into m[1]. Dimension mismatch
The model I specified is
model = "model
{
for (i in 1:N) {
z[i] ~ dnorm(m[i],tau)
m[i] <- beta0 + beta1*x + beta2*y
}
beta0 ~ dnorm(0.0,1.0E-3)
beta1 ~ dnorm(0.0,1.0E-3)
beta2 ~ dnorm(0.0,1.0E-3)
tau ~ dgamma(0.01,0.01)
}"
Then I specify the data and the initial values. Finally, I run the model.
data = list(z = wolfcamp$data, x = wolfcamp$coords[,1], y = wolfcamp$coords[,2], N = length(wolfcamp$data))
parameters=c("beta0","beta1","beta2","tau")
init1 = list(beta0 = 0, beta1 = 0, beta2 = 0, tau = 0)
init2 = list(beta0 = 1, beta1 = 1, beta2 = 1, tau = 1)
initial.values=list(init1, init2)
model1=jags(data=data, inits=initial.values,
parameters.to.save=parameters,
model.file=textConnection(model),
n.chains=2,
n.burnin=1000,
n.iter=11000)
Can you help me? Do you know why I get this error?
Thanks in advance.
It looks like you are not indexing through the vectors x and y, which causes a dimension mismatch.
Change this line:
m[i] <- beta0 + beta1*x + beta2*y
to this:
m[i] <- beta0 + beta1*x[i] + beta2*y[i]
and you should be good to go (so long as x and y are of length N)

R Jags Multinomial error "length mismatch in node:setValue"

I am trying to run a multinomial model in rjags and I keep getting this error
Error in jags.model(model.file, data = data, inits = init.values,
n.chains = n.chains, :
Error in node dmulti(b,639)
Length mismatch in Node::setValue
Here is the code that creates the error:
n = c(294, 307,38)
m= c(288, 332,19)
x=1
y=1
z=1
model_string <-"model {
for(j in 1:3) {
n[j] ~ dmulti( a[1:3], 639)
m[j] ~ dmulti(b[1:3], 639)
}
a[1:3] ~ ddirich(c(x,y,z))
b[1:3]~ ddirich(c(x,y,z))
prob = step(b[1]-a[1])
RD = b[1]-a[1]
}"
jags.param <- c("a[1]","b[1]", "prob", "RD")
jags.data <- list(n=n,m=m, x=x, y=y,z=z)
jagsfit4 <- jags(data=jags.data, n.chains = 4, inits=NULL, parameters.to.save = jags.param,
model.file=textConnection(model_string), n.thin = 1 ,n.iter=5000,
n.burnin=1500, DIC=TRUE)
Thank you!

Variable Lengths Differ Error

Throughout my function I have arguments z and y
I want z to be equal to a data set (for example birthwt) and y to be equal to a response variable (for example birthwt$low)
library("MASS")
library("dplyr")
data(birthwt)
foo=function(z,y){
n.folds <- 10
folds <- cut(sample(seq_len(nrow(z))), breaks=n.folds, labels=FALSE)
all.confusion.tables <- list()
for (i in seq_len(n.folds)) {
train <- filter(z, folds != i)
test <- filter(z, folds == i)
glm.train <- glm(y ~.,family = binomial, data = train)
mod_pred_probs =predict(glm.train,test, type= "response")
pred.class <- ifelse(mod_pred_probs< 0, 0, 1)
all.confusion.tables[[i]] <- table(pred = pred.class, true = test$y)
}
misclassrisk <- function(x) { (sum(x) - sum(diag(x)))/sum(x) }
risk <- sapply(all.confusion.tables, misclassrisk)
return(table(risk))
mean(risk)}
When I run foo(birtht,"low")
I get the error:
Error in model.frame.default(formula = y ~ ., data = train, drop.unused.levels = TRUE) :
variable lengths differ (found for 'low')
Does any one know why I am getting the error or how I can avoid it?

Resources