我有一些相关变量的数据,这些y
变量可以建模为协变量x1
和x2
. y
和x1
在“地块”级别x2
观察, 和 在“地点”级别观察。情节嵌套在站点内,分层。y
以下是 100 个与相关协变量数据相关的观察结果。
#generate covariate data at plot and site scales.
x1 <- runif(100,0,1) #100 plot level observations of x1
x2 <- runif(10,10,20) #10 site level observations of x2
#generate site values - in this case characters A:J
site_1 <- LETTERS[sort(rep(seq(1,10, by = 1),10))]
site_2 <- LETTERS[sort(seq(1,10, by = 1))]
#put together site level data - 10 observations for 10 sites.
site_data <- data.frame(site_2,x2)
colnames(site_data) <- c('site','x2')
#put together plot level data - 100 observations across 10 sites
plot_data <- data.frame(site_1,x1)
colnames(plot_data) <- c('site','x1')
plot_data <- merge(plot_data,site_data, all.x=T) #merge in site level data.
#pick parameter values.
b1 <- 10
b2 <- 0.2
#y is a function of the plot level covariate x1 and the site level covariate x2.
plot_data$y <- b1*plot_data$x1 + b2*plot_data$x2 + rnorm(100)
#check that the model fits. it does.
summary(lm(y ~ x1 + x2, data = plot_data))
我可以使用基本上复制 每个站点 10 次的站点级别观察的数据框架,将其建模为 jagsy
的函数x1
并且没有问题。x2
plot_data
x2
然而,我真正想做的是分层拟合模型,
y[i] ~ x1[i] + x2[j]
其中 , where[i]
表示地块级别的观察和[j]
索引站点。我怎样才能修改下面的 JAGS 代码来做到这一点?
#fit a JAGS model
jags.model = "
model{
# priors
b1 ~ dnorm(0, .001)
b2 ~ dnorm(0, .001)
tau <- pow(sigma, -2)
sigma ~ dunif(0, 100)
#normal model
for (i in 1:N){
y[i] ~ dnorm(y.hat[i], tau)
y.hat[i] <- b1*x1[i] + b2*x2[i]
}
} #end model
"
#setup jags data as a list
jd <- list(y=plot_data$y, x1=plot_data$x1, x2=plot_data$x2, N=length(plot_data$y))
library(runjags)
#run jags model
jags.out <- run.jags(jags.model,
data=jd,
adapt = 1000,
burnin = 1000,
sample = 2000,
n.chains=3,
monitor=c('b1', 'b2'))
summary(jags.out)