R: Fit models with TMB

glmmTMB {glmmTMB} | R Documentation

## Fit models with TMB

### Description

Fit models with TMB

### Usage

    glmmTMB(formula, data = NULL, family = gaussian(), ziformula = ~0,
      dispformula = ~1, weights = NULL, offset = NULL, na.action = na.fail,
      se = TRUE, verbose = FALSE, doFit = TRUE, control = glmmTMBControl(),
      REML = FALSE)

### Arguments

`formula` | combined fixed and random effects formula, following lme4 syntax
`data` | data frame
`family` | a family function, a character string naming a family function, or the result of a call to a family function family (variance/link function) information; see `family` for generic discussion of families or `family_glmmTMB` for details of `glmmTMB`-specific families.
`ziformula` | a one-sided (i.e., no response variable) formula for zero-inflation combining fixed and random effects: the default `~0` specifies no zero-inflation. Specifying `~.` will set the right-hand side of the zero-inflation formula identical to the right-hand side of the main (conditional effects) formula; terms can also be added or subtracted. Offset terms will automatically be dropped from the conditional effects formula when using `~.` The zero-inflation model uses a logit link.
`dispformula` | a one-sided formula for dispersion containing only fixed effects: the default `~1` specifies the standard dispersion given any family. The argument is ignored for families that do not have a dispersion parameter. For an explanation of the dispersion parameter for each family, see (`sigma`). The dispersion model uses a log link. In Gaussian mixed models, `dispformula=~0` fixes the parameter to be 0, forcing variance into the random effects.
`weights` | weights, as in `glm`. Not automatically scaled to have sum 1.
`offset` | offset for conditional model (only):
`na.action` | how to handle missing values (see `na.action` and `model.frame`); from `lm`, “The default is set by the `na.action` setting of `options`, and is `na.fail` if that is unset. The ‘factory-fresh’ default is `na.omit`.”
`se` | whether to return standard errors
`verbose` | logical indicating if some progress indication should be printed to the console.
`doFit` | whether to fit the full model, or (if FALSE) return the preprocessed data and parameter objects, without fitting the model
`control` | control parameters; see `glmmTMBControl`.
`REML` | Logical; Use REML estimation rather than maximum likelihood.

### Details

- binomial models with more than one trial (i.e., not binary/Bernoulli) can either be specified in the form `prob ~ ..., weights = N` or in the more typical two-column matrix (`cbind(successes,failures)~...`) form.

- Behavior of `REML=TRUE` for Gaussian responses matches `lme4::lmer`). It may also be useful in some cases with non-Gaussian responses (Millar 2011). Simulations should be done first to verify.

- Because the `df.residual` method for `glmmTMB` currently counts the dispersion parameter, one would need to multiply by `sqrt(nobs(fit)/(1+df.residual(fit)))` when comparing with `lm` ...

- by default, vector-valued random effects are fitted with unstructured (general positive definite) variance-covariance matrices. Structured variance-covariance matrices can be specified in the form `struc(terms|group)`, where `struc` is one of

  - `diag` (diagonal, heterogeneous variance)

  - `ar1` (autoregressive order-1, homogeneous variance)

  - `cs` (compound symmetric, heterogeneous variance)

  - `ou` (* Ornstein-Uhlenbeck, homogeneous variance)

  - `exp` (* exponential autocorrelation)

  - `gau` (* Gaussian autocorrelation)

  - `mat` (* Matérn process correlation)

  - `toep` (* Toeplitz)

  (note structures marked with * are experimental/untested)

- For backward compatibility, the `family` argument can also be specified as a list comprising the name of the distribution and the link function (e.g. ‘list(family="binomial", link="logit")’). However, this alternatives is now deprecated (it produces a warning and will be removed at some point in the future). Furthermore, certain capabilities such as Pearson residuals or predictions on the data scale will only be possible if components such as `variance` and `linkfun` are present (see `family`).

### References

- Millar, Russell B. Maximum Likelihood Estimation and Inference: With Examples in R, SAS and ADMB. John Wiley & Sons, 2011.

### Examples

    (m1 <- glmmTMB(count~ mined + (1|site),
      zi=~mined,
      family=poisson, data=Salamanders))
    summary(m1)

    ## Zero-inflated negative binomial model
    (m2 <- glmmTMB(count~spp + mined + (1|site),
      zi=~spp + mined,
      family=nbinom2, Salamanders))

    ## Hurdle Poisson model
    (m3 <- glmmTMB(count~spp + mined + (1|site),
      zi=~spp + mined,
      family=truncated_poisson, Salamanders))

    ## Binomial model
    data(cbpp, package="lme4")
    (tmbm1 <- glmmTMB(cbind(incidence, size-incidence) ~ period + (1 | herd),
                   data=cbpp, family=binomial))

    ## Dispersion model
    sim1=function(nfac=40, nt=100, facsd=.1, tsd=.15, mu=0, residsd=1)
    {
      dat=expand.grid(fac=factor(letters[1:nfac]), t= 1:nt)
      n=nrow(dat)
      dat$REfac=rnorm(nfac, sd= facsd)[dat$fac]
      dat$REt=rnorm(nt, sd= tsd)[dat$t]
      dat$x=rnorm(n, mean=mu, sd=residsd) + dat$REfac + dat$REt
      return(dat)
    }
    set.seed(101)
    d1 = sim1(mu=100, residsd =10)
    d2 = sim1(mu=200, residsd =5)
    d1$sd="ten"
    d2$sd="five"
    dat = rbind(d1, d2)
    m0 = glmmTMB(x~sd+(1|t), dispformula=~sd, dat)
    fixef(m0)$disp
    c(log(5^2), log(10^2)-log(5^2)) #expected dispersion model coefficients

[Package glmmTMB version 0.2.2.0 Index]