General regression splines with variable degrees and ness, smoothing splines.
Source:R/gsp.R
gsp.Rd
From https://github.com/gmonette/spida2 because namespace and dependencies are not properly listed. Source: https://github.com/gmonette/spida2/blob/master/R/gsp.R, https://github.com/gmonette/spida2/blob/master/R/gsp_util.R
Arguments
- x
value(s) where spline is evaluated.
- knots
vector of knots.
- degree
vector giving the degree of the spline in each interval. Note the number of intervals is equal to the number of knots + 1. A value of 0 corresponds to a constant in the interval. If the spline should evaluate to 0 in the interval, use the
intercept
argument to specify some value in the interval at which the spline must evaluate to 0.- smoothness
vector with the degree of smoothness at each knot (0 = continuity, 1 = smoothness with continuous first derivative, 2 = continuous second derivative, etc. The value -1 allows a discontinuity at the knot. A scalar is recycled so its length equals the number of knots. Alternatively, a list of length equal to the number of knots. Each element of the list is a vector of the orders of derivatives which are required to be smooth. THis allows non-sequential constraints, e.g., to have the same first and second derivative on either side of a knot but a possible discontinuity and change in higher-order derivatives, the vector would be c(1,2). Note that if a list is used, all elements must provide all desired constraints. That is the list argument corresponding to
smoothness = c(1,2,-1)
issmoothness=list(0:1, 0:2, -1)
.- lin
provides a matrix specifying additional linear contraints on the 'full' parametrization consisting of blocks of polynomials of degree equal to max(degree) in each of the length(knots)+1 intervals of the spline. See below for examples of a spline that is 0 outside of its boundary knots.
- periodic
if TRUE generates a period spline on the base interval (0,max(knots)). A constraint is generated so that the coefficients generate the same values to the right of max(knots) as they do to the right of 0. Note that all knots should be strictly positive.
- intercept
value(s) of x at which the spline has value 0, i.e., the value(s) of x for which yhat is estimated by the intercept term in the model. The default is 0. If NULL, the spline is not constrained to evaluate to 0 for any x.
- signif
number of significant digits used to label coefficients.
Author
Monette, G. georges@yorku.ca
Examples
simd <- data.frame(
age = rep(1:50, 2),
y = sin(2 * pi * (1:100) / 5) + rnorm(100),
G = rep(c("male", "female"), c(50, 50))
)
sp <- function(x) {
gsp(x, knots = c(10, 25, 40), degree = c(1, 2, 2, 1), smoothness = c(1, 1 ,1))
}
summary(lm(formula = y ~ sp(age) * G, data = simd))
#>
#> Call:
#> lm(formula = y ~ sp(age) * G, data = simd)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -2.6218 -0.8933 -0.0798 0.9523 3.7148
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 0.383406 0.628780 0.610 0.544
#> sp(age)D1(0) -0.030525 0.057323 -0.533 0.596
#> sp(age)C(10).2 0.004877 0.007181 0.679 0.499
#> sp(age)C(25).2 -0.008354 0.013385 -0.624 0.534
#> Gmale -0.858187 0.889229 -0.965 0.337
#> sp(age)D1(0):Gmale 0.054353 0.081067 0.670 0.504
#> sp(age)C(10).2:Gmale -0.006059 0.010155 -0.597 0.552
#> sp(age)C(25).2:Gmale 0.008921 0.018929 0.471 0.639
#>
#> Residual standard error: 1.272 on 92 degrees of freedom
#> Multiple R-squared: 0.03221, Adjusted R-squared: -0.04143
#> F-statistic: 0.4374 on 7 and 92 DF, p-value: 0.8763
#>