Search code examples
rjagsr2jagshierarchical-bayesian

How to deal with "Non-conforming parameters with inprod function" in JAGS model


I am trying to model the variance in overall species richness with the habitat covariates of a camera trapping station using R2jags. However, I keep getting the error:

"Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Non-conforming parameters in function inprod"

I used a very similar function in my previous JAGS model (to find the species richness) so I am not sure why it is not working now...

I have already tried formatting the covariates within the inprod function in different ways, as a data frame and a matrix, to no avail.

Variable specification:

J=length(ustations) #number of camera stations

NSite=Global.Model$BUGSoutput$sims.list$Nsite
NS=apply(NSite,2,function(x)c(mean(x)))

###What I think is causing the problem:

COV <- data.frame(as.numeric(station.cov$NDVI), as.numeric(station.cov$TRI), as.numeric(station.cov$dist2edge), as.numeric(station.cov$dogs), as.numeric(station.cov$Leopard_captures))

###but I have also tried:

COV <- cbind(station.cov$NDVI, station.cov$TRI, station.cov$dist2edge, station.cov$dogs, station.cov$Leopard_captures)

JAGS model:

sink("Variance_model.txt")
cat("model {
# Priors
Y ~ dnorm(0,0.001)              #Mean richness
X ~ dnorm(0,0.001)              #Mean variance

for (a in 1:length(COV)){
U[a] ~ dnorm(0,0.001)}      #Variance covariates

# Likelihood
for (i in 1:J) { 
mu[i] <- Y          #Hyper-parameter for station-specific all richness
NS[i] ~ dnorm(mu[i], tau[i])   #Likelihood
tau[i] <- (1/sigma2[i])
log(sigma2[i]) <- X + inprod(U,COV[i,])
}
}
", fill=TRUE)
sink()

var.data <- list(NS = NS, 
                 COV = COV,
                 J=J)

Bundle data:

# Inits function
var.inits <- function(){list(
  Y =rnorm(1), 
  X =rnorm(1), 
  U =rnorm(length(COV)))}

# Parameters to estimate
var.params <- c("Y","X","U")

# MCMC settings
nc <- 3
ni <-20000
nb <- 10000
nthin <- 10

Start Gibbs sampler:

jags(data=var.data,
     inits=var.inits,
     parameters.to.save=var.params,
     model.file="Variance_model.txt", 
     n.chains=nc,n.iter=ni,n.burnin=nb,n.thin=nthin)

Ultimately, I get the error:

Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Deleting model

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Non-conforming parameters in function inprod

In the end, I would like to calculate the mean and 95% credible interval (BCI) estimates of the habitat covariates hypothesized to influence the variance in station-specific (point-level) species richness.

Any help would be greatly appreciated!


Solution

  • It looks like you are using length to generate the priors for U. In JAGS this function will return the number of elements in a node array. In this case, that would be the number of rows ins COV multiplied by the number of columns.

    Instead, I would supply a scalar to your data list that you supply to jags.model.

    var.data <- list(NS = NS, 
                     COV = COV,
                     J=J,
                     ncov = ncol(COV)
    )
    

    Following this, you can modify your JAGS code where you are generating your priors for U. The model would then become:

    sink("Variance_model.txt")
    cat("model {
    # Priors
    Y ~ dnorm(0,0.001)              #Mean richness
    X ~ dnorm(0,0.001)              #Mean variance
    
    for (a in 1:ncov){ # THIS IS THE ONLY LINE OF CODE THAT I MODIFIED
    U[a] ~ dnorm(0,0.001)}      #Variance covariates
    
    # Likelihood
    for (i in 1:J) { 
    mu[i] <- Y          #Hyper-parameter for station-specific all richness
    NS[i] ~ dnorm(mu[i], tau[i])   #Likelihood
    tau[i] <- (1/sigma2[i])
    log(sigma2[i]) <- X + inprod(U,COV[i,])
    }
    }
    ", fill=TRUE)
    sink()