我有一个索赔计数数据集,y为索赔计数,16个协变量,即x1到x16(由0和1组成),我将其排列在名为X和E的设计矩阵中作为曝光(也称为偏移)。我正在尝试使用JAGS将Poisson回归拟合到此数据集。我为模型部分编写的代码如下:
Poisson.model <- function(){
for(i in 1:N){
y[i] ~ dpois(lambda[i])
log(lambda[i]) <- log(E[i]) + beta1+ beta2*x1[i] + beta3*x2[i] + beta4*x3[i] + beta5*x4[i] + beta6*x5[i] + beta7*x6[i] + beta8*x7[i] + beta9*x8[i] + beta10*x9[i] + beta11*x10[i] + beta12*x11[i] + beta13*x12[i] + beta14*x13[i] + beta15*x14[i] + beta16*x15[i] + beta17*x16[i]
}
###declare priors
beta1 ~ dnorm(0,0.0001)
beta2 ~ dnorm(0,0.0001)
beta3 ~ dnorm(0,0.0001)
beta4 ~ dnorm(0,0.0001)
beta5 ~ dnorm(0,0.0001)
beta6 ~ dnorm(0,0.0001)
beta7 ~ dnorm(0,0.0001)
beta8 ~ dnorm(0,0.0001)
beta9 ~ dnorm(0,0.0001)
beta10 ~ dnorm(0,0.0001)
beta11 ~ dnorm(0,0.0001)
beta12 ~ dnorm(0,0.0001)
beta13 ~ dnorm(0,0.0001)
beta14 ~ dnorm(0,0.0001)
beta15 ~ dnorm(0,0.0001)
beta16 ~ dnorm(0,0.0001)
beta17 ~ dnorm(0,0.0001)
}
我的问题是,1)如何将X和beta作为矩阵乘法来替换log(lambda [i])右侧的冗长等式2)如何将先验简化为单行?
答案 0 :(得分:4)
使用函数inprod
和函数model.matrix
传递矩阵,如下所示
X<-model.matrix(~covariate1+covariate2,data=data)
for(i in 1:17){ beta[i] ~ dnorm(0, 0.0001)}
inprod(beta[], X[i,])+log(E[i])