Learn R Programming

multilevelmod

multilevelmod enables the use of multi-level models (a.k.a mixed-effects models, Bayesian hierarchical models, etc.) with the parsnip package.

(meme courtesy of @ChelseaParlett)

Installation

You can install the released version of multilevelmod from CRAN with:

install.packages("multilevelmod")

For the development version:

devtools::install_github("tidymodels/multilevelmod")

Available Engines

The multilevelmod package provides engines for the models in the following table.

modelenginemode
linear_regstan_glmerregression
linear_reglmerregression
linear_regglmerregression
linear_reggeeregression
linear_reglmeregression
linear_regglsregression
logistic_reggeeclassification
logistic_regglmerclassification
logistic_regstan_glmerclassification
poisson_reggeeregression
poisson_regglmerregression
poisson_regstan_glmerregression

Example

Loading mixedlevelmod will trigger it to add a few modeling engines to the parsnip model database. For Bayesian models, there are now stan-glmer engines for linear_reg(), logistic_reg(), and poisson_reg().

To use these, the function parsnip::fit() function should be used instead of parsnip::fit_xy() so that the model terms can be specified using the lme/lme4 syntax.

The sleepstudy data is used as an example:

library(multilevelmod)
set.seed(1234)
data(sleepstudy, package = "lme4")

mixed_model_spec <- linear_reg() %>% set_engine("lmer")

mixed_model_fit <- 
  mixed_model_spec %>% 
  fit(Reaction ~ Days + (Days | Subject), data = sleepstudy)

mixed_model_fit
#> parsnip model object
#> 
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: Reaction ~ Days + (Days | Subject)
#>    Data: data
#> REML criterion at convergence: 1743.628
#> Random effects:
#>  Groups   Name        Std.Dev. Corr
#>  Subject  (Intercept) 24.741       
#>           Days         5.922   0.07
#>  Residual             25.592       
#> Number of obs: 180, groups:  Subject, 18
#> Fixed Effects:
#> (Intercept)         Days  
#>      251.41        10.47

For a Bayesian model:

hier_model_spec <- linear_reg() %>% set_engine("stan_glmer")

hier_model_fit <- 
  hier_model_spec %>% 
  fit(Reaction ~ Days + (Days | Subject), data = sleepstudy)

hier_model_fit
#> parsnip model object
#> 
#> stan_glmer
#>  family:       gaussian [identity]
#>  formula:      Reaction ~ Days + (Days | Subject)
#>  observations: 180
#> ------
#>             Median MAD_SD
#> (Intercept) 251.5    6.5 
#> Days         10.5    1.7 
#> 
#> Auxiliary parameter(s):
#>       Median MAD_SD
#> sigma 25.9    1.6  
#> 
#> Error terms:
#>  Groups   Name        Std.Dev. Corr
#>  Subject  (Intercept) 24           
#>           Days         7       0.08
#>  Residual             26           
#> Num. levels: Subject 18 
#> 
#> ------
#> * For help interpreting the printed output see ?print.stanreg
#> * For info on the priors used see ?prior_summary.stanreg

Contributing

This project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('multilevelmod')

Monthly Downloads

397

Version

1.0.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Hannah Frick

Last Published

June 17th, 2022

Functions in multilevelmod (1.0.0)

multilevelmod-package

parsnip methods for hierarchical models
riesby

Imipramine longitudinal data
gee_fit

GEE fitting function
msa_data

Measurement systems analysis data
longitudinal_counts

Simulated longitudinal Poisson counts