galamm | R Documentation |
This function fits a generalized additive latent and mixed model
(GALAMMs), as described in
\insertCitesorensenLongitudinalModelingAgeDependent2023;textualgalamm.
The building blocks of these models are generalized additive mixed models
(GAMMs) \insertCitewoodGeneralizedAdditiveModels2017galamm, of which
generalized linear mixed models
\insertCitebreslowApproximateInferenceGeneralized1993,harvilleMaximumLikelihoodApproaches1977,hendersonBestLinearUnbiased1975,lairdRandomEffectsModelsLongitudinal1982galamm
are special cases. GALAMMs extend upon GAMMs by allowing factor structures,
as commonly used to model hypothesized latent traits underlying observed
measurements. In this sense, GALAMMs are an extension of generalized linear
latent and mixed models (GLLAMMs)
\insertCiteskrondalGeneralizedLatentVariable2004,rabe-heskethGeneralizedMultilevelStructural2004galamm
which allows semiparametric estimation. The implemented algorithm used to
compute model estimates is described in
\insertCitesorensenLongitudinalModelingAgeDependent2023;textualgalamm,
and is an extension of the algorithm used for fitting generalized linear
mixed models by the lme4
package
\insertCitebatesFittingLinearMixedEffects2015galamm. The syntax used to
define factor structures is based on that used by the PLmixed
package, which is detailed in
\insertCiterockwoodEstimatingComplexMeasurement2019;textualgalamm.
galamm(
formula,
weights = NULL,
data,
family = gaussian,
family_mapping = rep(1, nrow(data)),
load.var = NULL,
lambda = NULL,
factor = NULL,
factor_interactions = NULL,
na.action = getOption("na.action"),
start = NULL,
control = galamm_control()
)
formula |
A formula specifying the model. Smooth terms are defined in
the style of the |
weights |
An optional formula object specifying an expression for the
residual variance. Defaults to |
data |
A data.frame containing all the variables specified by the model formula, with the exception of factor loadings. |
family |
A a list or character vector containing one or more model
families. For each element in |
family_mapping |
Optional vector mapping from the elements of
|
load.var |
Optional character specifying the name of the variable in
|
lambda |
Optional factor loading matrix. Numerical values indicate that
the given value is fixed, while
|
factor |
Optional character vector whose |
factor_interactions |
Optional list of length equal to the number of
columns in |
na.action |
Character of length one specifying a function which
indicates what should happen when the data contains |
start |
Optional named list of starting values for parameters. Possible
names of list elements are |
control |
Optional control object for the optimization procedure of
class |
A model object of class galamm
, containing the following
elements:
call
the matched call used when fitting the model.
random_effects
a list containing the following two elements:
b
random effects in original parametrization.
u
random effects standardized to have identity covariance matrix.
model
a list with various elements related to the model setup and
fit:
deviance
deviance of final model.
deviance_residuals
deviance residuals of the final model.
df
degrees of freedom of model.
family
a list of one or more family objects, as specified in the
family
arguments to galamm
.
factor_interactions
List of formulas specifying interactions
between latent and observed variables, as provided to the argument
factor_interactions
to galamm
. If not provided, it is
NULL
.
fit
a numeric vector with fitted values.
fit_population
a numeric vector with fitted values excluding
random effects.
hessian
Hessian matrix of final model, i.e., the second
derivative of the log-likelihood with respect to all model parameters.
lmod
Linear model object returned by lme4::lFormula
, which
is used internally for setting up the models.
loglik
Log-likelihood of final model.
n
Number of observations.
pearson_residual
Pearson residuals of final model.
reduced_hessian
Logical specifying whether the full Hessian
matrix was computed, or a Hessian matrix with derivatives only with respect
to beta and lambda.
response
A numeric vector containing the response values used when
fitting the model.
weights_object
Object with weights used in model fitting. Is
NULL
when no weights were used.
parameters
A list object with model parameters and related
information:
beta_inds
Integer vector specifying the indices of fixed
regression coefficients among the estimated model parameters.
dispersion_parameter
One or more dispersion parameters of the
final model.
lambda_dummy
Dummy matrix of factor loadings, which shows the
structure of the loading matrix that was supplied in the lambda
arguments.
lambda_inds
Integer vector specifying the indices of factor
loadings among the estimated model parameters.
lambda_interaction_inds
Integer vector specifying the indices
of regression coefficients for interactions between latent and observed
variables.
parameter_estimates
Numeric vector of final parameter estimates.
parameter_names
Names of all parameters estimates.
theta_inds
Integer vector specifying the indices of variance
components among the estimated model parameters. Technically these are the
entries of the Cholesky decomposition of the covariance matrix.
weights_inds
Integer vector specifying the indices of estimated
weights (used in heteroscedastic Gaussian models) among the estimated model
parameters.
gam
List containing information about smooth terms in the model.
If no smooth terms are contained in the model, then it is a list of length
zero.
Other modeling functions:
s()
,
t2()
# Mixed response model ------------------------------------------------------
# The mresp dataset contains a mix of binomial and Gaussian responses.
# We need to estimate a factor loading which scales the two response types.
loading_matrix <- matrix(c(1, NA), ncol = 1)
# Define mapping to families.
families <- c(gaussian, binomial)
family_mapping <- ifelse(mresp$itemgroup == "a", 1, 2)
# Fit the model
mod <- galamm(
formula = y ~ x + (0 + level | id),
data = mresp,
family = families,
family_mapping = family_mapping,
factor = "level",
load.var = "itemgroup",
lambda = loading_matrix
)
# Summary information
summary(mod)
# Heteroscedastic model -----------------------------------------------------
# Residuals allowed to differ according to the item variable
# We also set the initial value of the random intercept standard deviation
# to 1
mod <- galamm(
formula = y ~ x + (1 | id), weights = ~ (1 | item),
data = hsced, start = list(theta = 1)
)
summary(mod)
# Generalized additive mixed model with factor structures -------------------
# The cognition dataset contains simulated measurements of three latent
# time-dependent processes, corresponding to individuals' abilities in
# cognitive domains. We focus here on the first domain, and take a single
# random timepoint per person:
dat <- subset(cognition, domain == 1)
dat <- split(dat, f = dat$id)
dat <- lapply(dat, function(x) x[x$timepoint %in% sample(x$timepoint, 1), ])
dat <- do.call(rbind, dat)
dat$item <- factor(dat$item)
# At each timepoint there are three items measuring ability in the cognitive
# domain. We fix the factor loading for the first measurement to one, and
# estimate the remaining two. This is specified in the loading matrix.
loading_matrix <- matrix(c(1, NA, NA), ncol = 1)
# We can now estimate the model.
mod <- galamm(
formula = y ~ 0 + item + sl(x, factor = "loading") +
(0 + loading | id),
data = dat,
load.var = "item",
lambda = loading_matrix,
factor = "loading"
)
# We can plot the estimated smooth term
plot_smooth(mod, shade = TRUE)
# Interaction between observed and latent covariates ------------------------
# Define the loading matrix
lambda <- matrix(c(1, NA, NA), ncol = 1)
# Define the regression functions, one for each row in the loading matrix
factor_interactions <- list(~1, ~1, ~x)
# Fit the model
mod <- galamm(
formula = y ~ type + x:response + (0 + loading | id),
data = latent_covariates,
load.var = "type",
lambda = lambda,
factor = "loading",
factor_interactions = factor_interactions
)
# The summary output now include an interaction between the latent variable
# and x, for predicting the third element in "type"
summary(mod)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.