mixed_model {GLMMadaptive} | R Documentation |
Generalized Linear Mixed Effects Models
Description
Fits generalized linear mixed effects models under maximum likelihood using adaptive Gaussian quadrature.
Usage
mixed_model(fixed, random, data, family, weights = NULL,
na.action = na.exclude, zi_fixed = NULL, zi_random = NULL,
penalized = FALSE, n_phis = NULL, initial_values = NULL,
control = list(), ...)
Arguments
fixed |
a formula for the fixed-effects part of the model, including the outcome. |
random |
a formula for the random-effects part of the model. This should only contain
the right-hand side part, e.g., |
data |
a data.frame containing the variables required in |
family |
a |
weights |
a numeric vector of weights. These are simple multipliers on the log-likelihood contributions of each group/cluster, i.e., we presume that there are multiple replicates of each group/cluster denoted by the weights. The length of 'weights' need to be equal to the number of independent groups/clusters in the data. |
na.action |
what to do with missing values in |
zi_fixed , zi_random |
formulas for the fixed and random effects of the zero inflated part. |
penalized |
logical or a list. If logical and equal to |
n_phis |
a numeric scalar; in case the family corresponds to a distribution that has extra (dispersion/shape) parameters, you need to specify how many extra parameters are needed. |
initial_values |
a list of initial values. This can have up to three components, namely,
|
control |
a list with the following components:
|
... |
arguments passed to |
Details
General: The mixed_model()
function fits mixed effects models in which the
integrals over the random effects in the definition of the marginal log-likelihood cannot
be solved analytically and need to be approximated. The function works under the
assumption of normally distributed random effects with mean zero and variance-covariance
matrix D
. These integrals are approximated numerically using an adaptive
Gauss-Hermite quadrature rule. Using the control argument nAGQ
, the user can
specify the number of quadrature points used in the approximation.
User-defined family: The user can define its own family object; for an example,
see the help page of negative.binomial
.
Optimization: A hybrid approach is used, starting with iter_EM
iterations
and unless convergence was achieved it continuous with a direct optimization of the
log-likelihood using function optim
and the algorithm specified by
optim_method
. For stability and speed, the derivative of the log-likelihood with
respect to the parameters are internally programmed.
Value
An object of class "MixMod"
with components:
coefficients |
a numeric vector with the estimated fixed effects. |
phis |
a numeric vector with the estimated extra parameters. |
D |
a numeric matrix denoting the estimated covariance matrix of the random effects. |
post_modes |
a numeric matrix with the empirical Bayes estimates of the random effects. |
post_vars |
a list of numeric matrices with the posterior variances of the random effects. |
logLik |
a numeric scalar denoting the log-likelihood value at the end of the optimization procedure. |
Hessian |
a numeric matrix denoting the Hessian matrix at the end of the optimization procedure. |
converged |
a logical indicating whether convergence was attained. |
data |
a copy of the |
id |
a copy of the grouping variable from |
id_name |
a character string with the name of the grouping variable. |
Terms |
a list with two terms components, |
model_frames |
a list with two model.frame components, |
control |
a copy of the (user-specific) |
Funs |
a list of functions used in the optimization procedure. |
family |
a copy of the |
call |
the matched call. |
Author(s)
Dimitris Rizopoulos d.rizopoulos@erasmusmc.nl
See Also
methods.MixMod
,
effectPlotData
,
marginal_coefs
Examples
# simulate some data
set.seed(123L)
n <- 200
K <- 15
t.max <- 25
betas <- c(-2.13, -0.25, 0.24, -0.05)
D <- matrix(0, 2, 2)
D[1:2, 1:2] <- c(0.48, -0.08, -0.08, 0.18)
times <- c(replicate(n, c(0, sort(runif(K-1, 0, t.max)))))
group <- sample(rep(0:1, each = n/2))
DF <- data.frame(year = times, group = factor(rep(group, each = K)))
X <- model.matrix(~ group * year, data = DF)
Z <- model.matrix(~ year, data = DF)
b <- cbind(rnorm(n, sd = sqrt(D[1, 1])), rnorm(n, sd = sqrt(D[2, 2])))
id <- rep(1:n, each = K)
eta.y <- as.vector(X %*% betas + rowSums(Z * b[id, ]))
DF$y <- rbinom(n * K, 1, plogis(eta.y))
DF$id <- factor(id)
################################################
fm1 <- mixed_model(fixed = y ~ year * group, random = ~ 1 | id, data = DF,
family = binomial())
# fixed effects
fixef(fm1)
# random effects
head(ranef(fm1))
# detailed output
summary(fm1)
# fitted values for the 'mean subject', i.e., with
# random effects values equal to 0
head(fitted(fm1, type = "mean_subject"))
# fitted values for the conditioning on the estimated random effects
head(fitted(fm1, type = "subject_specific"))
##############
fm2 <- mixed_model(fixed = y ~ year, random = ~ 1 | id, data = DF,
family = binomial())
# likelihood ratio test between the two models
anova(fm2, fm1)
# the same hypothesis but with a Wald test
anova(fm1, L = rbind(c(0, 0, 1, 0), c(0, 0, 0, 1)))
##############
# An effects plot for the mean subject (i.e., with random effects equal to 0)
nDF <- with(DF, expand.grid(year = seq(min(year), max(year), length.out = 15),
group = levels(group)))
plot_data <- effectPlotData(fm2, nDF)
require("lattice")
xyplot(pred + low + upp ~ year | group, data = plot_data,
type = "l", lty = c(1, 2, 2), col = c(2, 1, 1), lwd = 2,
xlab = "Follow-up time", ylab = "log odds")
expit <- function (x) exp(x) / (1 + exp(x))
xyplot(expit(pred) + expit(low) + expit(upp) ~ year | group, data = plot_data,
type = "l", lty = c(1, 2, 2), col = c(2, 1, 1), lwd = 2,
xlab = "Follow-up time", ylab = "Probabilities")
# An effects plots for the marginal probabilities
plot_data_m <- effectPlotData(fm2, nDF, marginal = TRUE, cores = 1L)
expit <- function (x) exp(x) / (1 + exp(x))
xyplot(expit(pred) + expit(low) + expit(upp) ~ year | group, data = plot_data_m,
type = "l", lty = c(1, 2, 2), col = c(2, 1, 1), lwd = 2,
xlab = "Follow-up time", ylab = "Probabilities")
##############
# include random slopes
fm1_slp <- update(fm1, random = ~ year | id)
# increase the number of quadrature points to 15
fm1_slp_q15 <- update(fm1_slp, nAGQ = 15)
# a diagonal covariance matrix for the random effects
fm1_slp_diag <- update(fm1, random = ~ year || id)
anova(fm1_slp_diag, fm1_slp)