Vector Cubic Smoothing Spline
vsmooth.spline.RdFits a vector cubic smoothing spline.
Arguments
- x
A vector, matrix or a list. If a list, the
xcomponent is used. If a matrix, the first column is used.xmay also be a complex vector, in which case the real part is used, and the imaginary part is used for the response. In this help file,nis the number of unique values ofx.- y
A vector, matrix or a list. If a list, the
ycomponent is used. If a matrix, all but the first column is used. In this help file,Mis the number of columns ofyif there are no constraints on the functions.- w
The weight matrices or the number of observations. If the weight matrices, then this must be a
n-row matrix with the elements in matrix-band form (seeiam). If a vector, then these are the number of observations. By default,wis theMbyMidentity matrix, denoted bymatrix(1, n, M).- df
Numerical vector containing the degrees of freedom for each component function (smooth). If necessary, the vector is recycled to have length equal to the number of component functions to be estimated (
Mif there are no constraints), which equals the number of columns of thex-constraint matrix. A value of 2 means a linear fit, and each element ofdfshould lie between 2 andn. The larger the values ofdfthe more wiggly the smooths.- spar
Numerical vector containing the non-negative smoothing parameters for each component function (smooth). If necessary, the vector is recycled to have length equal to the number of component functions to be estimated (
Mif there are no constraints), which equals the number of columns of thex-constraint matrix. A value of zero means the smooth goes through the data and hence is wiggly. A value ofInfmay be assigned, meaning the smooth will be linear. By default, theNULLvalue ofsparmeansdfis used to determine the smoothing parameters.- all.knots
Logical. If
TRUEthen each distinct value ofxwill be a knot. By default, only a subset of the unique values ofxare used; typically, the number of knots isO(n^0.25)fornlarge, but ifn <= 40then all the unique values ofxare used.- i.constraint
A
M-row constraint matrix for the intercepts. It must be of full column rank. By default, the constraint matrix for the intercepts is theMbyMidentity matrix, meaning no constraints.- x.constraint
A
M-row constraint matrix forx. It must be of full column rank. By default, the constraint matrix for the intercepts is theMbyMidentity matrix, meaning no constraints.- constraints
An alternative to specifying
i.constraintandx.constraint, this is a list with two components corresponding to the intercept andxrespectively. They must both be aM-row constraint matrix with full column rank.- var.arg
Logical: return the pointwise variances of the fit? Currently, this corresponds only to the nonlinear part of the fit, and may be wrong.
- scale.w
Logical. By default, the weights
ware scaled so that the diagonal elements have mean 1.- nk
Number of knots. If used, this argument overrides
all.knots, and must lie between 6 andn+2 inclusive.- control.spar
See
smooth.spline.
Details
The algorithm implemented is detailed in Yee (2000).
It involves decomposing the component functions
into a linear and
nonlinear part, and using B-splines.
The cost of the computation is O(n M^3).
The argument spar contains scaled
smoothing parameters.
References
Yee, T. W. (2000). Vector Splines and Other Vector Smoothers. Pages 529–534. In: Bethlehem, J. G. and van der Heijde, P. G. M. Proceedings in Computational Statistics COMPSTAT 2000. Heidelberg: Physica-Verlag.
Note
This function is quite similar
to smooth.spline
but offers less functionality.
For example, cross validation is not implemented here.
For M = 1, the results will be generally different,
mainly due to the different way the knots are selected.
The vector cubic smoothing spline which s()
represents is
computationally demanding for large \(M\).
The cost is approximately \(O(n M^3)\) where
\(n\) is the number of unique abscissae.
Yet to be done: return the unscaled smoothing parameters.
WARNING
See vgam for information about an important bug.
See also
vsmooth.spline-class,
plot.vsmooth.spline,
predict.vsmooth.spline,
iam,
sm.os,
s,
smooth.spline.
Examples
nn <- 20; x <- 2 + 5*(nn:1)/nn
x[2:4] <- x[5:7] # Allow duplication
y1 <- sin(x) + rnorm(nn, sd = 0.13)
y2 <- cos(x) + rnorm(nn, sd = 0.13)
y3 <- 1 + sin(x) + rnorm(nn, sd = 0.13) # For constraints
y <- cbind(y1, y2, y3)
ww <- cbind(rep(3, nn), 4, (1:nn)/nn)
(fit <- vsmooth.spline(x, y, w = ww, df = 5))
#> Call:
#> vsmooth.spline(x = x, y = y, w = ww, df = 5)
#>
#> Smoothing Parameter (Spar): 0.5534466, 0.5534466, 0.5359578
#>
#> Equivalent Degrees of Freedom (Df): 4.999474, 4.999474, 4.999402
if (FALSE) { # \dontrun{
plot(fit) # The 1st & 3rd functions dont differ by a constant
} # }
mat <- matrix(c(1,0,1, 0,1,0), 3, 2)
(fit2 <- vsmooth.spline(x, y, w = ww, df = 5, i.constr = mat,
x.constr = mat))
#> Call:
#> vsmooth.spline(x = x, y = y, w = ww, df = 5, i.constraint = mat,
#> x.constraint = mat)
#>
#> Smoothing Parameter (Spar): 0.5535428, 0.5534466
#>
#> Equivalent Degrees of Freedom (Df): 4.999470, 4.999474
#>
#> Constraint matrices:
#> $`(Intercepts)`
#> [,1] [,2]
#> [1,] 1 0
#> [2,] 0 1
#> [3,] 1 0
#>
#> $x
#> [,1] [,2]
#> [1,] 1 0
#> [2,] 0 1
#> [3,] 1 0
#>
# The 1st and 3rd functions do differ by a constant:
mycols <- c("orange", "blue", "orange")
if (FALSE) plot(fit2, lcol = mycols, pcol = mycols, las = 1) # \dontrun{}
p <- predict(fit, x = model.matrix(fit, type = "lm"), deriv = 0)
max(abs(depvar(fit) - with(p, y))) # Should be 0
#> [1] 6.033285e-11
par(mfrow = c(3, 1))
ux <- seq(1, 8, len = 100)
for (dd in 1:3) {
pp <- predict(fit, x = ux, deriv = dd)
if (FALSE) { # \dontrun{
with(pp, matplot(x, y, type = "l", main = paste("deriv =", dd),
lwd = 2, ylab = "", cex.axis = 1.5,
cex.lab = 1.5, cex.main = 1.5)) } # }
}