help mkspline dialog: mkspline
-------------------------------------------------------------------------------
Title
[R] mkspline -- Linear and restricted cubic spline construction
Syntax
Linear spline with knots at specified points
mkspline newvar_1 #1 [newvar_2 #2 [...]] newvar_k = oldvar [if] [in]
[, marginal displayknots]
Linear spline with knots equally spaced or at percentiles of data
mkspline stubname # = oldvar [if] [in] [weight] [, marginal pctile
displayknots]
Restricted cubic spline
mkspline stubname = oldvar [if] [in] [weight] , cubic [nknots(#)
knots(numlist) displayknots]
fweights are allowed with the second and third syntax; see weight.
Menu
Data > Create or change data > Other variable-creation commands > Linear
and cubic spline construction
Description
mkspline creates variables containing a linear spline or a restricted
cubic spline of oldvar.
In the first syntax, mkspline creates newvar_1, ..., newvar_k containing
a linear spline of oldvar with knots at the specified #1, ..., #k-1.
In the second syntax, mkspline creates # variables named stubname1, ...,
stubname# containing a linear spline of oldvar. The knots are equally
spaced over the range of oldvar or are placed at the percentiles of
oldvar.
In the third syntax, mkspline creates variables containing a restricted
cubic spline of oldvar. This is also known as a natural spline. The
location and spacing of the knots is determined by the specification of
the nknots() and knots() options.
Options
+---------+
----+ Options +----------------------------------------------------------
marginal is allowed with the first or second syntax. It specifies that
the new variables be constructed so that, when used in estimation,
the coefficients represent the change in the slope from the preceding
interval. The default is to construct the variables so that, when
used in estimation, the coefficients measure the slopes for the
interval.
displayknots displays the values of the knots that were used in creating
the linear or restricted cubic spline.
pctile is allowed only with the second syntax. It specifies that the
knots be placed at percentiles of the data rather than being equally
spaced over the range.
nknots(#) is allowed only with the third syntax. It specifies the number
of knots that are to be used for a restricted cubic spline. This
number must be between 3 and 7 unless the knot locations are
specified using knots(). The default number of knots is 5.
knots(numlist) is allowed only with the third syntax. It specifies the
exact location of the knots to be used for a restricted cubic spline.
The values of these knots must be given in increasing order. When
this option is omitted, the default knot values are based on
Harrell's recommended percentiles with the additional restriction
that the smallest knot may not be less than the fifth-smallest value
of oldvar and the largest knot may not be greater than the
fifth-largest value of oldvar. If both nknots() and knots() are
given, they must specify the same number of knots.
Examples
Fit a regression of log income on education and age by using a piecewise
linear function for age
. webuse mksp1
. mkspline age1 20 age2 30 age4 50 age5 60 age6 = age
. regress lninc educ age1-age6
Fit the model so that the coefficients on the spline variables represent
the change in slope from the preceding group
. webuse mksp1, clear
. mkspline age1 20 age2 30 age4 50 age5 60 age6 = age, marginal
. regress lninc educ age1-age6
Create variables containing a linear spline of dosage with knots chosen
so that data are divided into five groups of equal size
. webuse mksp2, clear
. mkspline dose 5 = dosage, pctile
. logistic outcome dose1-dose5
Perform a logistic regression of outcome against a restricted cubic
spline function of dosage with four knots chosen according to Harrell's
recommended percentiles
. webuse mksp2, clear
. mkspline dose = dosage, cubic nknots(4)
. logistic outcome dose*
Saved results
mkspline saves the following in r():
Scalars
r(N_knots) number of knots
Matrices
r(knots) location of knots
Also see
Manual: [R] mkspline
Help: [R] fracpoly