Learn R Programming

LMMELSM

LMMELSM stands for latent, multidimensional mixed effects location scale models.

The mixed effects location scale model is a multilevel model in which both the location ($\mu$) and scale ($\sigma$) are modeled and have random effects. That is, in addition to random intercepts and coefficients, the MELSM allows variance to differ between groups, and across covariates. Within the MELSM framework, variances are meaningful, and can be directly modeled. MELSMs are useful for predicting volatility, variance, uncertainty, variation between group means, measurement error variance, and much more. As an added side effect, prediction intervals are tailored to each individual or group (differing predictive uncertainty), and fixed effects are more robust because those with less variance (more reliability) are implicitly weighted more.

LMMELSM extends the MELSM to include latent variables. Instead of predicting the expected values and variances of observed or computed variables, the LMMELSM predicts expected values and variances of multiple correlated latent factors. Assuming an adequate measurement model, the LMMELSM therefore models the variance of a reduced-error score.

LMMELSM is therefore useful for modeling conditional and group-specific latent means and variances. One such example is modeling the intraindividual variability of latent scores of a persons over time in an experience sampling methodology. Another example is modeling the heterogeneity of individuals' latent means from person-level covariates.

LMMELSM currently supports:

  • Observed outcomes
  • Latent outcomes
  • Single or multiple outcomes
  • Location modeling, with random intercepts and coefficients
  • Within-group scale modeling, with random intercepts and coefficients
  • Between-group scale modeling (on the between-group mean variances)

Installation

You can install the released version of LMMELSM from github with:

remotes::install_github("stephensrmmartin/LMMELSM")

Example

In this example, increasingly complex MELSMs are modeled, and demonstrate the simple formula syntax.

library(LMMELSM)

data(sim_data)

# Intercept-only model
fit <- lmmelsm(Agreeableness ~ A_1 + A_2 + A_3 + A_4 + A_5 + A_6, subject, sim_data)

# Random Time-varying predictor on location
fit <- lmmelsm(list(Agreeableness ~ A_1 + A_2 + A_3 + A_4 + A_5 + A_6,
                    location ~ x1 | x1),
               subject, sim_data)

# Random Time-varying predictors on both location and scale
fit <- lmmelsm(list(Agreeableness ~ A_1 + A_2 + A_3 + A_4 + A_5 + A_6,
                    location ~ x1 | x1,
                    scale ~ x2 | x2),
               subject, sim_data)

# Time-varying predictors, person-level predictors on location, scale, and between-group variance
# Multidimensional!
fit <- lmmelsm(list(Agreeableness ~ A_1 + A_2 + A_3 + A_4 + A_5 + A_6,
                    Neuroticism ~ N_1 + N_2 + N_3 + N_4 + N_5 + N_6,
                    location ~ x1 + baseline | x1,
                    scale ~ x2 + baseline | x2,
                    between ~ baseline),
               subject, sim_data)

# Time-varying predictors, person-level predictors on location, scale, and between-group variance
fit <- lmmelsm(list(Agreeableness ~ A_1 + A_2 + A_3 + A_4 + A_5 + A_6,
                    Neuroticism ~ N_1 + N_2 + N_3 + N_4 + N_5 + N_6,
                    location ~ x1 + baseline | x1,
                    scale ~ x1 + x2 + baseline | x1 + x2,
                    between ~ baseline),
               subject, sim_data)


# Non-latent Multivariate MELSM
fit <- lmmelsm(list(observed ~ A_1 + N_1,
                    location ~ x1 + baseline|x1,
                    scale ~ x2 + baseline|x2,
                    between ~ baseline),
               subject, sim_data)

# Summarize
summary(fit)

# Get subject-specific random effects
ranef(fit)

# Get subject-specific coefficients
coef(fit)

Copy Link

Version

Install

install.packages('LMMELSM')

Monthly Downloads

588

Version

0.2.0

License

MIT + file LICENSE

Maintainer

Stephen Martin

Last Published

December 15th, 2022

Functions in LMMELSM (0.2.0)

.sep

Print separator.
loo.lmmelsm

loo method for LMMELSM objects.
.simulate.X

Simulate covariates without correlation.
lmmelsm

Specify and fit the (latent) (multivariate) melsm.
fitted.lmmelsm

Extracted model fitted variates.
.newline

Print newline.
%IfNull%

Operator for testing NULL and returning expr if NULL
.combine_RHS

Combines multiple formulas' RHS into one.
.magicsep

Convert char vector to columns.
.get_RHS

Get RHS terms or variables.
.parse_formula.indicators

Compute indicator data.
.parse_formula.predictor

Compute predictor data.
.tidy_summary

Takes stan summary, returns summary with indices-as-columns.
.pars_to_indices

Convert stan par-string to numeric columns.
simulate_lmmelsm

Simulate data from latent uni/multidimensional MELSM
sim_data

Simulated data for fitting the LMMELSM
nlist

Creates named list.
predict.lmmelsm

Predict method for lmmelsm objects.
summary.lmmelsm

Summary method for lmmelsm objects.
.parse_formula

Convert spec to stan data.
coef.lmmelsm

Extract group-specific coefficients.
.get_indicator_spec

Get indicator spec for stan model.
.list_zip

Zip two lists together with function.
.summary_rearrange

Rearrange summary output.
print.summary.lmmelsm

Print method for summary.lmmelsm objects.
print.lmmelsm

Print method for lmmelsm objects.
.summarize

Compute posterior summaries.
row_multiply_list_mats

Multiply a row by a list of matrices
ranef.lmmelsm

Extract random effects.
.which_location_scale

Check for location-scale formulas
.detect_L2_only

Detect whether the predictors are L2-only
LMMELSM-package

The 'LMMELSM' package.
.get_LHS

Get LHS variable as string.
.full_to_lower_tri

Get indices for subsetting lower-tri summaries of square matrices.
.add_group_codings

Adds group codings for predictive df.
.get_elapsed_time

Gets elapsed time.
.get_formula_names

Get names in formula.