Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-spring/materials/worksheet_08/tests_worksheet_08.R
2707 views
1
# +
2
library(testthat)
3
library(digest)
4
library(rlang)
5
6
int_round <- function(x, digits){
7
x = x*10^digits
8
xint = as.integer(x)
9
xint1 = xint + 1L
10
if (abs(xint - x) < abs(xint1 - x)){
11
return(xint)
12
}
13
else {
14
return(xint1)
15
}
16
}
17
# -
18
19
# Round double to precise integer
20
#
21
# `int_round` works to create an integer corresponding to a number that is
22
# tested up to a particular decimal point of precision. This is useful when
23
# there is a need to compare a numeric value using hashes.
24
#
25
# @param x Double vector of length one.
26
# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.
27
#
28
# @return Integer vector of length one corresponding to a particular decimal point of precision.
29
#
30
# @examples
31
# # to get an integer up to two decimals of precision from 234.56789
32
# int_round(234.56789, 2)
33
#
34
# to get an integer rounded to the hundred digit from 234.56789
35
# int_round(234.56789, -2)
36
37
test_0.0 <- function(){
38
test_that('Solution is incorrect', {
39
expect_equal(digest(answer0.0), '3a5505c06543876fe45598b5e5e5195d')
40
})
41
print("Success!")
42
}
43
44
test_0.1 <- function(){
45
test_that('Solution is incorrect', {
46
expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6')
47
})
48
print("Success!")
49
}
50
51
test_0.2 <- function(){
52
test_that('Solution is incorrect', {
53
expect_equal(digest(int_round(answer0.2, 2)), '6953b334169bd7ec7da1c1eda5aaf6a5')
54
})
55
print("Success!")
56
}
57
58
test_0.3 <- function(){
59
test_that('Solution is incorrect', {
60
expect_equal(digest(answer0.3), '75f1160e72554f4270c809f041c7a776')
61
})
62
print("Success!")
63
}
64
65
test_1.0 <- function(){
66
test_that('Did not create an object named marathon', {
67
expect_true(exists("marathon"))
68
})
69
test_that('marathon should be a tibble.', {
70
expect_true('tbl' %in% class(marathon))
71
})
72
test_that('marathon does not contain the correct number of rows and/or columns.', {
73
expect_equal(dim(marathon), c(929, 13))
74
})
75
test_that('The marathon tibble is missing columns.', {
76
expect_true("time_hrs" %in% colnames(marathon))
77
expect_true("max" %in% colnames(marathon))
78
})
79
print("Success!")
80
}
81
82
test_2.0 <- function(){
83
properties <- c(answer2$mapping, answer2$layers[[1]]$mapping)
84
labels <- answer2$labels
85
test_that('Did not create a plot named answer2', {
86
expect_true(exists("answer2"))
87
})
88
test_that('marathon_50 does not contain the correct number of rows and/or columns.', {
89
expect_equal(dim(marathon_50), c(50, 13))
90
})
91
test_that('answer2 should use information from marathon_50', {
92
expect_equal(answer2$data, marathon_50)
93
})
94
test_that('max should be on the x-axis.', {
95
expect_true("max" %in% c(rlang::get_expr(properties$x),
96
rlang::get_expr(properties$x)))
97
})
98
test_that('time_hrs should be on the y-axis.', {
99
expect_true("time_hrs" %in% c(rlang::get_expr(properties$y),
100
rlang::get_expr(properties$y)))
101
})
102
test_that('answer2 should be a scatter plot.', {
103
expect_equal(digest(class(rlang::get_expr(answer2$layers[[1]]$geom))[1]),
104
'911e5b9debfb523f25ad2ccc01a4b2dd')
105
})
106
test_that('Labels on the axes should be descriptive and human readable.', {
107
expect_false((labels$y) == 'time_hrs')
108
expect_false((labels$x) == 'max')
109
})
110
print("Success!")
111
}
112
113
test_3.0 <- function(){
114
test_that('Did not create an object called answer3', {
115
expect_true(exists('answer3'))
116
})
117
test_that('answer3 is incorrect', {
118
expect_equal(digest(int_round(answer3, 1)), 'a266aa4a0aa711355be22e0f3b8d91af')
119
})
120
print("Success!")
121
}
122
123
test_4.0 <- function(){
124
test_that('Did not create an object called answer4', {
125
expect_true(exists('answer4'))
126
})
127
test_that('answer4 is incorrect', {
128
expect_equal(digest(int_round(answer4, 1)), '285d156b1b700fbb489df058fdb9e2ee')
129
})
130
print("Success!")
131
}
132
133
test_5.0 <- function(){
134
test_that('Did not create an object called answer5', {
135
expect_true(exists('answer5'))
136
})
137
test_that('Solution is incorrect', {
138
expect_equal(digest(answer5), '475bf9280aab63a82af60791302736f6')
139
})
140
print("Success!")
141
}
142
143
test_6.0 <- function(){
144
test_that('Did not create an object named marathon_split', {
145
expect_true(exists("marathon_split"))
146
})
147
test_that('marathon_split should be rsplit (not a tibble)', {
148
expect_true('rsplit' %in% class(marathon_split))
149
})
150
test_that('Did not create an object named marathon_training', {
151
expect_true(exists('marathon_training'))
152
})
153
test_that('marathon_training does not contain 0.75 of the data.', {
154
expect_equal(dim(marathon_training), c(698,13))
155
expect_equal(digest(int_round(sum(marathon_training$age), 0)), '5109988e81575a4e65652fddb747a18f')
156
})
157
test_that('Did not create an object named marathon_testing', {
158
expect_true(exists('marathon_testing'))
159
})
160
test_that('marathon_testing does not contain 0.25 of the data.', {
161
expect_equal(dim(marathon_testing), c(231, 13))
162
expect_equal(digest(int_round(sum(marathon_testing$age), 0)), '9ffe9b883d53974eaa9afb9bf0ec386b')
163
})
164
print("Success!")
165
}
166
167
test_7.0 <- function(){
168
test_that('Did not create an object named marathon_spec', {
169
expect_true(exists("marathon_spec"))
170
})
171
test_that('neighbors argument is incorrect', {
172
expect_equal(digest(as.character(get_expr(marathon_spec$args$neighbors))), '4b89cff22bb78b28a0a6b7fe28d371f6')
173
})
174
test_that('weight_func is incorrect', {
175
expect_equal(digest(as.character(get_expr(marathon_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
176
})
177
test_that('set_engine is incorrect', {
178
expect_equal(digest(as.character(marathon_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
179
})
180
test_that('mode is incorrect', {
181
expect_equal(digest(as.character(marathon_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')
182
})
183
test_that('Did not create an object named marathon_recipe', {
184
expect_true(exists("marathon_recipe"))
185
})
186
test_that('Data in marathon_recipe is not scaled and centered', {
187
expect_equal(digest(int_round(sum(marathon_recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')
188
expect_equal(digest(int_round(sum(marathon_recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')
189
})
190
print("Success!")
191
}
192
193
test_7.1 <- function(){
194
test_that('Did not create an object called marathon_vfold', {
195
expect_true(exists("marathon_vfold"))
196
})
197
test_that('marathon_vfold does not contain 5 folds', {
198
expect_equal(int_round(length(marathon_vfold$id), 0), 5)
199
})
200
test_that('marathon_vfold should be a cross-validation object', {
201
expect_true('vfold_cv' %in% class(marathon_vfold))
202
})
203
test_that('Did not create an object called marathon_workflow', {
204
expect_true(exists("marathon_workflow"))
205
})
206
test_that('marathon_workflow is not a workflow object', {
207
expect_true('workflow' %in% class(marathon_workflow))
208
})
209
test_that('marathon_workflow does not contain the correct model specification', {
210
expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$args$neighbors)), 'b68c9f555cfd94fe903b741afcace6c1')
211
expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')
212
expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
213
expect_true('nearest_neighbor' %in% class(marathon_workflow$fit$actions$model$spec))
214
})
215
test_that('marathon_workflow does not contain the correct recipe', {
216
expect_true('recipe' %in% class(marathon_workflow$pre$actions$recipe$recipe))
217
expect_equal(digest(int_round(sum(marathon_workflow$pre$actions$recipe$recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')
218
expect_equal(digest(int_round(sum(marathon_workflow$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')
219
})
220
print("Success!")
221
}
222
223
224
test_8.0 <- function(){
225
test_that('Did not create an object named gridvals', {
226
expect_true(exists('gridvals'))
227
})
228
test_that('gridvals does not contain the correct data and column name', {
229
expect_true('tbl' %in% class(gridvals))
230
expect_true('neighbors' %in% colnames(gridvals))
231
expect_equal(digest(int_round(sum(gridvals), 0)), '7f14b492534315a44244b698e5058e39')
232
})
233
test_that('Did not create an object named marathon_results', {
234
expect_true(exists('marathon_results'))
235
})
236
test_that('marathon_results is not a tibble', {
237
expect_true('tbl' %in% class(marathon_results))
238
})
239
test_that('marathon_results does not contain the correct data', {
240
expect_equal(dim(marathon_results), c(400, 7))
241
expect_equal(digest(int_round(sum(marathon_results$neighbors), 0)), '07d8f52916463a9c6cd99797f0531d42')
242
expect_equal(int_round(unique(marathon_results$n), 0), 5)
243
expect_equal(digest(int_round(sum(marathon_results$mean), 0)), '3c9f3a7a14786e414122855e84509f9d')
244
expect_equal(digest(int_round(sum(marathon_results$std_err), 0)), '7c7124efff5c7039a1b1e7cba65c5379')
245
})
246
print("Success!")
247
}
248
249
250
test_8.1 <- function(){
251
test_that('Did not create an object named marathon_min', {
252
expect_true(exists('marathon_min'))
253
})
254
test_that('marathon_min is not a tibble', {
255
expect_true('tbl' %in% class(marathon_min))
256
})
257
test_that('marathon_min does not contain the correct data', {
258
expect_equal(dim(marathon_min), c(1, 7))
259
expect_true('neighbors' %in% colnames(marathon_min))
260
expect_true('.metric' %in% colnames(marathon_min))
261
expect_true('.estimator' %in% colnames(marathon_min))
262
expect_true('mean' %in% colnames(marathon_min))
263
expect_true('n' %in% colnames(marathon_min))
264
expect_true('std_err' %in% colnames(marathon_min))
265
expect_true('.config' %in% colnames(marathon_min))
266
})
267
test_that('Best K value is incorrect', {
268
expect_equal(digest(int_round(marathon_min$neighbors, 2)), '9241e88f7548d793a2482a33d623b99f')
269
})
270
test_that('Metric is incorrect', {
271
expect_equal(digest(marathon_min$.metric), '91a8c46d46a2a25459eaabfa08f35967')
272
})
273
print("Success!")
274
}
275
276
test_8.2 <- function(){
277
test_that('Did not create an object named k_min', {
278
expect_true(exists('k_min'))
279
})
280
test_that('k_min is not correct', {
281
expect_true('integer' %in% class(k_min))
282
expect_equal(digest(int_round(k_min, 2)), '9241e88f7548d793a2482a33d623b99f')
283
})
284
test_that('Did not create an object named marathon_best_spec', {
285
expect_true(exists('marathon_best_spec'))
286
})
287
test_that('marathon_best_spec has incorrect specifications', {
288
expect_equal(digest(as.character(get_expr(marathon_best_spec$args$neighbors))), '0b942c90bc01f15b084d00fa29bf4cc0')
289
})
290
test_that('weight_func is incorrect', {
291
expect_equal(digest(as.character(get_expr(marathon_best_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
292
})
293
test_that('set_engine is incorrect', {
294
expect_equal(digest(as.character(marathon_best_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
295
})
296
test_that('mode is incorrect', {
297
expect_equal(digest(as.character(marathon_best_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')
298
})
299
test_that('Did not create an object named marathon_best_fit', {
300
expect_true(exists('marathon_best_fit'))
301
})
302
test_that('marathon_best_fit should be a workflow', {
303
expect_true('workflow' %in% class(marathon_best_fit))
304
})
305
test_that('marathon_best_fit does not contain the correct model specification', {
306
expect_equal(digest(get_expr(marathon_best_fit$fit$actions$model$spec$args$neighbors)), '7ad692ee809beafa13e6d291d0d5372f')
307
expect_equal(digest(as.character(marathon_best_fit$fit$actions$model$spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')
308
expect_equal(digest(as.character(marathon_best_fit$fit$actions$model$spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
309
expect_true('nearest_neighbor' %in% class(marathon_best_fit$fit$actions$model$spec))
310
})
311
test_that('marathon_best_fit does not contain the correct recipe', {
312
expect_true('recipe' %in% class(marathon_best_fit$pre$actions$recipe$recipe))
313
expect_equal(digest(int_round(sum(marathon_best_fit$pre$actions$recipe$recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')
314
expect_equal(digest(int_round(sum(marathon_best_fit$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')
315
})
316
test_that('Did not create an object named marathon_summary', {
317
expect_true(exists('marathon_summary'))
318
})
319
test_that('marathon_summary is not a tibble', {
320
expect_true('tbl' %in% class(marathon_summary))
321
})
322
test_that('marathon_summary contains the incorrect data', {
323
expect_true('.metric' %in% colnames(marathon_summary))
324
expect_true('.estimator' %in% colnames(marathon_summary))
325
expect_true('.estimate' %in% colnames(marathon_summary))
326
expect_equal(digest(int_round(sum(marathon_summary$.estimate), 0)), '4b5630ee914e848e8d07221556b0a2fb')
327
})
328
print("Success!")
329
}
330
331
test_8.3 <- function(){
332
test_that('Did not create an objected named answer8.3', {
333
expect_true(exists('answer8.3'))
334
})
335
test_that('answer is incorrect', {
336
expect_equal(digest(answer8.3), 'd2a90307aac5ae8d0ef58e2fe730d38b')
337
})
338
print("Success!")
339
}
340
341
test_9.0 <- function(){
342
properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)
343
labels <- marathon_plot$labels
344
test_that('Did not create an object named marathon_preds', {
345
expect_true(exists('marathon_preds'))
346
})
347
test_that('marathon_preds should be a tibble', {
348
expect_true('tbl' %in% class(marathon_preds))
349
})
350
test_that('marathon_preds contains incorrect data', {
351
expect_equal(dim(marathon_preds), c(698, 14))
352
expect_true('.pred' %in% colnames(marathon_preds))
353
expect_equal(digest(int_round(sum(marathon_preds$.pred), 2)), 'f5847c263ec9596eee1ae122cdd1e347')
354
expect_equal(digest(int_round(sum(marathon_preds$time_hrs), 2)), 'b9b7909060cc50e65fb7ff452897a2b4')
355
})
356
test_that('Did not create an object called marathon_plot', {
357
expect_true(exists('marathon_plot'))
358
})
359
test_that('max should be on the x-axis.', {
360
expect_true("max" == rlang::get_expr(properties$x))
361
})
362
test_that('time_hrs should be on the y-axis.', {
363
expect_true("time_hrs" == rlang::get_expr(properties$y))
364
})
365
test_that('marathon_plot should have full_predictions plotted as a blue line over the data points.', {
366
expect_true('blue' %in% as.character(marathon_plot$layers[[2]]$aes_params))
367
expect_true('GeomLine' %in% c(class(rlang::get_expr(marathon_plot$layers[[1]]$geom)), class(rlang::get_expr(marathon_plot$layers[[2]]$geom))))
368
})
369
test_that('max should be the x argument for geom_line', {
370
expect_true('max' == rlang::get_expr(marathon_plot$layers[[2]]$mapping$x))
371
})
372
test_that('.pred should be the y argument for geom_line',{
373
expect_true('.pred' == rlang::get_expr(marathon_plot$layers[[2]]$mapping$y))
374
})
375
test_that('Labels on the axes/title and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
376
expect_false((labels$y) == 'time_hrs')
377
expect_false((labels$x) == 'max')
378
expect_false((labels$title == 'k_min'))
379
})
380
print("Success!")
381
}
382
383