JAGS线性AR1预测模型具有嵌套组
我正在尝试用平衡的嵌套组编写AR1线性模型,以预测因变量中的结果。例如,假设我们有两组每组三个观测值,其中每组中的第三个观察结果都缺少。目的是编写一个模型,该模型使用结果变量中的先前观察结果预测每个组中的第三个观察结果。我很难找出编写嵌套模型的正确方法。下面的代码给出了一个错误,“索引超出了组的子集”。任何帮助将不胜感激。
data<-data.frame(group=c(1, 1, 1, 2, 2, 2), y=c(3, 5, NA, 10, 2, NA))
model<-function(){
for(i in 1:6){
y[group[i]]~dnorm(mu[group[i]], tau)
mu[group[i]]<-beta[1] + beta[2]*y[group[i-1]]
}
for(i in 1:2){
beta[i]~dnorm(0, .01)
}
tau~dgamma(.01, .01)
}
model.data<-list("group", "y")
model.data<-list(group=data$group, y=data$y)
model.params<-c("y")
model.fit<-jags(data=model.data, inits=NULL, model.params, n.chains=2, n.iter=10000, n.burnin=1000, model.file=model)
I'm trying to write an AR1 linear model in JAGS with balanced, nested groups, that forecasts outcomes in the dependent variable. As an example, suppose we have two groups with three observations per group where the third observation in each group is missing. The objective is to write a model that forecasts the third observation in each group using previous observations in the outcome variable. I'm having trouble figuring out the correct way to write the nested model. The code below gives an error, "Index out of range taking subset of group." Any help is greatly appreciated.
data<-data.frame(group=c(1, 1, 1, 2, 2, 2), y=c(3, 5, NA, 10, 2, NA))
model<-function(){
for(i in 1:6){
y[group[i]]~dnorm(mu[group[i]], tau)
mu[group[i]]<-beta[1] + beta[2]*y[group[i-1]]
}
for(i in 1:2){
beta[i]~dnorm(0, .01)
}
tau~dgamma(.01, .01)
}
model.data<-list("group", "y")
model.data<-list(group=data$group, y=data$y)
model.params<-c("y")
model.fit<-jags(data=model.data, inits=NULL, model.params, n.chains=2, n.iter=10000, n.burnin=1000, model.file=model)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
在2022-07-07创建的 preprex package (v2.0.1)< /sup>
如果您有平衡的数据,最简单的事情是将
y
放在n_obs
中xn_groups
矩阵。然后,AR(1)部分变得容易。How about this:
Created on 2022-07-07 by the reprex package (v2.0.1)
If you've got balanced data, the easiest thing to do is to put
y
in an_obs
xn_groups
matrix. Then, the AR(1) part becomes easy.