galamm: Fit a generalized additive latent and mixed model

View source: R/galamm.R

galammR Documentation

Fit a generalized additive latent and mixed model

Description

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.

Usage

galamm(
  formula,
  dispformula = NULL,
  weights = NULL,
  data,
  family = gaussian,
  family_mapping = NULL,
  load_var = NULL,
  load.var = NULL,
  lambda = NULL,
  factor = NULL,
  factor_interactions = NULL,
  na.action = getOption("na.action"),
  start = NULL,
  control = galamm_control()
)

Arguments

formula

A formula specifying the model. Smooth terms are defined in the style of the mgcv and gamm4 packages, see \insertCitewoodGeneralizedAdditiveModels2017galamm for an introduction. Random effects are specified using lme4 syntax, which is described in detail in \insertCitebatesFittingLinearMixedEffects2015galamm. Factor loadings will also be part of the model formula, and is based on the syntax of the PLmixed package \insertCiterockwoodEstimatingComplexMeasurement2019galamm.

dispformula

An optional formula object specifying an expression for the residual variance. Defaults to NULL, corresponding to homoscedastic errors. The formula is defined in lme4 style; see vignettes and examples for details.

weights

Deprecated. Use dispformula instead.

data

A data.frame containing all the variables specified by the model formula, with the exception of factor loadings.

family

A family object specifying the response distribution of the model. Currently gaussian, binomial, and poisson with canonical link functions are supported. Models with mixed response types, in which responses are distributed according to one of the three supported distributions, can be set up using the gfam() function.

family_mapping

Deprecated. See the documentation to gfam() for how to set up mixed response type models.

load_var

Optional character specifying the name of the variable in data identifying what the factors load onto. Default to NULL, which means that there are no loading variables. Argument is case sensitive.

load.var

Deprecated. Use load_var instead.

lambda

Optional factor loading matrix. Numerical values indicate that the given value is fixed, while NA means that the entry is a parameter to be estimated. Numerical values can only take the values 0 or

  1. The number of columns of lambda must be identical to the number of elements in factor. Defaults to NULL, which means that there is no factor loading matrix. If lambda is provided as a vector, it will be converted to a matrix with a single column.

factor

Optional character vector whose jth entry corresponds to the jth column of the corresponding matrix in lambda. The number of elements in factor must be equal to the number of columns in lambda. Defaults to NULL, which means that there are no factor loadings. Argument is case sensitive.

factor_interactions

Optional list of length equal to the number of columns in lambda. Each list element should be a formula object containing the write-hand side of a regression model, of the form ~ x + z. Defaults to NULL, which means that no factor interactions are used.

na.action

Character of length one specifying a function which indicates what should happen when the data contains NAs. The defaults is set to the na.action setting of options, which can be seen with options("na.action"). The other alternatives are "na.fail" or "na.exclude", which means that the function fails if there as NAs in data.

start

Optional named list of starting values for parameters. Possible names of list elements are "theta", "beta", "lambda", and "weights", all of should be numerical vectors with starting values. Default to NULL, which means that some relatively sensible defaults are used. Names of parameters must be given in all lower case.

control

Optional control object for the optimization procedure of class galamm_control resulting from calling galamm_control. Defaults to NULL, which means that the defaults of galamm_control are used.

Value

An object of type galamm as described in galammObject.

References

\insertAllCited

See Also

Other modeling functions: galammObject, gfam(), s(), t2()

Examples

# 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 <- gfam(list(gaussian, binomial))

# Fit the model
mod <- galamm(
  formula = y ~ x + (0 + level | id),
  data = mresp,
  family = families,
  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), dispformula = ~ (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)


galamm documentation built on Dec. 21, 2025, 5:07 p.m.