24
Stata Technical Bulletin
STB-57
Figure 2. Reference splines at 4 with unit reference points.
Example
In Stata’s auto data, we can use frencurv and regress (with the noconstant option) to fit a cubic spline for miles per
gallon with respect to weight:
. frencurv,xvar(weight) refpts(1760(770)4840) gen(cs) power(3)
. describe cs*
13. csl |
float |
7.8.4f |
Spline at 990 (INCOMPLETE) |
14. cs2 |
float |
7.8.4f |
Spline at 1,760 |
15. cs3 |
float |
7.8.4f |
Spline at 2,530 |
16. cs4 |
float |
7.8.4f |
Spline at 3,300 |
17. cs5 |
float |
7.8.4f |
Spline at 4,070 |
18. cs6 |
float |
7.8.4f |
Spline at 4,840 |
19. cs7 float 7.8.4f . regress mpg cs*,noconst robust |
Spline at 5,610 (INCOMPLETE) | ||
Regression with |
robust |
standard errors |
Number of obs = 74 |
— I ≡pg I |
Coef. |
Robust Std. Err. |
t |
p>∣t∣ |
[957. Conf. |
— Interval] |
csl I |
11.82559 |
15.56642 |
0.760 |
0.450 |
-19.24512 |
— 42.89629 |
cs2 I |
29.21133 |
1.761704 |
16.581 |
0.000 |
25.69495 |
32.72771 |
cs3 I |
22.65796 |
.7625134 |
29.715 |
0.000 |
21.13597 |
24.17994 |
cs4 I |
19.4749 |
.610094 |
31.921 |
0.000 |
18.25715 |
20.69266 |
cs5 I |
15.51593 |
.8409023 |
18.452 |
0.000 |
13.83748 |
17.19437 |
cs6 I |
10.60747 |
1.585487 |
6.690 |
0.000 |
7.442828 |
13.77212 |
cs7 I |
-28.19347 |
21.59599 |
-1.305 |
0.196 |
-71.29924 |
14.91229 |
We have arbitrarily chosen the reference points to be equally spaced from the minimum of weight (1,760 pounds) to
the maximum of weight (4,840 pounds). By default, frencurv ensures that the spline is complete in the range of X-values
spanned by the original reference points provided by the user. The describe command lists the reference splines with their
labels. Note that frencurv has added two extra reference points outside the spline’s completeness region (at weights of 990 and
5,610 pounds) and indicated this incompleteness in the variable labels. The coefficients fitted by regress (with the noconstant
option) are simply the fitted values of mpg at the reference points. Note that the ones corresponding to the splines cs2 to
cs6 have “sensible” values, corresponding to the expected levels of mpg at the appropriate value of weight, whereas the ones
corresponding to csl and cs7 have “nonsense” values because they correspond to reference “weights” extrapolated off the edge
of the range of sensible weight values. This is the price we pay for making all reference points equal to knots of the cubic
spline. Figure 3 shows observed and fitted values of mpg plotted against weight. The fitted curve is calculated using predict
(see [R] predict) and is interpolated cubically between the reference points.
Figure 3. Mileage plotted against weight (points) with fitted cubic spline (line).
The f rencurv parameterization allows us to use Iincom to calculate confidence intervals for differences (or other contrasts)