R version 3.2.2 RC (2015-08-08 r68914) -- "Fire Safety" Copyright (C) 2015 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > pkgname <- "splines" > source(file.path(R.home("share"), "R", "examples-header.R")) > options(warn = 1) > library('splines') > > base::assign(".oldSearch", base::search(), pos = 'CheckExEnv') > cleanEx() > nameEx("asVector") > ### * asVector > > flush(stderr()); flush(stdout()) > > ### Name: asVector > ### Title: Coerce an Object to a Vector > ### Aliases: asVector > ### Keywords: models > > ### ** Examples > > require(stats) > ispl <- interpSpline( weight ~ height, women ) > pred <- predict(ispl) > class(pred) [1] "xyVector" > utils::str(pred) List of 2 $ x: num [1:51] 58 58.3 58.6 58.8 59.1 ... $ y: num [1:51] 115 115 116 117 117 ... - attr(*, "class")= chr "xyVector" > asVector(pred) [1] 115.0000 115.4908 116.0170 116.6137 117.3153 118.1214 118.9894 119.8747 [9] 120.7384 121.5787 122.4078 123.2380 124.0759 124.9181 125.7601 126.5980 [17] 127.4340 128.2735 129.1220 129.9792 130.8301 131.6572 132.4454 133.2228 [25] 134.0528 135.0000 136.1000 137.2727 138.4090 139.4026 140.2416 141.0276 [33] 141.8690 142.8564 143.9707 145.1507 146.3356 147.4868 148.6046 149.6935 [41] 150.7604 151.8353 152.9613 154.1817 155.5178 156.9329 158.3808 159.8173 [49] 161.2269 162.6180 164.0000 > > > > cleanEx() > nameEx("backSpline") > ### * backSpline > > flush(stderr()); flush(stdout()) > > ### Name: backSpline > ### Title: Monotone Inverse Spline > ### Aliases: backSpline > ### Keywords: models > > ### ** Examples > > require(graphics) > ispl <- interpSpline( women$height, women$weight ) > bspl <- backSpline( ispl ) > plot( bspl ) # plots over the range of the knots > points( women$weight, women$height ) > > > > cleanEx() > nameEx("bs") > ### * bs > > flush(stderr()); flush(stdout()) > > ### Name: bs > ### Title: B-Spline Basis for Polynomial Splines > ### Aliases: bs > ### Keywords: smooth > > ### ** Examples > > require(stats); require(graphics) > bs(women$height, df = 5) 1 2 3 4 5 [1,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 0.000000000 [2,] 4.534439e-01 0.059857872 0.001639942 0.000000e+00 0.000000000 [3,] 5.969388e-01 0.203352770 0.013119534 0.000000e+00 0.000000000 [4,] 5.338010e-01 0.376366618 0.044278426 0.000000e+00 0.000000000 [5,] 3.673469e-01 0.524781341 0.104956268 0.000000e+00 0.000000000 [6,] 2.001640e-01 0.595025510 0.204719388 9.110787e-05 0.000000000 [7,] 9.110787e-02 0.566326531 0.336734694 5.830904e-03 0.000000000 [8,] 3.125000e-02 0.468750000 0.468750000 3.125000e-02 0.000000000 [9,] 5.830904e-03 0.336734694 0.566326531 9.110787e-02 0.000000000 [10,] 9.110787e-05 0.204719388 0.595025510 2.001640e-01 0.000000000 [11,] 0.000000e+00 0.104956268 0.524781341 3.673469e-01 0.002915452 [12,] 0.000000e+00 0.044278426 0.376366618 5.338010e-01 0.045553936 [13,] 0.000000e+00 0.013119534 0.203352770 5.969388e-01 0.186588921 [14,] 0.000000e+00 0.001639942 0.059857872 4.534439e-01 0.485058309 [15,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 1.000000000 attr(,"degree") [1] 3 attr(,"knots") 33.33333% 66.66667% 62.66667 67.33333 attr(,"Boundary.knots") [1] 58 72 attr(,"intercept") [1] FALSE attr(,"class") [1] "bs" "basis" "matrix" > summary(fm1 <- lm(weight ~ bs(height, df = 5), data = women)) Call: lm(formula = weight ~ bs(height, df = 5), data = women) Residuals: Min 1Q Median 3Q Max -0.31764 -0.13441 0.03922 0.11096 0.35086 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 114.8799 0.2167 530.146 < 2e-16 *** bs(height, df = 5)1 3.4657 0.4595 7.543 3.53e-05 *** bs(height, df = 5)2 13.0300 0.3965 32.860 1.10e-10 *** bs(height, df = 5)3 27.6161 0.4571 60.415 4.70e-13 *** bs(height, df = 5)4 40.8481 0.3866 105.669 3.09e-15 *** bs(height, df = 5)5 49.1296 0.3090 158.979 < 2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.2276 on 9 degrees of freedom Multiple R-squared: 0.9999, Adjusted R-squared: 0.9998 F-statistic: 1.298e+04 on 5 and 9 DF, p-value: < 2.2e-16 > > ## example of safe prediction > plot(women, xlab = "Height (in)", ylab = "Weight (lb)") > ht <- seq(57, 73, length.out = 200) > lines(ht, predict(fm1, data.frame(height = ht))) Warning in bs(height, degree = 3L, knots = c(62.6666666666667, 67.3333333333333 : some 'x' values beyond boundary knots may cause ill-conditioned bases > ## Don't show: > ## Consistency: > x <- c(1:3, 5:6) > stopifnot(identical(bs(x), bs(x, df = 3)), + identical(bs(x, df = 4), bs(x, df = 4, knots = NULL)), # not true till 2.15.2 + !is.null(kk <- attr(bs(x), "knots")), # not true till 1.5.1 + length(kk) == 0) > ## End(Don't show) > > > cleanEx() > nameEx("interpSpline") > ### * interpSpline > > flush(stderr()); flush(stdout()) > > ### Name: interpSpline > ### Title: Create an Interpolation Spline > ### Aliases: interpSpline > ### Keywords: models > > ### ** Examples > > require(graphics); require(stats) > ispl <- interpSpline( women$height, women$weight ) > ispl2 <- interpSpline( weight ~ height, women ) > # ispl and ispl2 should be the same > plot( predict( ispl, seq( 55, 75, length.out = 51 ) ), type = "l" ) > points( women$height, women$weight ) > plot( ispl ) # plots over the range of the knots > points( women$height, women$weight ) > splineKnots( ispl ) [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 > > > > cleanEx() > nameEx("ns") > ### * ns > > flush(stderr()); flush(stdout()) > > ### Name: ns > ### Title: Generate a Basis Matrix for Natural Cubic Splines > ### Aliases: ns > ### Keywords: smooth > > ### ** Examples > > require(stats); require(graphics) > ns(women$height, df = 5) 1 2 3 4 5 [1,] 0.000000e+00 0.000000e+00 0.00000000 0.00000000 0.0000000000 [2,] 7.592323e-03 0.000000e+00 -0.08670223 0.26010669 -0.1734044626 [3,] 6.073858e-02 0.000000e+00 -0.15030440 0.45091320 -0.3006088020 [4,] 2.047498e-01 6.073858e-05 -0.16778345 0.50335034 -0.3355668952 [5,] 4.334305e-01 1.311953e-02 -0.13244035 0.39732106 -0.2648807067 [6,] 6.256681e-01 8.084305e-02 -0.07399720 0.22199159 -0.1479943948 [7,] 6.477162e-01 2.468416e-01 -0.02616007 0.07993794 -0.0532919575 [8,] 4.791667e-01 4.791667e-01 0.01406302 0.02031093 -0.0135406187 [9,] 2.468416e-01 6.477162e-01 0.09733619 0.02286023 -0.0152401533 [10,] 8.084305e-02 6.256681e-01 0.27076826 0.06324188 -0.0405213106 [11,] 1.311953e-02 4.334305e-01 0.48059836 0.12526031 -0.0524087186 [12,] 6.073858e-05 2.047498e-01 0.59541597 0.19899261 0.0007809246 [13,] 0.000000e+00 6.073858e-02 0.50097182 0.27551020 0.1627793975 [14,] 0.000000e+00 7.592323e-03 0.22461127 0.35204082 0.4157555879 [15,] 0.000000e+00 0.000000e+00 -0.14285714 0.42857143 0.7142857143 attr(,"degree") [1] 3 attr(,"knots") 20% 40% 60% 80% 60.8 63.6 66.4 69.2 attr(,"Boundary.knots") [1] 58 72 attr(,"intercept") [1] FALSE attr(,"class") [1] "ns" "basis" "matrix" > summary(fm1 <- lm(weight ~ ns(height, df = 5), data = women)) Call: lm(formula = weight ~ ns(height, df = 5), data = women) Residuals: Min 1Q Median 3Q Max -0.38333 -0.12585 0.07083 0.15401 0.30426 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 114.7447 0.2338 490.88 < 2e-16 *** ns(height, df = 5)1 15.9474 0.3699 43.12 9.69e-12 *** ns(height, df = 5)2 25.1695 0.4323 58.23 6.55e-13 *** ns(height, df = 5)3 33.2582 0.3541 93.93 8.91e-15 *** ns(height, df = 5)4 50.7894 0.6062 83.78 2.49e-14 *** ns(height, df = 5)5 45.0363 0.2784 161.75 < 2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.2645 on 9 degrees of freedom Multiple R-squared: 0.9998, Adjusted R-squared: 0.9997 F-statistic: 9609 on 5 and 9 DF, p-value: < 2.2e-16 > > ## To see what knots were selected > attr(terms(fm1), "predvars") list(weight, ns(height, knots = c(60.8, 63.6, 66.4, 69.2), Boundary.knots = c(58, 72), intercept = FALSE)) > > ## example of safe prediction > plot(women, xlab = "Height (in)", ylab = "Weight (lb)") > ht <- seq(57, 73, length.out = 200) > lines(ht, predict(fm1, data.frame(height = ht))) > ## Don't show: > ## Consistency: > x <- c(1:3, 5:6) > stopifnot(identical(ns(x), ns(x, df = 1)), + identical(ns(x, df = 2), ns(x, df = 2, knots = NULL)), # not true till 2.15.2 + !is.null(kk <- attr(ns(x), "knots")), # not true till 1.5.1 + length(kk) == 0) > ## End(Don't show) > > > > cleanEx() > nameEx("periodicSpline") > ### * periodicSpline > > flush(stderr()); flush(stdout()) > > ### Name: periodicSpline > ### Title: Create a Periodic Interpolation Spline > ### Aliases: periodicSpline > ### Keywords: models > > ### ** Examples > > require(graphics); require(stats) > xx <- seq( -pi, pi, length.out = 16 )[-1] > yy <- sin( xx ) > frm <- data.frame( xx, yy ) > pispl <- periodicSpline( xx, yy, period = 2 * pi) > pispl2 <- periodicSpline( yy ~ xx, frm, period = 2 * pi ) > stopifnot(all.equal(pispl, pispl2)) # pispl and pispl2 are the same > > plot( pispl ) # displays over one period > points( yy ~ xx, col = "brown") > plot( predict( pispl, seq(-3*pi, 3*pi, length.out = 101) ), type = "l" ) > > > > cleanEx() > nameEx("polySpline") > ### * polySpline > > flush(stderr()); flush(stdout()) > > ### Name: polySpline > ### Title: Piecewise Polynomial Spline Representation > ### Aliases: polySpline as.polySpline > ### Keywords: models > > ### ** Examples > > require(graphics) > ispl <- polySpline(interpSpline( weight ~ height, women, bSpline = TRUE)) > print( ispl ) # print the piecewise polynomial representation polynomial representation of spline for weight ~ height constant linear quadratic cubic 58 115 1.731918 0.00000000 0.26808191 59 117 2.536164 0.80424574 -0.34040957 60 120 3.123427 -0.21698298 0.09355638 61 123 2.970130 0.06368616 -0.03381595 62 126 2.996054 -0.03776168 0.04170740 63 129 3.045653 0.08736054 -0.13301367 64 132 2.821333 -0.31168048 0.49034728 65 135 3.669014 1.15936136 -0.82837545 66 139 3.502610 -1.32576498 0.82315452 67 142 3.320544 1.14369857 -0.46424262 68 146 4.215213 -0.24902928 0.03381595 69 150 3.818603 -0.14758144 0.32897883 70 154 4.510376 0.83935505 -0.34973127 71 159 5.139893 -0.20983876 0.06994625 72 164 4.930054 0.00000000 0.00000000 > plot( ispl ) # plots over the range of the knots > points( women$height, women$weight ) > > > > cleanEx() > nameEx("predict.bSpline") > ### * predict.bSpline > > flush(stderr()); flush(stdout()) > > ### Name: predict.bSpline > ### Title: Evaluate a Spline at New Values of x > ### Aliases: predict.bSpline predict.nbSpline predict.pbSpline > ### predict.npolySpline predict.ppolySpline > ### Keywords: models > > ### ** Examples > > require(graphics); require(stats) > ispl <- interpSpline( weight ~ height, women ) > opar <- par(mfrow = c(2, 2), las = 1) > plot(predict(ispl, nseg = 201), # plots over the range of the knots + main = "Original data with interpolating spline", type = "l", + xlab = "height", ylab = "weight") > points(women$height, women$weight, col = 4) > plot(predict(ispl, nseg = 201, deriv = 1), + main = "First derivative of interpolating spline", type = "l", + xlab = "height", ylab = "weight") > plot(predict(ispl, nseg = 201, deriv = 2), + main = "Second derivative of interpolating spline", type = "l", + xlab = "height", ylab = "weight") > plot(predict(ispl, nseg = 401, deriv = 3), + main = "Third derivative of interpolating spline", type = "l", + xlab = "height", ylab = "weight") > par(opar) > > > > graphics::par(get("par.postscript", pos = 'CheckExEnv')) > cleanEx() > nameEx("predict.bs") > ### * predict.bs > > flush(stderr()); flush(stdout()) > > ### Name: predict.bs > ### Title: Evaluate a Spline Basis > ### Aliases: predict.bs predict.ns > ### Keywords: smooth > > ### ** Examples > > require(stats) > basis <- ns(women$height, df = 5) > newX <- seq(58, 72, length.out = 51) > # evaluate the basis at the new data > predict(basis, newX) 1 2 3 4 5 [1,] 0.0000000000 0.0000000000 0.000000000 0.00000000 0.000000000 [2,] 0.0001666667 0.0000000000 -0.025270112 0.07581034 -0.050540224 [3,] 0.0013333333 0.0000000000 -0.050033132 0.15009940 -0.100066264 [4,] 0.0045000000 0.0000000000 -0.073781966 0.22134590 -0.147563933 [5,] 0.0106666667 0.0000000000 -0.096009523 0.28802857 -0.192019047 [6,] 0.0208333333 0.0000000000 -0.116208710 0.34862613 -0.232417420 [7,] 0.0360000000 0.0000000000 -0.133872434 0.40161730 -0.267744868 [8,] 0.0571666667 0.0000000000 -0.148493603 0.44548081 -0.296987205 [9,] 0.0853333333 0.0000000000 -0.159565123 0.47869537 -0.319130247 [10,] 0.1215000000 0.0000000000 -0.166579904 0.49973971 -0.333159807 [11,] 0.1666666667 0.0000000000 -0.169030851 0.50709255 -0.338061702 [12,] 0.2211666667 0.0001666667 -0.166622161 0.49986648 -0.333244323 [13,] 0.2826666667 0.0013333333 -0.159903185 0.47970955 -0.319806370 [14,] 0.3481666667 0.0045000000 -0.149634561 0.44890368 -0.299269122 [15,] 0.4146666667 0.0106666667 -0.136576928 0.40973078 -0.273153855 [16,] 0.4791666667 0.0208333333 -0.121490924 0.36447277 -0.242981848 [17,] 0.5386666667 0.0360000000 -0.105137189 0.31541157 -0.210274379 [18,] 0.5901666667 0.0571666667 -0.088276362 0.26482909 -0.176552724 [19,] 0.6306666667 0.0853333333 -0.071669081 0.21500724 -0.143338162 [20,] 0.6571666667 0.1215000000 -0.056075985 0.16822795 -0.112151970 [21,] 0.6666666667 0.1666666667 -0.042257713 0.12677314 -0.084515425 [22,] 0.6571666667 0.2211666667 -0.030651111 0.09245333 -0.061635555 [23,] 0.6306666667 0.2826666667 -0.020397854 0.06519356 -0.043462374 [24,] 0.5901666667 0.3481666667 -0.010315824 0.04444747 -0.029631648 [25,] 0.5386666667 0.4146666667 0.000777096 0.02966871 -0.019779141 [26,] 0.4791666667 0.4791666667 0.014063024 0.02031093 -0.013540619 [27,] 0.4146666667 0.5386666667 0.030724078 0.01582777 -0.010551844 [28,] 0.3481666667 0.5901666667 0.051942375 0.01567287 -0.010448583 [29,] 0.2826666667 0.6306666667 0.078900034 0.01929990 -0.012866600 [30,] 0.2211666667 0.6571666667 0.112779171 0.02616249 -0.017441658 [31,] 0.1666666667 0.6666666667 0.154761905 0.03571429 -0.023809524 [32,] 0.1215000000 0.6571666667 0.205345238 0.04746429 -0.031476190 [33,] 0.0853333333 0.6306666667 0.262285714 0.06114286 -0.039428571 [34,] 0.0571666667 0.5901666667 0.322654762 0.07653571 -0.046523810 [35,] 0.0360000000 0.5386666667 0.383523810 0.09342857 -0.051619048 [36,] 0.0208333333 0.4791666667 0.441964286 0.11160714 -0.053571429 [37,] 0.0106666667 0.4146666667 0.495047619 0.13085714 -0.051238095 [38,] 0.0045000000 0.3481666667 0.539845238 0.15096429 -0.043476190 [39,] 0.0013333333 0.2826666667 0.573428571 0.17171429 -0.029142857 [40,] 0.0001666667 0.2211666667 0.592869048 0.19289286 -0.007095238 [41,] 0.0000000000 0.1666666667 0.595238095 0.21428571 0.023809524 [42,] 0.0000000000 0.1215000000 0.578428571 0.23571429 0.064357143 [43,] 0.0000000000 0.0853333333 0.543619048 0.25714286 0.113904762 [44,] 0.0000000000 0.0571666667 0.492809524 0.27857143 0.171452381 [45,] 0.0000000000 0.0360000000 0.428000000 0.30000000 0.236000000 [46,] 0.0000000000 0.0208333333 0.351190476 0.32142857 0.306547619 [47,] 0.0000000000 0.0106666667 0.264380952 0.34285714 0.382095238 [48,] 0.0000000000 0.0045000000 0.169571429 0.36428571 0.461642857 [49,] 0.0000000000 0.0013333333 0.068761905 0.38571429 0.544190476 [50,] 0.0000000000 0.0001666667 -0.036047619 0.40714286 0.628738095 [51,] 0.0000000000 0.0000000000 -0.142857143 0.42857143 0.714285714 attr(,"degree") [1] 3 attr(,"knots") 20% 40% 60% 80% 60.8 63.6 66.4 69.2 attr(,"Boundary.knots") [1] 58 72 attr(,"intercept") [1] FALSE attr(,"class") [1] "ns" "basis" "matrix" > > > > cleanEx() > nameEx("splineDesign") > ### * splineDesign > > flush(stderr()); flush(stdout()) > > ### Name: splineDesign > ### Title: Design Matrix for B-splines > ### Aliases: splineDesign spline.des > ### Keywords: models > > ### ** Examples > > require(graphics) > splineDesign(knots = 1:10, x = 4:7) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.1666667 0.6666667 0.1666667 0.0000000 0.0000000 0.0000000 [2,] 0.0000000 0.1666667 0.6666667 0.1666667 0.0000000 0.0000000 [3,] 0.0000000 0.0000000 0.1666667 0.6666667 0.1666667 0.0000000 [4,] 0.0000000 0.0000000 0.0000000 0.1666667 0.6666667 0.1666667 > ## visualize band structure > > knots <- c(1,1.8,3:5,6.5,7,8.1,9.2,10) # 10 => 10-4 = 6 Basis splines > x <- seq(min(knots)-1, max(knots)+1, length.out = 501) > bb <- splineDesign(knots, x = x, outer.ok = TRUE) > > plot(range(x), c(0,1), type = "n", xlab = "x", ylab = "", + main = "B-splines - sum to 1 inside inner knots") > mtext(expression(B[j](x) *" and "* sum(B[j](x), j == 1, 6)), adj = 0) > abline(v = knots, lty = 3, col = "light gray") > abline(v = knots[c(4,length(knots)-3)], lty = 3, col = "gray10") > lines(x, rowSums(bb), col = "gray", lwd = 2) > matlines(x, bb, ylim = c(0,1), lty = 1) > > > > cleanEx() > nameEx("splineKnots") > ### * splineKnots > > flush(stderr()); flush(stdout()) > > ### Name: splineKnots > ### Title: Knot Vector from a Spline > ### Aliases: splineKnots > ### Keywords: models > > ### ** Examples > > ispl <- interpSpline( weight ~ height, women ) > splineKnots( ispl ) [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 > > > > cleanEx() > nameEx("splineOrder") > ### * splineOrder > > flush(stderr()); flush(stdout()) > > ### Name: splineOrder > ### Title: Determine the Order of a Spline > ### Aliases: splineOrder > ### Keywords: models > > ### ** Examples > > splineOrder( interpSpline( weight ~ height, women ) ) [1] 4 > > > > cleanEx() > nameEx("xyVector") > ### * xyVector > > flush(stderr()); flush(stdout()) > > ### Name: xyVector > ### Title: Construct an 'xyVector' Object > ### Aliases: xyVector > ### Keywords: models > > ### ** Examples > > require(stats); require(graphics) > ispl <- interpSpline( weight ~ height, women ) > weights <- predict( ispl, seq( 55, 75, length.out = 51 )) > class( weights ) [1] "xyVector" > plot( weights, type = "l", xlab = "height", ylab = "weight" ) > points( women$height, women$weight ) > weights $x [1] 55.0 55.4 55.8 56.2 56.6 57.0 57.4 57.8 58.2 58.6 59.0 59.4 59.8 60.2 60.6 [16] 61.0 61.4 61.8 62.2 62.6 63.0 63.4 63.8 64.2 64.6 65.0 65.4 65.8 66.2 66.6 [31] 67.0 67.4 67.8 68.2 68.6 69.0 69.4 69.8 70.2 70.6 71.0 71.4 71.8 72.2 72.6 [46] 73.0 73.4 73.8 74.2 74.6 75.0 $y [1] 109.8042 110.4970 111.1898 111.8825 112.5753 113.2681 113.9608 114.6536 [9] 115.3485 116.0971 117.0000 118.1214 119.3694 120.6168 121.8162 123.0000 [17] 124.1961 125.3995 126.5980 127.7930 129.0000 130.2237 131.4243 132.5557 [25] 133.6865 135.0000 136.6001 138.2531 139.6541 140.8021 142.0000 143.4815 [33] 145.1507 146.8334 148.4468 150.0000 151.5249 153.1289 154.9329 156.9329 [41] 159.0000 161.0269 163.0134 164.9860 166.9580 168.9301 170.9021 172.8741 [49] 174.8461 176.8181 178.7902 attr(,"class") [1] "xyVector" > > > > ### *