[R] spline basis

David Winsemius dwinsemius at comcast.net
Mon Jun 22 23:25:56 CEST 2015


On Jun 22, 2015, at 1:09 PM, Glenn Schultz wrote:

> I have the following code which creates a spline function
> 
> x <- c(1, 12, 24, 36, 60, 120, 200, 240, 300, 360)
> y <- c(.2, 8, 8, 8, 8, 8, 8, 8, 18, 50)
> 
> 
> Baseline <- cbind(x,y)
> Turnover <- splinefun(Baseline[,1], Baseline[,2], method = "natural")
> plot(Turnover(seq(1, 360, 1)), type = "l")
> 
> If I change the Y ordinates the spline changes accordingly.  However, the code is the same on inspection.  Thus, I figure the basis must be stored somewhere.  I have look through the documentation and cannot find its location.  Can anyone enlighten me on what is going on and where I can find the basis matrix? 

The Trunover-object is actually a closure with both a function call and an enclosing environment:

> Turnover
function (x, deriv = 0L) 
{
    deriv <- as.integer(deriv)
    if (deriv < 0L || deriv > 3L) 
        stop("'deriv' must be between 0 and 3")
    if (deriv > 0L) {
        z0 <- double(z$n)
        z[c("y", "b", "c")] <- switch(deriv, list(y = z$b, b = 2 * 
            z$c, c = 3 * z$d), list(y = 2 * z$c, b = 6 * z$d, 
            c = z0), list(y = 6 * z$d, b = z0, c = z0))
        z[["d"]] <- z0
    }
    res <- .splinefun(x, z)
    if (deriv > 0 && z$method == 2 && any(ind <- x <= z$x[1L])) 
        res[ind] <- ifelse(deriv == 1, z$y[1L], 0)
    res
}
<bytecode: 0x3b00a0ea8>
<environment: 0x37f324158>

> ls(envir=environment(Turnover))
[1] "z"
> environment(Turnover)$z
$method
[1] 2

$n
[1] 10

$x
 [1]   1  12  24  36  60 120 200 240 300 360

$y
 [1]  0.2  8.0  8.0  8.0  8.0  8.0  8.0  8.0 18.0 50.0

$b
 [1]  0.891067507  0.345137714 -0.094715653  0.033724898 -0.012918083
 [6]  0.006114335 -0.011309452  0.030871189  0.362608232  0.618695884

$c
 [1]  0.000000e+00 -4.962998e-02  1.297553e-02 -2.272155e-03
 [5]  3.286972e-04 -1.149022e-05 -2.063071e-04  1.260823e-03
 [9]  4.268128e-03  0.000000e+00

$d
 [1] -1.503939e-03  1.739042e-03 -4.235469e-04  3.612294e-05
 [5] -1.889930e-06 -8.117371e-07  1.222609e-05  1.670725e-05
 [9] -2.371182e-05  0.000000e+00

> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list