1 year ago

#379294

test-img

Harsh

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.

r

mixed-models

r2jags

0 Answers

Your Answer

Accepted video resources