| Title: | Hierarchical Linear Modeling with Visualization and Decomposition |
|---|---|
| Description: | Provides functions for visualization and decomposition in hierarchical linear models (HLM) for applications in education, psychology, and the social sciences. Includes variance decomposition for two-level and three-level data structures following Snijders and Bosker (2012, ISBN:9781849202015), intraclass correlation (ICC) estimation and design effect computation as described in Shrout and Fleiss (1979) <doi:10.1037/0033-2909.86.2.420>, and contextual effect decomposition via the Mundlak (1978) <doi:10.2307/1913646> specification distinguishing within- and between-cluster components. Supports visualization of random slopes and cross-level interactions following Hofmann and Gavin (1998) <doi:10.1177/014920639802400504> and Hamaker and Muthen (2020) <doi:10.1037/met0000239>. Multilevel models are estimated using 'lme4' (Bates et al., 2015 <doi:10.18637/jss.v067.i01>). An optional 'Shiny' application enables interactive exploration of model components and parameter variation. The implementation follows the multilevel modeling framework of Raudenbush and Bryk (2002, ISBN:9780761919049). |
| Authors: | Subir Hait [aut, cre] (ORCID: <https://orcid.org/0009-0004-9871-9677>) |
| Maintainer: | Subir Hait <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-21 06:06:12 UTC |
| Source: | https://github.com/causalfragility-lab/hlmlab |
Given a multilevel model with a within-cluster centered predictor and its cluster-mean counterpart (Mundlak specification), this function extracts the within- and between-cluster effects and computes the contextual effect (between - within).
hlm_context(model, x_within, x_between)hlm_context(model, x_within, x_between)
model |
A fitted |
x_within |
Name of the within-cluster centered predictor (string). |
x_between |
Name of the cluster-mean predictor (string). |
An object of class hlm_context, which is a tibble with:
effect_type |
Within, between, or contextual. |
estimate |
Estimate of the effect. |
se |
Standard error (approximate for contextual). |
## Not run: m <- lme4::lmer(math_score ~ SES_c + SES_mean + (1 | school_id), data = d) ctx <- hlm_context(m, x_within = "SES_c", x_between = "SES_mean") ctx hlm_context_plot(ctx) ## End(Not run)## Not run: m <- lme4::lmer(math_score ~ SES_c + SES_mean + (1 | school_id), data = d) ctx <- hlm_context(m, x_within = "SES_c", x_between = "SES_mean") ctx hlm_context_plot(ctx) ## End(Not run)
Convenience wrapper to plot contextual effects
hlm_context_plot(object)hlm_context_plot(object)
object |
An object of class |
A ggplot object.
Decomposes a continuous variable into between-cluster, and within-cluster components (2-level), or into between-cluster (B), between-person (P), and within-person (W) components for longitudinal 3-level data.
hlm_decompose(data, var, cluster, id = NULL, time = NULL)hlm_decompose(data, var, cluster, id = NULL, time = NULL)
data |
A data frame. |
var |
Name of the focal variable (string). |
cluster |
Name of the cluster ID variable (e.g., school/classroom). |
id |
Optional person ID variable for longitudinal data. |
time |
Optional time variable (not required for the algebra, but noted in the output for clarity). |
This is primarily a teaching tool: it shows how total variance is partitioned across levels.
An object of class hlm_decompose with components:
data |
Original data with added decomposition columns. |
summary |
A tibble summarizing variance components and shares. |
## Not run: hlm_decompose(d, var = "math_score", cluster = "school_id") hlm_decompose(d, var = "math_score", cluster = "school_id", id = "student_id", time = "wave") ## End(Not run)## Not run: hlm_decompose(d, var = "math_score", cluster = "school_id") hlm_decompose(d, var = "math_score", cluster = "school_id", id = "student_id", time = "wave") ## End(Not run)
Convenience wrapper around hlm_decompose() for 3-level data:
clusters (e.g., schools), persons within clusters, and repeated measures
within persons over time.
hlm_decompose_long(data, var, cluster, id, time = NULL)hlm_decompose_long(data, var, cluster, id, time = NULL)
data |
A data frame. |
var |
Name of the focal variable (string). |
cluster |
Cluster ID (e.g., school/classroom) (string). |
id |
Person ID within cluster (string). |
time |
Optional time variable (string); stored in the output for reference but not required for the algebra. |
An object of class hlm_decompose with between-cluster (B),
between-person (P), and within-person (W) variance components and shares.
## Not run: hlm_decompose_long(data = d_long, var = "math_score", cluster = "school_id", id = "student_id", time = "wave") ## End(Not run)## Not run: hlm_decompose_long(data = d_long, var = "math_score", cluster = "school_id", id = "student_id", time = "wave") ## End(Not run)
Computes the intraclass correlation (ICC) and, optionally, a design effect
from a random-intercept multilevel model fitted with lme4::lmer().
hlm_icc(model, cluster_size = NULL)hlm_icc(model, cluster_size = NULL)
model |
A fitted |
cluster_size |
Optional scalar giving the average cluster size, used
to compute the design effect. If |
An object of class hlm_icc with components:
icc |
Estimated intraclass correlation. |
deff |
Design effect (if cluster_size supplied). |
re_var |
Random intercept variance. |
resid_var |
Residual variance. |
## Not run: library(lme4) m <- lmer(math_score ~ SES + (1 | school_id), data = d) hlm_icc(m, cluster_size = 25) ## End(Not run)## Not run: library(lme4) m <- lmer(math_score ~ SES + (1 | school_id), data = d) hlm_icc(m, cluster_size = 25) ## End(Not run)
Visualizes the intraclass correlation by plotting the between- and within-cluster variance components as a stacked bar. This is intended as a teaching diagram to help students see how the ICC reflects the share of variance that lies between clusters.
hlm_icc_plot(model, cluster_size = NULL)hlm_icc_plot(model, cluster_size = NULL)
model |
A fitted |
cluster_size |
Optional scalar giving the average cluster size,
passed to |
A ggplot object.
Produces a "fan plot" of predicted lines for each cluster to illustrate a cross-level interaction (random slope) in a multilevel model.
hlm_xint_geom(model, x_within, cluster, n_points = 20, n_clusters = 20)hlm_xint_geom(model, x_within, cluster, n_points = 20, n_clusters = 20)
model |
A fitted |
x_within |
Name of the Level-1 predictor with a random slope (string). |
cluster |
Name of the clustering factor (string). |
n_points |
Number of points to plot along the x-axis. Defaults to 20. |
n_clusters |
Maximum number of clusters to display (sampled) for clarity. |
A ggplot object showing predicted lines by cluster.
## Not run: m <- lme4::lmer(math_score ~ SES_c + SES_mean + (SES_c | school_id), data = d) hlm_xint_geom(m, x_within = "SES_c", cluster = "school_id") ## End(Not run)## Not run: m <- lme4::lmer(math_score ~ SES_c + SES_mean + (SES_c | school_id), data = d) hlm_xint_geom(m, x_within = "SES_c", cluster = "school_id") ## End(Not run)
Produces an error-bar plot of within, between, and contextual effects with 95% confidence intervals. Intended as a teaching diagram.
## S3 method for class 'hlm_context' plot(x, ...)## S3 method for class 'hlm_context' plot(x, ...)
x |
An object of class |
... |
Not used. |
A ggplot object.
Produces a simple bar chart of variance shares across components, suitable for teaching how variance is partitioned across levels.
## S3 method for class 'hlm_decompose' plot(x, ...)## S3 method for class 'hlm_decompose' plot(x, ...)
x |
An object of class |
... |
Not used. |
A ggplot object.