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