3

Apologies if this is a repeat question. Many have posted looking looking for a way to do post-hoc analyses on the conditional model (fixed factors) in glmmTMB. I want to do plannned contrasts between certain groups, not test every pairwise comparison (e.g. Tukey).

The code below worked well on nlme:lme for a lmm. However, it returns an error on the code below.

Error in modelparm.default(model, ...) : 
  dimensions of coefficients and covariance matrix don't match

Is there a way to do planned contrasts on a glmmTMB?

#filtdens is a dataframe and TRT,DATE,BURN,VEG are factors
filtdens <- merged %>% filter(!BLOCK %in% c("JB2","JB4","JB5") & MEAS =="DENS" & 
                      group == "TOT" & BURN == "N" & VEG == "C")
filtdens$TD <- interaction(filtdens$TRT, filtdens$DATE)
mod2 <- glmmTMB(count~(TD)+(1|BLOCK),
                 data=filtdens,
        zi=~1,
        family=nbinom1(link = "log"))

k1 <- matrix(c(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,

       0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0,

       0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0,

       0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1), byrow = T, ncol = 12)

summary(glht(mod2, linfct=k1),test=adjusted("bonferroni"))
eisbehr
  • 12,243
  • 7
  • 38
  • 63
LHF
  • 33
  • 3

1 Answers1

4

A reproducible example would be helpful, but: this vignette in the development version offers code that ought to enable multcomp::linfct, i.e.:

glht_glmmTMB <- function (model, ..., component="cond") {
    glht(model, ...,
         coef. = function(x) fixef(x)[[component]],
         vcov. = function(x) vcov(x)[[component]],
         df = NULL)
}
modelparm.glmmTMB <- function (model, 
                               coef. = function(x) fixef(x)[[component]],
                               vcov. = function(x) vcov(x)[[component]],
                               df = NULL, component="cond", ...) {
    multcomp:::modelparm.default(model, coef. = coef., vcov. = vcov.,
                        df = df, ...)
}

Test (this example is with Tukey, but I don't see why it shouldn't work more generally ...)

library(glmmTMB)
data("cbpp",package="lme4")
cbpp_b1 <- glmmTMB(incidence/size~period+(1|herd),
               weights=size,family=binomial,
               data=cbpp)
g1 <- glht(cbpp_b1, linfct = mcp(period = "Tukey"))
summary(g1)

This works with the current CRAN version, but the current development version of glmmTMB offers more options (e.g. emmeans(); see the above-linked vignette). You'll need to install via devtools::install_github("glmmTMB/glmmTMB/glmmTMB") (you'll need compilation tools installed as well).

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks, Ben, for this and all the other work you do. – LHF Aug 17 '18 at 16:46
  • 1
    For those having compatibility issues between compatibility issues between R3.5.1. and Rtools3.5 when running devtools, this fixes the problem: https://community.rstudio.com/t/rtools-not-recognized-r3-5-1-rtools-3-5/11311 – LHF Aug 17 '18 at 17:25
  • With `glht` I get the error `Error in glht.matrix(fitUSA, linfct = B) : ‘ncol(linfct)’ is not equal to ‘length(coef(model))’` and with `glht_glmmTMB` I get the error ` Error in glht.matrix(model, ..., coef. = function(x) fixef(x)[[component]], : ‘ncol(linfct)’ is not equal to ‘length(coef(model))’ `. I have tried with `linfct = B`, where B is a matrix (like `k1` above) with so many rows as fixed effect estimates in model and 1 column or 2 columns, but both options produce me the same error. – iago Jun 07 '20 at 21:26
  • I had to try in that way, because I do not want to contrast 2 categories of same factor, but two distinct variables – iago Jun 07 '20 at 21:31
  • @iago, could you post a reproducible example as a new question? – Ben Bolker Jun 07 '20 at 21:57
  • Already posted, Ben. Thank you. Here is the link: https://stackoverflow.com/questions/62252468/contrast-between-variables-in-glmmtmb – iago Jun 07 '20 at 22:26