Learn R Programming

glmmTMB (version 0.1.1)

glmmTMB: Fit models with TMB

Description

Fit models with TMB

Usage

glmmTMB(formula, data = NULL, family = gaussian(), ziformula = ~0,
  dispformula = ~1, weights = NULL, offset = NULL, se = TRUE,
  verbose = FALSE, debug = FALSE)

Arguments

formula
combined fixed and random effects formula, following lme4 syntax
data
data frame
family
family (variance/link function) information; see family for details. As in glm, family can be specified as (1) a character string referencing an existing family-construction function (e.g. ‘"binomial"’); (2) a symbol referencing such a function (‘binomial’); or (3) the output of such a function (‘binomial()’). In addition, for families such as betabinomial that are special to glmmTMB, family can be specified as (4) a list comprising the name of the distribution and the link function (‘list(family="binomial",link="logit")’).
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. 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 paramameter to be 0, forcing variance into the random effects.
weights
weights, as in glm. Only implemented for binomial and betabinomial families.
offset
offset
se
whether to return standard errors
verbose
logical indicating if some progress indication should be printed to the console.
debug
whether to return the preprocessed data and parameter objects, without fitting the model

Details

  • binomial models with more than one trial (i.e., not binary/Bernoulli) must be specified in the form prob ~ ..., weights = N rather than in the more typical two-column matrix (cbind(successes,failures)~...) form.
  • in all cases glmmTMB returns maximum likelihood estimates - random effects variance-covariance matrices are not REML (so use REML=FALSE when comparing with lme4::lmer), and residual standard deviations (sigma) are not bias-corrected. 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 ...

Examples

Run this code
(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=list(family="truncated_poisson", link="log"), Salamanders))

## Binomial model
data(cbpp, package="lme4")
(tmbm1 <- glmmTMB(incidence/size ~ period + (1 | herd), weights=size,
               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

Run the code above in your browser using DataLab