*! version 0.1 2022-03-16

Title

gensplines - generate various types of spline basis functions

Syntax

gensplines varname [if] [in],
[options]

Options
allknots(numlist) full list of knots

bkknots boundary knots

center center splines at mean

center(#) center at #

degree(#) degree of spline function

dgen(stub) generate derivatives of splines

df(#) df for splines

fw(varname) frequency weights

gen(stub) generate spline variables

intercept return all basis functions

knots(numlist) internal knots

type(splinetype) type of spline function

subcentile(exp) knot positions based on expression

winsor(numlist) winsorise before calculation of spline variables

Description

genplines calculates various types of spline basis functions. This includes
splines based on truncated powers, restricted cubic splines, B-splines, M-splines, I-splines and, natural splines.
Note that natural splines and restricted splines are two different ways to impose linearity constraints beyond the
boundary knots and will result in the same fitted values in a regression model.

For B-splines, M-splines, I-splines and, natural splines the methods follow those of Wang 2021,
and implemented in R in the splines2 package.

It is also possible to generate integrated B-splines and integrated natural splines.

If using gensplines varname then new variables are created.

If using gensplines # then new scalars are created.

Options

allknots(numlist, [options]) a list of the location of the knots. The boundary knots are included in the numlist.
If you use the percentiles option then knots are calculated at the list of percentiles.

bknots(numlist) a numlist of length 2 giving the boundary knots.
The default boundary knots are at the minimum and maxiumum of varname.

center(#) will center the spline variables around a single value #. The created spline variables will be zero at this value.

center will center the spline variables around the mean of x.

degree(#) the degree for truncated powers, B-splines, M-splines and I-Splines.

dgen(stub) gives a stubname for the derivatives of the spline variables.
For example, dgen(dbs) will create variables dbs1, dbs2, ....

df(#) sets the desired degrees of freedom (df), i.e. the number of spline variables that are created.
For B-splines, M-Splines and I-splines the number of internal knots is df - degree + 1.
For natural and restricted cubic splines the number of internal knots is df - 1.
Knots are placed at equally spaced centiles of the distribution of varname.
For example, df(5) places internal knots at the 20th, 40th, 60th, 80th centiles
of the distribution of varname.

fw(varname) gives the name of the variable containing frequency weights when
generating knots using df() option.
This option may be useful for aggregated (collapsed) data.

gen(stub) gives a stubname for the splines variables.
For example, gen(bs) will create variable bs1, bs2, ....

intercept generates all splines basis variables.
By default the first basis variable is not generated.

knots(numlist) a list of the location of the internal knots.

type(splinetype) The type of spline function.
The different types of splines are listed below.

bs B-splines

ns natural cubic splines

rcs restricted cubic splines

ms M-splines

is I-splines

tp truncated powers

ibs integrated B-splines

ins integrated natural cubic splines

subcentile(exp) gives an expression to calculate the knots based on a subset
of the data. For example in survival (time-to-event) data when using splines
for the time scale it is common to calculate the knot locations based on the
distribution of uncensored event times.

winsor(#1 #2) gives 2 centiles to winsorise varname.
When calculating the spline variables values < than the value defined by the {#1 th} centile will
be replaced by that value and values > than the value defined by the {#2 th} centile will
be replaced by that value. If the suboption values is specified then the actual
values, rather than the centiles are used.

Examples

Natural (cubic) splines

. webuse mksp1

. gensplines age, df(3) type(ns) gen(age_ns)

. regress lninc age_ns*

Quadratic bsplines - user defined knots

. webuse mksp1

. gensplines age, knots(25 45 60) degree(2) type(bs) gen(age_bs)

. regress lninc age_bs*

Restricted cubic splines - knots based on user defined centiles

. webuse mksp1

. gensplines age, allknots(5 33 67 95, percentile) type(rcs) gen(age_rcs)

. regress lninc age_rcs*

Natural (cubic) splines with winsorizing at 2nd and 98th percentiles

. webuse mksp1

. gensplines age, df(3) type(ns) gen(age_ns) winsor(2 98)

. regress lninc age_ns*

Author

Paul C. Lambert

Biostatistics Research Group

Department of Population Health Sciences

University of Leicester

Leicester, UK

and

Department of Medical Epidemiology and Biostatistics

Karolinska Institutet

Stockholm, Sweden

paul.lambert@le.ac.uk

References

Wang W. Yan J. Shape-Restricted Regression Splines with R Package splines2."
Journal of Data Science 2021;19;498–517.