1

我有一些相关变量的数据,这些y变量可以建模为协变量x1x2. yx1在“地块”级别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并且没有问题。x2plot_datax2

然而,我真正想做的是分层拟合模型, 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)
4

1 回答 1

3

您只需要一个对应于站点内唯一级别的响应级别的索引向量(如果将其编码为一个因素,这是最简单的)。以下模型与您已有的模型完全相同:

jags.model = "
model{
    # priors
    b1 ~ dnorm(0, .001)
    b2 ~ dnorm(0, .001)
    tau <- pow(sigma, -2)
    sigma ~ dunif(0, 100)

    # Response:
    for (i in 1:N){
        y[i] ~ dnorm(y.hat[i], tau)
        y.hat[i] <- b1*x1[i] + site_effect[plot_site[i]]
    }

    # Effect of site:
    for (s in 1:S){
        site_effect[s] <- b2 * x2_site[site_site[s]]
    }

}
"
# Ensure the site is coded as a factor with the same levels in both data frames:
plot_data$site <- factor(plot_data$site)
site_data$site <- factor(site_data$site, levels=levels(plot_data$site))

#setup jags data as a list
jd <- list(y=plot_data$y, x1=plot_data$x1, plot_site=plot_data$site, 
            site_site=site_data$site, x2_site=site_data$x2, 
            N=length(plot_data$y), S=nrow(site_data))

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)

分层方法的优点是现在可以修改站点的效果以例如合并随机效果或其他效果。

马特


编辑以添加随机效果的示例


以下代码添加了站点的随机效果以及对应于 x2 的固定效果:

jags.model = "
model{
    # priors
    b1 ~ dnorm(0, .001)
    b2 ~ dnorm(0, .001)
    tau <- pow(sigma, -2)
    sigma ~ dunif(0, 100)
    tau.site <- pow(sigma.site, -2)
    sigma.site ~ dunif(0, 100)

    # Response:
    for (i in 1:N){
        y[i] ~ dnorm(y.hat[i], tau)
        y.hat[i] <- b1*x1[i] + site_effect[plot_site[i]]
    }

    # Effect of site (fixed and random effects):
    for (s in 1:S){
        site_effect[s] <- b2 * x2_site[site_site[s]] + random[site_site[s]]
        random[site_site[s]] ~ dnorm(0, tau.site)
    }

}
"
# Ensure the site is coded as a factor with the same levels in both data frames:
plot_data$site <- factor(plot_data$site)
site_data$site <- factor(site_data$site, levels=levels(plot_data$site))

#setup jags data as a list
jd <- list(y=plot_data$y, x1=plot_data$x1, plot_site=plot_data$site, 
            site_site=site_data$site, x2_site=site_data$x2, 
            N=length(plot_data$y), S=nrow(site_data))

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', 'sigma.site', 'sigma'))
summary(jags.out)

对于您的应用程序来说,这可能是一个合理的模型,也可能不是——这只是一个示例。在这种情况下,sigma.site 估计非常小,因为它没有在数据模拟中出现。

于 2017-10-25T08:33:46.890 回答