help rcsgen-------------------------------------------------------------------------------

Title

rcsgen-- Generate restriced cubic splines and derivatives

Syntax

rcsgenvarname[if] [in] [,options]

optionsDescription ------------------------------------------------------------------------- Optionsgen(stub)stubname for generated spline variablesdgen(stub)stubname for generated derivatives of spline variablesknots(numlist)location of knotspercentiles(numlist)location of knots using percentilesdf(#)degrees of freedom for knotsbknots(numlist)location of boundary knotsorthogorthogonalize generates spline variablesrmatrix(matname)use supplied matrix for orthogonalizationif2(string)use extra condition when generating knots usingdforpercentileoptionsfw(varname)name of variable containing weights when generating knots using thedforpercentileoptionsreversederives the spline variables in reversed orderscalar(#)a single value to calculate the spline basis forOne (and only one) of the

knots,percentilesordfoptions should be specified. If they are not then only 1 variable is created which is a copy ofvarname.

Description

rcsgengenerates basis functions for restricted cubic splines and (optionally) their derivatives. Restriced cubic spline functions assume linearity beyond the two boundary knots. It is possible to specify knots on the original scale, as default percentiles or user specified pecentiles. Orthogonalization can be peformed using Gram-Schmidt orthogonalization. When orthogonalizing, a matrix is returned, which can be useful for regenerating the orthogonalized spline variables for out of sample predictions.

Options

gen(stub)gives a stubname for the generated cubic splines variables. For example,gen(rcs)will create variablercs1, rcs2, ....

dgen(stub)gives a stubname for the derivatives of the restricted cubic splines variables. For example,dgen(drcs)will create variabledrcs1, drcs2, ....

knots(numlist)list of the location of the knots. The boundary knots are included in thenumlist.

percentiles(numlist)list of percentiles for the location of the knots. The boundary knots are included in thenumlist.

df(#)sets the desired degrees of freedom (df). The number of knots is one less than the df. Knots are placed at equally spaced centiles of the distribution ofvarname. For example, fordf(5)knots are placed at the 20th, 40th, 60th, 80th centiles of the distribution ofvarname. In addition boundary knots are placed at the maximum and minimum values ofvarnameor those specified using thebknots()option.

bknots(numlist)list of boundary knots when using thedf()option. By default these are the minimum and maximum of thevarname

orthogwill orthogonalize the generated spline variables using Gram-Schmidt orthogonalization.

rmatrix(matname)will orthogonalize the generated spline variables using the supplied R matrix. If X is the N*p matrix of untransformed spline variables and Q is the N*(p+1) matrix of orthogonlized variables plus a column of ones, then X=QR.

if2(condition)supplies a condition when generating the knots using thedforpercentileoptions. 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.

fw(weight)gives the name of the variable containing weights when generating knots usingdforpercentileoptions.

reversewill make the spline variables to be derived in reversed order, treating the last knot as the first and the first knot as the last. This can be used to add a constraint to a regression model for a constant effect after the last knot.

scalarwill calculate the spline variables for a single value and store the results in a series of Stata scalars. It is useful when obtaining in or out of sample predictions in large datasets and you want to predict at a certain value ofvarname.

You can specify where to position the knots.Example:

. rcsgen x, knots(10 30 50 70 90) gen(rcs)Alternatively, you can generate the knots positions according to the distribution of

varname. In the example below thedf(3)option is used which means that 4 knots are used at 0th 33rd 67th and 100th centiles ofweight.

. sysuse auto, clear. rcsgen weight, gen(rcs) df(3). regress mpg rcs1-rcs3. predictnl pred = xb(), ci(lci uci). twoway (rarea lci uci weight, sort) ///(scatter mpg weight, sort) ///(line pred weight, sort lcolor(black)), legend(off)(click to run)

AuthorsThis command is based on rcs written by Chris Nelson (cn46@le.ac.uk) that comes with the strsrcs command available from SSC.

Paul Lambert (paul.lambert@le.ac.uk) added the percentile, rmatrix, if2, fw and scalar options. He also wrote the mata code for the Gram-Schmidt orthogonalization (as opposed to using the orthog command).

Mark Rutherford (mjr40@le.ac.uk) added the df and bknots options.

Therese Andersson (therese.m-l.andersson@ki.se) added the reverse option. Also see