@@ -4,53 +4,38 @@ test_that("single quantile_pred works, quantiles are accessible", {
4
4
quantile_levels = c(.2 , .4 , .5 , .6 , .8 )
5
5
)
6
6
expect_equal(median(z ), 3 )
7
- expect_equal(
8
- quantile(z , c(.2 , .4 , .5 , .6 , .8 )),
9
- hardhat :: quantile_pred(matrix (1 : 5 , nrow = 1 ), c(.2 , .4 , .5 , .6 , .8 ))
10
- )
7
+ expect_equal(quantile(z , c(.2 , .4 , .5 , .6 , .8 )), matrix (1 : 5 , nrow = 1 ))
11
8
expect_equal(
12
9
quantile(z , c(.3 , .7 ), middle = " linear" ),
13
- hardhat :: quantile_pred( matrix (c(1.5 , 4.5 ), nrow = 1 ), c( .3 , .7 ) )
10
+ matrix (c(1.5 , 4.5 ), nrow = 1 )
14
11
)
15
12
16
13
Q <- stats :: splinefun(c(.2 , .4 , .5 , .6 , .8 ), 1 : 5 , method = " hyman" )
17
- expect_equal(quantile(z , c(.3 , .7 ), middle = " cubic " ), Q(c(.3 , .7 )))
14
+ expect_equal(quantile(z , c(.3 , .7 )), Q(c(.3 , .7 )))
18
15
expect_identical(
19
16
extrapolate_quantiles(z , c(.3 , .7 ), middle = " linear" ),
20
- hardhat :: quantile_pred(c(1 , 1.5 , 2 , 3 , 4 , 4.5 , 5 ), 2 : 8 / 10 )
17
+ hardhat :: quantile_pred(matrix ( c(1 , 1.5 , 2 , 3 , 4 , 4.5 , 5 ), nrow = 1 ), 2 : 8 / 10 )
21
18
)
22
- # empty values slot results in a length zero distribution
23
- # see issue #361
24
- # expect_length(dist_quantiles(list(), c(.1, .9)), 0L)
25
- # expect_identical(
26
- # dist_quantiles(list(), c(.1, .9)),
27
- # distributional::dist_degenerate(double())
28
- # )
29
19
})
30
20
31
21
32
22
test_that(" quantile extrapolator works" , {
33
- dstn <- dist_normal(c(10 , 2 ), c(5 , 10 ))
34
- qq <- extrapolate_quantiles(dstn , probs = c(.25 , 0.5 , .75 ))
35
- expect_s3_class(qq , " distribution" )
36
- expect_s3_class(vctrs :: vec_data(qq [1 ])[[1 ]], " dist_quantiles" )
37
- expect_length(parameters(qq [1 ])$ quantile_levels [[1 ]], 3L )
38
-
39
-
40
- dstn <- dist_quantiles(list (1 : 4 , 8 : 11 ), list (c(.2 , .4 , .6 , .8 )))
23
+ dstn <- hardhat :: quantile_pred(
24
+ matrix (c(1 : 4 , 8 : 11 ), nrow = 2 , byrow = TRUE ),
25
+ c(.2 , .4 , .6 , .8 )
26
+ )
41
27
qq <- extrapolate_quantiles(dstn , probs = c(.25 , 0.5 , .75 ))
42
- expect_s3_class(qq , " distribution" )
43
- expect_s3_class(vctrs :: vec_data(qq [1 ])[[1 ]], " dist_quantiles" )
44
- expect_length(parameters(qq [1 ])$ quantile_levels [[1 ]], 7L )
28
+ expect_s3_class(qq , c(" quantile_pred" , " vctrs_vctr" , " list" ))
29
+ expect_length(qq %@ % " quantile_levels" , 7L )
45
30
46
- dstn <- dist_quantiles( 1 : 4 , 1 : 4 / 5 )
31
+ dstn <- hardhat :: quantile_pred( matrix ( 1 : 4 , nrow = 1 ) , 1 : 4 / 5 )
47
32
qq <- extrapolate_quantiles(dstn , 1 : 9 / 10 )
48
- dstn_na <- dist_quantiles( c(1 , 2 , NA , 4 ), 1 : 4 / 5 )
33
+ dstn_na <- hardhat :: quantile_pred( matrix ( c(1 , 2 , NA , 4 ), nrow = 1 ), 1 : 4 / 5 )
49
34
qq2 <- extrapolate_quantiles(dstn_na , 1 : 9 / 10 )
50
35
expect_equal(qq , qq2 )
51
36
qq3 <- extrapolate_quantiles(dstn_na , 1 : 9 / 10 , replace_na = FALSE )
52
- qq2_vals <- field(vec_data( qq2 )[[ 1 ]], " values " )
53
- qq3_vals <- field(vec_data( qq3 )[[ 1 ]], " values " )
37
+ qq2_vals <- unlist( qq2 )
38
+ qq3_vals <- unlist( qq3 )
54
39
qq2_vals [6 ] <- NA
55
40
expect_equal(qq2_vals , qq3_vals )
56
41
})
@@ -60,7 +45,7 @@ test_that("small deviations of quantile requests work", {
60
45
v <- c(0.0890306 , 0.1424997 , 0.1971793 , 0.2850978 , 0.3832912 , 0.4240479 )
61
46
badl <- l
62
47
badl [1 ] <- badl [1 ] - 1e-14
63
- distn <- dist_quantiles( list ( v ), list ( l ) )
48
+ distn <- hardhat :: quantile_pred( matrix ( v , nrow = 1 ), l )
64
49
65
50
# was broken before, now works
66
51
expect_equal(quantile(distn , l ), quantile(distn , badl ))
@@ -69,50 +54,51 @@ test_that("small deviations of quantile requests work", {
69
54
# the smallest (largest) values or we could end up unsorted
70
55
l <- 1 : 9 / 10
71
56
v <- 1 : 9
72
- distn <- dist_quantiles( list ( v ), list ( l ) )
73
- expect_equal(quantile(distn , c(.25 , .75 )), list (c(2.5 , 7.5 )))
74
- expect_equal(quantile(distn , c(.1 , .9 )), list (c(1 , 9 )))
57
+ distn <- hardhat :: quantile_pred( matrix ( v , nrow = 1 ), l )
58
+ expect_equal(quantile(distn , c(.25 , .75 )), matrix (c(2.5 , 7.5 ), nrow = 1 ))
59
+ expect_equal(quantile(distn , c(.1 , .9 )), matrix (c(1 , 9 ), nrow = 1 ))
75
60
qv <- data.frame (q = l , v = v )
76
61
expect_equal(
77
- unlist (quantile(distn , c(.01 , .05 ))),
62
+ drop (quantile(distn , c(.01 , .05 ))),
78
63
tail_extrapolate(c(.01 , .05 ), head(qv , 2 ))
79
64
)
80
65
expect_equal(
81
- unlist (quantile(distn , c(.99 , .95 ))),
66
+ drop (quantile(distn , c(.99 , .95 ))),
82
67
tail_extrapolate(c(.95 , .99 ), tail(qv , 2 ))
83
68
)
84
69
})
85
70
86
71
test_that(" unary math works on quantiles" , {
87
- dstn <- dist_quantiles(list (1 : 4 , 8 : 11 ), list (c(.2 , .4 , .6 , .8 )))
88
- dstn2 <- dist_quantiles(list (log(1 : 4 ), log(8 : 11 )), list (c(.2 , .4 , .6 , .8 )))
72
+ dstn <- hardhat :: quantile_pred(
73
+ matrix (c(1 : 4 , 8 : 11 ), nrow = 2 , byrow = TRUE ),
74
+ 1 : 4 / 5
75
+ )
76
+ dstn2 <- hardhat :: quantile_pred(
77
+ log(matrix (c(1 : 4 , 8 : 11 ), nrow = 2 , byrow = TRUE )),
78
+ 1 : 4 / 5
79
+ )
89
80
expect_identical(log(dstn ), dstn2 )
90
81
91
- dstn2 <- dist_quantiles(list (cumsum(1 : 4 ), cumsum(8 : 11 )), list (c(.2 , .4 , .6 , .8 )))
92
- expect_identical(cumsum(dstn ), dstn2 )
93
82
})
94
83
95
84
test_that(" arithmetic works on quantiles" , {
96
- dstn <- dist_quantiles(list (1 : 4 , 8 : 11 ), list (c(.2 , .4 , .6 , .8 )))
97
- dstn2 <- dist_quantiles(list (1 : 4 + 1 , 8 : 11 + 1 ), list (c(.2 , .4 , .6 , .8 )))
85
+ dstn <- hardhat :: quantile_pred(
86
+ matrix (c(1 : 4 , 8 : 11 ), nrow = 2 , byrow = TRUE ),
87
+ 1 : 4 / 5
88
+ )
89
+ dstn2 <- hardhat :: quantile_pred(
90
+ matrix (c(1 : 4 , 8 : 11 ), nrow = 2 , byrow = TRUE ) + 1 ,
91
+ 1 : 4 / 5
92
+ )
98
93
expect_identical(dstn + 1 , dstn2 )
99
94
expect_identical(1 + dstn , dstn2 )
100
95
101
- dstn2 <- dist_quantiles(list (1 : 4 / 4 , 8 : 11 / 4 ), list (c(.2 , .4 , .6 , .8 )))
96
+ dstn2 <- hardhat :: quantile_pred(
97
+ matrix (c(1 : 4 , 8 : 11 ), nrow = 2 , byrow = TRUE ) / 4 ,
98
+ 1 : 4 / 5
99
+ )
102
100
expect_identical(dstn / 4 , dstn2 )
103
101
expect_identical((1 / 4 ) * dstn , dstn2 )
104
102
105
- expect_snapshot(error = TRUE , sum(dstn ))
106
- expect_snapshot(error = TRUE , suppressWarnings(dstn + distributional :: dist_normal()))
107
- })
108
-
109
- test_that(" quantile.dist_quantile works for NA vectors" , {
110
- distn <- dist_quantiles(
111
- list (c(NA , NA )),
112
- list (1 : 2 / 3 )
113
- )
114
- expect_true(is.na(quantile(distn , p = 0.5 )))
115
- expect_true(is.na(median(distn )))
116
- expect_true(is.na(mean(distn )))
117
- expect_equal(format(distn ), " quantiles(NA)[2]" )
103
+ expect_error(sum(dstn ))
118
104
})
0 commit comments