Search code examples
rregressionpoissonjags

Poisson regression with multiple covariates using JAGS:How to simplify model


I have a claim count dataset with y as claim counts,16 covariates namely x1 to x16(consists of 0 and 1) which I arranged in a design matrix called X and E as exposure (also called offset). I'm trying to fit Poisson regression to this dataset using JAGS. The codes I wrote for the model part are as below:

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)

}

My question is, 1)How do I make X and beta as matrix multiplication to substitute the lengthy equation on the right side of log(lambda[i]) 2)How do I simplify the priors to one single line?


Solution

  • with the function inprod and the function model.matrix you pass the matrix as follows

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