从菜单中输入一个项目,或0退出

时间:2018-07-18 15:58:28

标签: r hierarchical-data jags

我正在运行来自生态学中的分层建模的代码(来自Kéry和Royle(2015)),以了解此主题。但是我收到以下警告:No suitable frames for recover()。我想知道这讲的意思是什么。 这是代码:

cat(file = "ANCOVA2.txt","
model {
# Priors
for(k in 1:4){
alpha[k] ~ dnorm(0, 1.0E-06) # Priors for intercepts
beta[k] ~ dnorm(0, 1.0E-06) # Priors for slopes
}
tau <- pow(sd, -2)
sd ~ dunif(0, 1000) # Prior for dispersion on sd scale
# Likelihood
for (i in 1:M){
Cmean[i] ~ dnorm(mu[i], tau) # precision tau = 1 / variance
mu[i] <- alpha[facFor[i]] + beta[facFor[i]] * elev[i]
}
# Derived quantities: comparison of slopes 
for(k in 1:4){
diff.vs1[k] <- beta[k] - beta[1] # Differences relative to beta[1]
diff.vs2[k] <- beta[k] - beta[2] # ... relative to beta[2]
diff.vs3[k] <- beta[k] - beta[3] # ... relative to beta[3]
diff.vs4[k] <- beta[k] - beta[4] # ... relative to beta[4]
  }
  }
  ")
 # Initial values
inits <- function() list(alpha = rnorm(4,,10), beta = rnorm(4,,10))
# Parameters monitored
 params <- c("alpha", "beta", "sd", "diff.vs1", "diff.vs2", "diff.vs3", 
 "diff.vs4")
 # MCMC settings
 ni <- 6000 ; nt <- 1 ; nb <- 1000 ; nc <- 3
 out4J <- jags(win.data, inits, params, "ANCOVA2.txt", n.chains = nc,
          n.thin = nt, n.iter = ni, n.burnin = nb)
traceplot(out4J)
print(out4J, 2)

#plot the predicted response as a function of the explanatory variables 
facFor and elev 

plot(elev[facFor==1], Cmean[facFor==1], col = "red", ylim = c(0, 20), xlab = 
"Elevation",
 ylab = "", frame.plot = F)
points(elev[facFor==2], Cmean[facFor==2], col = "blue")
points(elev[facFor==3], Cmean[facFor==3], col = "green")
points(elev[facFor==4], Cmean[facFor==4], col = "black")


attach.bugs(out4J) # Allows to directly address the sims.list 
#No sé si esta función se puede usar con JAGSUI, tal vez con rjags
str(diff.vs3)
par(mfrow = c(1, 3), mar = c(5,5,3,2), cex.lab = 1.5, cex.axis = 1.5)
hist(diff.vs3[,1], col = "grey", breaks = 100, main = "", freq=F, ylim = c(0, 
0.8))
abline(v = 1, lwd = 3, col = "red")
hist(diff.vs3[,2], col = "grey", breaks = 100, main = "", freq=F, ylim = c(0, 
0.8))
abline(v = 1, lwd = 3, col = "red")
hist(diff.vs3[,4], col = "grey", breaks = 100, main = "", freq=F, ylim = c(0, 
0.8))
abline(v = 1, lwd = 3, col = "red")
diff.vs3
# Prob. difference greater than 1
mean(diff.vs3[,1] > 1)
mean(diff.vs3[,2] > 1)
mean(diff.vs3[,4] > 1)

非常感谢! 问候 安托拉

0 个答案:

没有答案