Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206235

Translate Negative Binomial Type I and II dispersion model from glmmTMB to JAGS

$
0
0

I want to translate a nbinom1 model from glmmTMB to JAGS. But I don't know how to translate the dispersion formula to JAGS. For this example I use the nbinom2 because it is simpler but actually I would prefer to implement the dispersion model from the nbinom1 family from glmmTMB to JAGS.

Note that the regression example here is completely out of context and does not make sense. This is only to provide code as the actual model I am working with is much more complex.

We have a negative binomial model which is defined in R like this:

library(glmmTMB)
library(rjags)

Owls <- transform(Owls,Nest=reorder(Nest,NegPerChick),NCalls=SiblingNegotiation,FT=FoodTreatment)

fit_nbinom2 <- glmmTMB(
  NCalls~ FT,
  data=Owls,
  dispformula = ~SexParent,
  family=nbinom2)


runjags::template.jags(formula = NCalls ~ FT + ArrivalTime
                       ,data = Owls
                       ,family = "negative binomial")

The modified output from runjags::template.jags looks like this:

model{
  # In the BUGS/JAGS language we must use an explicit for loop:
  for (i in 1:N) {
    # These lines describe the response distribution and linear model terms:
    NCalls[i] ~ dpois(regression_fitted[i])
    regression_residual[i] <- NCalls[i] - regression_fitted[i]
    dispersion[i] ~ dgamma(k, k)
    regression_fitted[i] <- regression_mean[i] * dispersion[i]
    # Note: this formulation of a gamma-Poisson is exactly equivalent to a Negative Binomial
    log(regression_mean[i]) <-
      intercept + ArrivalTime_coefficient * ArrivalTime[i] + FT_effect[FT[i]]
  }

  # These lines give the prior distributions for the parameters to be estimated:
  k ~ dgamma(0.001, 0.001)
  intercept ~ dnorm(0, 10 ^ -6)
  ArrivalTime_coefficient ~ dnorm(0, 10 ^ -6)

  FT_effect[1] <- 0    # Factor level Deprived
  FT_effect[2] ~ dnorm(0, 10 ^ -6)    # Factor level Satiated
}

How could you implement the dispformula = ~SexParent part from the glmmTMB model?

The help ?glmmTMB::sigma.glmmTMB says this:

nbinom1

returns an overdispersion parameter (usually denoted alpha as in Hardin and Hilbe (2007)): such that the variance equals mu(1+alpha).

nbinom2

returns an overdispersion parameter (usually denoted theta or k); in contrast to most other families, larger theta corresponds to a lower variance which is mu(1+mu/theta).

From this I tried to make the dispersion parameter dependent on the variable SexParent. Like this

model{
  # In the BUGS/JAGS language we must use an explicit for loop:
  for (i in 1:N) {
    # These lines describe the response distribution and linear model terms:
    NCalls[i] ~ dpois(regression_fitted[i])
    regression_residual[i] <- NCalls[i] - regression_fitted[i]
    dispersion[i] ~ dgamma(k[SexParent[i]], k[SexParent[i]])
    regression_fitted[i] <- regression_mean[i] * dispersion[i]
    # Note: this formulation of a gamma-Poisson is exactly equivalent to a Negative Binomial
    log(regression_mean[i]) <-
      intercept + ArrivalTime_coefficient * ArrivalTime[i] + FT_effect[FT[i]]
  }

  # These lines give the prior distributions for the parameters to be estimated:
  #k ~ dgamma(0.001, 0.001)
  intercept ~ dnorm(0, 10 ^ -6)
  ArrivalTime_coefficient ~ dnorm(0, 10 ^ -6)

  FT_effect[1] <- 0    # Factor level Deprived
  FT_effect[2] ~ dnorm(0, 10 ^ -6)    # Factor level Satiated
  for (i_sex in 1:N.parent){
    k[i_sex] ~ dgamma(0.001, 0.001)
  }  

}

But sometimes I run into problems because k must be > 0 or I get different results for the parameters like in summary(fit_nbinom2). I simply do not know how to reproduce these results.

Is the way I model the Dispersion correct? I have the feeling that I am missing something. It would be great if someone could help.


Viewing all articles
Browse latest Browse all 206235

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>