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_07/tests_worksheet_07.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('Did not create an object named answer0.1', {
36
expect_true(exists('answer0.1'))
37
})
38
test_that('Solution is incorrect', {
39
expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6')
40
})
41
print("Success!")
42
43
}
44
45
test_1.0 <- function(){
46
test_that('Did not create an object named fruit_data', {
47
expect_true(exists("fruit_data"))
48
})
49
test_that('fruit_data does not contain the correct number of rows and/or columns.', {
50
expect_equal(dim(fruit_data), c(59, 7))
51
})
52
test_that('The fruit_name column in fruit_data should be of class factor.', {
53
expect_true(is.factor(fruit_data$fruit_name))
54
})
55
test_that('Columns in fruit_data contain incorrect values.', {
56
expect_equal(digest(int_round(sum(fruit_data$mass, na.rm = TRUE), 2)), '8c7433f4d278ef1e1e0f8d0ccb217614') # we hid the answer to the test here so you can't see it, but we can still run the test
57
})
58
print("Success!")
59
}
60
61
# # +
62
test_1.1 <- function(){
63
test_that('Did not create an object called answer1.1', {
64
expect_true(exists('answer1.1'))
65
})
66
test_that('Solution is incorrect', {
67
expect_equal(digest(answer1.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
68
69
})
70
print("Success!")
71
}
72
# -
73
74
test_1.2 <- function(){
75
test_that('Did not create an object named fruit_dist_2', {
76
expect_true(exists("fruit_dist_2"))
77
})
78
test_that('fruit_dist_2 is incorrect.', {
79
expect_equal(digest(int_round(fruit_dist_2, 3)), 'a29a5d18050c6ce0aa2dc501684e1375')
80
})
81
print("Success!")
82
}
83
84
test_1.3 <- function(){
85
test_that('Did not create an object named fruit_dist_44', {
86
expect_true(exists("fruit_dist_44"))
87
})
88
test_that('fruit_dist_44 is incorrect.', {
89
expect_equal(digest(int_round(fruit_dist_44, 2)), 'ea07cf8b74030ff04b56ac69dd094adc')
90
})
91
print("Success!")
92
}
93
94
95
test_1.4 <- function(){
96
test_that('Did not create an object named answer1.4', {
97
expect_true(exists('answer1.4'))
98
})
99
test_that('Solution is incorrect', {
100
expect_equal(digest(answer1.4), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
101
})
102
print("Success!")
103
}
104
105
test_1.5 <- function(){
106
test_that('Did not create an object named fruit_data_scaled', {
107
expect_true(exists("fruit_data_scaled"))
108
})
109
test_that('fruit_data_scaled does not contain the correct number of rows and/or columns.', {
110
expect_equal(dim(fruit_data_scaled), c(59, 11))
111
})
112
test_that('The fruit_name column in fruit_data_scaled should be of class factor.', {
113
expect_true(is.factor(fruit_data_scaled$fruit_name))
114
})
115
test_that('Columns in fruit_data_scaled contain incorrect values.', {
116
expect_equal(digest(int_round(sum(fruit_data_scaled$mass, na.rm = TRUE), 2)), '8c7433f4d278ef1e1e0f8d0ccb217614') # we hid the answer to the test here so you can't see it, but we can still run the test
117
})
118
test_that('The mass, height, color score, and width columns in fruit_data_Scaled should be scaled.', {
119
expect_equal(digest(int_round(sum(fruit_data_scaled$scaled_mass), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
120
expect_equal(digest(int_round(sum(fruit_data_scaled$scaled_width), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
121
expect_equal(digest(int_round(sum(fruit_data_scaled$scaled_height), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
122
expect_equal(digest(int_round(sum(fruit_data_scaled$scaled_color_score), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
123
})
124
print("Success!")
125
}
126
127
test_1.6 <- function(){
128
test_that('Did not create an object named distance_44', {
129
expect_true(exists("distance_44"))
130
})
131
test_that('Did not create an object named distance_2', {
132
expect_true(exists("distance_2"))
133
})
134
test_that('distance_44 should be a distance.', {
135
expect_true('dist' %in% class(distance_44))
136
})
137
test_that('distance_2 should be a distance.', {
138
expect_true('dist' %in% class(distance_2))
139
})
140
test_that('distance_44 is incorrect.', {
141
expect_equal(digest(int_round(distance_2, 2)), '192b298ed4661ab6d9a4a193b2e60b49')
142
})
143
test_that('distance_2 is incorrect.', {
144
expect_equal(digest(int_round(distance_44, 2)), '78f799aab6957dffdfd2bfb504f8cab5')
145
})
146
print("Success!")
147
}
148
149
test_2.0 <- function(){
150
test_that('Did not create an object named fruit_train', {
151
expect_true(exists("fruit_train"))
152
})
153
test_that('Did not create an object named fruit_test', {
154
expect_true(exists("fruit_test"))
155
})
156
test_that('fruit_train does not contain the correct number of rows and/or columns', {
157
expect_equal(dim(fruit_train), c(46, 7))
158
})
159
test_that('fruit_test does not contain the correct number of rows and/or columns',{
160
expect_equal(dim(fruit_test), c(13, 7))
161
})
162
test_that('fruit_train contains the wrong data', {
163
expect_equal(digest(int_round(sum(fruit_train$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')
164
})
165
test_that('fruit_test contains the wrong data', {
166
expect_equal(digest(int_round(sum(fruit_test$mass), 2)), 'fcb11cb4ce2aa88708f4f5895d59abbe')
167
})
168
print("Success!")
169
}
170
171
172
test_2.1 <- function(){
173
test_that('Did not create an object named fruit_recipe', {
174
expect_true(exists("fruit_recipe"))
175
})
176
test_that('fruit_recipe should be a recipe.', {
177
expect_true('recipe' %in% class(fruit_recipe))
178
})
179
test_that('fruit_recipe contains the wrong columns', {
180
expect_true('color_score' %in% colnames(fruit_recipe$template))
181
expect_true('mass' %in% colnames(fruit_recipe$template))
182
expect_true('fruit_name' %in% colnames(fruit_recipe$template))
183
})
184
test_that('fruit_recipe contains the wrong data', {
185
expect_equal(digest(int_round(sum(fruit_recipe$template$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')
186
expect_equal(digest(int_round(sum(fruit_recipe$template$color_score), 2)), '656cbb68c33ed8c769ed3fb3a423f886')
187
})
188
test_that('all_predictors() is not scaled and centered', {
189
expect_equal(digest(as.character(get_expr(fruit_recipe$steps[[1]]$terms))), 'f34b27deb5a8023de51b602e9aacf535')
190
expect_equal(digest(as.character(get_expr(fruit_recipe$steps[[2]]$terms))), 'f34b27deb5a8023de51b602e9aacf535')
191
})
192
test_that('fruit_name was not placed before predictors', {
193
expect_equal(digest(as.character(fruit_recipe$var_info$variable[3])), '1298acdeb848b96767603d30382d6aff')
194
})
195
print("Success!")
196
}
197
198
test_2.2 <- function(){
199
test_that('Did not create an object named knn_spec', {
200
expect_true(exists("knn_spec"))
201
})
202
test_that('knn_spec should be a model specification', {
203
expect_true("model_spec" %in% class(knn_spec))
204
})
205
test_that('k is not 3', {
206
expect_equal(int_round(get_expr(knn_spec$args$neighbors), 0), 3)
207
})
208
test_that('weight_func is incorrect', {
209
expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
210
})
211
test_that('set_engine is incorrect', {
212
expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
213
})
214
test_that('mode is incorrect', {
215
expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
216
})
217
test_that('Did not create an object named fruit_fit', {
218
expect_true(exists("fruit_fit"))
219
})
220
test_that('fruit_fit should be a workflow.', {
221
expect_true('workflow' %in% class(fruit_fit))
222
})
223
test_that('fruit_fit does not contain scaled data', {
224
expect_equal(digest(int_round(sum(fruit_fit$pre$mold$predictors$mass), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
225
expect_equal(digest(int_round(sum(fruit_fit$pre$mold$predictors$color_score), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
226
})
227
print("Success!")
228
}
229
230
test_2.3 <- function(){
231
test_that('Did not create an object named fruit_test_predictions', {
232
expect_true(exists("fruit_test_predictions"))
233
})
234
test_that('fruit_test_predictions should be a tibble.', {
235
expect_true('tbl' %in% class(fruit_test_predictions))
236
})
237
test_that('fruit_test_predictions should contain the original data and the new prediction column', {
238
expect_equal(dim(fruit_test_predictions), c(13, 8))
239
expect_true('.pred_class' %in% colnames(fruit_test_predictions))
240
})
241
print("Success!")
242
}
243
244
test_2.4 <- function(){
245
test_that('Did not create an object named fruit_prediction_accuracy', {
246
expect_true(exists("fruit_prediction_accuracy"))
247
})
248
test_that('fruit_prediction_accuracy should be a tibble', {
249
expect_true('tbl' %in% class(fruit_prediction_accuracy))
250
})
251
test_that('estimates are incorrect', {
252
expect_equal(digest(int_round(sum(fruit_prediction_accuracy$.estimate), 2)), '44865f1c212fb27ca7ab5b7154dcf398')
253
})
254
test_that('the estimator should be a multiclass classification', {
255
expect_true('multiclass' %in% fruit_prediction_accuracy$.estimator)
256
})
257
print("Success!")
258
}
259
260
test_2.5 <- function(){
261
test_that('Did not create an object named fruit_mat', {
262
expect_true(exists("fruit_mat"))
263
})
264
test_that('fruit_mat is not a confusion matrix', {
265
expect_true('conf_mat' %in% class(fruit_mat))
266
})
267
test_that('Number of observations is incorrect', {
268
expect_equal(digest(int_round(sum(as.tibble(fruit_mat$table)[3]), 2)), '306a937dfa0335e74514e4c6044755f6')
269
})
270
print("Success!")
271
}
272
273
274
test_2.6 <- function(){
275
test_that('Did not create an object named answer2.6', {
276
expect_true(exists("answer2.6"))
277
})
278
test_that('Answer is incorrect', {
279
expect_equal(digest(answer2.6), 'c1f86f7430df7ddb256980ea6a3b57a4')
280
})
281
print("Success!")
282
}
283
284
test_3.1 <- function(){
285
test_that('Did not create an object named fruit_vfold', {
286
expect_true(exists("fruit_vfold"))
287
})
288
test_that('fruit_vfold is not a cross validation object',{
289
expect_true('vfold_cv' %in% class(fruit_vfold))
290
})
291
test_that('fruit_vfold does not contain 5 folds', {
292
expect_equal(int_round(length(fruit_vfold$id), 0), 5)
293
})
294
test_that('fruit_vfold contains the incorrect data', {
295
expect_equal(dim(fruit_vfold), c(5, 2))
296
})
297
test_that('fruit_vfold does not use the training data', {
298
expect_equal(digest(int_round(sum(fruit_vfold$splits[[1]]$data$color_score), 2)), '656cbb68c33ed8c769ed3fb3a423f886')
299
expect_equal(digest(int_round(sum(fruit_vfold$splits[[1]]$data$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')
300
})
301
test_that('strata argument is not fruit_name', {
302
expect_equal(digest(int_round(sum(fruit_vfold$splits[[1]]$in_id), 2)), 'df9b1bae6656d96dfbe896782bd9de05')
303
expect_equal(digest(int_round(sum(fruit_vfold$splits[[2]]$in_id), 2)), 'd01f1e59ae3a6b2db6831e601606b1c0')
304
expect_equal(digest(int_round(sum(fruit_vfold$splits[[3]]$in_id), 2)), '71321611fabe5aee0df74bb96fe3a545')
305
expect_equal(digest(int_round(sum(fruit_vfold$splits[[4]]$in_id), 2)), 'd01f1e59ae3a6b2db6831e601606b1c0')
306
expect_equal(digest(int_round(sum(fruit_vfold$splits[[5]]$in_id), 2)), '28db4e404a1c7e394adb8f9a21711424')
307
})
308
print("Success!")
309
}
310
311
test_3.2 <- function(){
312
test_that('Did not create an object named fruit_resample_fit', {
313
expect_true(exists("fruit_resample_fit"))
314
})
315
test_that('fruit_resample_fit is not a resample_result', {
316
expect_true('resample_results' %in% class(fruit_resample_fit))
317
})
318
test_that('fruit_resample_fit contains the incorrect data', {
319
expect_equal(dim(fruit_resample_fit), c(5, 4))
320
})
321
test_that('number of splits is not 5' ,{
322
expect_equal(int_round(length(fruit_resample_fit$splits), 0), 5)
323
})
324
test_that('fruit_vfold should contain 5 folds', {
325
expect_equal(int_round(length(fruit_vfold$id), 0), 5)
326
})
327
test_that('fruit_vfold contains the incorrect data', {
328
expect_equal(dim(fruit_vfold), c(5, 2))
329
})
330
print("Success!")
331
}
332
333
test_3.3 <- function(){
334
test_that('Did not create an object named fruit_metrics', {
335
expect_true(exists("fruit_metrics"))
336
})
337
test_that('fruit_metrics contains the wrong data', {
338
expect_equal(dim(fruit_metrics), c(2, 5))
339
expect_true('mean' %in% colnames(fruit_metrics))
340
expect_true('std_err' %in% colnames(fruit_metrics))
341
expect_equal(digest(int_round(sum(fruit_metrics$mean), 2)), '3bb12916e7f6fda4645dd4ecaedb76b9')
342
expect_equal(digest(int_round(sum(fruit_metrics$std_err), 2)), '8eaca7c9b35d05ab15c9125bc92372fa')
343
expect_equal(digest(int_round(sum(fruit_metrics$n), 2)), 'b6a6227038bf9be67533a45a6511cc7e')
344
})
345
print("Success!")
346
}
347
348
test_4.0 <- function(){
349
test_that('Did not create an object named knn_tune', {
350
expect_true(exists("knn_tune"))
351
})
352
test_that('knn_tune should be a model specification', {
353
expect_true("model_spec" %in% class(knn_tune))
354
})
355
test_that('k is not set to tune', {
356
expect_equal(as.character(get_expr(knn_tune$args$neighbors)), 'tune')
357
})
358
test_that('weight_func is incorrect', {
359
expect_equal(digest(as.character(get_expr(knn_tune$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
360
})
361
test_that('set_engine is incorrect', {
362
expect_equal(digest(as.character(knn_tune$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
363
})
364
test_that('mode is incorrect', {
365
expect_equal(digest(as.character(knn_tune$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
366
})
367
print("Success!")
368
}
369
370
test_4.1 <- function(){
371
test_that('Did not create an object called knn_results',{
372
expect_true(exists("knn_results"))
373
})
374
test_that('knn_results should be a tibble', {
375
expect_true('tbl' %in% class(knn_results))
376
})
377
test_that('knn_results does not contain the correct data',{
378
expect_equal(dim(knn_results), c(20, 7))
379
expect_equal(digest(int_round(sum(knn_results$neighbors), 2)), 'bc0bb1b780c5a2b3fbe18f1017288655')
380
expect_equal(digest(int_round(sum(knn_results$mean), 2)), '2fbed5c22f5fcb638e9cad6f0d588e47')
381
expect_equal(digest(int_round(sum(knn_results$std_err), 2)), '6c3a3556917f12517be89f353d7b93ff')
382
})
383
test_that('grid is not set to 10', {
384
expect_equal(int_round(length(unique(knn_results$.config)), 0), 10)
385
})
386
print("Success!")
387
}
388
389
390
test_4.2 <- function(){
391
test_that('Did not create an object called accuracies', {
392
expect_true(exists("accuracies"))
393
})
394
test_that('accuracies .metric column should only contain accuracy', {
395
expect_true(unique(accuracies$.metric) == "accuracy")
396
})
397
properties <- c(accuracy_versus_k$layers[[1]]$mapping, accuracy_versus_k$mapping)
398
labels <- accuracy_versus_k$labels
399
test_that('Did not create a plot named accuracy_versus_k', {
400
expect_true(exists("accuracy_versus_k"))
401
})
402
test_that('neighbors should be on the x-axis.', {
403
expect_true("neighbors" == rlang::get_expr(properties$x))
404
})
405
test_that('mean should be on the y-axis.', {
406
expect_true("mean" == rlang::get_expr(properties$y))
407
})
408
test_that('accuracy_versus_k should be a scatter/line plot.', {
409
expect_true("GeomPoint" %in% c(class(accuracy_versus_k$layers[[1]]$geom)))
410
expect_true("GeomLine" %in% c(class(accuracy_versus_k$layers[[2]]$geom)))
411
})
412
print("Success!")
413
}
414
415