File: test-mSpline.R

package info (click to toggle)
r-cran-splines2 0.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,020 kB
  • sloc: cpp: 3,232; sh: 13; makefile: 2
file content (429 lines) | stat: -rw-r--r-- 16,647 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
## get implementations of v0.2.8 for reference
v2 <- new.env()
source("../v0.2.8.R", v2)
source("utils.R")

## helper functions
isNumMatrix <- v2$isNumMatrix

### 1. check correctness first
x <- c(NA, seq.int(0, 0.5, 0.1), NA, seq.int(0.6, 1, 0.1), NA)
knots <- c(0.25, 0.5, 0.75)
x2 <- c(- 1, 2, x)
b_knots <- c(0, 1)

## default cubic splines without internal knots
expect_eqt(mSpline(x), v2$mSpline(x))

## cubic splines with specified df
expect_eqt(mSpline(x, df = 5),
           v2$mSpline(x, df = 5))

## cubic splines with specified internal knots
expect_eqt(mSpline(x, knots = knots),
           v2$mSpline(x, knots = knots))

## qudractic splines without internal knots
expect_eqt(mSpline(x, degree = 2L),
           v2$mSpline(x, degree = 2L))

## complete basis with intercept
expect_eqt(mSpline(x, intercept = TRUE),
           v2$mSpline(x, intercept = TRUE))

## specified knots
expect_eqt(mSpline(x, knots = knots, intercept = TRUE),
           v2$mSpline(x, knots = knots, intercept = TRUE))

## specified df
expect_eqt(mSpline(x, df = 6, intercept = TRUE),
           v2$mSpline(x, df = 6, intercept = TRUE))

## for testing splines with degree zero
msMat0a <- mSpline(x, degree = 0, intercept = TRUE)
msMat0b <- mSpline(x, knots = knots, degree = 1)
msMat0b2 <- mSpline(x, knots = knots, degree = 1, derivs = 1, integral = TRUE)
isMat0a <- mSpline(x, knots = knots, degree = 1, integral = TRUE)
msMat0c <- mSpline(x, knots = knots, degree = 1, intercept = TRUE)
msMat0d <- mSpline(x, knots = knots, degree = 2)
msMat0e <- mSpline(x, knots = knots, degree = 2, intercept = TRUE)
msMat0f <- mSpline(0.1, knots = knots, degree = 2, intercept = TRUE,
                   Boundary.knots = c(0, 1), derivs = 1)
msMat0g <- mSpline(x, df = 6, degree = 2, intercept = TRUE,
                   Boundary.knots = c(0, 1), derivs = 2)
msMat0h <- mSpline(0.1, knots = knots, degree = 2,
                   Boundary.knots = c(0, 1), derivs = 3)
expect_true(isNumMatrix(msMat0a, 14L, 1L))
expect_equal(sum(is.na(msMat0b)), 12L) # keep NA's as is
expect_eqt(msMat0b, msMat0b2)
expect_eqt(deriv(isMat0a), msMat0b2)
expect_true(isNumMatrix(msMat0b, 14L, 4L))
expect_true(isNumMatrix(msMat0c, 14L, 5L))
expect_true(isNumMatrix(msMat0d, 14L, 5L))
expect_true(isNumMatrix(msMat0e, 14L, 6L))
expect_true(isNumMatrix(msMat0f, 1L, 6L))
expect_true(isNumMatrix(msMat0g, 14L, 6L))
expect_true(isNumMatrix(msMat0h, 1L, 5L))
expect_error(mSpline(x, degree = 0))
expect_warning(mSpline(c(x, 10), knots = knots, degree = 0,
                       Boundary.knots = c(0, 1)),
               "beyond boundary knots")

## intercept = FALSE
msMat0f <- mSpline(0.1, knots = knots, degree = 2,
                   Boundary.knots = c(0, 1), derivs = 1)
msMat0g <- mSpline(x, df = 6, degree = 2,
                   Boundary.knots = c(0, 1), derivs = 2)
expect_true(isNumMatrix(msMat0f, 1, length(knots) + 2))
expect_true(isNumMatrix(msMat0g, length(x), 6))

## true closed-form formula given the all knots and degree
## transformation of constant basis
x3 <- seq.int(0, 7, 0.1)
m0_1 <- function(x) as.numeric(x < 1)
m0_2 <- function(x) as.numeric(x >= 1 & x < 3) * 0.5
m0_3 <- function(x) as.numeric(x >= 3 & x <= 7) * 0.25
expect_eqt(mSpline(x3, knots = c(1, 3), degree = 0L, intercept = TRUE),
           cbind(m0_1(x3), m0_2(x3), m0_3(x3)))

## transformation of linear basis
x4 <- seq.int(0, 3, 0.1)
ind01 <- function(x) as.numeric(x >= 0 & x < 1)
ind12 <- function(x) as.numeric(x >= 1 & x < 2)
ind23 <- function(x) as.numeric(x >= 2 & x <= 3)
m0_1 <- function(x) ind01(x) * (2 - 2 * x)
m0_2 <- function(x) ind01(x) * x + ind12(x) * (2 - x)
m0_3 <- function(x) ind12(x) * (x - 1) + ind23(x) * (3 - x)
m0_4 <- function(x) ind23(x) * (2 * x - 4)
expect_eqt(mSpline(x4, knots = c(1, 2), degree = 1L, intercept = TRUE),
           cbind(m0_1(x4), m0_2(x4), m0_3(x4), m0_4(x4)))

## x outside of boundary
suppressWarnings({
    expect_eqt(
        mSpline(x2, df = 6, degree = 3, Boundary.knots = b_knots),
        v2$mSpline(x2, df = 6, degree = 3, Boundary.knots = b_knots)
    )
})
suppressWarnings({
    expect_eqt(
        mSpline(x2, knots = knots, degree = 3, Boundary.knots = b_knots),
        v2$mSpline(x2, knots = knots, degree = 3, Boundary.knots = b_knots)
    )
})

## keep names of x
names(x) <- sample(LETTERS, length(x), replace = TRUE)
expect_equal(rownames(bSpline(x)), names(x))

### 2. check designed features with expectation
## NA is only allowed in x

## error if all of x are NA's
expect_error(mSpline(c(NA_real_, NA_real_), degree = 0))
expect_error(mSpline(c(NA, NA), df = 5))

## error if degree has NA or negative
expect_error(mSpline(x, degree = - 1))
expect_error(mSpline(x, degree = NA))

## error if df has NA or negative
expect_error(mSpline(x, df = - 1))
expect_error(mSpline(x, df = NA))

## error if knots has NA
expect_error(mSpline(x, knots = c(0.1, 0.5, NA)))
expect_error(mSpline(x, Boundary.knots = c(0.1, 0.5, NA)))

## error if boundary knots are inappropriate
expect_error(mSpline(x, Boundary.knots = 0.1))
expect_error(mSpline(x, Boundary.knots = c(0.1, 0.1)))
expect_error(mSpline(x, Boundary.knots = c(0.1, 0.5, 1)))

## error if empty matrix
expect_true(isNumMatrix(mSpline(x, degree = 0, intercept = TRUE),
                        length(x), 1))
expect_error(mSpline(x, degree = 0))
expect_error(mSpline(x, degree = 0, derivs = 1))

## error if any internal knot is placed outside boundary
expect_error(mSpline(x, knots = c(- 0.1, 0.5), degree = 0))

## warning if any x outside of boundary
expect_warning(mSpline(c(x, 10), knots = knots, degree = 0,
                       Boundary.knots = c(0, 1)))
expect_warning(mSpline(c(x, 10), knots = knots, degree = 3,
                       Boundary.knots = c(0, 1)))

### 3. periodic M-splines
## 3.1. with specified df
x <- c(seq.int(0, 3, 0.01), NA, seq.int(3, 4, 0.1), NA)
b_knots <- c(0, 1)

is_in <- function(x, a = 0, b = 1) {
    x >= a & x <= b
}

## without specified boundary knots
tmp <- mSpline(x, df = 8, degree = 2, periodic = TRUE)
expect_equal(attr(tmp, "Boundary.knots"), range(x, na.rm = TRUE))
expect_equal(length(attr(tmp, "knots")), 8)

