*! version 3.0 Fernando Rios-Avila September 2022
help f_spline

Title

f_spline - Module for the construction of polynomial splines for f_able

Syntax

Polynomial spline of degree #d with knots at specified points

f_spline
prefix
=
oldvar
[if] [in]
[, knots(numlist) degree(#d) ]

Polynomial spline of degree #d with equally spaced #k knots

f_spline
prefix
=
oldvar
[if] [in]
[, nknots(#k) degree(#d) ]

Polynomial spline of degree #d with knots set at predefined percentiles

f_spline
prefix
=
oldvar
[if] [in]
[, weight(varname) kpctile(numlist) degree(#d)]

Polynomial spline of degree #d with #k knots set at equally distant #k percentiles.

f_spline
prefix
=
oldvar
[if] [in]
[, weight(varname) npctile(#k) degree(#d)]

Description

f_spline is a companion program for f_able that
creates variables containing a polynomial spline of any degree for an existing variable.

It will use prefix to create the necessary number of new variables to create the splines.
Each new variable will be named consecutively starting from 2. For example, if one creates a linear spline
with 2 knots, the program will generate 2 new variables named prefix2 and prefix3.

For a given set of knots kn_1, kn_2,..., kn_k, and degre #d, the new variables are created using the following formula:

v_j=oldvar^j for j=2...d if d>=2

v_j=max(oldvar-kn_h,0)^d for j=d+1...d+k & h=j-d

where the definition for knots kn_1,...,kn_k depend on which syntax was used.

Options

Options

degree(numlist) Indicates the degree of the spline polynomials.
The default is 1 (linear spline).

knots(numlist) When used, the list of numbers provided
are used to define the relevant knots. This knots have to be larger
than the minimum value of oldvar but smaller than the maximum value.

nknots(#nk) When used, #k equally spaced values between the minimum
and maximum value of oldvar are used as knots.

kpctile(numlist) When used, the list of numbers corresponds to the
percentiles that would be used define the knots based on the of the empirical
distribution of oldvar. The list of numbers has to be >0 and <100.

npctile(#nk) When used, #k equally spaced percentiles are
used to define the knots based on the empirical distribution of oldvar.

weight(varname) Used to indicate a weight variable to be used internally
to determine the empirical distribution of oldvar. It can only be used with
the 3rd and 4th syntax of f_spline.

replace Request replacing variables if they already exist in memory.

Examples

Fit a regression of log income on education and age by using a piecewise
linear function for age, with predefined knots

webuse mksp1, clear

f_spline sage = age, knots(20 30 40 50 60)

regress lninc educ age sage2-sage6

f_able sage2-sage6, auto

margins, dydx(age)

Fit a regression of log income on education and age by using a piecewise
linear function for age, using 5 equally spaced percentiles

webuse mksp1, clear

f_spline sage = age, npctile(5)

regress lninc educ age sage2-sage6

f_able sage2-sage6, auto

margins, dydx(age)

Perform a logistic regression of outcome against a quadratic spline with
3 knots defined at specific percentiles

webuse mksp2, clear

f_spline dose = dosage, degree(2) kpctile(25 50 75)

logistic outcome dosage dose2-dose5

f_able dose2-dose5, auto

margins, dydx(dosage)

Also see

Help: f_able, f_rcspline, mkspline