Hi Dr. Hollanders,
here is the code, almost the same as in the book.
# Draw a replicate data set under the fitted model
for (i in 1:nsites){
for (j in 1:nsurveys){
yrep[i,j] ~ dbern(z[i] * p)
}
}
# Compute detection frequencies for observed and replicated data
for (i in 1:nsites){
# Det. frequencies for observed and replicated data
detfreq[i] <- sum(y[i,])
detfreqrep[i] <- sum(yrep[i,])
# Expected detection frequencies under the model
for (j in 1:nsurveys){
tmp[i,j] <- z[i] * p
}
E[i] <- sum(tmp[i,]) # Expected number of detections
# Chi-square and Freeman-Tukey discrepancy measures
# ..... for actual data set
x2[i] <- pow((detfreq[i] - E[i]),2) / (E[i]+0.00000001)
ft[i] <- pow((sqrt(detfreq[i]) - sqrt(E[i])),2)
# ..... for replicated data set
x2rep[i] <- pow((detfreqrep[i] - E[i]),2) / (E[i]+0.00000001)
ftrep[i] <- pow((sqrt(detfreqrep[i]) - sqrt(E[i])),2)
}
# Add up Chi-square and FT discrepancies and compute fit stat ratio (closed part)
Chi2 <- sum(x2)
FT <- sum(ft)
Chi2rep <- sum(x2rep)
FTrep <- sum(ftrep)
Chi2.chat <- Chi2 / Chi2rep
FT.chat <- FT / FTrep
Chi2.bpv <- step(Chi2rep-Chi2)
FT.bpv <- step(Chi2rep-Chi2)
Thanks for your response,
Luca