You're getting the error:
ambiguous abbreviation
r(111);
because the gsem command in Stata does not support pspline() as a function in the model specification. That syntax actually doesn't exist in gsem or general Stata syntax — it's a bit of an R-style shortcut that doesn’t work in Stata.
🔍 Why this error happens
Stata interprets pspline() as a user-undefined function. When you write:
gsem (chronic_knee_pain ~ pspline(age_recruit, df(10))), family(binomial) link(logit)
Stata tries to figure out what pspline() means, but fails — and instead throws the ambiguous abbreviation error (or might even say “function not found” in other contexts).
✅ Correct ways to use splines in Stata
Stata supports splines via the mkspline and mkspline, cubic commands. But these don't offer automatic penalized splines like R's pspline().
So what are your options?
✅ Option 1: Use mkspline to Fit Cubic Splines
You manually define the spline basis with knots, then pass those variables into your model.
Example:
* Choose knots at specific ages (say 40, 50, 60)
mkspline age_spline = age_recruit, cubic knots(40 50 60)
* Fit logistic model
logit chronic_knee_pain age_spline1 age_spline2 age_spline3 age_spline4
That works well, but it's not penalized. You're giving the model full freedom in each segment.
🔄 Option 2: Use Restricted Cubic Splines (RCS) via fracpoly or user-written packages
A. Use Fractional Polynomials (alternative to splines)
fp generate age_fp = age_recruit, family(logistic)
fpfit chronic_knee_pain age_fp
This is a good alternative to splines and allows a flexible functional form for age.
B. Use crcspline from marginscont (if installed)
If you have access, the crcspline (restricted cubic splines) add-on allows for smoother control over knots and provides marginal effects.
Install:
ssc install crcspline
Use:
crcspline age_recruit, gen(rcs_age) knots(40 50 60)
logit chronic_knee_pain rcs_age1 rcs_age2 rcs_age3
🚫 What Stata does not have (yet)
Stata does not currently support penalized splines (P-splines) natively, like in R’s pspline() or mgcv::gam().
If P-splines are important to you, two alternatives:
🧠 Option 3 (Advanced): Use R for P-splines
Use the mgcv package in R for smoothing terms:
library(mgcv)
gam(chronic_knee_pain ~ s(age_recruit, bs = "ps"), family = binomial, data = your_data)
You can export from Stata and run this part in R.
outsheet age_recruit chronic_knee_pain using data.csv, comma
✅ Summary
| Goal | Option | Works in Stata? |
|---|
| Cubic Splines with knots | mkspline, cubic | ✅ Yes |
| Restricted Cubic Splines | Use crcspline, or manually | ✅ Yes |
| Fractional Polynomials | fp generate, fpfit | ✅ Yes |
| Penalized Splines (P-splines) | pspline() – R only | ❌ No (R only) |
📌 Fix your command
To correct your Stata code, switch to using mkspline:
mkspline age_spline = age_recruit, cubic knots(40 50 60)
logit chronic_knee_pain age_spline1 age_spline2 age_spline3 age_spline4
Let me know if you want to install a package that behaves more like P-splines or if you're open to using R for this analysis.