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