在JAGS中为R指定分层模型

在JAGS中为R指定分层模型,r,jags,runjags,rjags,R,Jags,Runjags,Rjags,我有一些相关变量y的数据,这些变量可以建模为协变量x1和x2的函数y和x1在“绘图”级别观察到,而x2在“场地”级别观察到。绘图按层次嵌套在站点中。这里有100个观察到的y,以及相关的协变量数据 #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 o

我有一些相关变量
y
的数据,这些变量可以建模为协变量
x1
x2
的函数<代码>y和
x1
在“绘图”级别观察到,而
x2
在“场地”级别观察到。绘图按层次嵌套在站点中。这里有100个观察到的
y
,以及相关的协变量数据

#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))

您只需要在响应级别上创建一个索引向量,该索引向量对应于站点中的唯一级别(如果将其编码为一个因子,则最简单)。以下模型与您已有的模型完全相同:

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)
jags.model=”
模型{
#前科
b1~dnorm(0.001)
b2~dnorm(0.001)

tau一如既往地感谢Matt!你有没有可能在这个示例中添加我如何添加站点的随机效果,或者你认为值得打开一个单独的问题?这只是对模型的一个小修改,因此不需要单独的问题-请参阅我的更新答案。
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)
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)