Node inconsistent with parents

35 views
Skip to first unread message

Alaa Eldeen

unread,
Sep 14, 2023, 6:44:34 AM9/14/23
to hmecology: Hierarchical Modeling in Ecology
Dear All,

I'm very beginner in Jags, this is why I could figure out how to solve the  "Node inconsistent with parents" error message.

I used the following code, but it gives node incosistent error message 

Error in checkForRemoteErrors(val) :
  one node produced an error: Error in node n[2]
Node inconsistent with parents
#####################
# JAGS model

library(spatstat)

# Camera data

obs<- read.csv("foxdet_march2014.csv",header=T)
obs<- as.matrix(obs)
colnames(obs)<- 1:dim(obs)[2]
fox.obs<- obs

n<- rowSums(fox.obs,na.rm=T)
K<- apply(fox.obs,1,function(x) length(x[!is.na(x)]))

# camera locations
locs<- read.csv("camlocs_march2014.csv",header=T)
locs<- locs/1000

xlim<- range(locs[,1])
ylim<- range(locs[,2])
buffer<- 2
xlim[1]<- xlim[1] - buffer
xlim[2]<- xlim[2] + buffer
ylim[1]<- ylim[1] - buffer
ylim[2]<- ylim[2] + buffer
area<- (xlim[2]-xlim[1]) * (ylim[2]-ylim[1])


X<- as.matrix(locs)
M=200
J<- nrow(locs)

data<- list("n","X","M","J","K","xlim","ylim")

inits<- function(){list(sigma=runif(1,0.2,1),g0=runif(1,0.1,0.5),psi=0.5,w=rbinom(200,1,0.5))}

params<- c("N","g0","sigma")

modelFilename = "SPA_JAGS.txt"

cat("
model {

for(i in 1:M) {
  w[i] ~ dbern(psi)
  S[i,1] ~ dunif(xlim[1],xlim[2])
  S[i,2] ~ dunif(ylim[1],ylim[2])

  for(j in 1:J) {
    d2[i,j]<- (S[i,1] - X[j,1])^2 + (S[i,2] - X[j,2])^2
    prob[i,j]<- g0 * exp(-d2[i,j]/2/sigma^2) * w[i]

  }
}

for(j in 1:J) {
  Ptrap[j]<- 1-prod(1-prob[,j])
  n[j] ~ dbin(Ptrap[j],K[j])    
  }


sigma ~ dgamma(18,30)
g0 ~ dbeta(1,1)
psi ~ dbeta(1,1)
N<- sum(w)

}
", fill=TRUE, file=modelFilename)

# NOTE increase n.iter in the following to achive convergence if needed.
library(R2jags)
fit.gp = jags.parallel(data=data, inits=inits, params, model.file=modelFilename, n.chains=3, n.iter=2000, n.burnin=1000, n.thin=1)


I appreciate your help

Alaa
camlocs_march2014.csv
foxdet_march2014.csv
Reply all
Reply to author
Forward
0 new messages