我试图从JAGS的GeoBUGS手册中复制苏格兰唇癌的例子。但是,我不断从dmnorm函数中得到以下错误"无效的父值"。我手动将精确矩阵输入JAGS,我知道这是可以接受的。不知道发生了什么事,看到一双新鲜的眼睛会很高兴。下面是(希望)一个可重复的例子。
#######################################
## Understanding CAR prior with JAGS ##
#######################################
#Load libraries
library(rjags)
library(rgdal)
library(spdep)
library(utils)
#Download data
setwd(tempdir())
setInternet2(use=TRUE)
download.file('https://geodacenter.org/downloads/data-files/scotlip.zip','scotlip.zip')
unzip('scotlip.zip')
scotland = readOGR('scotlip.shp',layer='scotlip')
#Extract data (get rid of islands for simplicity)
scot.data = scotland@data[which(!1:56%in%c(6,8,11)),c('DISTRICT','CANCER','CEXP','AFF')]
#Extract adjacency matrix (get rid of islands for simplicity)
scot.adj = poly2nb(scotland[which(!1:56%in%c(6,8,11)),])
#Visualize
plot(scotland[which(!1:56%in%c(6,8,11)),],border="grey")
plot(scot.adj,coordinates(scotland[which(!1:56%in%c(6,8,11)),]),pch=16,add=TRUE)
#Construct weight matrix (or proximity matrix)
scot.weights = nb2mat(scot.adj,style="B")
#Set spatial correlation for ICAR and compute precision matrix
alpha = 1/max(eigen(scot.weights)$values)
tau2 = 1
P = diag(rowSums(scot.weights))%*%(diag(nrow(scot.weights))-alpha*scot.weights)/tau2
#Is P valid?
library(MASS)
Sigma = chol2inv(P)
mvrnorm(n=1,mu=rep(0,dim(Sigma)[1]),Sigma=Sigma) #Useful for simulations later on
#Load the data
lips.data = list(
N=nrow(scot.data),
O=scot.data$CANCER,
E=scot.data$CEXP,
X=scot.data$AFF,
Tau=P
)
#Model file
sink("ScottishLipCancer.txt")
cat("data{
for(i in 1:N){ zeros[i] <- 0 }
}model{
#Likelihood
for(i in 1:N){
O[i] ~ dpois(mu[i])
log(mu[i]) <- log(E[i]) + alpha0 + alpha1 * X[i]/10 + b[i]
#Area-specific relative risk (for maps)
RR[i] <- exp(alpha0 + alpha1 * X[i]/10 + b[i])
}
#ICAR prior distribution for random effects:
b[1:N] ~ dmnorm(zeros[],Tau[,])
#Other priors:
alpha0 ~ dnorm(0.0,1.0E-5)
alpha1 ~ dnorm(0.0,1.0E-5)
tau ~ dgamma(0.5,0.0005)
sigma <- sqrt(1/tau)
b.mean <- sum(b[])
}
",fill=TRUE)
sink()
#Initial values for MCMC chains
lips.inits = list(tau=1,alpha0=0,alpha1=0,b=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
#Run JAGS
m = jags.model(file='ScottishLipCancer.txt',data=lips.data,inits=lapply(1:3,function(x)lips.inits),n.chains=3,n.adapt=10)
我想使用JAGS的后退是你必须指定联合分布(通过dmnorm)而不是像ICAR之前的BUGS那样使用一堆条件。任何帮助,将不胜感激!
答案 0 :(得分:0)
就我个人而言,我认为这是你的零[]代码不正确。 jags无法告诉zeros []的大小,所以应该在“数据”中明确说明。部分,或作为R
的变量传入尝试运行
cat("
data zeros[N];
model{
for(i in 1:N){
zeros[i] <- 0
}
...
}
")
答案 1 :(得分:0)
Martyn Plummer纠正了我的代码中的一些错误,并在JAGS网站上发布了答案。这个讨论可以在这里找到 http://sourceforge.net/p/mcmc-jags/discussion/610037/thread/b862b65b/