| Title: | Linear Mixed Models with Correlated Residuals |
|---|---|
| Description: | Fits Gaussian linear mixed models by explicitly optimizing the maximum likelihood or restricted maximum likelihood criterion. Random effects and correlated residual structures can be combined in the same model. Fixed-effect inference includes Satterthwaite and Kenward-Roger denominator degrees of freedom, type III tests, likelihood-ratio model comparisons, and estimated marginal means. |
| Authors: | Aurélien Nicosia [aut, cre] |
| Maintainer: | Aurélien Nicosia <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.0 |
| Built: | 2026-10-19 21:01:09 UTC |
| Source: | https://github.com/AurelienNicosiaULaval/lmmix |
Augment data with fitted values and residuals
## S3 method for class 'lmm' augment(x, data = NULL, newdata = NULL, ...)## S3 method for class 'lmm' augment(x, data = NULL, newdata = NULL, ...)
x |
An |
data |
Optional data used for augmentation. By default, excluded rows
are restored when the model used |
newdata |
Optional new data. If supplied, it takes precedence over
|
... |
Additional arguments passed to |
A tibble with .fitted, .resid, and .std.resid columns.
Extract fixed effects
fixef(object, ...)fixef(object, ...)
object |
A fitted model. |
... |
Additional arguments. |
lmm modelOne-row model summary for an lmm model
## S3 method for class 'lmm' glance(x, ...)## S3 method for class 'lmm' glance(x, ...)
x |
An |
... |
Additional arguments. |
A one-row tibble.
lmm() explicitly evaluates a profiled ML or REML criterion for
V = Z G Z' + R. It can combine random effects with an independent,
compound-symmetric, AR(1), full or fixed-band Toeplitz, or unstructured
residual covariance.
Missing values are handled according to na.action.
lmm( data, formula, random = NULL, repeated = NULL, structure = c("id", "cs", "ar1", "toep", "un"), method = c("REML", "ML"), ddf = c("satterthwaite", "residual", "kenward-roger"), weights = NULL, offset = NULL, contrasts = NULL, control = lmm_control(), na.action = stats::na.omit )lmm( data, formula, random = NULL, repeated = NULL, structure = c("id", "cs", "ar1", "toep", "un"), method = c("REML", "ML"), ddf = c("satterthwaite", "residual", "kenward-roger"), weights = NULL, offset = NULL, contrasts = NULL, control = lmm_control(), na.action = stats::na.omit )
data |
A data frame or tibble. Placing |
formula |
A two-sided fixed-effects formula. |
random |
Optional one-sided random-effects formula such as
|
repeated |
Optional one-sided repeated-measures formula such as
|
structure |
Residual covariance structure: |
method |
Estimation method: restricted maximum likelihood ( |
ddf |
Denominator degrees-of-freedom method: |
weights |
Optional known relative precision weights. The residual
covariance is scaled on both sides by |
offset |
Optional numeric offset added to the fixed-effect predictor.
An |
contrasts |
Optional contrasts passed to |
control |
An object returned by |
na.action |
Missing-value action: |
The random argument accepts a grouping formula or a list of independent
grouping formulas. The repeated argument
accepts one ordering variable and one grouping expression. Each repeated
group may have at most one observation per ordering value. The likelihood
factors independent connected covariance components separately. The fitted
object retains the complete marginal covariance for model methods.
An object of class lmm. The object stores fixed and random-effect
estimates, covariance components, fitted values, residuals, optimization
diagnostics, the analysis data, and the model matrices.
Harville, D. A. (1977). Maximum likelihood approaches to variance component estimation and to related problems. Journal of the American Statistical Association, 72(358), 320-338. doi:10.1080/01621459.1977.10480998
LaMotte, L. R. (2007). A direct derivation of the REML likelihood function. Statistical Papers, 48(2), 321-327. doi:10.1007/s00362-006-0335-6
Pinheiro, J. C., and Bates, D. M. (2000). Mixed-Effects Models in S and S-PLUS. Springer. doi:10.1007/b98882
Kenward, M. G., and Roger, J. H. (1997). Small sample inference for fixed effects from restricted maximum likelihood. Biometrics, 53(3), 983-997. doi:10.2307/2533558
fit <- lmm( data = nlme::Orthodont, formula = distance ~ age + Sex, random = ~ 1 | Subject ) summary(fit)fit <- lmm( data = nlme::Orthodont, formula = distance ~ age + Sex, random = ~ 1 | Subject ) summary(fit)
lmm()
Control numerical optimization for lmm()
lmm_control( optimizer = c("auto", "nlminb", "optim"), optim_method = "BFGS", max_iter = 1000L, rel_tol = 1e-08, x_tol = 1e-08, initial = NULL, lower = -20, upper = 20, deriv_method = "Richardson", max_restarts = 3L, restart_scale = 0.25 )lmm_control( optimizer = c("auto", "nlminb", "optim"), optim_method = "BFGS", max_iter = 1000L, rel_tol = 1e-08, x_tol = 1e-08, initial = NULL, lower = -20, upper = 20, deriv_method = "Richardson", max_restarts = 3L, restart_scale = 0.25 )
optimizer |
Optimizer strategy: |
optim_method |
Method passed to |
max_iter |
Maximum number of optimizer iterations. |
rel_tol |
Relative convergence tolerance. |
x_tol |
Parameter convergence tolerance for |
initial |
Optional numeric vector of unconstrained starting values. |
lower, upper
|
Bounds for unconstrained covariance parameters. |
deriv_method |
Numerical differentiation method passed to |
max_restarts |
Maximum number of deterministic restart attempts after an unsuccessful initial fit. |
restart_scale |
Size of deterministic perturbations to starting values. |
An object of class lmm_control.
lmm fitsMethods are provided for extracting likelihood quantities, fitted values, residuals, model matrices and formulas, as well as for prediction, type III testing and fixed-effect confidence intervals.
## S3 method for class 'lmm' print(x, ...) ## S3 method for class 'lmm' coef(object, ...) ## S3 method for class 'lmm' vcov(object, adjusted = TRUE, ...) ## S3 method for class 'lmm' logLik(object, ...) ## S3 method for class 'lmm' AIC(object, ..., k = 2) ## S3 method for class 'lmm' BIC(object, ...) ## S3 method for class 'lmm' deviance(object, ...) ## S3 method for class 'lmm' nobs(object, ...) ## S3 method for class 'lmm' fitted(object, type = c("conditional", "marginal"), ...) ## S3 method for class 'lmm' residuals(object, type = c("response", "pearson", "marginal"), ...) ## S3 method for class 'lmm' plot(x, which = c("residuals", "qq", "fitted"), ...) ## S3 method for class 'lmm' sigma(object, ...) ## S3 method for class 'lmm' predict( object, newdata = NULL, re.form = NULL, allow.new.levels = FALSE, se.fit = FALSE, interval = c("none", "confidence", "prediction"), level = 0.95, na.action = stats::na.pass, ... ) ## S3 method for class 'lmm' simulate(object, nsim = 1, seed = NULL, ...) ## S3 method for class 'lmm' update(object, formula., ..., evaluate = TRUE) ## S3 method for class 'lmm' model.matrix(object, data = NULL, ...) ## S3 method for class 'lmm' model.frame(formula, ...) ## S3 method for class 'lmm' formula(x, fixed.only = TRUE, ...) ## S3 method for class 'lmm' terms(x, ...) ## S3 method for class 'lmm' anova( object, ..., type = 3, refit = TRUE, test = c("chisq", "parametric.bootstrap"), nsim = 199L, seed = NULL ) ## S3 method for class 'lmm' confint(object, parm = names(object$coefficients), level = 0.95, ...)## S3 method for class 'lmm' print(x, ...) ## S3 method for class 'lmm' coef(object, ...) ## S3 method for class 'lmm' vcov(object, adjusted = TRUE, ...) ## S3 method for class 'lmm' logLik(object, ...) ## S3 method for class 'lmm' AIC(object, ..., k = 2) ## S3 method for class 'lmm' BIC(object, ...) ## S3 method for class 'lmm' deviance(object, ...) ## S3 method for class 'lmm' nobs(object, ...) ## S3 method for class 'lmm' fitted(object, type = c("conditional", "marginal"), ...) ## S3 method for class 'lmm' residuals(object, type = c("response", "pearson", "marginal"), ...) ## S3 method for class 'lmm' plot(x, which = c("residuals", "qq", "fitted"), ...) ## S3 method for class 'lmm' sigma(object, ...) ## S3 method for class 'lmm' predict( object, newdata = NULL, re.form = NULL, allow.new.levels = FALSE, se.fit = FALSE, interval = c("none", "confidence", "prediction"), level = 0.95, na.action = stats::na.pass, ... ) ## S3 method for class 'lmm' simulate(object, nsim = 1, seed = NULL, ...) ## S3 method for class 'lmm' update(object, formula., ..., evaluate = TRUE) ## S3 method for class 'lmm' model.matrix(object, data = NULL, ...) ## S3 method for class 'lmm' model.frame(formula, ...) ## S3 method for class 'lmm' formula(x, fixed.only = TRUE, ...) ## S3 method for class 'lmm' terms(x, ...) ## S3 method for class 'lmm' anova( object, ..., type = 3, refit = TRUE, test = c("chisq", "parametric.bootstrap"), nsim = 199L, seed = NULL ) ## S3 method for class 'lmm' confint(object, parm = names(object$coefficients), level = 0.95, ...)
x, object, formula
|
An |
... |
Additional fitted |
adjusted |
Whether |
k |
Penalty per parameter used by |
type |
For |
which |
Diagnostic plot to draw: standardized residuals against fitted values, a normal quantile-quantile plot, or observed against fitted values. |
newdata |
Optional data frame used for prediction. |
re.form |
|
allow.new.levels |
Whether conditional predictions may include new grouping levels. Their random contribution is zero when allowed. |
se.fit |
Whether |
interval |
Prediction interval type. |
level |
Confidence level for fixed-effect and covariance intervals. |
na.action |
Missing-value action retained for prediction-method compatibility. |
nsim |
Number of simulations for a parametric-bootstrap comparison. |
seed |
Optional integer seed for a reproducible parametric bootstrap. |
formula. |
Optional formula update accepted by |
evaluate |
Whether |
data |
Optional data frame used to construct a fixed-effects model matrix. |
fixed.only |
Whether |
refit |
Whether REML models that differ in fixed effects are refitted with ML before comparison. |
test |
Reference test for likelihood-ratio model comparisons. The
default |
parm |
Parameters requested from |
Conditional fitted values and predictions include empirical random effects. Marginal values use fixed effects only. In prediction from new data, known grouping levels use their fitted random effect. New levels are rejected by default and receive a random contribution of zero only when explicitly allowed.
ranef() always returns a named list with one tibble per random-effect
term, including for models with a single term. It returns an empty list for
marginal models. sigma() is defined as sqrt(mean(diag(R))); it is a
descriptive residual scale when residual variances differ across
observations.
The return value follows the corresponding base R generic.
Self, S. G., and Liang, K.-Y. (1987). Asymptotic properties of maximum likelihood estimators and likelihood ratio tests under nonstandard conditions. Journal of the American Statistical Association, 82(398), 605-610. doi:10.1080/01621459.1987.10478472
Davison, A. C., and Hinkley, D. V. (1997). Bootstrap Methods and Their Application. Cambridge University Press. doi:10.1017/CBO9780511802843
lmm modelMarginal means use equal weights over nuisance-factor levels. Numeric
covariates not listed in specs are held at their observed means.
Multiplicity adjustment applies to pairwise p-values. Simultaneous
Bonferroni confidence intervals are used automatically when p-values are
adjusted, unless conf_adjust = "none" is requested.
lsmeans( object, specs, pairwise = FALSE, at = list(), level = 0.95, adjust = "none", conf_adjust = c("auto", "none", "bonferroni"), ... )lsmeans( object, specs, pairwise = FALSE, at = list(), level = 0.95, adjust = "none", conf_adjust = c("auto", "none", "bonferroni"), ... )
object |
An |
specs |
Variables defining the marginal means, supplied as a character
vector or one-sided formula. Use |
pairwise |
Whether to return pairwise differences as well as means. |
at |
Named list overriding reference-grid values. |
level |
Confidence level. |
adjust |
Multiplicity adjustment passed to |
conf_adjust |
Confidence-interval adjustment. |
... |
Reserved for future extensions. |
A tibble, or a list with lsmeans and contrasts tibbles when
pairwise comparisons are requested.
An unbalanced experiment with three drugs in three centers and three repeated measurement times. This is the multilocation repeated-measures example described by Milliken and Johnson (2009, Section 28.3).
multicentremulticentre
A data frame with 153 rows and 5 variables:
Center identifier with levels R, S, and T.
Drug identifier with levels 1, 2, and 3.
Subject identifier within each center and drug.
Repeated-measurement occasion with levels 1, 2, and 3.
Continuous response, including 28 missing values.
Milliken, G. A., and Johnson, D. E. (2009). Analysis of Messy Data, Volume 1: Designed Experiments (2nd ed.), Section 28.3. Chapman and Hall/CRC. doi:10.1201/EBK1584883340
Extract random effects
ranef(object, ...)ranef(object, ...)
object |
A fitted model. |
... |
Additional arguments. |
emmeans
These methods allow emmeans::emmeans() to recover the fixed-effects
reference grid and use model-specific Satterthwaite degrees of freedom.
## S3 method for class 'lmm' recover_data(object, frame = object$model_frame, ...) ## S3 method for class 'lmm' emm_basis(object, trms, xlev, grid, ...)## S3 method for class 'lmm' recover_data(object, frame = object$model_frame, ...) ## S3 method for class 'lmm' emm_basis(object, trms, xlev, grid, ...)
object |
An |
frame |
Model frame. |
... |
Additional arguments passed by |
trms, xlev, grid
|
Arguments supplied by |
An emmeans data-recovery object.
A basis list used by emmeans.
Growth measurements for 11 girls and 16 boys at ages 8, 10, 12, and 14. SAS uses these Potthoff and Roy data in Example 79.2 of the SAS/STAT 14.3 documentation for the MIXED procedure.
sas_growthsas_growth
A data frame with 108 rows and 4 variables:
Person identifier with 27 levels.
Gender factor with levels F and M.
Age in years.
Continuous growth response.
SAS Institute Inc. (2017). SAS/STAT 14.3 User's Guide, Example 79.2, Repeated Measures. https://support.sas.com/documentation/onlinedoc/stat/
Winter-wheat yields for three cultivars in three blocks. Each plot contains twelve subplots arranged on two sides of a line-source sprinkler. SAS uses these data in Example 79.6 of the SAS/STAT 14.3 documentation to combine three random-intercept terms with a four-band Toeplitz residual covariance.
sas_line_sourcesas_line_source
A data frame with 108 rows and 6 variables:
Block identifier with three levels.
Winter-wheat cultivar with three levels.
Ordered subplot position from 1 to 12.
Irrigation level with six levels.
Side of the sprinkler with levels North and South.
Continuous yield response.
SAS Institute Inc. (2017). SAS/STAT 14.3 User's Guide, Example 79.6, Line-Source Sprinkler Irrigation. https://support.sas.com/documentation/onlinedoc/stat/
Pharmaceutical stability measurements for three batches at six shelf ages. SAS uses these simulated data in Example 79.5 of the SAS/STAT 14.3 documentation for the MIXED procedure.
sas_random_coefficientssas_random_coefficients
A data frame with 108 rows and 4 variables:
Batch identifier with three levels.
Shelf age in months.
Replicate identifier within a batch and month.
Assay response, including 24 missing values.
SAS Institute Inc. (2017). SAS/STAT 14.3 User's Guide, Example 79.5, Random Coefficients. https://support.sas.com/documentation/onlinedoc/stat/
A balanced split-plot experiment with four blocks, three whole-plot levels, and two subplot levels. SAS uses this data set in Example 79.1 of the SAS/STAT 14.3 documentation for the MIXED procedure.
sas_split_plotsas_split_plot
A data frame with 24 rows and 4 variables:
Block identifier with four levels.
Whole-plot treatment factor with three levels.
Subplot treatment factor with two levels.
Continuous response.
SAS Institute Inc. (2017). SAS/STAT 14.3 User's Guide, Example 79.1, Split-Plot Design. https://support.sas.com/documentation/onlinedoc/stat/
lmm fitSummarize an lmm fit
## S3 method for class 'lmm' summary(object, level = 0.95, ...)## S3 method for class 'lmm' summary(object, level = 0.95, ...)
object |
An |
level |
Confidence level for fixed effects. |
... |
Additional arguments. |
An object of class summary.lmm.
lmm modelTidy an lmm model
## S3 method for class 'lmm' tidy( x, effects = c("fixed", "ran_pars", "all"), conf.int = FALSE, conf.level = 0.95, ... )## S3 method for class 'lmm' tidy( x, effects = c("fixed", "ran_pars", "all"), conf.int = FALSE, conf.level = 0.95, ... )
x |
An |
effects |
Effects to return: fixed effects, covariance parameters, or both. |
conf.int |
Whether to include confidence limits for fixed effects. |
conf.level |
Confidence level. |
... |
Additional arguments. |
A tibble.
Extract covariance components
VarCorr(object, ...)VarCorr(object, ...)
object |
A fitted model. |
... |
Additional arguments. |