1
2context("Calculation Tests")
3data(testDF)
4glmModel <- glm(y ~ ., data=testDF, family="binomial")
5Preds <- predict(glmModel, type = 'response')
6
7
8test_that("logLoss returns correct values", {
9
10  expect_equal(logLoss(testDF$y, Preds), 0.1546854, tolerance = .000001)
11  expect_equal(logLoss(testDF$y, Preds, 'poisson'), 0.6910357, tolerance = .000001)
12  expect_equal(logLoss(glmModel), 0.1546854, tolerance = .000001)
13
14})
15
16
17test_that("auc returns correct values", {
18
19  expect_equal(auc(testDF$y, Preds), 0.9872666, tolerance = .000001)
20  expect_equal(auc(c(testDF$y,testDF$y), c(Preds, Preds)), 0.9872666, tolerance = .000001)
21  expect_equal(auc(glmModel), 0.9872666, tolerance = .000001)
22
23})
24
25test_that("gini returns correct values", {
26
27  expect_equal(gini(testDF$y, Preds), 0.9745332, tolerance = .000001)
28  expect_equal(gini(c(testDF$y,testDF$y), c(Preds, Preds)), 0.9745332, tolerance = .000001)
29  expect_equal(gini(glmModel), 0.9745332, tolerance = .000001)
30
31})
32
33
34test_that("rmse returns correct values", {
35
36  expect_equal(rmse(testDF$y, Preds), 0.2188343, tolerance = .000001)
37  expect_equal(rmse(glmModel), 0.2188343, tolerance = .000001)
38
39})
40
41
42test_that("mse returns correct values", {
43
44  expect_equal(mse(testDF$y, Preds), 0.04788846, tolerance = .000001)
45  expect_equal(mse(glmModel), 0.04788846, tolerance = .000001)
46
47})
48
49
50test_that("ppv returns correct values", {
51
52  expect_equal(ppv(testDF$y, Preds, .5), 0.9365079, tolerance = .000001)
53  expect_equal(precision(testDF$y, Preds, .5), 0.9365079, tolerance = .000001)
54
55})
56
57
58test_that("npv returns correct values", {
59
60  expect_equal(npv(testDF$y, Preds, .5), 0.9189189, tolerance = .000001)
61
62})
63
64
65test_that("specificity returns correct values", {
66
67  tempTab <- table(testDF$y, Preds > .5)
68  SPC <- tempTab[1,1]/sum(tempTab[1,])
69
70  expect_equal(specificity(testDF$y, Preds, .5), SPC, tolerance = .000001)
71  expect_equal(tnr(testDF$y, Preds, .5), SPC, tolerance = .000001)
72
73})
74
75
76test_that("sensitivity returns correct values", {
77
78  expect_equal(recall(testDF$y, Preds, .5), 0.9516129, tolerance = .000001)
79  expect_equal(sensitivity(testDF$y, Preds, .5), 0.9516129, tolerance = .000001)
80  expect_equal(tpr(testDF$y, Preds, .5), 0.9516129, tolerance = .000001)
81
82})
83
84test_that("f1 score returns correct values", {
85
86  expect_equal(f1Score(testDF$y, Preds, .5), 0.944, tolerance = .000001)
87
88})
89
90test_that("f1 score and F score agree with beta 1 (default value)", {
91
92  expect_equal(f1Score(testDF$y, Preds, .5), fScore(testDF$y, Preds, .5, 1), tolerance = .000001)
93
94})
95
96test_that("mcc returns correct values", {
97
98  expect_equal(mcc(testDF$y, Preds, .5), 0.8508762, tolerance = .000001)
99
100})
101
102
103test_that("brier returns correct values", {
104
105  expect_equal(brier(testDF$y, Preds), 0.04788846, tolerance = .000001)
106  expect_equal(brier(glmModel), 0.04788846, tolerance = .000001)
107
108})
109
110
111
112test_that("mae returns correct values", {
113
114  expect_equal(mae(testDF$y, Preds), 0.09440662, tolerance = .000001)
115  expect_equal(mae(glmModel), 0.09440662, tolerance = .000001)
116
117})
118
119
120test_that("msle returns correct values", {
121
122  expect_equal(msle(testDF$y, Preds), 0.02318011, tolerance = .000001)
123  expect_equal(msle(glmModel), 0.02318011, tolerance = .000001)
124
125})
126
127
128test_that("rmsle returns correct values", {
129
130  expect_equal(rmsle(testDF$y, Preds), 0.1522501, tolerance = .000001)
131  expect_equal(rmsle(glmModel), 0.1522501, tolerance = .000001)
132
133
134})
135
136
137test_that("rmsle returns correct values", {
138
139  A <- c(rep(1, 63), rep(0, 31))
140  B <- c(rep(1, 61), rep(0, 25), rep(1, 6), rep(0, 2))
141
142  tab <- table(A, B)
143
144  a = tab[2,2]
145  b = tab[2,1]
146  c = tab[1,2]
147  d = tab[1,1]
148
149  marginA = ((a + b)*(a + c))/(a + b + c + d)
150  marginB = ((c + d)*(b + d))/(a + b + c + d)
151
152  Pe = (marginA + marginB)/(a + b + c + d)
153  Po = (a + d)/(a + b + c + d)
154
155  manualKappa = (Po - Pe)/(1 - Pe)
156
157  expect_equal(kappa(A, B), manualKappa, tolerance = .000001)
158
159
160})
161
162
163