Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2019-fall/materials/worksheet_07/tests_worksheet_07.R
2707 views
1
library(testthat)
2
library(digest)
3
4
test_0.1 <- function(){
5
test_that('Solution is incorrect', {
6
expect_that(exists('answer1'), is_true())
7
expect_equal(digest(answer1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
8
9
})
10
print("Success!")
11
}
12
13
test_1.0 <- function(){
14
test_that('Did not create an object named fruit_data', {
15
expect_true(exists("fruit_data"))
16
})
17
test_that('fruit_data does not contain the correct number of rows and/or columns.', {
18
expect_equal(dim(fruit_data), c(59, 7))
19
})
20
test_that('The fruit_name column in fruit_data should be of class factor.', {
21
expect_true(is.factor(fruit_data$fruit_name))
22
})
23
test_that('Columns in fruit_data contain incorrect values.', {
24
expect_equal(digest(as.numeric(sum(fruit_data$mass, na.rm = TRUE))), '351bee4c830e886e0f3a17749569fb33') # we hid the answer to the test here so you can't see it, but we can still run the test
25
})
26
print("Success!")
27
}
28
29
test_1.1 <- function(){
30
test_that('Solution is incorrect', {
31
expect_that(exists('answer1.1'), is_true())
32
expect_equal(digest(answer1.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
33
34
})
35
print("Success!")
36
}
37
38
test_1.2 <- function(){
39
test_that('Did not create an object named fruit_dist_2', {
40
expect_true(exists("fruit_dist_2"))
41
})
42
test_that('fruit_dist_2 should be a distance.', {
43
expect_true('dist' %in% class(fruit_dist_2))
44
})
45
test_that('fruit_dist_2 is incorrect.', {
46
expect_equal(digest(fruit_dist_2), 'bc63ef43e7c7349f09aca14a55e2c9c0')
47
})
48
print("Success!")
49
}
50
51
test_1.3 <- function(){
52
test_that('Did not create an object named fruit_dist_44', {
53
expect_true(exists("fruit_dist_44"))
54
})
55
test_that('fruit_dist_44 should be a distance.', {
56
expect_true('dist' %in% class(fruit_dist_44))
57
})
58
test_that('fruit_dist_44 is incorrect.', {
59
expect_equal(digest(fruit_dist_44), 'd775055ad9a05350f4548e1dbe872297')
60
})
61
print("Success!")
62
}
63
64
65
test_1.5 <- function(){
66
test_that('Did not create an object named fruit_data', {
67
expect_true(exists("fruit_data"))
68
})
69
test_that('fruit_data does not contain the correct number of rows and/or columns.', {
70
expect_equal(dim(fruit_data), c(59, 11))
71
})
72
test_that('The fruit_name column in fruit_data should be of class factor.', {
73
expect_true(is.factor(fruit_data$fruit_name))
74
})
75
test_that('Columns in fruit_data contain incorrect values.', {
76
expect_equal(digest(as.numeric(sum(fruit_data$mass, na.rm = TRUE))), '351bee4c830e886e0f3a17749569fb33') # we hid the answer to the test here so you can't see it, but we can still run the test
77
})
78
test_that('The mass, height, color score, and width columns in fruit_data should be scaled.', {
79
expect_equal(fruit_data$scaled_mass, scale(fruit_data$mass, center = TRUE))
80
expect_equal(fruit_data$scaled_color_score, scale(fruit_data$color_score, center = TRUE))
81
expect_equal(fruit_data$scaled_height, scale(fruit_data$height, center = TRUE))
82
expect_equal(fruit_data$scaled_width, scale(fruit_data$width, center = TRUE))
83
})
84
print("Success!")
85
}
86
87
test_1.6 <- function(){
88
test_that('Did not create an object named distance_44', {
89
expect_true(exists("distance_44"))
90
})
91
test_that('Did not create an object named distance_2', {
92
expect_true(exists("distance_2"))
93
})
94
test_that('distance_44 should be a distance.', {
95
expect_true('dist' %in% class(distance_44))
96
})
97
test_that('distance_2 should be a distance.', {
98
expect_true('dist' %in% class(distance_2))
99
})
100
test_that('distance_44 is incorrect.', {
101
expect_equal(digest(distance_44), '8b4aad1050358e59d907ad361c4eb9fb')
102
})
103
test_that('distance_2 is incorrect.', {
104
expect_equal(digest(distance_2), '5c276c2a08b1d63009a6574aefd55212')
105
})
106
print("Success!")
107
}
108
109
test_2.0 <- function(){
110
test_that('Did not create an object named training_rows', {
111
expect_true(exists("training_rows"))
112
})
113
test_that('training_rows does not contain the correct number of rows and/or columns.', {
114
expect_equal(dim(training_rows), c(46, 1))
115
})
116
test_that('training_rows should only contain fruit names and should be a matrix', {
117
expect_true(is.matrix(training_rows))
118
})
119
print("Success!")
120
}
121
122
123
test_2.1 <- function(){
124
test_that('Did not create an object named training_set', {
125
expect_true(exists("training_set"))
126
})
127
test_that('training_set should contain all the columns in fruit_data and only the rows in training_rows.', {
128
expect_equal(dim(training_set), c(46, 11))
129
})
130
test_that('Did not create an object named testing_set', {
131
expect_true(exists("testing_set"))
132
})
133
test_that('testing_set should contain all the columns in fruit_data and the rows not in training_rows.', {
134
expect_equal(dim(testing_set), c(13, 11))
135
})
136
print("Success!")
137
}
138
139
test_2.2 <- function(){
140
test_that('Did not create an object named X_simple', {
141
expect_true(exists("X_simple"))
142
})
143
test_that('X_simple should be a data frame.', {
144
expect_true('data.frame' %in% class(X_simple))
145
})
146
test_that('X_simple does not contain the correct number of rows and/or columns.', {
147
expect_equal(dim(X_simple), c(46, 2))
148
})
149
test_that('X_simple does not contain the column(s) scaled_color_score and/or scaled_mass', {
150
expect_true('scaled_color_score' %in% colnames(X_simple))
151
expect_true('scaled_mass' %in% colnames(X_simple))
152
})
153
test_that('Did not create an object named Y_fruit', {
154
expect_true(exists("Y_fruit"))
155
})
156
test_that('Y_fruit should be a factor', {
157
expect_true('factor' %in% class(Y_fruit))
158
})
159
test_that('Y_fruit is not the correct length.', {
160
expect_equal(length(Y_fruit), 46)
161
})
162
print("Success!")
163
}
164
165
test_2.3 <- function(){
166
test_that('Did not create an object named ks', {
167
expect_true(exists("ks"))
168
})
169
test_that('ks should be a data frame.', {
170
expect_true('data.frame' %in% class(ks))
171
})
172
test_that('ks should contain a vector containing all the odd numbers from 1 to 11.', {
173
expect_equal(as.numeric(sum(ks)), 36)
174
})
175
print("Success!")
176
}
177
178
test_2.4 <- function(){
179
test_that('Did not create an object named train_control', {
180
expect_true(exists("train_control"))
181
})
182
test_that('method should be cv (for cross-validation)', {
183
expect_equal(train_control$method, 'cv')
184
})
185
test_that('train_control should be set up for a 10-fold cross-validation (number should be 10))', {
186
expect_equal(train_control$number, 10)
187
})
188
print("Success!")
189
}
190
191
test_2.5 <- function(){
192
test_that('Did not create an object named choose_k', {
193
expect_true(exists("choose_k"))
194
})
195
test_that('method should be knn', {
196
expect_equal(as.character(choose_k$method), 'knn')
197
})
198
test_that('k should be chosen from ks', {
199
expect_equal(as.numeric(sum(choose_k$results$k)), 36)
200
})
201
test_that('choose_k should use 10 fold cross-validation.', {
202
expect_equal(choose_k$control$method, 'cv')
203
expect_equal(choose_k$control$number, 10)
204
})
205
print("Success!")
206
}
207
208
209
test_2.6 <- function(){
210
properties <- c(choose_k_plot$layers[[1]]$mapping, choose_k_plot$layers[[2]]$mapping, choose_k_plot$mapping)
211
labels <- choose_k_plot$labels
212
test_that('Did not create a plot named choose_k_plot', {
213
expect_true(exists("choose_k_plot"))
214
})
215
test_that('choose_k_plot is not using information from k_accuracies', {
216
expect_equal(choose_k_plot$data, k_accuracies)
217
})
218
test_that('k should be on the x-axis.', {
219
expect_that("k" == rlang::get_expr(properties$x), is_true())
220
})
221
test_that('Accuracy should be on the y-axis.', {
222
expect_that("Accuracy" == rlang::get_expr(properties$y), is_true())
223
})
224
test_that('choose_k_plot should have both a line geometric object and point geometric object.', {
225
expect_that('GeomPoint' %in% c(class(rlang::get_expr(choose_k_plot$layers[[1]]$geom)), class(rlang::get_expr(choose_k_plot$layers[[2]]$geom))), is_true())
226
expect_that('GeomLine' %in% c(class(rlang::get_expr(choose_k_plot$layers[[1]]$geom)), class(rlang::get_expr(choose_k_plot$layers[[2]]$geom))), is_true())
227
})
228
print("Success!")
229
}
230
231
test_2.7 <- function(){
232
test_that('Solution is incorrect', {
233
expect_true(answer2.7 == 3)
234
})
235
print("Success!")
236
}
237
238
test_2.8 <- function(){
239
test_that('Solution is incorrect', {
240
expect_true(answer2.8 > 0.9)
241
expect_true(answer2.8 < 0.95)
242
})
243
print("Success!")
244
}
245
246
test_3.0 <- function(){
247
test_that('method should be knn', {
248
expect_equal(as.character(simple$method), 'knn')
249
})
250
test_that('k should be 3', {
251
expect_equal(as.numeric(simple$results$k), 3)
252
})
253
test_that('simple should not use cross-validation.', {
254
expect_equal(simple$control$method, 'boot')
255
expect_equal(simple$control$number, 25)
256
})
257
print("Success!")
258
}
259
260
test_3.1 <- function(){
261
test_that('training_pred prediction is incorrect.', {
262
expect_equal(length(training_pred), 46)
263
expect_equal(class(training_pred), 'factor')
264
expect_equal(c("apple", "lemon", "mandarin", "orange") %in% levels(training_pred), c(TRUE, TRUE, TRUE, TRUE))
265
expect_equal(length(training_pred[training_pred == "apple"]), 15)
266
expect_equal(length(training_pred[training_pred == "mandarin"]), 4)
267
expect_equal(length(training_pred[training_pred == "orange"]), 14)
268
expect_equal(length(training_pred[training_pred == "lemon"]), 13)
269
})
270
print("Success!")
271
}
272
273
test_3.2 <- function(){
274
test_that('Solution is incorrect', {
275
expect_equal(class(training_results), 'confusionMatrix')
276
expect_equal(c("apple", "lemon", "mandarin", "orange") %in% colnames(training_results$table), c(TRUE, TRUE, TRUE, TRUE))
277
expect_equal(round(training_results$overall[[1]], 2), 0.93)
278
})
279
print("Success!")
280
}
281
282
test_3.3 <- function(){
283
test_that('Solution is incorrect', {
284
expect_true(answer3.3 > 0.92)
285
expect_true(answer3.3 < 0.94)
286
})
287
print("Success!")
288
}
289
290
291
292
test_4.0 <- function(){
293
test_that('Did not create an object named X_complex', {
294
expect_true(exists("X_complex"))
295
})
296
test_that('X_complex should be a data frame.', {
297
expect_true('data.frame' %in% class(X_complex))
298
})
299
test_that('X_complex does not contain the correct columns.', {
300
expect_that("scaled_height" %in% colnames(X_complex), is_true())
301
expect_that("scaled_width" %in% colnames(X_complex), is_true())
302
expect_that("scaled_mass" %in% colnames(X_complex), is_true())
303
expect_that("scaled_color_score" %in% colnames(X_complex), is_true())
304
})
305
test_that('Did not create an object named complex', {
306
expect_true(exists("complex"))
307
})
308
test_that('x in complex should be X_complex', {
309
expect_equal(as.character(complex$call$x), 'X_complex')
310
})
311
test_that('method should be knn', {
312
expect_equal(as.character(complex$method), 'knn')
313
})
314
test_that('k should be chosen from ks', {
315
expect_equal(as.numeric(sum(complex$results$k)), 36)
316
})
317
test_that('complex should use 10 fold cross-validation.', {
318
expect_equal(complex$control$method, 'cv')
319
expect_equal(complex$control$number, 10)
320
})
321
print("Success!")
322
}
323
324
test_4.1 <- function(){
325
test_that('Did not create an object named k_accuracies_again', {
326
expect_true(exists("k_accuracies_again"))
327
})
328
test_that('k_accuracies_again contains incorrect information.', {
329
expect_equal(k_accuracies_again, complex$results %>% select(k, Accuracy))
330
})
331
test_that('Did not create a plot named choose_k_again_plot', {
332
expect_true(exists("choose_k_again_plot"))
333
})
334
test_that('choose_k_again_plot is not using information from k_accuracies_again', {
335
expect_equal(choose_k_again_plot$data, k_accuracies_again)
336
})
337
test_that('k should be on the x-axis.', {
338
expect_that("k" %in% c(rlang::get_expr(choose_k_again_plot$mapping$x),rlang::get_expr(choose_k_again_plot$layers[[1]]$mapping$x)), is_true())
339
})
340
test_that('Accuracy should be on the y-axis.', {
341
expect_that("Accuracy" %in% c(rlang::get_expr(choose_k_again_plot$mapping$y), rlang::get_expr(choose_k_again_plot$layers[[1]]$mapping$y)) , is_true())
342
})
343
test_that('choose_k_again_plot should be a scatter plot and a line plot.', {
344
expect_that('GeomPoint' %in% c(class(rlang::get_expr(choose_k_again_plot$layers[[1]]$geom)), class(rlang::get_expr(choose_k_again_plot$layers[[2]]$geom))), is_true())
345
expect_that('GeomLine' %in% c(class(rlang::get_expr(choose_k_again_plot$layers[[1]]$geom)), class(rlang::get_expr(choose_k_again_plot$layers[[2]]$geom))), is_true())
346
})
347
print("Success!")
348
}
349
350
test_4.2 <- function(){
351
test_that('Solution is incorrect', {
352
expect_equal(answer4.2, 5)
353
})
354
print("Success!")
355
}
356
357
test_4.3 <- function(){
358
test_that('Solution is incorrect', {
359
expect_true(answer4.3 > 0.97)
360
expect_true(answer4.3 < 1)
361
})
362
print("Success!")
363
}
364
365
test_4.4 <- function(){
366
test_that('Solution is incorrect', {
367
expect_equal(digest(answer4.4), '96c24a598c808db5ff9c1aa505c6aa15')
368
})
369
print("Success!")
370
}
371
372
373
test_5.0 <- function(){
374
test_that('Did not create an object named final_k', {
375
expect_true(exists("final_k"))
376
})
377
test_that('final_k should be a data frame.', {
378
expect_true('data.frame' %in% class(final_k))
379
})
380
test_that('Did not create an object named final_classifier', {
381
expect_true(exists("final_classifier"))
382
})
383
test_that('x in the final_classifier should be X_complex', {
384
expect_equal(as.character(final_classifier$call$x), 'X_complex')
385
})
386
test_that('method should be knn', {
387
expect_equal(as.character(final_classifier$method), 'knn')
388
})
389
test_that('k should be 5', {
390
expect_equal(as.numeric(sum(final_classifier$results$k)), 5)
391
})
392
test_that('final_classifier should not be using cross-validation.', {
393
expect_equal(final_classifier$control$method, 'boot')
394
expect_equal(final_classifier$control$number, 25)
395
})
396
print("Success!")
397
}
398
399
test_5.1 <- function(){
400
test_that('Did not create an object named X_test', {
401
expect_true(exists("X_test"))
402
})
403
test_that('X_test should be a data frame.', {
404
expect_true('data.frame' %in% class(X_test))
405
})
406
test_that('X_test does not contain the correct number of rows and/or columns.', {
407
expect_equal(dim(X_test), c(13, 4))
408
})
409
test_that('X_test does not contain the column(s) scaled_mass, scaled_width, scaled_height, and/or scaled_color_score', {
410
expect_true('scaled_color_score' %in% colnames(X_test))
411
expect_true('scaled_mass' %in% colnames(X_test))
412
expect_true('scaled_width' %in% colnames(X_test))
413
expect_true('scaled_height' %in% colnames(X_test))
414
})
415
test_that('Did not create an object named Y_test', {
416
expect_true(exists("Y_test"))
417
})
418
test_that('Y_fruit should be a factor', {
419
expect_true('factor' %in% class(Y_fruit))
420
})
421
test_that('Y_fruit is not the correct length.', {
422
expect_equal(length(Y_test), 13)
423
})
424
test_that('test_results should be a confusion matrix.', {
425
expect_equal(class(test_results), 'confusionMatrix')
426
})
427
test_that('test_results does not contain correct information.', {
428
expect_equal(c("apple", "lemon", "mandarin", "orange") %in% colnames(test_results$table), c(TRUE, TRUE, TRUE, TRUE))
429
})
430
test_that('prediction accuracy is not correct', {
431
expect_equal(round(test_results$overall[[1]], digits=2), 1.)
432
})
433
print("Success!")
434
}
435
436
test_5.2 <- function(){
437
test_that('Solution is incorrect', {
438
expect_true(answer5.2 > .95)
439
expect_true(answer5.2 < 1.05)
440
})
441
print("Success!")
442
}
443