gsl.bs generates the B-spline basis matrix for a polynomial spline and (optionally) the B-spline basis matrix derivative of a specified order with respect to each predictor
x: the predictor variable. Missing values are not allowed
degree: degree of the piecewise polynomial - default is 3 (cubic spline)
nbreak: number of breaks in each interval - default is 2
deriv: the order of the derivative to be computed-default if 0
x.min: the lower bound on which to construct the spline - defaults to min(x)
x.max: the upper bound on which to construct the spline - defaults to max(x)
intercept: if TRUE , an intercept is included in the basis; default is FALSE
knots: a vector (default knots="NULL") specifying knots for the spline basis (default enables uniform knots, otherwise those provided are used)
...: optional arguments
Details
Typical usages are (see below for a list of options and also the examples at the end of this help file)
B <- gsl.bs(x,degree=10)
B.predict <- predict(gsl.bs(x,degree=10),newx=xeval)
Returns
gsl.bs returns a gsl.bs object. A matrix of dimension c(length(x), degree+nbreak-1) . The generic function predict extracts (or generates) predictions from the returned object.
A primary use is in modelling formulas to directly specify a piecewise polynomial term in a model. See https://www.gnu.org/software/gsl/
for further details.
References
Li, Q. and J.S. Racine (2007), Nonparametric Econometrics: Theory and Practice, Princeton University Press.
Ma, S. and J.S. Racine and L. Yang (2015), Spline Regression in the Presence of Categorical Predictors, Journal of Applied Econometrics, Volume 30, 705-717.
Ma, S. and J.S. Racine (2013), Additive Regression Splines with Irrelevant Categorical and ContinuousRegressors,
## Plot the spline bases and their first order derivativesx <- seq(0,1,length=100)matplot(x,gsl.bs(x,degree=5),type="l")matplot(x,gsl.bs(x,degree=5,deriv=1),type="l")## Regression examplen <-1000x <- sort(runif(n))y <- cos(2*pi*x)+ rnorm(n,sd=.25)B <- gsl.bs(x,degree=5,intercept=FALSE)plot(x,y,cex=.5,col="grey")lines(x,fitted(lm(y~B)))