簡體   English   中英

在JAGS中為R指定分層模型

[英]specifying a hierarchical model in JAGS for R

我有一些相關變量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))

我可以使用數據框plot_datay建模為x1x2的函數,而在plot_data沒有問題,它基本上將每個站點的x2站點級別觀測值復制10次。

但是我真正想做的是對模型進行分層擬合,以使y[i] ~ x1[i] + x2[j] ,其中[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)

您只需要在響應級別上與站點內唯一級別相對應的索引向量即可(如果將其編碼為一個因子,這是最簡單的)。 以下模型與您已經擁有的模型完全等效:

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估計很小,因為它沒有出現在數據模擬中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM