Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-fall/materials/worksheet_06/tests_worksheet_06.R
2051 views
1
library(testthat)
2
library(digest)
3
library(rlang)
4
5
# Round double to precise integer
6
#
7
# `int_round` works to create an integer corresponding to a number that is
8
# tested up to a particular decimal point of precision. This is useful when
9
# there is a need to compare a numeric value using hashes.
10
#
11
# @param x Double vector of length one.
12
# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.
13
#
14
# @return Integer vector of length one corresponding to a particular decimal point of precision.
15
#
16
# @examples
17
# # to get an integer up to two decimals of precision from 234.56789
18
# int_round(234.56789, 2)
19
#
20
# to get an integer rounded to the hundred digit from 234.56789
21
# int_round(234.56789, -2)
22
int_round <- function(x, digits){
23
x = x * 10^digits
24
xint = as.integer(x)
25
xint1 = xint + 1L
26
if (abs(xint - x) < abs(xint1 - x)){
27
return(xint)
28
}
29
else {
30
return(xint1)
31
}
32
}
33
34
test_0.1 <- function(){
35
test_that('Solution is incorrect', {
36
expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
37
})
38
print("Success!")
39
}
40
41
test_0.2 <- function(){
42
test_that('Solution is incorrect', {
43
expect_equal(digest(answer0.2), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
44
})
45
print("Success!")
46
}
47
48
test_1.0 <- function(){
49
test_that('Did not create an object named cancer', {
50
expect_true(exists("cancer"))
51
})
52
test_that('cancer should be a data frame.', {
53
expect_true('data.frame' %in% class(cancer))
54
})
55
test_that('cancer does not contain the correct number of rows and/or columns.', {
56
expect_equal(dim(cancer), c(569, 12))
57
})
58
test_that('cancer does not contain the correct data.', {
59
expect_equal(digest(int_round(sum(cancer$Area), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
60
expect_equal(colnames(cancer), c("ID", "Class", "Radius", "Texture", "Perimeter", "Area", "Smoothness", "Compactness", "Concavity", "Concave_points", "Symmetry", "Fractal_dimension"))
61
})
62
test_that('read.csv() instead of read_csv() function is used.', {
63
expect_true(class(cancer$Class) == 'character')
64
})
65
print("Success!")
66
}
67
68
test_1.1 <- function(){
69
if (digest(answer1.1) == '05ca18b596514af73f6880309a21b5dd'){
70
print("Look at the values in the Area column - are they categorical? Remember, classification problems involve predicting class labels for categorical data.")
71
}
72
test_that('Solution is incorrect', {
73
expect_equal(digest(answer1.1), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test
74
})
75
print("Success!")
76
}
77
78
test_1.2 <- function(){
79
properties <- c(cancer_plot$layers[[1]]$mapping, cancer_plot$mapping)
80
labels <- cancer_plot$labels
81
test_that('Did not create a plot named cancer_plot', {
82
expect_true(exists("cancer_plot"))
83
})
84
test_that('Symmetry should be on the x-axis.', {
85
expect_true("Symmetry" == rlang::get_expr(properties$x))
86
})
87
test_that('Radius should be on the y-axis.', {
88
expect_true("Radius" == rlang::get_expr(properties$y))
89
})
90
test_that('Points should be coloured by Class.', {
91
expect_true("Class" == rlang::get_expr(properties$colour))
92
})
93
test_that('cancer_plot should be a scatter plot.', {
94
expect_true("GeomPoint" %in% class(cancer_plot$layers[[1]]$geom))
95
})
96
test_that('cancer_plot should map Class to colour.', {
97
expect_true(digest(rlang::get_expr(properties$colour)) %in% c('a4abb3d43fde633563dd1f5c3ea31f31', 'f9e884084b84794d762a535f3facec85'))
98
})
99
test_that('axis labels do not state that the data is standardized (which it is!)', {
100
expect_true(labels$x != "Symmetry")
101
expect_true(labels$y != "Radius")
102
})
103
print("Success!")
104
}
105
106
test_1.3 <- function(){
107
test_that('Solution is incorrect', {
108
expect_equal(digest(answer1.3), '891e8a631267b478c03e25594808709d') # we hid the answer to the test here so you can't see it, but we can still run the test
109
})
110
print("Success!")
111
}
112
113
test_1.4 <- function(){
114
test_that('xa is incorrect.', {
115
expect_equal(digest(int_round(xa, 2)), 'c0048c4f8677b795155d8aa41e26a54d')
116
})
117
test_that('ya is incorrect.', {
118
expect_equal(digest(int_round(ya, 2)), 'a6e8462a7cace5673e544d1e8d238b52')
119
})
120
test_that('xb is incorrect.', {
121
expect_equal(digest(int_round(xb, 2)), '10aeddd8594c6ce210c731b8b94af435')
122
})
123
test_that('yb is incorrect.', {
124
expect_equal(digest(int_round(yb, 2)), '48139aad2994737e7e801156a24281ed')
125
})
126
print("Success!")
127
}
128
129
test_1.5 <- function(){
130
test_that('answer1.5 is incorrect', {
131
expect_equal(digest(int_round(answer1.5, 2)), 'a95ceee8390cb47bb05410a8d23c76cf') # we hid the answer to the test here so you can't see it, but we can still run the test
132
})
133
print("Success!")
134
}
135
136
test_1.6 <- function(){
137
test_that('Did not create an object named zb', {
138
expect_true(exists('zb'))
139
})
140
test_that('zb is incorrect.', {
141
expect_equal(digest(int_round(zb,2)), 'b78a46ebc0bb9a4cc7f4f4b962f0b2ef')
142
})
143
test_that('Did not create an object named za', {
144
expect_true(exists('za'))
145
})
146
test_that('za is incorrect.', {
147
expect_equal(digest(int_round(za,2)), 'b35d8adab2b7c839e5a8e2861080b03e')
148
})
149
print("Success!")
150
}
151
152
test_1.7 <- function(){
153
test_that('answer1.7 is incorrect', {
154
expect_equal(digest(int_round(answer1.7, 2)), 'c7fd80062a02f15d212704a20fae75fb') # we hid the answer to the test here so you can't see it, but we can still run the test
155
})
156
print("Success!")
157
}
158
159
test_1.8 <- function(){
160
test_that('point_a is incorrect.', {
161
expect_equal(digest(int_round(sum((point_a)),2)), '44014eaa19f1aef8e92b1020c47d662b')
162
})
163
test_that('point_b is incorrect.', {
164
expect_equal(digest(int_round(sum((point_b)),2)), 'e064b40c9ca28b04b874bcd8bdefa41e')
165
})
166
print("Success!")
167
}
168
169
test_1.09 <- function(){
170
test_that('dif_square is incorrect', {
171
expect_equal(digest(int_round(sum(dif_square),2)), 'e276884e43714ac361db1a1998bb6bc9') # we hid the answer to the test here so you can't see it, but we can still run the test
172
})
173
print("Success!")
174
}
175
176
177
test_1.09.1 <- function(){
178
test_that('dif_sum is incorrect', {
179
expect_equal(digest(int_round(dif_sum,2)), 'e276884e43714ac361db1a1998bb6bc9') # we hid the answer to the test here so you can't see it, but we can still run the test
180
})
181
print("Success!")
182
}
183
184
test_1.09.2 <- function(){
185
test_that('root_dif_sum is incorrect', {
186
expect_equal(digest(int_round(root_dif_sum,2)), 'c7fd80062a02f15d212704a20fae75fb') # we hid the answer to the test here so you can't see it, but we can still run the test
187
})
188
print("Success!")
189
}
190
191
test_1.09.3 <- function(){
192
test_that('dist_cancer_two_rows is incorrect', {
193
expect_equal(digest(int_round(dist_cancer_two_rows,2)), 'c7fd80062a02f15d212704a20fae75fb') # we hid the answer to the test here so you can't see it, but we can still run the test
194
195
})
196
print("Success!")
197
}
198
199
200
test_1.09.4 <- function(){
201
test_that('Solution is incorrect', {
202
expect_equal(digest(answer1.09.4), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test
203
204
})
205
print("Success!")
206
}
207
208
test_2.0.0 <- function(){
209
test_that('Did not create an object named small_sample', {
210
expect_true(exists("small_sample"))
211
})
212
test_that('small_sample should be a data frame.', {
213
expect_true('data.frame' %in% class(small_sample))
214
})
215
test_that('small_sample does not contain the correct number of rows and/or columns.', {
216
expect_equal(dim(small_sample), c(5, 3))
217
})
218
test_that('small_sample does not contain the correct columns.', {
219
expect_true('Symmetry' %in% colnames(small_sample))
220
expect_true('Radius' %in% colnames(small_sample))
221
expect_true('Class' %in% colnames(small_sample))
222
})
223
print("Success!")
224
}
225
226
227
test_2.0.1 <- function(){
228
properties <- c(small_sample_plot$layers[[1]]$mapping, small_sample_plot$mapping)
229
labels <- small_sample_plot$labels
230
test_that('Did not create a plot named small_sample_plot', {
231
expect_true(exists("small_sample_plot"))
232
})
233
test_that('Did not use small_sample data to create small_sample_plot', {
234
expect_equal(digest(int_round(sum(small_sample_plot$data$Symmetry),2)), '727b6cd45f0340de38d1cfe8403adb3e')
235
})
236
test_that('Symmetry should be on the x-axis.', {
237
expect_true("Symmetry" == rlang::get_expr(properties$x))
238
})
239
test_that('Radius should be on the y-axis.', {
240
expect_true("Radius" == rlang::get_expr(properties$y))
241
})
242
test_that('small_sample_plot should be a scatter plot.', {
243
expect_true("GeomPoint" %in% c(class(small_sample_plot$layers[[1]]$geom)))
244
})
245
test_that('small_sample_plot should map Benign / Malignant to colour.', {
246
expect_true("Class" == rlang::get_expr(properties$colour))
247
})
248
test_that('axis labels do not state that the data is standardized (which it is!)', {
249
expect_true(labels$x != "Symmetry")
250
expect_true(labels$y != "Radius")
251
})
252
print("Success!")
253
}
254
255
test_2.1 <- function(){
256
test_that('Did not create an object named newData', {
257
expect_true(exists("newData"))
258
})
259
test_that('newData should be a data frame.', {
260
expect_true('data.frame' %in% class(newData))
261
})
262
test_that('The last row of the Class column should be "unknown".', {
263
expect_equal((newData$Class[6]), 'unknown')
264
})
265
test_that('newData does not contain the correct number of rows and/or columns.', {
266
expect_equal(dim(newData), c(6, 3))
267
})
268
test_that('small_sample does not contain the correct data.', {
269
expect_equal(digest(int_round(sum(newData$Radius),2)), '740dbeffda6d0ffb1b86f797df4c2a25')
270
expect_equal(digest(int_round(sum(newData$Symmetry),2)), 'a14e0862232f39bc203a2c5021371b54')
271
})
272
print("Success!")
273
}
274
275
test_2.2 <- function(){
276
test_that('Did not create an object named dist_matrix', {
277
expect_true(exists("dist_matrix"))
278
})
279
test_that('dist_matrix should be a matrix.', {
280
expect_true('matrix' %in% class(dist_matrix))
281
})
282
test_that('dist_matrix does not contain the correct number of rows and/or columns.', {
283
expect_equal(dim(dist_matrix), c(6, 6))
284
})
285
test_that('dist_matrix does not contain the correct data.', {
286
expect_equal(digest(int_round(sum(dist_matrix[1, ]),2)), '8435efbd9cf83356ac4a26c6889c8fa5')
287
expect_equal(digest(int_round(sum(dist_matrix[2, ]),2)), 'abc3f5458b96456d63865f0922593548')
288
expect_equal(digest(int_round(sum(dist_matrix[5, ]),2)), 'c3ad708acb2b90a9e40e48f729083e69')
289
expect_equal(digest(int_round(sum(dist_matrix[6, ]),2)), 'f0f12367a5beee1f65d2633294474dc9')
290
})
291
print("Success!")
292
}
293
294
test_2.3 <- function(){
295
if (typeof(answer2.3) == 'double') {
296
print("Remember to surround your answer with quotes!")
297
}
298
test_that('Solution is incorrect', {
299
expect_equal(digest(answer2.3), 'ee340e888492be0703f2bcc9abfb390c') # we hid the answer to the test here so you can't see it, but we can still run the test
300
})
301
print("Success!")
302
}
303
304
test_2.4 <- function(){
305
test_that('Solution is incorrect', {
306
expect_equal(digest(answer2.4), '891e8a631267b478c03e25594808709d') # we hid the answer to the test here so you can't see it, but we can still run the test
307
})
308
print("Success!")
309
}
310
311
test_2.5 <- function(){
312
test_that('Solution is incorrect', {
313
expect_equal(digest(answer2.5), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
314
})
315
print("Success!")
316
}
317
318
test_2.6 <- function(){
319
test_that('Solution is incorrect', {
320
expect_equal(digest(answer2.6), '9c8cb5538e7778bf0b1bd53e45fb78c9') # we hid the answer to the test here so you can't see it, but we can still run the test
321
322
})
323
print("Success!")
324
}
325
326
test_2.7 <- function(){
327
test_that('Solution is incorrect', {
328
expect_equal(digest(answer2.7), '863dfc36ab2bfe97404cc8fc074a5241') # we hid the answer to the test here so you can't see it, but we can still run the test
329
330
})
331
print("Success!")
332
}
333
334
test_3.1 <- function(){
335
test_that('Did not create an object named knn_spec', {
336
expect_true(exists("knn_spec"))
337
})
338
test_that('k should be 7', {
339
expect_equal(as.numeric(get_expr(knn_spec$args$neighbors)), 7)
340
})
341
test_that('weight_func is incorrect', {
342
expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
343
})
344
test_that('set_engine is incorrect', {
345
expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
346
})
347
test_that('mode is incorrect', {
348
expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
349
})
350
print("Success!")
351
}
352
353
test_3.2 <- function(){
354
test_that('Did not create an object named knn_fit', {
355
expect_true(exists("knn_fit"))
356
})
357
test_that('knn_fit should be a fit model.', {
358
expect_true('model_fit' %in% class(knn_fit))
359
})
360
test_that('knn_fit does not include cancer_train dataset', {
361
expect_equal(digest(as.character(knn_fit$fit$data$Class)), '93ecaae439b9f4e8e4297d3a851929f9')
362
expect_equal(digest(int_round(sum(knn_fit$fit$data$Symmetry),2)), '1473d70e5646a26de3c52aa1abd85b1f')
363
expect_equal(digest(int_round(sum(knn_fit$fit$data$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')
364
})
365
test_that('knn_fit does not contain knn_spec', {
366
expect_equal(digest(int_round(get_expr(knn_fit$spec$args$neighbors),2)), '51465273097370367115dfe0228831f3')
367
expect_equal(digest(as.character(get_expr(knn_fit$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
368
expect_equal(digest(knn_fit$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')
369
})
370
test_that('knn_fit does not use the correct columns and/or the correct model formula', {
371
expect_setequal(c('Class', 'Radius', 'Symmetry'), row.names(attributes(knn_fit$fit$terms)$factors))
372
})
373
print("Success!")
374
}
375
376
test_3.3 <- function(){
377
test_that('Did not create an object named new_obs',{
378
expect_true(exists("new_obs"))
379
})
380
test_that('new_obs is not a tibble', {
381
expect_true('data.frame' %in% class(new_obs))
382
})
383
test_that('Wrong values for Symmetry and Radius', {
384
expect_equal(as.numeric(new_obs$Symmetry), 1)
385
expect_equal(as.numeric(new_obs$Radius), 0)
386
})
387
test_that('Did not create an object named class_prediction',{
388
expect_true(exists("class_prediction"))
389
})
390
test_that('Wrong class prediction', {
391
expect_equal(digest(as.character(class_prediction$.pred_class)), '5f0922939c45ef1054f852e83f91c660')
392
})
393
print("Success!")
394
}
395
396
test_3.4 <- function(){
397
test_that('k should be 7', {
398
expect_equal(int_round(get_expr(knn_spec$args$neighbors),0), 7)
399
})
400
test_that('weight_func is incorrect', {
401
expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
402
})
403
test_that('set_engine is incorrect', {
404
expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
405
})
406
test_that('mode is incorrect', {
407
expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
408
})
409
test_that('Did not create an object named knn_fit_2', {
410
expect_true(exists("knn_fit_2"))
411
})
412
test_that('knn_fit_2 should be a fit model.', {
413
expect_true('model_fit' %in% class(knn_fit_2))
414
})
415
test_that('knn_fit_2 does not use the correct columns and/or the correct model formula', {
416
expect_setequal(c('Class', 'Radius', 'Symmetry', 'Concavity'), row.names(attributes(knn_fit_2$fit$terms)$factors))
417
})
418
test_that('knn_fit_2 does not include knn_train_2 dataset', {
419
expect_equal(digest(as.character(knn_fit_2$fit$data$Class)), '93ecaae439b9f4e8e4297d3a851929f9')
420
expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Symmetry),2)), '1473d70e5646a26de3c52aa1abd85b1f')
421
expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')
422
expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Concavity),2)), '1473d70e5646a26de3c52aa1abd85b1f')
423
})
424
test_that('knn_fit_2 does not contain knn_spec_2', {
425
expect_equal(digest(as.numeric(get_expr(knn_fit_2$spec$args$neighbors))), '90a7653d717dc1553ee564aa27b749b9')
426
expect_equal(digest(as.character(get_expr(knn_fit_2$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
427
expect_equal(digest(knn_fit_2$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')
428
})
429
test_that('Did not create an object named new_obs_2',{
430
expect_true(exists("new_obs_2"))
431
})
432
test_that('new_obs_2 is not a tibble', {
433
expect_true('data.frame' %in% class(new_obs_2))
434
})
435
test_that('Wrong values for Symmetry, Radius, and Concavity', {
436
expect_equal(int_round(new_obs_2$Symmetry, 0), 1)
437
expect_equal(int_round(new_obs_2$Radius, 0), 0)
438
expect_equal(int_round(new_obs_2$Concavity, 0), 1)
439
})
440
test_that('Did not create an object named class_prediction_2',{
441
expect_true(exists("class_prediction_2"))
442
})
443
test_that('Wrong class prediction', {
444
expect_equal(digest(as.character(class_prediction_2$.pred_class)), '5f0922939c45ef1054f852e83f91c660')
445
})
446
print("Success!")
447
}
448
449
test_3.5 <- function(){
450
test_that('Did not create a object named knn_recipe', {
451
expect_true(exists("knn_recipe"))
452
})
453
test_that('knn_recipe is not a recipe object', {
454
expect_equal(digest(class(knn_recipe)), '4b3ed1334bff94d43e32a36a1f16a2f2')
455
})
456
test_that('knn_recipe does not remove ID', {
457
expect_false("ID" %in% (knn_recipe %>% prep() %>% bake(cancer) %>% colnames()))
458
})
459
test_that('cancer does not contain the correct data.', {
460
expect_equal(dim(bake(prep(knn_recipe), cancer)), c(569,11))
461
})
462
print("Success!")
463
}
464
465
test_3.6 <- function(){
466
test_that('Did not create an object named knn_workflow', {
467
expect_true(exists("knn_workflow"))
468
})
469
test_that('knn_workflow is not a workflow', {
470
expect_true('workflow' %in% class(knn_workflow))
471
})
472
test_that('knn_workflow does not contain the right model specification', {
473
expect_equal(int_round(get_expr(knn_workflow$fit$actions$model$spec$args$neighbors),0), 7)
474
})
475
test_that('Did not add knn_recipe', {
476
expect_true('recipe' %in% class(knn_workflow$pre$actions$recipe$recipe))
477
})
478
test_that('knn_recipe does not contain the cancer dataset', {
479
expect_equal(digest(int_round(sum(knn_workflow$pre$actions$recipe$recipe$template$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')
480
expect_equal(digest(int_round(sum(knn_workflow$pre$actions$recipe$recipe$template$Area),2)), '1473d70e5646a26de3c52aa1abd85b1f')
481
})
482
print("Success!")
483
}
484
485
test_3.7 <- function(){
486
test_that('Did not create an object named class_prediction_all',{
487
expect_true(exists("class_prediction_all"))
488
})
489
test_that('Wrong class prediction', {
490
expect_equal(digest(as.character(class_prediction_all$.pred_class)), '3a5505c06543876fe45598b5e5e5195d')
491
})
492
print("Success!")
493
}
494
495
test_4.0 <- function(){
496
test_that('Solution is incorrect', {
497
expect_equal(digest(as.character(answer4.0)), '75f1160e72554f4270c809f041c7a776')
498
})
499
print("Success!")
500
}
501
502
test_4.1 <- function(){
503
test_that('Solution is incorrect', {
504
expect_equal(digest(as.character(answer4.1)), '475bf9280aab63a82af60791302736f6')
505
})
506
print("Success!")
507
}
508
509