*! version 1.0.4 Subir Hait 01mar2026

help for multispline

Title

multispline -- Nonlinear multilevel spline modeling

Syntax

multispline
depvar

[if] [in],
cluster(varname)
[options]

Required
cluster(varname) grouping variable for multilevel model

Optional
nknots(#) number of spline knots; default is nknots(4)

autoknots automatically select optimal number of knots

at(numlist) predict over range defined by numlist

plot display plot of nonlinear fit

Description

multispline fits nonlinear multilevel regression models using
natural cubic spline basis expansion. It provides a unified workflow
for fitting, predicting, visualizing, and summarizing nonlinear effects
in hierarchical or longitudinal data.

The command is particularly suited to large-scale education and health
datasets such as ECLS-K, HSLS, and PISA where outcomes are expected
to have nonlinear relationships with predictors such as socioeconomic
status (SES) or treatment dosage.

While Stata provides mixed for multilevel models and
mkspline for spline construction, no existing Stata command
provides a unified workflow for fitting, predicting, visualizing,
and computing ICCs from nonlinear multilevel models.
multispline fills this gap.

Options

cluster(varname) specifies the grouping variable for the
multilevel model. Required.

nknots(#) number of knots for cubic spline basis.
Default is 4. Must be >= 3.

autoknots automatically selects optimal number of knots
between 4 and 7 based on number of unique values of predictor.

at(numlist) generates predictions over a 50-point grid
spanning the range defined by the numlist values.
Useful for smooth prediction curves.

plot displays plot of predicted nonlinear relationship.

Examples

Example 1: Education example

. multispline math_score ses, cluster(schid) nknots(4) plot

Example 2: Automatic knot selection

. multispline math_score ses, cluster(schid) autoknots plot

Example 3: Grid predictions

. multispline math_score ses, cluster(schid) nknots(4) at(-3 -2 -1 0 1 2 3) plot

Example 4: Health science example

. multispline bloodpressure dosage, cluster(hospital) nknots(4) plot

Example 5: Real Stata data

. sysuse nlsw88, clear

. multispline wage age, cluster(industry) nknots(4) plot

Stored results

multispline stores the following in r():

Scalars

r(nknots) number of knots used

r(nsplines) number of spline terms

r(interior) number of interior knots

Macros

r(y) outcome variable name

r(x) predictor variable name

r(cluster) cluster variable name

r(knots) interior knot locations

r(cmd) command name

Author

Subir Hait

Michigan State University

haitsubi@msu.edu

https://github.com/causalfragility-lab/MultiSpline-Stata

References

Bates, D., Maechler, M., Bolker, B., and Walker, S. (2015).
Fitting linear mixed-effects models using lme4.
Journal of Statistical Software, 67(1), 1-48.

Hastie, T. and Tibshirani, R. (1990).
Generalized Additive Models.
CRC Press.

Raudenbush, S. W. and Bryk, A. S. (2002).
Hierarchical Linear Models.
Sage Publications.

Also see

mixed, mkspline, estat icc