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