Title: | Generalized Additive Latent and Mixed Models |
Version: | 0.2.3 |
Description: | Estimates generalized additive latent and mixed models using maximum marginal likelihood, as defined in Sorensen et al. (2023) <doi:10.1007/s11336-023-09910-z>, which is an extension of Rabe-Hesketh and Skrondal (2004)'s unifying framework for multilevel latent variable modeling <doi:10.1007/BF02295939>. Efficient computation is done using sparse matrix methods, Laplace approximation, and automatic differentiation. The framework includes generalized multilevel models with heteroscedastic residuals, mixed response types, factor loadings, smoothing splines, crossed random effects, and combinations thereof. Syntax for model formulation is close to 'lme4' (Bates et al. (2015) <doi:10.18637/jss.v067.i01>) and 'PLmixed' (Rockwood and Jeon (2019) <doi:10.1080/00273171.2018.1516541>). |
License: | GPL (≥ 3) |
URL: | https://github.com/LCBC-UiO/galamm, https://lcbc-uio.github.io/galamm/ |
BugReports: | https://github.com/LCBC-UiO/galamm/issues |
Encoding: | UTF-8 |
Imports: | lme4, Matrix, memoise, methods, mgcv, nlme, Rcpp, Rdpack, stats |
Depends: | R (≥ 3.5.0) |
LinkingTo: | Rcpp, RcppEigen |
LazyData: | true |
RoxygenNote: | 7.3.2 |
Suggests: | covr, gamm4, knitr, PLmixed, rmarkdown, testthat (≥ 3.0.0) |
Config/testthat/edition: | 3 |
VignetteBuilder: | knitr, rmarkdown |
RdMacros: | Rdpack |
NeedsCompilation: | yes |
SystemRequirements: | C++17 |
Packaged: | 2025-07-03 13:46:01 UTC; oyss |
Author: | Øystein Sørensen |
Maintainer: | Øystein Sørensen <oystein.sorensen@psykologi.uio.no> |
Repository: | CRAN |
Date/Publication: | 2025-07-03 14:00:02 UTC |
galamm: Generalized Additive Latent and Mixed Models
Description
Estimates generalized additive latent and mixed models using maximum marginal likelihood, as defined in Sorensen et al. (2023) doi:10.1007/s11336-023-09910-z, which is an extension of Rabe-Hesketh and Skrondal (2004)'s unifying framework for multilevel latent variable modeling doi:10.1007/BF02295939. Efficient computation is done using sparse matrix methods, Laplace approximation, and automatic differentiation. The framework includes generalized multilevel models with heteroscedastic residuals, mixed response types, factor loadings, smoothing splines, crossed random effects, and combinations thereof. Syntax for model formulation is close to 'lme4' (Bates et al. (2015) doi:10.18637/jss.v067.i01) and 'PLmixed' (Rockwood and Jeon (2019) doi:10.1080/00273171.2018.1516541).
Author(s)
Maintainer: Øystein Sørensen oystein.sorensen@psykologi.uio.no (ORCID)
Other contributors:
Douglas Bates [contributor]
Ben Bolker [contributor]
Martin Maechler [contributor]
Allan Leal [contributor]
Fabian Scheipl [contributor]
Steven Walker [contributor]
Simon Wood [contributor]
References
Sørensen Ø, Fjell AM, Walhovd KB (2023). “Longitudinal Modeling of Age-Dependent Latent Traits with Generalized Additive Latent and Mixed Models.” Psychometrika, 88(2), 456–486. ISSN 1860-0980, doi:10.1007/s11336-023-09910-z.
See Also
Useful links:
Report bugs at https://github.com/LCBC-UiO/galamm/issues
Extract variance and correlation components from model
Description
Extract variance and correlation components from model
Usage
## S3 method for class 'galamm'
VarCorr(x, sigma = 1, ...)
Arguments
x |
An object of class |
sigma |
Numeric value used to multiply the standard deviations. Defaults to 1. |
... |
Other arguments passed onto other methods. Currently not used. |
Value
An object of class c("VarCorr.galamm", "VarCorr.merMod")
.
See Also
print.VarCorr.galamm()
for the print function.
Other details of model fit:
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
# Extract information on variance and covariance
VarCorr(mod)
# Convert to data frame
# (this invokes lme4's function as.data.frame.VarCorr.merMod)
as.data.frame(VarCorr(mod))
Compare likelihoods of galamm objects
Description
Anova function for comparing different GALAMMs fitted on the same data.
Usage
## S3 method for class 'galamm'
anova(object, ...)
Arguments
object |
An object of class |
... |
Other fitted models of class |
Value
A table with model comparison metric.
Author(s)
Some of the source code for this function is adapted from
lme4:::anova.merMod
, with authors Douglas M. Bates, Martin Maechler,
Ben Bolker, and Steve Walker.
References
Bates DM, Mächler M, Bolker B, Walker S (2015). “Fitting Linear Mixed-Effects Models Using Lme4.” Journal of Statistical Software, 67(1), 1–48. ISSN 1548-7660, doi:10.18637/jss.v067.i01.
See Also
summary.galamm()
for the summary method and anova()
for the
generic function.
Other summary functions:
plot.galamm()
,
plot_smooth.galamm()
,
print.galamm()
,
print.summary.galamm()
,
summary.galamm()
Examples
# Poisson GLMM
count_mod <- galamm(
formula = y ~ lbas * treat + lage + v4 + (1 | subj),
data = epilep, family = poisson
)
# Model without interaction
count_mod0 <- galamm(
formula = y ~ lbas + treat + lage + v4 + (1 | subj),
data = epilep, family = poisson
)
# Model comparison
anova(count_mod, count_mod0)
Extract galamm coefficients
Description
Currently, this function only returns the fixed effects.
Usage
## S3 method for class 'galamm'
coef(object, ...)
Arguments
object |
An object of class |
... |
Optional arguments passed on to other methods. Currently not used. |
Value
A matrix with the requested coefficients.
See Also
fixef.galamm()
for fixed effects, ranef.galamm()
for random
effects, and coef()
for the generic function.
Other details of model fit:
VarCorr()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Poisson GLMM
count_mod <- galamm(
formula = y ~ lbas * treat + lage + v4 + (1 | subj),
data = epilep, family = poisson
)
# Extract coefficients
coef(count_mod)
Simulated Data with Measurements of Cognitive Abilities
Description
Simulated dataset mimicking the measurement of abilities in three cognitive
domains. The latent traits (cognitive ability in a given domain) are based on
the functions in mgcv::gamSim
(Wood 2017), and depend on the
explanatory variable x.
Usage
cognition
Format
cognition
A data frame with 14400 rows and 7 columns:
- id
Subject ID.
- domain
Factor variable denoting the cognitive domain.
- x
Explanatory variable.
- timepoint
Factor variable denoting the timepoint.
- item
Factor variable denoting the item within the tests of each cognitive domain.
- trials
Number of trials, if applicable.
- y
Response variable. For domain 1 a real number, for domain 2 a binomially distributed variable based on a single trial, for domain 3 a real number.
References
Wood SN (2017). Generalized Additive Models: An Introduction with R, 2 edition. Chapman and Hall/CRC.
See Also
Other datasets:
diet
,
epilep
,
hsced
,
latent_covariates
,
latent_covariates_long
,
lifespan
,
mresp
,
mresp_hsced
Confidence intervals for model parameters
Description
Confidence intervals for model parameters
Usage
## S3 method for class 'galamm'
confint(object, parm, level = 0.95, method = "Wald", ...)
Arguments
object |
An object of class |
parm |
Parameters for which to compute intervals. Use |
level |
Decimal number specifying the confidence level. Defaults to 0.95. |
method |
Character of length one specifying the type of confidence interval. Currently only "Wald" is available. The argument is case sensitive. |
... |
Other arguments passed on to other methods. Currently not used. |
Value
A matrix with the requested confidence intervals.
See Also
fixef.galamm()
for fixed effects, coef.galamm()
for coefficients
more generally, and vcov.galamm()
for the variance-covariance matrix.
confint()
is the generic function.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Poisson GLMM
count_mod <- galamm(
formula = y ~ lbas * treat + lage + v4 + (1 | subj),
data = epilep, family = poisson
)
confint(count_mod, parm = "beta", level = .99)
Extract deviance of galamm object
Description
Extract deviance of galamm object
Usage
## S3 method for class 'galamm'
deviance(object, ...)
Arguments
object |
Object of class |
... |
Other arguments passed on to other methods. Currently not used. |
Value
A numeric value giving the deviance of the model fit.
See Also
logLik.galamm()
for a function returning the log likelihood and
deviance()
for the generic function.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
# Extract deviance
deviance(mod)
Diet Data
Description
Longitudinal epilepsy data from Morris et al. (1977). This documentation is based on Chapter 14.2 of Skrondal and Rabe-Hesketh (2004), where the dataset is used. See also Rabe-Hesketh et al. (2003).
Usage
diet
Format
diet
A data frame with 236 rows and 7 columns:
- id
Subject ID.
- age
Age (standardized).
- bus
Dummy variable indicating whether the subject is a bus driver or banking staff.
- item
Integer indicating whether the outcome is fiber intake at time 1 (item = 1), fiber intake at time 2 (item = 2), or coronary heart disease (item = 3).
- y
Outcome.
- chd
Dummy variable indicating whether y is an indicator for coronary heart disease, coded as 0/1.
- fiber
Dummy variable indicating whether y is a fiber measurement at either timepoint 1 or 2.
- fiber2
Dummy variable indicating whether y is a fiber measurement at timepoint 2.
Source
http://www.gllamm.org/books/readme.html#14.2
References
Morris JN, Marr JW, Clayton DG (1977).
“Diet and Heart: A Postscript.”
Br Med J, 2(6098), 1307–1314.
ISSN 0007-1447, 1468-5833, doi:10.1136/bmj.2.6098.1307.
Rabe-Hesketh S, Pickles A, Skrondal A (2003).
“Correcting for Covariate Measurement Error in Logistic Regression Using Nonparametric Maximum Likelihood Estimation.”
Statistical Modelling, 3(3), 215–232.
ISSN 1471-082X, doi:10.1191/1471082X03st056oa.
Skrondal A, Rabe-Hesketh S (2004).
Generalized Latent Variable Modeling, Interdisciplinary Statistics Series.
Chapman and Hall/CRC, Boca Raton, Florida.
See Also
Other datasets:
cognition
,
epilep
,
hsced
,
latent_covariates
,
latent_covariates_long
,
lifespan
,
mresp
,
mresp_hsced
Epilepsy Data
Description
Longitudinal epilepsy data from Leppik et al. (1987). This documentation is based on Chapter 11.3 of Skrondal and Rabe-Hesketh (2004), where the dataset is used.
Usage
epilep
Format
epilep
A data frame with 236 rows and 7 columns:
- subj
Subject ID.
- y
Number of seizures.
- treat
Dummy variable for treatment group.
- visit
Time at visit.
- v4
Dummy for visit 4.
- lage
Logarithm of age.
- lbas
Logarithm of a quarter of the number of seizures in the eight weeks preceding entry into the trial.
Source
http://www.gllamm.org/books/readme.html#11.3
References
Leppik IE, Dreifuss FE, Porter R, Bowman T, Santilli N, Jacobs M, Crosby C, Cloyd J, Stackman J, Graves N, Sutula T, Welty T, Vickery J, Brundage R, Gates J, Gumnit RJ, Gutierrez A (1987).
“A Controlled Study of Progabide in Partial Seizures: Methodology and Results.”
Neurology, 37(6), 963–963.
ISSN 0028-3878, 1526-632X, doi:10.1212/WNL.37.6.963.
Skrondal A, Rabe-Hesketh S (2004).
Generalized Latent Variable Modeling, Interdisciplinary Statistics Series.
Chapman and Hall/CRC, Boca Raton, Florida.
See Also
Other datasets:
cognition
,
diet
,
hsced
,
latent_covariates
,
latent_covariates_long
,
lifespan
,
mresp
,
mresp_hsced
Extract parameters from fitted model for use as initial values
Description
This function extracts parameter values from a fitted model object in a form that can be directly provided as initial values for a new model fit.
Usage
## S3 method for class 'galamm'
extract_optim_parameters(object)
Arguments
object |
Object of class |
Value
A list
object containing the following elements:
-
theta
Numerical vector of variance components, i.e., entries of the lower Cholesky form of the covariance matrix of random effects. -
beta
Fixed regression coefficients. -
lambda
Factor loadings. -
weights
Weights for heteroscedastic residuals.
See Also
Other optimization functions:
galamm_control()
Examples
# Fit linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
# Extract parameters
start <- extract_optim_parameters(mod)
# Fit again using the Nelder-Mead algorithm, using start as initial values:
mod_nm <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced,
start = start,
control = galamm_control(method = "Nelder-Mead")
)
Extract factor loadings from galamm object
Description
Extract factor loadings from galamm object
Usage
## S3 method for class 'galamm'
factor_loadings(object)
Arguments
object |
Object of class |
Details
This function has been named factor_loadings
rather than just
loadings
to avoid conflict with stats::loadings
.
Value
A matrix containing the estimated factor loadings with corresponding standard deviations.
Author(s)
The example for this function comes from PLmixed
, with authors
Nicholas Rockwood and Minjeong Jeon
(Rockwood and Jeon 2019).
See Also
fixef.galamm()
for fixed regression coefficients,
confint.galamm()
for confidence intervals, and coef.galamm()
for
coefficients more generally.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Logistic mixed model with factor loadings, example from PLmixed
data("IRTsim", package = "PLmixed")
# Reduce data size for the example to run faster
IRTsub <- IRTsim[IRTsim$item < 4, ]
IRTsub <- IRTsub[sample(nrow(IRTsub), 300), ]
IRTsub$item <- factor(IRTsub$item)
# Fix loading for first item to 1, and estimate the two others freely
loading_matrix <- matrix(c(1, NA, NA), ncol = 1)
# Estimate model
mod <- galamm(y ~ item + (0 + ability | sid) + (0 + ability | school),
data = IRTsub, family = binomial, load.var = "item",
factor = "ability", lambda = loading_matrix
)
# Show estimated factor loadings, with standard errors
factor_loadings(mod)
Extract family or families from fitted galamm
Description
This function returns a list of families for an object of class
galamm
, returned from galamm
.
Usage
## S3 method for class 'galamm'
family(object, ...)
Arguments
object |
An object of class |
... |
Optional arguments passed on to other methods. Currently not used. |
Value
A list of family objects.
See Also
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Mixed response model
loading_matrix <- matrix(c(1, NA), ncol = 1)
families <- c(gaussian, binomial)
family_mapping <- ifelse(mresp$itemgroup == "a", 1, 2)
mixed_resp <- galamm(
formula = y ~ x + (0 + level | id),
data = mresp,
family = families,
family_mapping = family_mapping,
load.var = "itemgroup",
lambda = loading_matrix,
factor = "level"
)
# This model has two family objects
family(mixed_resp)
Extract model fitted values
Description
Extracts fitted values from a model including random effects.
Usage
## S3 method for class 'galamm'
fitted(object, ...)
Arguments
object |
An object of class |
... |
Optional arguments passed on to other methods. Currently not used. |
Value
A numerical vector with fit values for each row in the input data.
See Also
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
# Extract fitted values and plot against x
plot(hsced$x, fitted(mod))
Extract fixed effects from galamm objects
Description
Extract the fixed regression coefficients.
Usage
## S3 method for class 'galamm'
fixef(object, ...)
Arguments
object |
An object of class |
... |
Optional arguments passed on to other methods. Currently not used. |
Value
A named numeric
vector containing the requested fixed effects.
See Also
ranef.galamm()
for random effects, coef.galamm()
for
coefficients more generally, and confint.galamm()
for confidence intervals.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Poisson GLMM
count_mod <- galamm(
formula = y ~ lbas * treat + lage + v4 + (1 | subj),
data = epilep, family = poisson
)
# Extract fixed effects
fixef(count_mod)
Extract formula from fitted galamm object
Description
Extract formula from fitted galamm object
Usage
## S3 method for class 'galamm'
formula(x, ...)
Arguments
x |
Object of class |
... |
Optional arguments passed on to other methods. Currently not used. |
Value
The formula used to fit the model.
See Also
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
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 <- 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
)
# Formula
formula(mod)
Fit a generalized additive latent and mixed model
Description
This function fits a generalized additive latent and mixed model
(GALAMMs), as described in
Sørensen et al. (2023).
The building blocks of these models are generalized additive mixed models
(GAMMs) (Wood 2017), of which
generalized linear mixed models
(Breslow and Clayton 1993; Harville 1977; Henderson 1975; Laird and Ware 1982)
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)
(Skrondal and Rabe-Hesketh 2004; Rabe-Hesketh et al. 2004)
which allows semiparametric estimation. The implemented algorithm used to
compute model estimates is described in
Sørensen et al. (2023),
and is an extension of the algorithm used for fitting generalized linear
mixed models by the lme4
package
(Bates et al. 2015). The syntax used to
define factor structures is based on that used by the PLmixed
package, which is detailed in
Rockwood and Jeon (2019).
Usage
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()
)
Arguments
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 |
Value
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 thefamily
arguments togalamm
. -
factor_interactions
List of formulas specifying interactions between latent and observed variables, as provided to the argumentfactor_interactions
togalamm
. If not provided, it isNULL
. -
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 bylme4::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. IsNULL
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 thelambda
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.
References
Bates DM, Mächler M, Bolker B, Walker S (2015).
“Fitting Linear Mixed-Effects Models Using Lme4.”
Journal of Statistical Software, 67(1), 1–48.
ISSN 1548-7660, doi:10.18637/jss.v067.i01.
Breslow NE, Clayton DG (1993).
“Approximate Inference in Generalized Linear Mixed Models.”
Journal of the American Statistical Association, 88(421), 9–25.
ISSN 0162-1459, doi:10.2307/2290687.
Harville DA (1977).
“Maximum Likelihood Approaches to Variance Component Estimation and to Related Problems.”
Journal of the American Statistical Association, 72(358), 320–338.
ISSN 0162-1459, doi:10.2307/2286796.
Henderson CR (1975).
“Best Linear Unbiased Estimation and Prediction under a Selection Model.”
Biometrics, 31(2), 423–447.
ISSN 0006-341X, doi:10.2307/2529430.
Laird NM, Ware JH (1982).
“Random-Effects Models for Longitudinal Data.”
Biometrics, 38(4), 963–974.
ISSN 0006-341X, doi:10.2307/2529876.
Rabe-Hesketh S, Skrondal A, Pickles A (2004).
“Generalized Multilevel Structural Equation Modeling.”
Psychometrika, 69(2), 167–190.
ISSN 1860-0980, doi:10.1007/BF02295939.
Rockwood NJ, Jeon M (2019).
“Estimating Complex Measurement and Growth Models Using the R Package PLmixed.”
Multivariate Behavioral Research, 54(2), 288–306.
ISSN 0027-3171, doi:10.1080/00273171.2018.1516541.
Skrondal A, Rabe-Hesketh S (2004).
Generalized Latent Variable Modeling, Interdisciplinary Statistics Series.
Chapman and Hall/CRC, Boca Raton, Florida.
Sørensen Ø, Fjell AM, Walhovd KB (2023).
“Longitudinal Modeling of Age-Dependent Latent Traits with Generalized Additive Latent and Mixed Models.”
Psychometrika, 88(2), 456–486.
ISSN 1860-0980, doi:10.1007/s11336-023-09910-z.
Wood SN (2017).
Generalized Additive Models: An Introduction with R, 2 edition.
Chapman and Hall/CRC.
See Also
Other modeling functions:
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 <- 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)
Control values for galamm fit
Description
This function can be called for controling the optimization
procedure used when fitting GALAMMs using galamm
.
Usage
galamm_control(
optim_control = list(),
method = c("L-BFGS-B", "Nelder-Mead"),
maxit_conditional_modes = 10,
pirls_tol_abs = 0.01,
reduced_hessian = FALSE
)
Arguments
optim_control |
List containing optimization parameters. If |
method |
Character string defining the algorithm to be used for
maximizing the marginal log-likelihood. The default is |
maxit_conditional_modes |
Maximum number of iterations in penalized
iteratively reweighted least squares algorithm. Ignored if |
pirls_tol_abs |
Absolute convergence criterion for penalized iteratively reweighted least squares algorithm. Defaults to 0.01, which means that when the reduction in marginal likelihood between two iterations is below 0.01, the iterations stop. |
reduced_hessian |
Logical value. Defaults to |
Value
Object of class galamm_control
, which typically will be
provided as an argument to galamm
.
References
Bates DM, Mächler M, Bolker B, Walker S (2015). “Fitting Linear Mixed-Effects Models Using Lme4.” Journal of Statistical Software, 67(1), 1–48. ISSN 1548-7660, doi:10.18637/jss.v067.i01.
BROYDEN CG (1970). “The Convergence of a Class of Double-rank Minimization Algorithms 1. General Considerations.” IMA Journal of Applied Mathematics, 6(1), 76–90. ISSN 0272-4960, doi:10.1093/imamat/6.1.76.
Byrd RH, Lu P, Nocedal J, Zhu C (1995). “A Limited Memory Algorithm for Bound Constrained Optimization.” SIAM Journal on Scientific Computing, 16(5), 1190–1208. ISSN 1064-8275, doi:10.1137/0916069.
Fletcher R (1970). “A New Approach to Variable Metric Algorithms.” The Computer Journal, 13(3), 317–322. ISSN 0010-4620, doi:10.1093/comjnl/13.3.317.
Goldfarb D (1970). “A Family of Variable-Metric Methods Derived by Variational Means.” Mathematics of Computation, 24(109), 23–26. ISSN 0025-5718, 1088-6842, doi:10.1090/S0025-5718-1970-0258249-6.
Nelder JA, Mead R (1965). “A Simplex Method for Function Minimization.” The Computer Journal, 7(4), 308–313. ISSN 0010-4620, doi:10.1093/comjnl/7.4.308.
Shanno DF (1970). “Conditioning of Quasi-Newton Methods for Function Minimization.” Mathematics of Computation, 24(111), 647–656. ISSN 0025-5718, 1088-6842, doi:10.1090/S0025-5718-1970-0274029-X.
See Also
Other optimization functions:
extract_optim_parameters.galamm()
Examples
# Define control object with quite a high degree of verbosity (trace = 6)
# and using the last 20 BFGS updates to estimate the Hessian in L-BFGS-B.
control <- galamm_control(optim_control = list(trace = 6, lmm = 20))
Example Data with Heteroscedastic Residuals
Description
Simulated dataset with residual standard deviation that varies between items.
Usage
hsced
Format
hsced
A data frame with 1200 rows and 5 columns:
- id
Subject ID.
- age
Timepoint.
- item
Item indicator.
- x
Explanatory variable
- y
Outcome.
References
There are no references for Rd macro \insertAllCites
on this help page.
See Also
Other datasets:
cognition
,
diet
,
epilep
,
latent_covariates
,
latent_covariates_long
,
lifespan
,
mresp
,
mresp_hsced
Simulated Data with Latent and Observed Covariates Interaction
Description
Simulated dataset for use in examples and testing with a latent covariate interacting with an observed covariate.
Usage
latent_covariates
Format
latent_covariates
A data frame with 600 rows and 5 columns:
- id
Subject ID.
- type
Type of observation in the
y
variable. If it equals"measurement1"
or"measurement2"
then the observation is a measurement of the latent variable. If it equals"response"
, then the observation is the actual response.- x
Explanatory variable.
- y
Observed response. Note, this includes both the actual response, and the measurements of the latent variable, since mathematically they are all treated as responses.
- response
Dummy variable indicating whether the given row is a response or not.
See Also
Other datasets:
cognition
,
diet
,
epilep
,
hsced
,
latent_covariates_long
,
lifespan
,
mresp
,
mresp_hsced
Simulated Longitudinal Data with Latent and Observed Covariates Interaction
Description
Simulated dataset for use in examples and testing with a latent covariate interacting with an observed covariate. In this data, each response has been measured six times for each subject.
Usage
latent_covariates_long
Format
latent_covariates_long
A data frame with 800 rows and 5
columns:
- id
Subject ID.
- type
Type of observation in the
y
variable. If it equals"measurement1"
or"measurement2"
then the observation is a measurement of the latent variable. If it equals"response"
, then the observation is the actual response.- x
Explanatory variable.
- y
Observed response. Note, this includes both the actual response, and the measurements of the latent variable, since mathematically they are all treated as responses.
- response
Dummy variable indicating whether the given row is a response or not.
See Also
Other datasets:
cognition
,
diet
,
epilep
,
hsced
,
latent_covariates
,
lifespan
,
mresp
,
mresp_hsced
Simulated Dataset with Lifespan Trajectories of Three Cognitive Domains
Description
This dataset is simulated based on the data used in Section 4.1 of Sørensen et al. (2023).
Usage
lifespan
Format
lifespan
A data frame with 54,457 rows and 7 columns:
- id
Subject ID.
- domain
Cognitive domain being measured. One of
"epmem"
for episodic memory,"wmem"
for working memory and"execfun"
for executive function/speed.- timepoint
Integer indicating the timepoint number.
- age
Age of participant at the timepoint.
- test
The particular test at this observation.
- y
Response. For
"epmem"
and"wmem"
this is the number of successes in 16 trials. For"execfun"
it is the time in seconds to complete the task.- retest
Integer indicating whether the participant has taken the test at a previous timepoint.
- domainepmem
Dummy variable for
domain=="epmem"
.- domainwmem
Dummy variable for
domain=="wmem"
.- domainexecfun
Dummy variable for
domain=="execfun"
.
References
Sørensen Ø, Fjell AM, Walhovd KB (2023). “Longitudinal Modeling of Age-Dependent Latent Traits with Generalized Additive Latent and Mixed Models.” Psychometrika, 88(2), 456–486. ISSN 1860-0980, doi:10.1007/s11336-023-09910-z.
See Also
Other datasets:
cognition
,
diet
,
epilep
,
hsced
,
latent_covariates
,
latent_covariates_long
,
mresp
,
mresp_hsced
Extract log likelihood, AIC, and related statistics from a GALAMM
Description
This function is assembles the values used by summary.galamm
.
Usage
llikAIC(object)
Arguments
object |
Object of class |
Value
A list containing AIC, BIC, log likelihood, deviance and residual degrees of freedom.
See Also
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Extract Log-Likelihood of galamm Object
Description
Extract Log-Likelihood of galamm Object
Usage
## S3 method for class 'galamm'
logLik(object, ...)
Arguments
object |
Object |
... |
Other arguments |
Value
Object of class logLik
See Also
deviance.galamm()
for a function returning deviance and
logLik()
for the generic function.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
# Extract log likelihood
logLik(mod)
Simulated Mixed Response Data
Description
Very basic mixed response dataset with one set of normally distributed responses and one set of binomially distributed responses.
Usage
mresp
Format
mresp
A data frame with 4000 rows and 5 columns:
- id
Subject ID.
- x
Predictor variable.
- y
Response.
- itemgroup
Factor variable which equals "a" for the normally distributed responses and "b" for the binomially distributed response (with 1 trial).
See Also
Other datasets:
cognition
,
diet
,
epilep
,
hsced
,
latent_covariates
,
latent_covariates_long
,
lifespan
,
mresp_hsced
Simulated Mixed Response Data with Heteroscedastic Residuals
Description
Mixed response dataset with one set of normally distributed responses and one set of binomially distributed responses. The normally distributed response follow two different residual standard deviations.
Usage
mresp_hsced
Format
mresp
A data frame with 4000 rows and 5 columns:
- id
Subject ID.
- x
Predictor variable.
- y
Response.
- itemgroup
Factor variable which equals "a" for the normally distributed responses and "b" for the binomially distributed response (with 1 trial).
- grp
Grouping variable denoting which of the two residual standard deviations apply. Only relevant for the normally distributed responses.
- isgauss
Dummy variable indicating whether the observation on the given line is normally (Gaussian) distributed or not.
See Also
Other datasets:
cognition
,
diet
,
epilep
,
hsced
,
latent_covariates
,
latent_covariates_long
,
lifespan
,
mresp
Extract the Number of Observations from a galamm Fit
Description
Extract the Number of Observations from a galamm Fit
Usage
## S3 method for class 'galamm'
nobs(object, ...)
Arguments
object |
An object of class |
... |
Optional arguments passed on to other methods. Currently not used. |
Value
A number
See Also
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Example model from lme4
data(sleepstudy, package = "lme4")
fm1 <- galamm(Reaction ~ Days + (Days | Subject), data = sleepstudy)
# There are 180 observations, which matches the number of rows in sleepstudy
nobs(fm1)
nrow(sleepstudy)
Diagnostic plots for galamm objects
Description
Diagnostic plots for galamm objects
Usage
## S3 method for class 'galamm'
plot(x, ...)
Arguments
x |
An object of class |
... |
Optional arguments passed on to the |
Value
A plot is displayed.
See Also
residuals.galamm()
for extracting residuals and plot()
for the
generic function.
Other summary functions:
anova.galamm()
,
plot_smooth.galamm()
,
print.galamm()
,
print.summary.galamm()
,
summary.galamm()
Examples
# Linear mixed model example from lme4
data("sleepstudy", package = "lme4")
mod <- galamm(Reaction ~ Days + (Days | Subject), data = sleepstudy)
# Diagnostic plot
plot(mod)
Plot smooth terms for galamm fits
Description
Plots smooth terms of a fitted galamm
object. This function is a thin
wrapper around mgcv::plot.gam
(Wood 2017).
Usage
## S3 method for class 'galamm'
plot_smooth(object, ...)
Arguments
object |
Object of class |
... |
Other optional arguments, passed on to |
Value
A plot is displayed on the screen.
References
Wood SN (2017). Generalized Additive Models: An Introduction with R, 2 edition. Chapman and Hall/CRC.
See Also
Other summary functions:
anova.galamm()
,
plot.galamm()
,
print.galamm()
,
print.summary.galamm()
,
summary.galamm()
Examples
# 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)
# We can turn off the rug at the bottom
plot_smooth(mod, shade = TRUE, rug = FALSE)
Predictions from a model at new data values
Description
Predictions are given at the population level, i.e., with random
effects set to zero. For fitted models including random effects, see
fitted.galamm
. For mixed response models, only predictions on
the scale of the linear predictors is supported.
Usage
## S3 method for class 'galamm'
predict(object, newdata = NULL, type = c("link", "response"), ...)
Arguments
object |
An object of class |
newdata |
Data from for which to evaluate predictions, in a
|
type |
Character argument specifying the type of prediction object to be returned. Case sensitive. |
... |
Optional arguments passed on to other methods. Currently used for
models with smooth terms, for which these arguments are forwarded to
|
Value
A numeric vector of predicted values.
See Also
fitted.galamm()
for model fits, residuals.galamm()
for
residuals, and predict()
for the generic function.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Poisson GLMM
count_mod <- galamm(
formula = y ~ lbas * treat + lage + v4 + (1 | subj),
data = epilep, family = poisson
)
# Plot response versus link:
plot(
predict(count_mod, type = "link"),
predict(count_mod, type = "response")
)
# Predict on a new dataset
nd <- data.frame(lbas = c(.3, .2), treat = c(0, 1), lage = 0.2, v4 = -.2)
predict(count_mod, newdata = nd)
predict(count_mod, newdata = nd, type = "response")
Print method for variance-covariance objects
Description
Print method for variance-covariance objects
Usage
## S3 method for class 'VarCorr.galamm'
print(
x,
digits = max(3, getOption("digits") - 2),
comp = c("Std.Dev.", "Variance"),
corr = any(comp == "Std.Dev."),
...
)
Arguments
x |
An object of class |
digits |
Optional arguments specifying number of digits to use when printing. |
comp |
Character vector of length 1 or 2 specifying which variance components to print. Case sensitive. Can take one of the values "Std.Dev." and "Variance". |
corr |
Logical value indicating whether covariances or correlations should be printed. |
... |
Optional arguments passed on to other methods. Currently not used. |
Value
The variance-covariance information is printed to the console and the
argument x
is silently returned.
Author(s)
This function is derived from lme4:::print.VarCorr.merMod
written by Douglas M. Bates, Martin Maechler, Ben Bolker, and Steve Walker.
References
Bates DM, Mächler M, Bolker B, Walker S (2015). “Fitting Linear Mixed-Effects Models Using Lme4.” Journal of Statistical Software, 67(1), 1–48. ISSN 1548-7660, doi:10.18637/jss.v067.i01.
See Also
VarCorr.galamm()
for the function creating the variance-covariance
objects.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
# Extract information on variance and covariance
VarCorr(mod)
Print method for GALAMM fits
Description
Print method for GALAMM fits
Usage
## S3 method for class 'galamm'
print(x, ...)
Arguments
x |
An object of class |
... |
Further arguments passed on to other methods. Currently not used. |
Value
Summary printed to screen. Invisibly returns the argument x
.
See Also
summary.galamm()
for the summary function and print()
for the
generic.
Other summary functions:
anova.galamm()
,
plot.galamm()
,
plot_smooth.galamm()
,
print.summary.galamm()
,
summary.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
print(mod)
Print method for summary GALAMM fits
Description
Print method for summary GALAMM fits
Usage
## S3 method for class 'summary.galamm'
print(x, digits = max(3, getOption("digits") - 3), ...)
Arguments
x |
An object of class |
digits |
Number of digits to present in outputs. |
... |
Further arguments passed on to other methods. Currently used by
|
Value
Summary printed to screen. Invisibly returns the argument x
.
Author(s)
Some of the code for producing summary information has been derived
from the summary methods of mgcv
(author: Simon Wood) and
lme4
(Bates et al. 2015)
(authors: Douglas M. Bates, Martin Maechler, Ben Bolker, and Steve Walker).
References
There are no references for Rd macro \insertAllCites
on this help page.
See Also
summary.galamm()
for the summary function and print()
for the
generic function.
Other summary functions:
anova.galamm()
,
plot.galamm()
,
plot_smooth.galamm()
,
print.galamm()
,
summary.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
summary(mod)
Extract random effects from galamm object.
Description
Extract random effects from galamm object.
Usage
## S3 method for class 'galamm'
ranef(object, ...)
Arguments
object |
An object of class |
... |
Optional parameters passed on to other methods. Currently not used. |
Value
An object of class ranef.galamm
, containing the requested
random effects.
Author(s)
This function is derived from lme4::ranef.merMod
, written by
Douglas Bates, Martin Maechler, Ben Bolker, Steve Walker.
References
Bates DM, Mächler M, Bolker B, Walker S (2015). “Fitting Linear Mixed-Effects Models Using Lme4.” Journal of Statistical Software, 67(1), 1–48. ISSN 1548-7660, doi:10.18637/jss.v067.i01.
See Also
fixef.galamm()
for fixed effects and coef.galamm()
for
coefficients more generally.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Poisson GLMM
count_mod <- galamm(
formula = y ~ lbas * treat + lage + v4 + (1 | subj),
data = epilep, family = poisson
)
# Extract random effects
ranef(count_mod)
Residuals of galamm objects
Description
Residuals of galamm objects
Usage
## S3 method for class 'galamm'
residuals(object, type = c("pearson", "deviance"), ...)
Arguments
object |
An object of class |
type |
Character of length one describing the type of residuals to be
returned. One of |
... |
Optional arguments passed on to other methods. Currently not used. |
Value
Numeric vector of residual values.
See Also
fitted.galamm()
for model fitted values, predict.galamm()
for
model predictions, and plot.galamm()
for diagnostic plots. The generic
function is residuals()
.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
response()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Poisson GLMM
count_mod <- galamm(
formula = y ~ lbas * treat + lage + v4 + (1 | subj),
data = epilep, family = poisson
)
# Extract residuals
residuals(count_mod)
Extract response values
Description
Extracts response values from a model.
Usage
response(object, ...)
Arguments
object |
An object of class |
... |
Optional arguments passed on to other methods. Currently not used. |
Value
A numerical vector with fit response values for each row in the input data.
See Also
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
sigma.galamm()
,
vcov.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
# Plot response versus fitted values
plot(fitted(mod), response(mod))
Set up smooth term with factor loading
Description
This is a very thin wrapper around mgcv::s
. It enables
the specification of loading variables for smooth terms. The last letter "l",
which stands for "loading", has been added to avoid namespace conflicts with
mgcv
and gamm4
.
Usage
sl(..., factor = NULL)
Arguments
... |
Arguments passed on to |
factor |
Optional character argument specifying the loading variable. Case sensitive. |
Details
The documentation of the function mgcv::s
should be consulted
for details on how to properly set up smooth terms. In particular, note
that these terms distinguish between ordered and unordered factor terms
in the by
variable, which can be provided in ...
and is
forwarded to mgcv::s
.
Value
An object of class xx.smooth.spec
, where xx
is a basis
identifying code given by the bs
argument of s
. It differs
from the smooth returned by mgcv::s
in that it has an additional
attribute named "factor"
which specifies any factor loading which
this smooth term should be multiplied with in order to produce the observed
outcome.
References
Wood SN (2003). “Thin Plate Regression Splines.” Journal of the Royal Statistical Society. Series B (Statistical Methodology), 65(1), 95–114. ISSN 1369-7412.
Wood SN (2017). Generalized Additive Models: An Introduction with R, 2 edition. Chapman and Hall/CRC.
See Also
Other modeling functions:
galamm()
,
t2()
Examples
# Linear mixed model with factor structures
dat <- subset(cognition, domain == 1 & timepoint == 1)
loading_matrix <- matrix(c(1, NA, NA), ncol = 1)
# Model with four thin-plate regression splines as basis functions
mod <- galamm(
formula = y ~ 0 + item + sl(x, k = 4, factor = "loading"),
data = dat,
load.var = "item",
lambda = loading_matrix,
factor = "loading"
)
# Model with four cubic regression splines as basis functions
mod <- galamm(
formula = y ~ 0 + item +
sl(x, bs = "cr", k = 4, factor = "loading"),
data = dat,
load.var = "item",
lambda = loading_matrix,
factor = "loading"
)
Extract square root of dispersion parameter from galamm object
Description
Extracts the square root of the dispersion parameter(s) from an object of
class galamm
, returned from galamm
. In the case of
conditionally Gaussian responses, this is the residual standard deviation.
When there are multiple dispersion parameters, e.g., with mixed response
type models, the square root of all of them are returned in a numeric vector.
Usage
## S3 method for class 'galamm'
sigma(object, ...)
Arguments
object |
An object of class |
... |
Optional parameters passed on to other methods. Currently not used. |
Value
The square root of one or more dispersion parameters.
See Also
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
vcov.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
# Extract residual standard deviation.
sigma(mod)
# The residual standard deviation applies to the base case. The variance
# function shown in the model output shows the estimated multiplier for
# various grouping levels:
summary(mod)
Summarizing GALAMM fits
Description
Summary method for class "galamm".
Usage
## S3 method for class 'galamm'
summary(object, ...)
Arguments
object |
An object of class |
... |
Further arguments passed on to other methods. Currently not used. |
Value
A list of summary statistics of the fitted model of class
summary.galamm
, containing the following elements:
-
AICtab
a table of model fit measures, returned byllikAIC
. -
call
the matched call used when fitting the model. -
fixef
a matrix with fixed effect estimated, returned byfixef
. -
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. -
model
a list with various elements related to the model setup and fit. See?galamm
for details. -
parameters
A list object with model parameters and related information. See?galamm
for details. -
Lambda
An object containing the estimated factor loadings. Returned fromfactor_loadings.galamm
. If there are no estimated factor loadings, then this object isNULL
. -
random_effects
a list containing the random effects. See?galamm
for details. -
VarCorr
An object of classVarCorr.galamm
, returned fromVarCorr.galamm
. -
weights
An object containing information about estimated variance functions, when there are heteroscedastic residuals. Otherwise the object isNULL
.
Author(s)
Some of the code for producing summary information has been derived
from the summary methods of mgcv
(author: Simon Wood) and
lme4
(Bates et al. 2015)
(authors: Douglas M. Bates, Martin Maechler, Ben Bolker, and Steve Walker).
See Also
print.summary.galamm()
for the print method and summary()
for
the generic.
Other summary functions:
anova.galamm()
,
plot.galamm()
,
plot_smooth.galamm()
,
print.galamm()
,
print.summary.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
summary(mod)
Set up smooth term with factor loading
Description
This is a very thin wrapper around mgcv::t2
. It enables
the specification of loading variables for smooth terms. The last letter
"l", which stands for "loading", has been added to avoid namespace
conflicts with mgcv
and gamm4
.
Usage
t2l(..., factor = NULL)
Arguments
... |
Arguments passed on to |
factor |
Optional character of length one specifying the loading variable. Case sensitive. |
Details
The documentation of the function mgcv::t2
should be consulted
for details on how to properly set up smooth terms. In particular, note
that these terms distinguish between ordered and unordered factor terms
in the by
variable, which can be provided in ...
and is
forwarded to mgcv::t2
.
Value
An object of class xx.smooth.spec
, where xx
is a basis
identifying code given by the bs
argument of t2
. It differs
from the smooth returned by mgcv::s
in that it has an additional
attribute named "factor"
which specifies any factor loading which
this smooth term should be multiplied with in order to produce the observed
outcome.
References
Wood SN (2003). “Thin Plate Regression Splines.” Journal of the Royal Statistical Society. Series B (Statistical Methodology), 65(1), 95–114. ISSN 1369-7412.
Wood SN (2017). Generalized Additive Models: An Introduction with R, 2 edition. Chapman and Hall/CRC.
See Also
Other modeling functions:
galamm()
,
s()
Examples
# Linear mixed model with factor structures
dat <- subset(cognition, domain == 1 & timepoint == 1)
loading_matrix <- matrix(c(1, NA, NA), ncol = 1)
# Model with four cubic regression splines as basis functions
mod <- galamm(
formula = y ~ 0 + item + t2l(x, k = 4, factor = "loading"),
data = dat,
load.var = "item",
lambda = loading_matrix,
factor = "loading"
)
# Model with four thin-plate regression splines as basis functions
mod <- galamm(
formula = y ~ 0 + item +
t2l(x, bs = "tp", k = 4, factor = "loading"),
data = dat,
load.var = "item",
lambda = loading_matrix,
factor = "loading"
)
Calculate variance-covariance matrix for GALAMM fit
Description
Calculate variance-covariance matrix for GALAMM fit
Usage
## S3 method for class 'galamm'
vcov(object, parm = "beta", ...)
Arguments
object |
Object of class |
parm |
The parameters for which the variance-covariance matrix should be calculated. Character vector with one or more of the elements "theta", "beta", "lambda", and "weights". Can also be an integer vector. When given as a character, it must be in only lowercase letters. |
... |
Further arguments passed on to other methods. Currently not used. |
Value
A variance-covariance matrix.
See Also
confint.galamm()
for the method computing confidence intervals.
See vcov()
for the generic function.
Other details of model fit:
VarCorr()
,
coef.galamm()
,
confint.galamm()
,
deviance.galamm()
,
factor_loadings.galamm()
,
family.galamm()
,
fitted.galamm()
,
fixef()
,
formula.galamm()
,
llikAIC()
,
logLik.galamm()
,
nobs.galamm()
,
predict.galamm()
,
print.VarCorr.galamm()
,
ranef.galamm()
,
residuals.galamm()
,
response()
,
sigma.galamm()
Examples
# Linear mixed model with heteroscedastic residuals
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = hsced
)
# Extract covariance matrix for fixed regression coefficients
vcov(mod, parm = "beta")
# and then for weights, which gives us the variance.
vcov(mod, parm = "weights")