File Exchange

image thumbnail

Restricted Cubic Spline

version 1.0.0.0 (5.93 KB) by Leonidas Bantis
Fits the so called restricted cubic spline via least squares and obtains 95% bootstrap based CIs.

3 Downloads

Updated 11 Apr 2013

View License

%Fits the so called restricted cubic spline via least squares (see Harrell
%(2001)). The obtained spline is linear beyond the first and the last
%knot. The truncated power basis representation is used. That is, the
%fitted spline is of the form:
%f(x)=b0+b1*x+b2*(x-t1)^3*(x>t1)+b3*(x-t2)^3*(x>t2)+...
%where t1 t2,... are the desired knots.
%95% confidence intervals are provided based on the bootstrap procedure.

%For more information see also:
%Frank E Harrell Jr, Regression Modelling Strategies (With application to
%linear models, logistic regression and survival analysis), 2001,
%Springer Series in Statistics, pages 20-21.
%
%INPUT ARGUMENTS:
%x: A vector containing the covariate values x.
%y: A vector of length(x) that contains the response values y.
%knots: A vector of points at which the knots are to be placed.
% Alternatively, it can be set as 'prc3', 'prc4', ..., 'prc8' and 3
% or 4 or...8 knots placed at equally spaced percentiles will be used.
% It can also be set to 'eq3', 'eq4', ...,'eq8' to use 3 or 4 or ...
% or 8 equally spaced knots. There is a difference in using one of
% these strings to define the knots instead of passing them directly
% as a vector of numbers and the difference involves only the
% bootstrap option and not the fit itself. When the bootstrap is used
% and the knots are passed in as numbers, then the knot sequence will
% be considered fixed as provided by the user for each bootstrap
% iteration. If a string as the ones mentioned above is used, then
% the knot sequence is re-evaluated for each bootstrap sample
% based on this choice.
%
%OPTIONAL INPUT ARGUMENTS: (These can be not reached at all or set as [] to
%proceed to the next optional input argument):
%
%bootsams: The number of bootstrap samples if the user wants to derive 95% CIs.
%atwhich: a vector of x values at which the CIs of the f(x) are to be evaluated.
%plots: If set to 1, it returns a plot of the spline and the data.
% Otherwise it is ignored. This input argument can also not be reached
% at all. (It also plots the CIs provided that they are requested).
%
%OUTPUT ARGUMENTS:
%bhat: the estimated spline coefficients.
%f: a function handle from which you can evaluate the spline value at a
% given x (which can be a scalar or a vector). For example ff(2) will
% yield the spline value for x=2. You can use a vector (grid) of x values to
% plot the f(x) by requesting plot(x,f(x)).
%sse: equals to sum((y-ff(x)).^2)
%knots: the knots used for fitting the spline.
%CI : 95% bootstrap based confidence intervals.
% Obtained only if the bootstrap is requested and and only fot the
% points at which the CIs were requested. Hence, CI is a three column
% matrix with its first column be the spline value at the points
% supplied by the user, and the second and third column are
% respectively the lower and upper CI limits for that points.
%
%References: Frank E. Harrell, Jr. Regression Modeling Strategies (With
%applications to linear models, logistic regression, and survival
%analysis). Springer 2001.
%
%
%Code author: Leonidas E. Bantis,
%Dept. of Statistics & Actuarial-Financial Mathematics, School of Sciences
%University of the Aegean, Samos Island.
%
%E-mail: leobantis@gmail.com
%Date: January 14th, 2013.
%Version: 1.

Cite As

Leonidas Bantis (2021). Restricted Cubic Spline (https://www.mathworks.com/matlabcentral/fileexchange/41241-restricted-cubic-spline), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

This is amazing, surprised MatLab doesn't offer this. Thank you!!

Julio Chirinos

this is wonderful!. Quick question, how do I obtain the R2 of the model?

Aurelien

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!