ns
Generate a Basis Matrix for Natural Cubic Splines
Description
Generates the B-spline basis matrix for a natural cubic spline.
Usage
ns(x, df = NULL, knots = NULL, intercept = FALSE, Boundary.knots = range(x))
Arguments
x |
a numeric vector specifying the values to evaluate the basis matrix.
NAs are accepted.
|
df |
an integer specifying the degrees of freedom. If df is specified
and knots is not specified, then the function chooses (df - 1 - intercept)
knots from quantiles of x. (NAs in x are ignored.)
|
knots |
a numeric vector specifying the knots position to define a spline. The default
is numeric(0) if df is not specified; otherwise, it is the quantiles of x.
See also df.
|
intercept |
a logical value. If TRUE, an intercept is contained in the basis matrix.
The default is FALSE.
|
Boundary.knots |
a length-2 numeric vector specifying the boundary of knots. The default is the
range of x. If it is specified, the x values could be outside of it.
|
Details
The ns function uses the function splineDesign to generate
a basis matrix for a natural cubic spline with the specified knots,
evaluated at the values of x.
Value
- returns a matrix with length(x) rows and df columns if df is
specified.
- returns length(knots) + 1 + intercept columns if knots is specified.
The matrix has the class name "ns","basis", "matrix" and the
attributes "knots", "degree" (always 3), "Boundary.knots" and "intercept"
containing the arguments for the ns function.
The row names of the matrix are copied from the names of x,
and the column names are the string representation of 1:ncol(matrix).
Differences between Spotfire Enterprise Runtime for R and Open-source R
When the number of non-missing values in x is less than 2, Spotfire Enterprise Runtime for R and Open-source R
currently give different results, neither of which is very useful.
See Also
Examples
ns(1:6, df=3)
lm(ozone ~ ns(wind, df=4), data=Sdatasets::air)