rk | R Documentation |

Generate a reproducing kernel basis matrix for a nominal, ordinal, or polynomial smoothing spline.

```
rk(x, df = NULL, knots = NULL, m = NULL, intercept = FALSE,
Boundary.knots = NULL, warn.outside = TRUE,
periodic = FALSE, xlev = levels(x))
```

`x` |
the predictor vector of length |

`df` |
the degrees of freedom, i.e., number of knots to place at quantiles of |

`knots` |
the breakpoints (knots) defining the spline. If |

`m` |
the derivative penalty order: 0 = ordinal spline, 1 = linear spline, 2 = cubic spline, 3 = quintic spline |

`intercept` |
should an intercept be included in the basis? |

`Boundary.knots` |
the boundary points for spline basis. Defaults to |

`warn.outside` |
if |

`periodic` |
should the spline basis functions be constrained to be periodic with respect to the |

`xlev` |
levels of |

Given a vector of function realizations `f`

, suppose that `f = X \beta`

, where `X`

is the (unregularized) spline basis and `\beta`

is the coefficient vector. Let `Q`

denote the postive semi-definite penalty matrix, such that `\beta^\top Q \beta`

defines the roughness penalty for the spline. See Helwig (2017) for the form of `X`

and `Q`

for the various types of splines.

Consider the spectral parameterization of the form `f = Z \alpha`

where

`Z = X Q^{-1/2}`

is the regularized spline basis (that is returned by this function), and `\alpha = Q^{1/2} \beta`

are the reparameterized coefficients. Note that `X \beta = Z \alpha`

and `\beta^\top Q \beta = \alpha^\top \alpha`

, so the spectral parameterization absorbs the penalty into the coefficients (see Helwig, 2021, 2024).

Syntax of this function is designed to mimic the syntax of the `bs`

function.

Returns a basis function matrix of dimension `n`

by `df`

(plus 1 if an `intercept`

is included) with the following attributes:

`df` |
degrees of freedom |

`knots` |
knots for spline basis |

`m` |
derivative penalty order |

`intercept` |
was an intercept included? |

`Boundary.knots` |
boundary points of |

`periodic` |
is the basis periodic? |

`xlev` |
factor levels (if applicable) |

The (default) type of spline basis depends on the `class`

of the input `x`

object:

* If `x`

is an unordered factor, then a nominal spline basis is used

* If `x`

is an ordered factor (and `m = NULL`

), then an ordinal spline basis is used

* If `x`

is an integer or numeric (and `m = NULL`

), then a cubic spline basis is used

Note that you can override the default behavior by specifying the `m`

argument.

Nathaniel E. Helwig <helwig@umn.edu>

Helwig, N. E. (2017). Regression with ordered predictors via ordinal smoothing splines. Frontiers in Applied Mathematics and Statistics, 3(15), 1-13. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.3389/fams.2017.00015")}

Helwig, N. E. (2021). Spectrally sparse nonparametric regression via elastic net regularized smoothers. Journal of Computational and Graphical Statistics, 30(1), 182-191. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1080/10618600.2020.1806855")}

Helwig, N. E. (2024). Precise tensor product smoothing via spectral splines. Stats, 7(1), 34-53. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.3390/stats7010003")}

```
######***###### NOMINAL SPLINE BASIS ######***######
x <- as.factor(LETTERS[1:5])
basis <- rk(x)
plot(1:5, basis[,1], t = "l", ylim = extendrange(basis))
for(j in 2:ncol(basis)){
lines(1:5, basis[,j], col = j)
}
######***###### ORDINAL SPLINE BASIS ######***######
x <- as.ordered(LETTERS[1:5])
basis <- rk(x)
plot(1:5, basis[,1], t = "l", ylim = extendrange(basis))
for(j in 2:ncol(basis)){
lines(1:5, basis[,j], col = j)
}
######***###### LINEAR SPLINE BASIS ######***######
x <- seq(0, 1, length.out = 101)
basis <- rk(x, m = 1)
plot(x, basis[,1], t = "l", ylim = extendrange(basis))
for(j in 2:ncol(basis)){
lines(x, basis[,j], col = j)
}
######***###### CUBIC SPLINE BASIS ######***######
x <- seq(0, 1, length.out = 101)
basis <- rk(x)
basis <- scale(basis) # for visualization only!
plot(x, basis[,1], t = "l", ylim = extendrange(basis))
for(j in 2:ncol(basis)){
lines(x, basis[,j], col = j)
}
######***###### QUINTIC SPLINE BASIS ######***######
x <- seq(0, 1, length.out = 101)
basis <- rk(x, m = 3)
basis <- scale(basis) # for visualization only!
plot(x, basis[,1], t = "l", ylim = extendrange(basis))
for(j in 2:ncol(basis)){
lines(x, basis[,j], col = j)
}
```

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.