The mlstats package provides tools for multilevel descriptive
statistics and data preparation. It computes within-group and
between-group correlations (via variance decomposition or two-level
structural equation modeling), intraclass correlation coefficients
(ICCs), and descriptive statistics for nested data (e.g., repeated
measurements per person), supporting both frequentist (via lme4 or
lavaan) and Bayesian (via brms) estimation. Results are formatted
according to APA standards and can be exported as tables using gt or
tinytable. The package also includes functions for decomposing
variables into within-group and between-group components for use in
Random Effects Within-Between (REWB) models.
You can install mlstats from CRAN:
install.packages("mlstats")You can also install the development version from GitHub:
# install.packages("pak")
pak::pak("felixdidi/mlstats")mlstats is built around two main tasks:
- Multilevel descriptives:
mldesc()computes means, standard deviations, ranges, ICCs, and a combined within-/between-group correlation matrix in a single call.within_between_correlations()computes only the correlation matrix. Both support multiple print methods for publication-ready output. - Variable decomposition:
decompose_within_between()splits time-varying predictors into within-group deviations (situational fluctuations) and between-group means (stable differences), ready for use in REWB models.
This example uses the simulated media_diary dataset included with
mlstats. It mimics a study in which 100 participants were asked to
complete a brief daily survey for up to 14 consecutive days (N = 100
persons, T = 5–14 daily observations per person, 1,184 total).
library(mlstats)
data("media_diary")
media_diary |>
mldesc(
group = "person",
vars = c("self_control", "wellbeing", "screen_time", "stress")
)## # Multilevel Descriptive Statistics
## ============ ===== ====== ===== ========= ===== ===== ===== ===== =====
## variable n_obs m sd range `1` `2` `3` `4` icc
## ------------ ----- ------ ----- --------- ----- ----- ----- ----- -----
## 1 Self control 100 3.93 0.70 2.40–6.20 – NA NA NA 1.00
## 2 Wellbeing 1,184 4.42 0.89 1.50–7.00 .47* – .42* -.40* .50
## 3 Screen time 1,184 132.05 39.01 15–246 -.66* -.32* – .29* .34
## 4 Stress 1,184 3.77 0.95 1–7 -.55* -.27* .46* – .39
## ============ ===== ====== ===== ========= ===== ===== ===== ===== =====
## # ℹ Within-person correlations above, between-person correlations below the
## # diagonal.
## # ℹ All correlations marked with a star are significant at p < .05.
## # ℹ Based on 100 persons and 1,184 observations (median 12 per person; range:
## # 5–14).
## # ℹ Correlations estimated via variance decomposition.
## # ℹ Group-weighted multilevel descriptive statistics computed with mlstats.
mlstats comes with documentation vignettes to help you get started:
- Learn a best practice workflow in
vignette("mlstats"). - Learn how to report multilevel descriptive statistics in
vignette("multilevel-descriptives")and how to transform them into publication-ready multilevel descriptive tables invignette("tables"). - Learn how to use decomposed variables and interpret effects in
Random Effects Within-Between (REWB) models in
vignette("rewb-models"). - Learn about the different estimation approaches (decomposition,
SEM, and Bayesian) for within- and between-group correlations in
vignette("correlation-methods").
Bell, A., Fairbrother, M., & Jones, K. (2019). Fixed and random effects models: Making an informed choice. Quality & Quantity, 53(2), 1051–1074. https://doi.org/10.1007/s11135-018-0802-x
Enders, C. K., & Tofighi, D. (2007). Centering predictor variables in cross-sectional multilevel models: A new look at an old issue. Psychological Methods, 12(2), 121–138. https://doi.org/10.1037/1082-989X.12.2.121
Pedhazur, E. J. (1997). Multiple regression in behavioral research: Explanation and prediction (3rd ed.). Harcourt Brace.