## intercept = TRUE
## basis
res0 <- mSpline(x, df = 6, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots, periodic = TRUE)
tmp <- mSpline(x, df = 6, degree = 3, intercept = TRUE,
               Boundary.knots = b_knots, periodic = TRUE,
               derivs = 1, integral = TRUE)
expect_equal(res0[1, ], res0[nrow(res0) - 1, ])
expect_true(isNumMatrix(res0, length(x), 6))
expect_eqt(matrix(predict(res0, 0.25), nrow = 4,
                  ncol = ncol(res0), byrow = TRUE),
           predict(res0, 0.25 + 1:4))
expect_true(all(is_in(attr(res0, "knots"), 0, 1)))
expect_eqt(res0, tmp)

## first derivatives
res1 <- mSpline(x, df = 6, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 1)
expect_equal(res1[1, ], res1[nrow(res1) - 1, ])
expect_true(isNumMatrix(res1, length(x), 6))
expect_true(all(is_in(attr(res1, "knots"), 0, 1)))
expect_eqt(deriv(res0), res1)
expect_eqt(deriv(tmp), res1)
expect_eqt(matrix(predict(res1, 0.25), nrow = 4,
                  ncol = ncol(res1), byrow = TRUE),
           predict(res1, 0.25 + 1:4))

## second derivatives
res2 <- mSpline(x, df = 6, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 2)
expect_equal(res2[1, ], res2[nrow(res2) - 1, ])
expect_true(isNumMatrix(res2, length(x), 6))
expect_true(all(is_in(attr(res2, "knots"), 0, 1)))
expect_eqt(deriv(res1), res2)
expect_eqt(deriv(res0, 2), res2)
expect_eqt(deriv(tmp, 2), res2)
expect_eqt(matrix(predict(res2, 0.25), nrow = 4,
                  ncol = ncol(res2), byrow = TRUE),
           predict(res2, 0.25 + 1:4))

## third derivatives
res3 <- mSpline(x, df = 6, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 3)
expect_equal(res3[1, ], res3[nrow(res3) - 1, ])
expect_true(isNumMatrix(res3, length(x), 6))
expect_true(all(is_in(attr(res3, "knots"), 0, 1)))
expect_eqt(deriv(res2), res3)
expect_eqt(deriv(res1, 2), res3)
expect_eqt(deriv(res0, 3), res3)
expect_eqt(deriv(tmp, 3), res3)
expect_eqt(matrix(predict(res3, 0.25), nrow = 4,
                  ncol = ncol(res3), byrow = TRUE),
           predict(res3, 0.25 + 1:4))

## fourth derivatives
res4 <- mSpline(x, df = 6, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 4)
expect_equal(res4[1, ], res4[nrow(res4) - 1, ])
expect_true(isNumMatrix(res4, length(x), 6))
expect_eqt(res4[1, , drop = FALSE],
           matrix(0, ncol = ncol(res4), nrow = 1))

## integrals
res3 <- mSpline(x, df = 6, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots,
                periodic = TRUE, integral = TRUE)
expect_true(isNumMatrix(res3, length(x), 6))
expect_true(all(is_in(attr(res3, "knots"), 0, 1)))
expect_eqt(deriv(res3), res0)
expect_eqt(deriv(res3), tmp)
expect_eqt(deriv(res3, 2), res1)
expect_eqt(deriv(res3, 3), res2)
expect_eqt(matrix(predict(res3, 0.25), byrow = TRUE,
                  nrow = 4, ncol = ncol(res3)) + seq_len(4),
           predict(res3, 0.25 + 1:4))

## intercept = FALSE
## basis
res0 <- mSpline(x, df = 6, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots, periodic = TRUE)
tmp <- mSpline(x, df = 6, degree = 3, intercept = FALSE,
               Boundary.knots = b_knots, periodic = TRUE,
               derivs = 1, integral = TRUE)
expect_equal(res0[1, ], res0[nrow(res0) - 1, ])
expect_eqt(matrix(predict(res0, 0.25), nrow = 4,
                  ncol = ncol(res0), byrow = TRUE),
           predict(res0, 0.25 + 1:4))
