means that Stata does not recognize pspline() as a valid function in the context of gsem. This is a common confusion, because Stata’s base gsem command does not support a pspline() function for penalized splines. The syntax you tried is inspired by R or other software, but it’s not available in Stata’s standard commands.
What can you do in Stata?
1. Restricted Cubic Splines (rcspline)
Stata supports restricted cubic splines (also called natural splines) via the mkspline command, but not penalized splines directly. For example:
If you want penalized splines or GAMs in Stata, you need a user-written package like staggam (from Stata Journal) or use R (e.g., mgcv::gam). Stata’s support for penalized splines is limited.
4. Why the error?
pspline() is not a built-in function in Stata’s gsem.
Stata’s gsem does not have a direct way to specify penalized splines as in R’s mgcv or Python’s statsmodels.
Summary Table
Spline Type
Stata Command
Penalized?
Notes
Cubic/Restricted
mkspline, rcspline
No
Must specify knots manually
B-splines
bspline (user)
No
Must specify knots manually
Penalized splines
Not natively supported
Yes
Use R or Python for full support
GAM
staggam (user)
Yes
Limited, user-written, not official
What should you do?
For most Stata users: Use mkspline or rcspline for restricted cubic splines.
If you need penalized splines: Consider running your analysis in R (mgcv::gam) or Python.
If you want to stay in Stata: Use quantile-based knots with mkspline as described in the previous answer.
Example: Restricted Cubic Spline in Stata
stata
* Install rcspline if not already installed
ssc install rcspline
* Fit a logistic regression with 5 knots
rcspline chronic_knee_pain age_recruit, nknots(5) logit
Or, using mkspline:
stata
* Find quantile-based knots
xtile knot1 = age_recruit, nq(6)
sum age_recruit if knot1==1
sum age_recruit if knot1==2
sum age_recruit if knot1==3
sum age_recruit if knot1==4
sum age_recruit if knot1==5
* Suppose the knots are 30, 40, 50, 60, 70
mkspline age_spline = age_recruit, cubic knots(30 40 50 60 70)
logit chronic_knee_pain age_spline*
If you need more help with a specific Stata spline workflow, let me know!