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