expect_eqt(res0, tmp)
expect_true(isNumMatrix(res0, length(x), 6))
expect_true(all(is_in(attr(res0, "knots"), 0, 1)))
## first derivatives
res1 <- mSpline(x, df = 6, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 1)
expect_equal(res1[1, ], res1[nrow(res1) - 1, ])
expect_true(isNumMatrix(res1, length(x), 6))
expect_true(all(is_in(attr(res1, "knots"), 0, 1)))
expect_eqt(deriv(res0), res1)
expect_eqt(deriv(tmp), res1)
expect_eqt(matrix(predict(res1, 0.25), nrow = 4,
                  ncol = ncol(res1), byrow = TRUE),
           predict(res1, 0.25 + 1:4))
## second derivatives
res2 <- mSpline(x, df = 6, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 2)
expect_equal(res2[1, ], res2[nrow(res2) - 1, ])
expect_true(isNumMatrix(res2, length(x), 6))
expect_true(all(is_in(attr(res2, "knots"), 0, 1)))
expect_eqt(deriv(res1), res2)
expect_eqt(deriv(res0, 2), res2)
expect_eqt(deriv(tmp, 2), res2)
expect_eqt(matrix(predict(res2, 0.25), nrow = 4,
                  ncol = ncol(res2), byrow = TRUE),
           predict(res2, 0.25 + 1:4))

## third derivatives
res3 <- mSpline(x, df = 6, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 3)
expect_equal(res3[1, ], res3[nrow(res3) - 1, ])
expect_true(isNumMatrix(res3, length(x), 6))
expect_true(all(is_in(attr(res3, "knots"), 0, 1)))
expect_eqt(deriv(res2), res3)
expect_eqt(deriv(res1, 2), res3)
expect_eqt(deriv(res0, 3), res3)
expect_eqt(deriv(tmp, 3), res3)
expect_eqt(matrix(predict(res3, 0.25), nrow = 4,
                  ncol = ncol(res3), byrow = TRUE),
           predict(res3, 0.25 + 1:4))

## fourth derivatives
res4 <- mSpline(x, df = 6, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 4)
expect_equal(res4[1, ], res4[nrow(res4) - 1, ])
expect_true(isNumMatrix(res4, length(x), 6))
expect_eqt(res4[1, , drop = FALSE],
           matrix(0, ncol = ncol(res4), nrow = 1))

## integrals
res3 <- mSpline(x, df = 6, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots,
                periodic = TRUE, integral = TRUE)
expect_true(isNumMatrix(res3, length(x), 6))
expect_true(all(is_in(attr(res3, "knots"), 0, 1)))
expect_eqt(deriv(res3), res0)
expect_eqt(deriv(res3), tmp)
expect_eqt(deriv(res3, 2), res1)
expect_eqt(deriv(res3, 3), res2)
expect_eqt(matrix(predict(res3, 0.25), byrow = TRUE,
                  nrow = 4, ncol = ncol(res3)) + seq_len(4),
           predict(res3, 0.25 + 1:4))

## 3.2. with specified knots
x <- c(seq.int(0, 3, 0.01), NA, seq.int(3, 4, 0.1), NA)
knots <- c(0.3, 0.6, 0.8)
b_knots <- c(0, 1)

## intercept = TRUE
## basis
res0 <- mSpline(x, knots = knots, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots, periodic = TRUE)
tmp <- mSpline(x, knots = knots, degree = 3, intercept = TRUE,
               Boundary.knots = b_knots, periodic = TRUE,
               derivs = 1, integral = TRUE)
expect_equal(res0[1, ], res0[nrow(res0) - 1, ])
expect_eqt(res0, tmp)
expect_true(isNumMatrix(res0, length(x), length(knots) + 1L))
expect_eqt(matrix(predict(res0, 0.25), nrow = 4,
                  ncol = ncol(res0), byrow = TRUE),
           predict(res0, 0.25 + 1:4))

