我有兴趣在WinBUGS中拟合一个贝叶斯层次模型,该模型可以处理多年以来每个位置的重复测量。
我模拟了一些数据以显示我要适应的条件。总共有两年,每年访问许多地点。在第1年,访问了5个位置,第2年,还访问了2个新位置,总共访问了7个位置。每个位置都有对应的每周计数测量(周= 15)。
每周测量值定义为周之间函数的差。该函数定义为:
f(week) = alpha\*exp(-exp(beta - gamma\*(week - delta))) - alpha\*exp(-exp(beta - gamma\*((week-1) - delta)))
因此,参数alpha和beta将针对区域和年份。伽玛和增量仅针对特定年份。问题在于,每年的营业地点数量不平衡,第二年访问了新的营业地点。因此,我很难解决这个问题。
我尝试了嵌套索引方法,但是拟合度很差,并且我怀疑这与代码有关。
library(R2WinBUGS)
set.seed(902010)
#define Modified Gompertz
Gompertz_fn <- function(alpha, beta, gamma, delta, t){
round(alpha*exp(-exp(beta-gamma*(t-delta))))
}
#coefficients for GC, with alpha and beta different between years
alpha <- list(alpha1 = round(rnorm(5,100,1)), alpha2=round(rnorm(7, 75, 1)))
beta <- list(beta1 = (rnorm(5,1.5,0.05)), beta2=(rnorm(7,1, 0.05)))
gamma <- 0.5
delta<- 2.5
weeks <- 1:15
#matrix to store observed values
y1 <- matrix(NA,nrow=15,ncol=5) #year one, five areas for year one, 15 measurements
y2 <- matrix(NA,nrow=15,ncol=7) #year two, seven areas for year two, 15 measurements
#obtain observed counts for each area for year 1 and 2
for(i in 1:5){
y1[,i] <- Gompertz_fn(alpha=alpha[[1]][i], beta=beta[[1]][i], gamma=gamma, delta=delta, t=weeks) }
for(i in 1:7){
y2[,i] <- Gompertz_fn(alpha=alpha[[2]][i], beta=beta[[2]][i], gamma=gamma, delta=delta, t=weeks) }
#save in data frame
df <- data.frame(obs= c(y1,y2), area=c(rep(1:5,each=15), rep(1:7,each=15)),
week=rep(1:15,12), year = c(rep(1, 5*15), rep(2,7*15)))
#Winbugs model
##########
sink("model1.txt")
cat("
model{
for(i in 1:n.obs){ #loop through observations
y[i] ~ dpois(mu_area[i])
log(mu_area[i]) <- alpha[area[i]]*exp(-exp(beta[area[i]] - gamma[year[i]]*(week[i] - delta[year[i]]))) -
alpha[area[i]]*exp(-exp(beta[area[i]] - gamma[year[i]]*((week[i]-1) - delta[year[i]])))
}
for(i in 1:n.areas){ #loop through areas
alpha[area.2[i]] ~ dnorm(mu_alpha[years_areas[i]],tau_alpha[years_areas[i]])
beta[area.2[i]] ~ dnorm(mu_beta[years_areas[i]],tau_beta[years_areas[i]])
}
for(i in 1:n.years){ #loop through years
mu_alpha[i] ~dnorm(0,0.001)
mu_beta[i] ~dnorm(0,0.001)
tau_alpha[i]~dgamma(0.1,1)
tau_beta[i] ~ dgamma(0.1,1)
gamma[i] ~ dnorm(0,0.1)
delta[i] ~ dnorm(0,0.1)
}
}
",fill=TRUE)
sink()
##############
##################################################################################
#winbugs data
df1 <- list(y=df$obs,year= df$year,
week=df$week,area=df$area,
n.obs=180,n.areas=12,n.years=2 ,
years_areas=c(rep(1,5), rep(2,7)),
area.2 = 1:12)
#params to save
params <- c("alpha","beta","gamma","delta",
"mu_alpha","mu_beta","tau_alpha","tau_beta")
#initial values
inits <- list(alpha=c(rep(100,5),rep(75,7)),
beta = c(rep(3,5), rep(2,7)),
gamma=rep(0.5,2), delta=rep(0,2),
mu_alpha=c(100,50), mu_beta=c(3,2),
tau_alpha=rep(1,2), tau_beta=rep(1,2))
#run model
b2 <- bugs(data = df1, inits = list(inits),
parameters.to.save = params,
n.chains = 1, n.iter = 10000,
n.burnin =5000,
n.thin = 10, DIC=T,
codaPkg = F,
model.file = "model1.txt",
bugs.directory = paste0(Sys.getenv(c("USERPROFILE")), "\\WinBUGS14"),debug=T)
我希望获得每个位置和年份的参数alpha和beta。 γ,δ,mu_alpha,tau_alpha,mu_beta和tau_beta是特定年份。但是我的输出显示,alpha和beta是特定于区域的,但不能按年份区分。