| 56186 |
murdoch |
1 |
|
| 88673 |
hornik |
2 |
R Under development (unstable) (2025-08-22 r88671) -- "Unsuffered Consequences"
|
|
|
3 |
Copyright (C) 2025 The R Foundation for Statistical Computing
|
|
|
4 |
Platform: x86_64-pc-linux-gnu
|
| 56186 |
murdoch |
5 |
|
|
|
6 |
R is free software and comes with ABSOLUTELY NO WARRANTY.
|
|
|
7 |
You are welcome to redistribute it under certain conditions.
|
|
|
8 |
Type 'license()' or 'licence()' for distribution details.
|
|
|
9 |
|
|
|
10 |
R is a collaborative project with many contributors.
|
|
|
11 |
Type 'contributors()' for more information and
|
|
|
12 |
'citation()' on how to cite R or R packages in publications.
|
|
|
13 |
|
|
|
14 |
Type 'demo()' for some demos, 'help()' for on-line help, or
|
|
|
15 |
'help.start()' for an HTML browser interface to help.
|
|
|
16 |
Type 'q()' to quit R.
|
|
|
17 |
|
|
|
18 |
> pkgname <- "splines"
|
|
|
19 |
> source(file.path(R.home("share"), "R", "examples-header.R"))
|
|
|
20 |
> options(warn = 1)
|
|
|
21 |
> library('splines')
|
|
|
22 |
>
|
| 61787 |
ripley |
23 |
> base::assign(".oldSearch", base::search(), pos = 'CheckExEnv')
|
| 73819 |
hornik |
24 |
> base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv')
|
| 56186 |
murdoch |
25 |
> cleanEx()
|
|
|
26 |
> nameEx("asVector")
|
|
|
27 |
> ### * asVector
|
|
|
28 |
>
|
|
|
29 |
> flush(stderr()); flush(stdout())
|
|
|
30 |
>
|
|
|
31 |
> ### Name: asVector
|
|
|
32 |
> ### Title: Coerce an Object to a Vector
|
|
|
33 |
> ### Aliases: asVector
|
|
|
34 |
> ### Keywords: models
|
|
|
35 |
>
|
|
|
36 |
> ### ** Examples
|
|
|
37 |
>
|
|
|
38 |
> require(stats)
|
|
|
39 |
> ispl <- interpSpline( weight ~ height, women )
|
|
|
40 |
> pred <- predict(ispl)
|
|
|
41 |
> class(pred)
|
|
|
42 |
[1] "xyVector"
|
|
|
43 |
> utils::str(pred)
|
|
|
44 |
List of 2
|
|
|
45 |
$ x: num [1:51] 58 58.3 58.6 58.8 59.1 ...
|
|
|
46 |
$ y: num [1:51] 115 115 116 117 117 ...
|
|
|
47 |
- attr(*, "class")= chr "xyVector"
|
|
|
48 |
> asVector(pred)
|
|
|
49 |
[1] 115.0000 115.4908 116.0170 116.6137 117.3153 118.1214 118.9894 119.8747
|
|
|
50 |
[9] 120.7384 121.5787 122.4078 123.2380 124.0759 124.9181 125.7601 126.5980
|
|
|
51 |
[17] 127.4340 128.2735 129.1220 129.9792 130.8301 131.6572 132.4454 133.2228
|
|
|
52 |
[25] 134.0528 135.0000 136.1000 137.2727 138.4090 139.4026 140.2416 141.0276
|
|
|
53 |
[33] 141.8690 142.8564 143.9707 145.1507 146.3356 147.4868 148.6046 149.6935
|
|
|
54 |
[41] 150.7604 151.8353 152.9613 154.1817 155.5178 156.9329 158.3808 159.8173
|
|
|
55 |
[49] 161.2269 162.6180 164.0000
|
|
|
56 |
>
|
|
|
57 |
>
|
|
|
58 |
>
|
|
|
59 |
> cleanEx()
|
|
|
60 |
> nameEx("backSpline")
|
|
|
61 |
> ### * backSpline
|
|
|
62 |
>
|
|
|
63 |
> flush(stderr()); flush(stdout())
|
|
|
64 |
>
|
|
|
65 |
> ### Name: backSpline
|
|
|
66 |
> ### Title: Monotone Inverse Spline
|
|
|
67 |
> ### Aliases: backSpline
|
|
|
68 |
> ### Keywords: models
|
|
|
69 |
>
|
|
|
70 |
> ### ** Examples
|
|
|
71 |
>
|
|
|
72 |
> require(graphics)
|
|
|
73 |
> ispl <- interpSpline( women$height, women$weight )
|
|
|
74 |
> bspl <- backSpline( ispl )
|
|
|
75 |
> plot( bspl ) # plots over the range of the knots
|
|
|
76 |
> points( women$weight, women$height )
|
|
|
77 |
>
|
|
|
78 |
>
|
|
|
79 |
>
|
|
|
80 |
> cleanEx()
|
|
|
81 |
> nameEx("bs")
|
|
|
82 |
> ### * bs
|
|
|
83 |
>
|
|
|
84 |
> flush(stderr()); flush(stdout())
|
|
|
85 |
>
|
|
|
86 |
> ### Name: bs
|
|
|
87 |
> ### Title: B-Spline Basis for Polynomial Splines
|
|
|
88 |
> ### Aliases: bs
|
|
|
89 |
> ### Keywords: smooth
|
|
|
90 |
>
|
|
|
91 |
> ### ** Examples
|
|
|
92 |
>
|
|
|
93 |
> require(stats); require(graphics)
|
|
|
94 |
> bs(women$height, df = 5)
|
|
|
95 |
1 2 3 4 5
|
|
|
96 |
[1,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 0.000000000
|
|
|
97 |
[2,] 4.534439e-01 0.059857872 0.001639942 0.000000e+00 0.000000000
|
|
|
98 |
[3,] 5.969388e-01 0.203352770 0.013119534 0.000000e+00 0.000000000
|
|
|
99 |
[4,] 5.338010e-01 0.376366618 0.044278426 0.000000e+00 0.000000000
|
|
|
100 |
[5,] 3.673469e-01 0.524781341 0.104956268 0.000000e+00 0.000000000
|
|
|
101 |
[6,] 2.001640e-01 0.595025510 0.204719388 9.110787e-05 0.000000000
|
|
|
102 |
[7,] 9.110787e-02 0.566326531 0.336734694 5.830904e-03 0.000000000
|
|
|
103 |
[8,] 3.125000e-02 0.468750000 0.468750000 3.125000e-02 0.000000000
|
|
|
104 |
[9,] 5.830904e-03 0.336734694 0.566326531 9.110787e-02 0.000000000
|
|
|
105 |
[10,] 9.110787e-05 0.204719388 0.595025510 2.001640e-01 0.000000000
|
|
|
106 |
[11,] 0.000000e+00 0.104956268 0.524781341 3.673469e-01 0.002915452
|
|
|
107 |
[12,] 0.000000e+00 0.044278426 0.376366618 5.338010e-01 0.045553936
|
|
|
108 |
[13,] 0.000000e+00 0.013119534 0.203352770 5.969388e-01 0.186588921
|
|
|
109 |
[14,] 0.000000e+00 0.001639942 0.059857872 4.534439e-01 0.485058309
|
|
|
110 |
[15,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 1.000000000
|
|
|
111 |
attr(,"degree")
|
|
|
112 |
[1] 3
|
|
|
113 |
attr(,"knots")
|
| 84250 |
maechler |
114 |
[1] 62.66667 67.33333
|
| 56186 |
murdoch |
115 |
attr(,"Boundary.knots")
|
|
|
116 |
[1] 58 72
|
|
|
117 |
attr(,"intercept")
|
|
|
118 |
[1] FALSE
|
|
|
119 |
attr(,"class")
|
|
|
120 |
[1] "bs" "basis" "matrix"
|
|
|
121 |
> summary(fm1 <- lm(weight ~ bs(height, df = 5), data = women))
|
|
|
122 |
|
|
|
123 |
Call:
|
|
|
124 |
lm(formula = weight ~ bs(height, df = 5), data = women)
|
|
|
125 |
|
|
|
126 |
Residuals:
|
|
|
127 |
Min 1Q Median 3Q Max
|
|
|
128 |
-0.31764 -0.13441 0.03922 0.11096 0.35086
|
|
|
129 |
|
|
|
130 |
Coefficients:
|
|
|
131 |
Estimate Std. Error t value Pr(>|t|)
|
|
|
132 |
(Intercept) 114.8799 0.2167 530.146 < 2e-16 ***
|
|
|
133 |
bs(height, df = 5)1 3.4657 0.4595 7.543 3.53e-05 ***
|
|
|
134 |
bs(height, df = 5)2 13.0300 0.3965 32.860 1.10e-10 ***
|
|
|
135 |
bs(height, df = 5)3 27.6161 0.4571 60.415 4.70e-13 ***
|
|
|
136 |
bs(height, df = 5)4 40.8481 0.3866 105.669 3.09e-15 ***
|
|
|
137 |
bs(height, df = 5)5 49.1296 0.3090 158.979 < 2e-16 ***
|
|
|
138 |
---
|
| 61435 |
ripley |
139 |
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
|
| 56186 |
murdoch |
140 |
|
|
|
141 |
Residual standard error: 0.2276 on 9 degrees of freedom
|
| 61435 |
ripley |
142 |
Multiple R-squared: 0.9999, Adjusted R-squared: 0.9998
|
|
|
143 |
F-statistic: 1.298e+04 on 5 and 9 DF, p-value: < 2.2e-16
|
| 56186 |
murdoch |
144 |
|
|
|
145 |
>
|
|
|
146 |
> ## example of safe prediction
|
|
|
147 |
> plot(women, xlab = "Height (in)", ylab = "Weight (lb)")
|
|
|
148 |
> ht <- seq(57, 73, length.out = 200)
|
| 61164 |
ripley |
149 |
> lines(ht, predict(fm1, data.frame(height = ht)))
|
| 84250 |
maechler |
150 |
Warning in bs(height, degree = 3L, knots = c(62.6666666666667, 67.3333333333333 :
|
| 56186 |
murdoch |
151 |
some 'x' values beyond boundary knots may cause ill-conditioned bases
|
| 59720 |
maechler |
152 |
> ## Don't show:
|
|
|
153 |
> ## Consistency:
|
| 61169 |
ripley |
154 |
> x <- c(1:3, 5:6)
|
| 59720 |
maechler |
155 |
> stopifnot(identical(bs(x), bs(x, df = 3)),
|
| 61169 |
ripley |
156 |
+ identical(bs(x, df = 4), bs(x, df = 4, knots = NULL)), # not true till 2.15.2
|
|
|
157 |
+ !is.null(kk <- attr(bs(x), "knots")), # not true till 1.5.1
|
| 59720 |
maechler |
158 |
+ length(kk) == 0)
|
| 66943 |
maechler |
159 |
> ## End(Don't show)
|
| 56186 |
murdoch |
160 |
>
|
|
|
161 |
>
|
| 88673 |
hornik |
162 |
>
|
| 56186 |
murdoch |
163 |
> cleanEx()
|
|
|
164 |
> nameEx("interpSpline")
|
|
|
165 |
> ### * interpSpline
|
|
|
166 |
>
|
|
|
167 |
> flush(stderr()); flush(stdout())
|
|
|
168 |
>
|
|
|
169 |
> ### Name: interpSpline
|
|
|
170 |
> ### Title: Create an Interpolation Spline
|
|
|
171 |
> ### Aliases: interpSpline
|
|
|
172 |
> ### Keywords: models
|
|
|
173 |
>
|
|
|
174 |
> ### ** Examples
|
|
|
175 |
>
|
|
|
176 |
> require(graphics); require(stats)
|
|
|
177 |
> ispl <- interpSpline( women$height, women$weight )
|
|
|
178 |
> ispl2 <- interpSpline( weight ~ height, women )
|
|
|
179 |
> # ispl and ispl2 should be the same
|
|
|
180 |
> plot( predict( ispl, seq( 55, 75, length.out = 51 ) ), type = "l" )
|
|
|
181 |
> points( women$height, women$weight )
|
|
|
182 |
> plot( ispl ) # plots over the range of the knots
|
|
|
183 |
> points( women$height, women$weight )
|
|
|
184 |
> splineKnots( ispl )
|
|
|
185 |
[1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
|
|
186 |
>
|
|
|
187 |
>
|
|
|
188 |
>
|
|
|
189 |
> cleanEx()
|
|
|
190 |
> nameEx("ns")
|
|
|
191 |
> ### * ns
|
|
|
192 |
>
|
|
|
193 |
> flush(stderr()); flush(stdout())
|
|
|
194 |
>
|
|
|
195 |
> ### Name: ns
|
|
|
196 |
> ### Title: Generate a Basis Matrix for Natural Cubic Splines
|
|
|
197 |
> ### Aliases: ns
|
|
|
198 |
> ### Keywords: smooth
|
|
|
199 |
>
|
|
|
200 |
> ### ** Examples
|
|
|
201 |
>
|
|
|
202 |
> require(stats); require(graphics)
|
|
|
203 |
> ns(women$height, df = 5)
|
|
|
204 |
1 2 3 4 5
|
|
|
205 |
[1,] 0.000000e+00 0.000000e+00 0.00000000 0.00000000 0.0000000000
|
|
|
206 |
[2,] 7.592323e-03 0.000000e+00 -0.08670223 0.26010669 -0.1734044626
|
|
|
207 |
[3,] 6.073858e-02 0.000000e+00 -0.15030440 0.45091320 -0.3006088020
|
|
|
208 |
[4,] 2.047498e-01 6.073858e-05 -0.16778345 0.50335034 -0.3355668952
|
|
|
209 |
[5,] 4.334305e-01 1.311953e-02 -0.13244035 0.39732106 -0.2648807067
|
|
|
210 |
[6,] 6.256681e-01 8.084305e-02 -0.07399720 0.22199159 -0.1479943948
|
|
|
211 |
[7,] 6.477162e-01 2.468416e-01 -0.02616007 0.07993794 -0.0532919575
|
|
|
212 |
[8,] 4.791667e-01 4.791667e-01 0.01406302 0.02031093 -0.0135406187
|
|
|
213 |
[9,] 2.468416e-01 6.477162e-01 0.09733619 0.02286023 -0.0152401533
|
|
|
214 |
[10,] 8.084305e-02 6.256681e-01 0.27076826 0.06324188 -0.0405213106
|
|
|
215 |
[11,] 1.311953e-02 4.334305e-01 0.48059836 0.12526031 -0.0524087186
|
|
|
216 |
[12,] 6.073858e-05 2.047498e-01 0.59541597 0.19899261 0.0007809246
|
|
|
217 |
[13,] 0.000000e+00 6.073858e-02 0.50097182 0.27551020 0.1627793975
|
|
|
218 |
[14,] 0.000000e+00 7.592323e-03 0.22461127 0.35204082 0.4157555879
|
|
|
219 |
[15,] 0.000000e+00 0.000000e+00 -0.14285714 0.42857143 0.7142857143
|
|
|
220 |
attr(,"degree")
|
|
|
221 |
[1] 3
|
|
|
222 |
attr(,"knots")
|
| 84250 |
maechler |
223 |
[1] 60.8 63.6 66.4 69.2
|
| 56186 |
murdoch |
224 |
attr(,"Boundary.knots")
|
|
|
225 |
[1] 58 72
|
|
|
226 |
attr(,"intercept")
|
|
|
227 |
[1] FALSE
|
|
|
228 |
attr(,"class")
|
|
|
229 |
[1] "ns" "basis" "matrix"
|
|
|
230 |
> summary(fm1 <- lm(weight ~ ns(height, df = 5), data = women))
|
|
|
231 |
|
|
|
232 |
Call:
|
|
|
233 |
lm(formula = weight ~ ns(height, df = 5), data = women)
|
|
|
234 |
|
|
|
235 |
Residuals:
|
|
|
236 |
Min 1Q Median 3Q Max
|
|
|
237 |
-0.38333 -0.12585 0.07083 0.15401 0.30426
|
|
|
238 |
|
|
|
239 |
Coefficients:
|
|
|
240 |
Estimate Std. Error t value Pr(>|t|)
|
|
|
241 |
(Intercept) 114.7447 0.2338 490.88 < 2e-16 ***
|
|
|
242 |
ns(height, df = 5)1 15.9474 0.3699 43.12 9.69e-12 ***
|
|
|
243 |
ns(height, df = 5)2 25.1695 0.4323 58.23 6.55e-13 ***
|
|
|
244 |
ns(height, df = 5)3 33.2582 0.3541 93.93 8.91e-15 ***
|
|
|
245 |
ns(height, df = 5)4 50.7894 0.6062 83.78 2.49e-14 ***
|
|
|
246 |
ns(height, df = 5)5 45.0363 0.2784 161.75 < 2e-16 ***
|
|
|
247 |
---
|
| 61435 |
ripley |
248 |
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
|
| 56186 |
murdoch |
249 |
|
|
|
250 |
Residual standard error: 0.2645 on 9 degrees of freedom
|
| 61435 |
ripley |
251 |
Multiple R-squared: 0.9998, Adjusted R-squared: 0.9997
|
|
|
252 |
F-statistic: 9609 on 5 and 9 DF, p-value: < 2.2e-16
|
| 56186 |
murdoch |
253 |
|
|
|
254 |
>
|
| 62223 |
ripley |
255 |
> ## To see what knots were selected
|
|
|
256 |
> attr(terms(fm1), "predvars")
|
| 84250 |
maechler |
257 |
list(weight, ns(height, knots = c(60.8, 63.6, 66.4, 69.2), Boundary.knots = c(58,
|
|
|
258 |
72), intercept = FALSE))
|
| 62223 |
ripley |
259 |
>
|
| 56186 |
murdoch |
260 |
> ## example of safe prediction
|
|
|
261 |
> plot(women, xlab = "Height (in)", ylab = "Weight (lb)")
|
| 74701 |
maechler |
262 |
> ht <- seq(57, 73, length.out = 200) ; nD <- data.frame(height = ht)
|
|
|
263 |
> lines(ht, p1 <- predict(fm1, nD))
|
|
|
264 |
> stopifnot(all.equal(p1, predict(update(fm1, . ~
|
|
|
265 |
+ splines::ns(height, df=5)), nD)))
|
|
|
266 |
> # not true in R < 3.5.0
|
| 59720 |
maechler |
267 |
> ## Don't show:
|
|
|
268 |
> ## Consistency:
|
| 61169 |
ripley |
269 |
> x <- c(1:3, 5:6)
|
| 59720 |
maechler |
270 |
> stopifnot(identical(ns(x), ns(x, df = 1)),
|
| 74701 |
maechler |
271 |
+ identical(ns(x, df = 2),
|
|
|
272 |
+ ns(x, df = 2, knots = NULL)), # not true till 2.15.2
|
| 61169 |
ripley |
273 |
+ !is.null(kk <- attr(ns(x), "knots")), # not true till 1.5.1
|
| 59720 |
maechler |
274 |
+ length(kk) == 0)
|
| 66943 |
maechler |
275 |
> ## End(Don't show)
|
| 56186 |
murdoch |
276 |
>
|
|
|
277 |
>
|
|
|
278 |
>
|
|
|
279 |
> cleanEx()
|
|
|
280 |
> nameEx("periodicSpline")
|
|
|
281 |
> ### * periodicSpline
|
|
|
282 |
>
|
|
|
283 |
> flush(stderr()); flush(stdout())
|
|
|
284 |
>
|
|
|
285 |
> ### Name: periodicSpline
|
|
|
286 |
> ### Title: Create a Periodic Interpolation Spline
|
|
|
287 |
> ### Aliases: periodicSpline
|
|
|
288 |
> ### Keywords: models
|
|
|
289 |
>
|
|
|
290 |
> ### ** Examples
|
|
|
291 |
>
|
|
|
292 |
> require(graphics); require(stats)
|
|
|
293 |
> xx <- seq( -pi, pi, length.out = 16 )[-1]
|
|
|
294 |
> yy <- sin( xx )
|
|
|
295 |
> frm <- data.frame( xx, yy )
|
|
|
296 |
> pispl <- periodicSpline( xx, yy, period = 2 * pi)
|
|
|
297 |
> pispl2 <- periodicSpline( yy ~ xx, frm, period = 2 * pi )
|
| 61169 |
ripley |
298 |
> stopifnot(all.equal(pispl, pispl2)) # pispl and pispl2 are the same
|
| 56186 |
murdoch |
299 |
>
|
|
|
300 |
> plot( pispl ) # displays over one period
|
|
|
301 |
> points( yy ~ xx, col = "brown")
|
|
|
302 |
> plot( predict( pispl, seq(-3*pi, 3*pi, length.out = 101) ), type = "l" )
|
|
|
303 |
>
|
|
|
304 |
>
|
|
|
305 |
>
|
|
|
306 |
> cleanEx()
|
|
|
307 |
> nameEx("polySpline")
|
|
|
308 |
> ### * polySpline
|
|
|
309 |
>
|
|
|
310 |
> flush(stderr()); flush(stdout())
|
|
|
311 |
>
|
|
|
312 |
> ### Name: polySpline
|
|
|
313 |
> ### Title: Piecewise Polynomial Spline Representation
|
|
|
314 |
> ### Aliases: polySpline as.polySpline
|
|
|
315 |
> ### Keywords: models
|
|
|
316 |
>
|
|
|
317 |
> ### ** Examples
|
|
|
318 |
>
|
|
|
319 |
> require(graphics)
|
| 56288 |
ripley |
320 |
> ispl <- polySpline(interpSpline( weight ~ height, women, bSpline = TRUE))
|
| 79466 |
ripley |
321 |
> ## IGNORE_RDIFF_BEGIN
|
| 56186 |
murdoch |
322 |
> print( ispl ) # print the piecewise polynomial representation
|
|
|
323 |
polynomial representation of spline for weight ~ height
|
|
|
324 |
constant linear quadratic cubic
|
|
|
325 |
58 115 1.731918 0.00000000 0.26808191
|
|
|
326 |
59 117 2.536164 0.80424574 -0.34040957
|
|
|
327 |
60 120 3.123427 -0.21698298 0.09355638
|
|
|
328 |
61 123 2.970130 0.06368616 -0.03381595
|
|
|
329 |
62 126 2.996054 -0.03776168 0.04170740
|
|
|
330 |
63 129 3.045653 0.08736054 -0.13301367
|
|
|
331 |
64 132 2.821333 -0.31168048 0.49034728
|
|
|
332 |
65 135 3.669014 1.15936136 -0.82837545
|
|
|
333 |
66 139 3.502610 -1.32576498 0.82315452
|
|
|
334 |
67 142 3.320544 1.14369857 -0.46424262
|
|
|
335 |
68 146 4.215213 -0.24902928 0.03381595
|
|
|
336 |
69 150 3.818603 -0.14758144 0.32897883
|
|
|
337 |
70 154 4.510376 0.83935505 -0.34973127
|
|
|
338 |
71 159 5.139893 -0.20983876 0.06994625
|
|
|
339 |
72 164 4.930054 0.00000000 0.00000000
|
| 79466 |
ripley |
340 |
> ## IGNORE_RDIFF_END
|
| 56186 |
murdoch |
341 |
> plot( ispl ) # plots over the range of the knots
|
|
|
342 |
> points( women$height, women$weight )
|
|
|
343 |
>
|
|
|
344 |
>
|
|
|
345 |
>
|
|
|
346 |
> cleanEx()
|
|
|
347 |
> nameEx("predict.bSpline")
|
|
|
348 |
> ### * predict.bSpline
|
|
|
349 |
>
|
|
|
350 |
> flush(stderr()); flush(stdout())
|
|
|
351 |
>
|
|
|
352 |
> ### Name: predict.bSpline
|
|
|
353 |
> ### Title: Evaluate a Spline at New Values of x
|
|
|
354 |
> ### Aliases: predict.bSpline predict.nbSpline predict.pbSpline
|
|
|
355 |
> ### predict.npolySpline predict.ppolySpline
|
|
|
356 |
> ### Keywords: models
|
|
|
357 |
>
|
|
|
358 |
> ### ** Examples
|
|
|
359 |
>
|
|
|
360 |
> require(graphics); require(stats)
|
|
|
361 |
> ispl <- interpSpline( weight ~ height, women )
|
|
|
362 |
> opar <- par(mfrow = c(2, 2), las = 1)
|
|
|
363 |
> plot(predict(ispl, nseg = 201), # plots over the range of the knots
|
|
|
364 |
+ main = "Original data with interpolating spline", type = "l",
|
| 61435 |
ripley |
365 |
+ xlab = "height", ylab = "weight")
|
| 56186 |
murdoch |
366 |
> points(women$height, women$weight, col = 4)
|
|
|
367 |
> plot(predict(ispl, nseg = 201, deriv = 1),
|
|
|
368 |
+ main = "First derivative of interpolating spline", type = "l",
|
| 61435 |
ripley |
369 |
+ xlab = "height", ylab = "weight")
|
| 56186 |
murdoch |
370 |
> plot(predict(ispl, nseg = 201, deriv = 2),
|
|
|
371 |
+ main = "Second derivative of interpolating spline", type = "l",
|
| 61435 |
ripley |
372 |
+ xlab = "height", ylab = "weight")
|
| 56186 |
murdoch |
373 |
> plot(predict(ispl, nseg = 401, deriv = 3),
|
|
|
374 |
+ main = "Third derivative of interpolating spline", type = "l",
|
| 61435 |
ripley |
375 |
+ xlab = "height", ylab = "weight")
|
| 56186 |
murdoch |
376 |
> par(opar)
|
|
|
377 |
>
|
|
|
378 |
>
|
|
|
379 |
>
|
|
|
380 |
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
|
|
|
381 |
> cleanEx()
|
|
|
382 |
> nameEx("predict.bs")
|
|
|
383 |
> ### * predict.bs
|
|
|
384 |
>
|
|
|
385 |
> flush(stderr()); flush(stdout())
|
|
|
386 |
>
|
|
|
387 |
> ### Name: predict.bs
|
|
|
388 |
> ### Title: Evaluate a Spline Basis
|
|
|
389 |
> ### Aliases: predict.bs predict.ns
|
|
|
390 |
> ### Keywords: smooth
|
|
|
391 |
>
|
|
|
392 |
> ### ** Examples
|
|
|
393 |
>
|
|
|
394 |
> require(stats)
|
|
|
395 |
> basis <- ns(women$height, df = 5)
|
|
|
396 |
> newX <- seq(58, 72, length.out = 51)
|
|
|
397 |
> # evaluate the basis at the new data
|
|
|
398 |
> predict(basis, newX)
|
|
|
399 |
1 2 3 4 5
|
|
|
400 |
[1,] 0.0000000000 0.0000000000 0.000000000 0.00000000 0.000000000
|
|
|
401 |
[2,] 0.0001666667 0.0000000000 -0.025270112 0.07581034 -0.050540224
|
|
|
402 |
[3,] 0.0013333333 0.0000000000 -0.050033132 0.15009940 -0.100066264
|
|
|
403 |
[4,] 0.0045000000 0.0000000000 -0.073781966 0.22134590 -0.147563933
|
|
|
404 |
[5,] 0.0106666667 0.0000000000 -0.096009523 0.28802857 -0.192019047
|
|
|
405 |
[6,] 0.0208333333 0.0000000000 -0.116208710 0.34862613 -0.232417420
|
|
|
406 |
[7,] 0.0360000000 0.0000000000 -0.133872434 0.40161730 -0.267744868
|
|
|
407 |
[8,] 0.0571666667 0.0000000000 -0.148493603 0.44548081 -0.296987205
|
|
|
408 |
[9,] 0.0853333333 0.0000000000 -0.159565123 0.47869537 -0.319130247
|
|
|
409 |
[10,] 0.1215000000 0.0000000000 -0.166579904 0.49973971 -0.333159807
|
|
|
410 |
[11,] 0.1666666667 0.0000000000 -0.169030851 0.50709255 -0.338061702
|
|
|
411 |
[12,] 0.2211666667 0.0001666667 -0.166622161 0.49986648 -0.333244323
|
|
|
412 |
[13,] 0.2826666667 0.0013333333 -0.159903185 0.47970955 -0.319806370
|
|
|
413 |
[14,] 0.3481666667 0.0045000000 -0.149634561 0.44890368 -0.299269122
|
|
|
414 |
[15,] 0.4146666667 0.0106666667 -0.136576928 0.40973078 -0.273153855
|
|
|
415 |
[16,] 0.4791666667 0.0208333333 -0.121490924 0.36447277 -0.242981848
|
|
|
416 |
[17,] 0.5386666667 0.0360000000 -0.105137189 0.31541157 -0.210274379
|
|
|
417 |
[18,] 0.5901666667 0.0571666667 -0.088276362 0.26482909 -0.176552724
|
|
|
418 |
[19,] 0.6306666667 0.0853333333 -0.071669081 0.21500724 -0.143338162
|
|
|
419 |
[20,] 0.6571666667 0.1215000000 -0.056075985 0.16822795 -0.112151970
|
|
|
420 |
[21,] 0.6666666667 0.1666666667 -0.042257713 0.12677314 -0.084515425
|
|
|
421 |
[22,] 0.6571666667 0.2211666667 -0.030651111 0.09245333 -0.061635555
|
|
|
422 |
[23,] 0.6306666667 0.2826666667 -0.020397854 0.06519356 -0.043462374
|
|
|
423 |
[24,] 0.5901666667 0.3481666667 -0.010315824 0.04444747 -0.029631648
|
|
|
424 |
[25,] 0.5386666667 0.4146666667 0.000777096 0.02966871 -0.019779141
|
|
|
425 |
[26,] 0.4791666667 0.4791666667 0.014063024 0.02031093 -0.013540619
|
|
|
426 |
[27,] 0.4146666667 0.5386666667 0.030724078 0.01582777 -0.010551844
|
|
|
427 |
[28,] 0.3481666667 0.5901666667 0.051942375 0.01567287 -0.010448583
|
|
|
428 |
[29,] 0.2826666667 0.6306666667 0.078900034 0.01929990 -0.012866600
|
|
|
429 |
[30,] 0.2211666667 0.6571666667 0.112779171 0.02616249 -0.017441658
|
|
|
430 |
[31,] 0.1666666667 0.6666666667 0.154761905 0.03571429 -0.023809524
|
|
|
431 |
[32,] 0.1215000000 0.6571666667 0.205345238 0.04746429 -0.031476190
|
|
|
432 |
[33,] 0.0853333333 0.6306666667 0.262285714 0.06114286 -0.039428571
|
|
|
433 |
[34,] 0.0571666667 0.5901666667 0.322654762 0.07653571 -0.046523810
|
|
|
434 |
[35,] 0.0360000000 0.5386666667 0.383523810 0.09342857 -0.051619048
|
|
|
435 |
[36,] 0.0208333333 0.4791666667 0.441964286 0.11160714 -0.053571429
|
|
|
436 |
[37,] 0.0106666667 0.4146666667 0.495047619 0.13085714 -0.051238095
|
|
|
437 |
[38,] 0.0045000000 0.3481666667 0.539845238 0.15096429 -0.043476190
|
|
|
438 |
[39,] 0.0013333333 0.2826666667 0.573428571 0.17171429 -0.029142857
|
|
|
439 |
[40,] 0.0001666667 0.2211666667 0.592869048 0.19289286 -0.007095238
|
|
|
440 |
[41,] 0.0000000000 0.1666666667 0.595238095 0.21428571 0.023809524
|
|
|
441 |
[42,] 0.0000000000 0.1215000000 0.578428571 0.23571429 0.064357143
|
|
|
442 |
[43,] 0.0000000000 0.0853333333 0.543619048 0.25714286 0.113904762
|
|
|
443 |
[44,] 0.0000000000 0.0571666667 0.492809524 0.27857143 0.171452381
|
|
|
444 |
[45,] 0.0000000000 0.0360000000 0.428000000 0.30000000 0.236000000
|
|
|
445 |
[46,] 0.0000000000 0.0208333333 0.351190476 0.32142857 0.306547619
|
|
|
446 |
[47,] 0.0000000000 0.0106666667 0.264380952 0.34285714 0.382095238
|
|
|
447 |
[48,] 0.0000000000 0.0045000000 0.169571429 0.36428571 0.461642857
|
|
|
448 |
[49,] 0.0000000000 0.0013333333 0.068761905 0.38571429 0.544190476
|
|
|
449 |
[50,] 0.0000000000 0.0001666667 -0.036047619 0.40714286 0.628738095
|
|
|
450 |
[51,] 0.0000000000 0.0000000000 -0.142857143 0.42857143 0.714285714
|
|
|
451 |
attr(,"degree")
|
|
|
452 |
[1] 3
|
|
|
453 |
attr(,"knots")
|
| 84250 |
maechler |
454 |
[1] 60.8 63.6 66.4 69.2
|
| 56186 |
murdoch |
455 |
attr(,"Boundary.knots")
|
|
|
456 |
[1] 58 72
|
|
|
457 |
attr(,"intercept")
|
|
|
458 |
[1] FALSE
|
|
|
459 |
attr(,"class")
|
|
|
460 |
[1] "ns" "basis" "matrix"
|
|
|
461 |
>
|
|
|
462 |
>
|
|
|
463 |
>
|
|
|
464 |
> cleanEx()
|
|
|
465 |
> nameEx("splineDesign")
|
|
|
466 |
> ### * splineDesign
|
|
|
467 |
>
|
|
|
468 |
> flush(stderr()); flush(stdout())
|
|
|
469 |
>
|
|
|
470 |
> ### Name: splineDesign
|
|
|
471 |
> ### Title: Design Matrix for B-splines
|
|
|
472 |
> ### Aliases: splineDesign spline.des
|
|
|
473 |
> ### Keywords: models
|
|
|
474 |
>
|
|
|
475 |
> ### ** Examples
|
|
|
476 |
>
|
|
|
477 |
> require(graphics)
|
|
|
478 |
> splineDesign(knots = 1:10, x = 4:7)
|
|
|
479 |
[,1] [,2] [,3] [,4] [,5] [,6]
|
|
|
480 |
[1,] 0.1666667 0.6666667 0.1666667 0.0000000 0.0000000 0.0000000
|
|
|
481 |
[2,] 0.0000000 0.1666667 0.6666667 0.1666667 0.0000000 0.0000000
|
|
|
482 |
[3,] 0.0000000 0.0000000 0.1666667 0.6666667 0.1666667 0.0000000
|
|
|
483 |
[4,] 0.0000000 0.0000000 0.0000000 0.1666667 0.6666667 0.1666667
|
| 80081 |
hornik |
484 |
> splineDesign(knots = 1:10, x = 4:7, derivs = 1)
|
| 69510 |
maechler |
485 |
[,1] [,2] [,3] [,4] [,5] [,6]
|
|
|
486 |
[1,] -0.5 0.0 0.5 0.0 0.0 0.0
|
|
|
487 |
[2,] 0.0 -0.5 0.0 0.5 0.0 0.0
|
|
|
488 |
[3,] 0.0 0.0 -0.5 0.0 0.5 0.0
|
|
|
489 |
[4,] 0.0 0.0 0.0 -0.5 0.0 0.5
|
| 63381 |
ripley |
490 |
> ## visualize band structure
|
| 56186 |
murdoch |
491 |
>
|
| 61169 |
ripley |
492 |
> knots <- c(1,1.8,3:5,6.5,7,8.1,9.2,10) # 10 => 10-4 = 6 Basis splines
|
| 61164 |
ripley |
493 |
> x <- seq(min(knots)-1, max(knots)+1, length.out = 501)
|
|
|
494 |
> bb <- splineDesign(knots, x = x, outer.ok = TRUE)
|
| 56186 |
murdoch |
495 |
>
|
| 61164 |
ripley |
496 |
> plot(range(x), c(0,1), type = "n", xlab = "x", ylab = "",
|
|
|
497 |
+ main = "B-splines - sum to 1 inside inner knots")
|
|
|
498 |
> mtext(expression(B[j](x) *" and "* sum(B[j](x), j == 1, 6)), adj = 0)
|
|
|
499 |
> abline(v = knots, lty = 3, col = "light gray")
|
|
|
500 |
> abline(v = knots[c(4,length(knots)-3)], lty = 3, col = "gray10")
|
|
|
501 |
> lines(x, rowSums(bb), col = "gray", lwd = 2)
|
|
|
502 |
> matlines(x, bb, ylim = c(0,1), lty = 1)
|
| 56186 |
murdoch |
503 |
>
|
|
|
504 |
>
|
|
|
505 |
>
|
|
|
506 |
> cleanEx()
|
|
|
507 |
> nameEx("splineKnots")
|
|
|
508 |
> ### * splineKnots
|
|
|
509 |
>
|
|
|
510 |
> flush(stderr()); flush(stdout())
|
|
|
511 |
>
|
|
|
512 |
> ### Name: splineKnots
|
|
|
513 |
> ### Title: Knot Vector from a Spline
|
|
|
514 |
> ### Aliases: splineKnots
|
|
|
515 |
> ### Keywords: models
|
|
|
516 |
>
|
|
|
517 |
> ### ** Examples
|
|
|
518 |
>
|
|
|
519 |
> ispl <- interpSpline( weight ~ height, women )
|
|
|
520 |
> splineKnots( ispl )
|
|
|
521 |
[1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
|
|
522 |
>
|
|
|
523 |
>
|
|
|
524 |
>
|
|
|
525 |
> cleanEx()
|
|
|
526 |
> nameEx("splineOrder")
|
|
|
527 |
> ### * splineOrder
|
|
|
528 |
>
|
|
|
529 |
> flush(stderr()); flush(stdout())
|
|
|
530 |
>
|
|
|
531 |
> ### Name: splineOrder
|
|
|
532 |
> ### Title: Determine the Order of a Spline
|
|
|
533 |
> ### Aliases: splineOrder
|
|
|
534 |
> ### Keywords: models
|
|
|
535 |
>
|
|
|
536 |
> ### ** Examples
|
|
|
537 |
>
|
|
|
538 |
> splineOrder( interpSpline( weight ~ height, women ) )
|
|
|
539 |
[1] 4
|
|
|
540 |
>
|
|
|
541 |
>
|
|
|
542 |
>
|
|
|
543 |
> cleanEx()
|
|
|
544 |
> nameEx("xyVector")
|
|
|
545 |
> ### * xyVector
|
|
|
546 |
>
|
|
|
547 |
> flush(stderr()); flush(stdout())
|
|
|
548 |
>
|
|
|
549 |
> ### Name: xyVector
|
|
|
550 |
> ### Title: Construct an 'xyVector' Object
|
|
|
551 |
> ### Aliases: xyVector
|
|
|
552 |
> ### Keywords: models
|
|
|
553 |
>
|
|
|
554 |
> ### ** Examples
|
|
|
555 |
>
|
|
|
556 |
> require(stats); require(graphics)
|
|
|
557 |
> ispl <- interpSpline( weight ~ height, women )
|
|
|
558 |
> weights <- predict( ispl, seq( 55, 75, length.out = 51 ))
|
|
|
559 |
> class( weights )
|
|
|
560 |
[1] "xyVector"
|
|
|
561 |
> plot( weights, type = "l", xlab = "height", ylab = "weight" )
|
|
|
562 |
> points( women$height, women$weight )
|
|
|
563 |
> weights
|
|
|
564 |
$x
|
|
|
565 |
[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
|
|
|
566 |
[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
|
|
|
567 |
[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
|
|
|
568 |
[46] 73.0 73.4 73.8 74.2 74.6 75.0
|
|
|
569 |
|
|
|
570 |
$y
|
|
|
571 |
[1] 109.8042 110.4970 111.1898 111.8825 112.5753 113.2681 113.9608 114.6536
|
|
|
572 |
[9] 115.3485 116.0971 117.0000 118.1214 119.3694 120.6168 121.8162 123.0000
|
|
|
573 |
[17] 124.1961 125.3995 126.5980 127.7930 129.0000 130.2237 131.4243 132.5557
|
|
|
574 |
[25] 133.6865 135.0000 136.6001 138.2531 139.6541 140.8021 142.0000 143.4815
|
|
|
575 |
[33] 145.1507 146.8334 148.4468 150.0000 151.5249 153.1289 154.9329 156.9329
|
|
|
576 |
[41] 159.0000 161.0269 163.0134 164.9860 166.9580 168.9301 170.9021 172.8741
|
|
|
577 |
[49] 174.8461 176.8181 178.7902
|
|
|
578 |
|
|
|
579 |
attr(,"class")
|
|
|
580 |
[1] "xyVector"
|
|
|
581 |
>
|
|
|
582 |
>
|
|
|
583 |
>
|
|
|
584 |
> ### * <FOOTER>
|
|
|
585 |
> ###
|
| 73819 |
hornik |
586 |
> cleanEx()
|
| 62439 |
ripley |
587 |
> options(digits = 7L)
|
| 61787 |
ripley |
588 |
> base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n")
|
| 88673 |
hornik |
589 |
Time elapsed: 0.152 0.012 0.164 0 0
|
| 56186 |
murdoch |
590 |
> grDevices::dev.off()
|
|
|
591 |
null device
|
|
|
592 |
1
|
|
|
593 |
> ###
|
|
|
594 |
> ### Local variables: ***
|
|
|
595 |
> ### mode: outline-minor ***
|
|
|
596 |
> ### outline-regexp: "\\(> \\)?### [*]+" ***
|
|
|
597 |
> ### End: ***
|
|
|
598 |
> quit('no')
|