The R Project SVN R

Rev

Rev 62864 | Rev 63381 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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