Description
Spline-Based Nonlinear Modeling for Multilevel and Longitudinal Data.
Description
Provides tools for fitting, predicting, and visualizing nonlinear relationships in single-level, multilevel, and longitudinal regression models. Nonlinear functional forms are represented using natural cubic splines from 'splines' and smooth terms from 'mgcv'. The package offers a unified interface for specifying nonlinear effects, interactions with time variables, random-intercept clustering structures, and additional linear covariates. Utilities are included to generate prediction grids and produce effect plots, facilitating interpretation and visualization of nonlinear relationships in applied regression workflows. The implementation builds on established methods for spline-based regression and mixed-effects modeling (Hastie and Tibshirani, 1990 <doi:10.1201/9780203738535>; Bates et al., 2015 <doi:10.18637/jss.v067.i01>; Wood, 2017 <doi:10.1201/9781315370279>). Applications include hierarchical and longitudinal data structures common in education, health, and social science research.
README.md
MultiSpline
An R package for fitting, predicting, and visualizing nonlinear relationships in single-level, multilevel, and longitudinal regression models using spline-based methods.
Overview
In social and health science research, nonlinear effects are very common in clustered or longitudinal data. MultiSpline provides a simple and unified interface for estimating these effects without requiring manual construction of spline terms or interaction structures.
Installation
# Install from CRAN
install.packages("MultiSpline")
# Or install the development version from GitHub
devtools::install_github("causalfragility-lab/MultiSpline")
Core Functions
| Function | Description |
|---|---|
nl_fit() | Fit a nonlinear single-level or multilevel model |
nl_summary() | Tidy coefficient table |
nl_predict() | Generate predictions with uncertainty |
nl_plot() | Visualize nonlinear effects |
nl_icc() | Compute intraclass correlations |
Example
library(MultiSpline)
# Simulate data
set.seed(42)
d <- data.frame(
schid = rep(1:10, each = 60),
id = rep(1:200, each = 3),
TimePoint = factor(rep(1:3, times = 200)),
SES = rnorm(600),
math_score = rnorm(600, mean = 50, sd = 10)
)
# Fit nonlinear multilevel model
fit <- nl_fit(
data = d,
y = "math_score",
x = "SES",
time = "TimePoint",
cluster = c("id", "schid"),
method = "ns",
df = 4
)
# Coefficient table
nl_summary(fit)
# Intraclass correlations
nl_icc(fit)
# Predictions and plot
pred <- nl_predict(fit)
nl_plot(pred, x = "SES", time = "TimePoint")
License
MIT © Subir Hait
Author
Subir Hait, Michigan State University
Citation
If you use MultiSpline in your research, please cite:
citation("MultiSpline")
Or:
Hait, S. (2026). MultiSpline: Spline-Based Nonlinear Modeling
for Multilevel and Longitudinal Data. R package version 0.1.0.
https://cran.r-project.org/package=MultiSpline