## first derivatives
res1 <- mSpline(x, knots = knots, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 1)
expect_equal(res1[1, ], res1[nrow(res1) - 1, ])
expect_true(isNumMatrix(res1, length(x), length(knots) + 1L))
expect_eqt(deriv(res0), res1)
expect_eqt(deriv(tmp), res1)
expect_eqt(matrix(predict(res1, 0.25), nrow = 4,
                  ncol = ncol(res1), byrow = TRUE),
           predict(res1, 0.25 + 1:4))

## second derivatives
res2 <- mSpline(x, knots = knots, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 2)
expect_equal(res2[1, ], res2[nrow(res2) - 1, ])
expect_true(isNumMatrix(res2, length(x), length(knots) + 1L))
expect_eqt(deriv(res1), res2)
expect_eqt(deriv(res0, 2), res2)
expect_eqt(deriv(tmp, 2), res2)
expect_eqt(matrix(predict(res2, 0.25), nrow = 4,
                  ncol = ncol(res2), byrow = TRUE),
           predict(res2, 0.25 + 1:4))

## integrals
res3 <- mSpline(x, knots = knots, degree = 3, intercept = TRUE,
                Boundary.knots = b_knots,
                periodic = TRUE, integral = TRUE)
expect_true(isNumMatrix(res3, length(x), length(knots) + 1L))
expect_eqt(deriv(res3), res0)
expect_eqt(deriv(res3), tmp)
expect_eqt(deriv(res3, 2), res1)
expect_eqt(deriv(res3, 3), res2)
expect_eqt(matrix(predict(res3, 0.25), byrow = TRUE,
                  nrow = 4, ncol = ncol(res3)) + seq_len(4),
           predict(res3, 0.25 + 1:4))

## intercept = FALSE
knots <- c(0.2, 0.5, 0.6, 0.75)
## basis
res0 <- mSpline(x, knots = knots, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots, periodic = TRUE)
tmp <- mSpline(x, knots = knots, degree = 3, intercept = FALSE,
               Boundary.knots = b_knots, periodic = TRUE,
               derivs = 1, integral = TRUE)
expect_true(isNumMatrix(res0, length(x), length(knots)))
expect_eqt(res0, tmp)
expect_eqt(matrix(predict(res0, 0.25), nrow = 4,
                  ncol = ncol(res0), byrow = TRUE),
           predict(res0, 0.25 + 1:4))

## first derivatives
res1 <- mSpline(x, knots = knots, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 1)
expect_equal(res1[1, ], res1[nrow(res1) - 1, ])
expect_true(isNumMatrix(res1, length(x), length(knots)))
expect_eqt(deriv(res0), res1)
expect_eqt(deriv(tmp), res1)
expect_eqt(matrix(predict(res1, 0.25), nrow = 4,
                  ncol = ncol(res1), byrow = TRUE),
           predict(res1, 0.25 + 1:4))

## second derivatives
res2 <- mSpline(x, knots = knots, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots, periodic = TRUE, derivs = 2)
expect_equal(res2[1, ], res2[nrow(res2) - 1, ])
expect_true(isNumMatrix(res2, length(x), length(knots)))
expect_eqt(deriv(res1), res2)
expect_eqt(deriv(res0, 2), res2)
expect_eqt(deriv(tmp, 2), res2)
expect_eqt(matrix(predict(res2, 0.25), nrow = 4,
                  ncol = ncol(res2), byrow = TRUE),
           predict(res2, 0.25 + 1:4))

## integrals
res3 <- mSpline(x, knots = knots, degree = 3, intercept = FALSE,
                Boundary.knots = b_knots,
                periodic = TRUE, integral = TRUE)
expect_true(isNumMatrix(res3, length(x), length(knots)))
expect_eqt(deriv(res3), res0)
expect_eqt(deriv(res3), tmp)
expect_eqt(deriv(res3, 2), res1)
expect_eqt(deriv(res3, 3), res2)
expect_eqt(matrix(predict(res3, 0.25), byrow = TRUE,
                  nrow = 4, ncol = ncol(res3)) + seq_len(4),
           predict(res3, 0.25 + 1:4))

### 4. catch errors and warnings for periodic splines
expect_error(mSpline(x, df = 2, periodic = TRUE))
expect_error(mSpline(x, df = 1, degree = 2, periodic = TRUE))