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_07/tests_worksheet_07.R
2710 views
1
library(testthat)
2
library(digest)
3
library(rlang)
4
5
int_round <- function(x, digits){
6
x = x*10^digits
7
xint = as.integer(x)
8
xint1 = xint + 1L
9
if (abs(xint - x) < abs(xint1 - x)){
10
return(xint)
11
}
12
else {
13
return(xint1)
14
}
15
}
16
17
test_2.0 <- function(){
18
test_that('Did not create an object named fruit_train', {
19
expect_true(exists("fruit_train"))
20
})
21
test_that('Did not create an object named fruit_test', {
22
expect_true(exists("fruit_test"))
23
})
24
test_that('fruit_train does not contain the correct number of rows and/or columns', {
25
expect_equal(dim(fruit_train), c(46, 7))
26
})
27
test_that('fruit_test does not contain the correct number of rows and/or columns',{
28
expect_equal(dim(fruit_test), c(13, 7))
29
})
30
test_that('fruit_train contains the wrong data', {
31
expect_equal(digest(int_round(sum(fruit_train$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')
32
})
33
test_that('fruit_test contains the wrong data', {
34
expect_equal(digest(int_round(sum(fruit_test$mass), 2)), 'fcb11cb4ce2aa88708f4f5895d59abbe')
35
})
36
print("Success!")
37
}
38
39
40
test_2.1 <- function(){
41
test_that('Did not create an object named fruit_recipe', {
42
expect_true(exists("fruit_recipe"))
43
})
44
test_that('fruit_recipe should be a recipe.', {
45
expect_true('recipe' %in% class(fruit_recipe))
46
})
47
test_that('fruit_recipe contains the wrong columns', {
48
expect_true('color_score' %in% colnames(fruit_recipe$template))
49
expect_true('mass' %in% colnames(fruit_recipe$template))
50
expect_true('fruit_name' %in% colnames(fruit_recipe$template))
51
})
52
test_that('fruit_recipe contains the wrong data', {
53
expect_equal(digest(int_round(sum(fruit_recipe$template$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')
54
expect_equal(digest(int_round(sum(fruit_recipe$template$color_score), 2)), '656cbb68c33ed8c769ed3fb3a423f886')
55
})
56
print("Success!")
57
}
58
59
test_2.2 <- function(){
60
test_that('Did not create an object named knn_spec', {
61
expect_true(exists("knn_spec"))
62
})
63
test_that('knn_spec should be a model specification', {
64
expect_true("model_spec" %in% class(knn_spec))
65
})
66
test_that('k is not 3', {
67
expect_equal(int_round(get_expr(knn_spec$args$neighbors), 0), 3)
68
})
69
test_that('weight_func is incorrect', {
70
expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
71
})
72
test_that('set_engine is incorrect', {
73
expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
74
})
75
test_that('mode is incorrect', {
76
expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
77
})
78
test_that('Did not create an object named fruit_fit', {
79
expect_true(exists("fruit_fit"))
80
})
81
test_that('fruit_fit should be a workflow.', {
82
expect_true('workflow' %in% class(fruit_fit))
83
})
84
test_that('fruit_fit does not contain scaled data', {
85
expect_equal(digest(int_round(sum(fruit_fit$pre$mold$predictors$mass), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
86
expect_equal(digest(int_round(sum(fruit_fit$pre$mold$predictors$color_score), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
87
})
88
print("Success!")
89
}
90
91
test_2.3 <- function(){
92
test_that('Did not create an object named fruit_test_predictions', {
93
expect_true(exists("fruit_test_predictions"))
94
})
95
test_that('fruit_test_predictions should be a tibble.', {
96
expect_true('tbl' %in% class(fruit_test_predictions))
97
})
98
test_that('fruit_test_predictions should contain the original data and the new prediction column', {
99
expect_equal(dim(fruit_test_predictions), c(13, 8))
100
expect_true('.pred_class' %in% colnames(fruit_test_predictions))
101
})
102
print("Success!")
103
}
104
105
test_2.4 <- function(){
106
test_that('Did not create an object named fruit_prediction_accuracy', {
107
expect_true(exists("fruit_prediction_accuracy"))
108
})
109
test_that('fruit_prediction_accuracy should be a tibble', {
110
expect_true('tbl' %in% class(fruit_prediction_accuracy))
111
})
112
test_that('estimates are incorrect', {
113
expect_equal(digest(int_round(sum(fruit_prediction_accuracy$.estimate), 2)), '44865f1c212fb27ca7ab5b7154dcf398')
114
})
115
test_that('the estimator should be a multiclass classification', {
116
expect_true('multiclass' %in% fruit_prediction_accuracy$.estimator)
117
})
118
print("Success!")
119
}
120
121
test_2.5 <- function(){
122
test_that('Did not create an object named fruit_mat', {
123
expect_true(exists("fruit_mat"))
124
})
125
test_that('fruit_mat is not a confusion matrix', {
126
expect_true('conf_mat' %in% class(fruit_mat))
127
})
128
test_that('Number of observations is incorrect', {
129
expect_equal(digest(int_round(sum(as.tibble(fruit_mat$table)[3]), 2)), '306a937dfa0335e74514e4c6044755f6')
130
})
131
print("Success!")
132
}
133
134
135
test_2.6 <- function(){
136
test_that('Did not create an object named answer2.6', {
137
expect_true(exists("answer2.6"))
138
})
139
test_that('Answer is incorrect', {
140
expect_equal(digest(answer2.6), 'c1f86f7430df7ddb256980ea6a3b57a4')
141
})
142
print("Success!")
143
}
144
145
test_3.1 <- function(){
146
test_that('Did not create an object named fruit_vfold', {
147
expect_true(exists("fruit_vfold"))
148
})
149
test_that('fruit_vfold is not a cross validation object',{
150
expect_true('vfold_cv' %in% class(fruit_vfold))
151
})
152
test_that('fruit_vfold does not contain 5 folds', {
153
expect_equal(int_round(length(fruit_vfold$id), 0), 5)
154
})
155
test_that('fruit_vfold contains the incorrect data', {
156
expect_equal(dim(fruit_vfold), c(5, 2))
157
})
158
print("Success!")
159
}
160
161
test_3.2 <- function(){
162
test_that('Did not create an object named fruit_resample_fit', {
163
expect_true(exists("fruit_resample_fit"))
164
})
165
test_that('fruit_resample_fit is not a resample_result', {
166
expect_true('resample_results' %in% class(fruit_resample_fit))
167
})
168
test_that('fruit_resample_fit contains the incorrect data', {
169
expect_equal(dim(fruit_resample_fit), c(5, 4))
170
})
171
test_that('number of splits is not 5' ,{
172
expect_equal(int_round(length(fruit_resample_fit$splits), 0), 5)
173
})
174
test_that('fruit_vfold should contain 5 folds', {
175
expect_equal(int_round(length(fruit_vfold$id), 0), 5)
176
})
177
test_that('fruit_vfold contains the incorrect data', {
178
expect_equal(dim(fruit_vfold), c(5, 2))
179
})
180
print("Success!")
181
}
182
183
test_3.3 <- function(){
184
test_that('Did not create an object named fruit_metrics', {
185
expect_true(exists("fruit_metrics"))
186
})
187
test_that('fruit_metrics contains the wrong data', {
188
expect_equal(dim(fruit_metrics), c(2, 5))
189
expect_true('mean' %in% colnames(fruit_metrics))
190
expect_true('std_err' %in% colnames(fruit_metrics))
191
expect_equal(digest(int_round(sum(fruit_metrics$mean), 2)), '99496b18802c49d6571120c16308addc')
192
expect_equal(digest(int_round(sum(fruit_metrics$std_err), 2)), '234a2a5581872457b9fe1187d1616b13')
193
expect_equal(digest(int_round(sum(fruit_metrics$n), 2)), 'b6a6227038bf9be67533a45a6511cc7e')
194
})
195
print("Success!")
196
}
197
198
test_4.0 <- function(){
199
test_that('Did not create an object named knn_tune', {
200
expect_true(exists("knn_tune"))
201
})
202
test_that('knn_tune should be a model specification', {
203
expect_true("model_spec" %in% class(knn_tune))
204
})
205
test_that('k is not set to tune', {
206
expect_equal(as.character(get_expr(knn_tune$args$neighbors)), 'tune')
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
print("Success!")
218
}
219
220
test_4.1 <- function(){
221
test_that('Did not create an object called knn_results',{
222
expect_true(exists("knn_results"))
223
})
224
test_that('knn_results should be a tibble', {
225
expect_true('tbl' %in% class(knn_results))
226
})
227
test_that('knn_results does not contain the correct data',{
228
expect_equal(dim(knn_results), c(20, 7))
229
expect_equal(digest(int_round(sum(knn_results$neighbors), 2)), 'bc0bb1b780c5a2b3fbe18f1017288655')
230
expect_equal(digest(int_round(sum(knn_results$mean), 2)), 'e948a7acb8065d6583060c83b763e871')
231
expect_equal(digest(int_round(sum(knn_results$std_err), 2)), '2af87dfdb66a77e47ec325e40c275f91')
232
})
233
test_that('grid is not set to 10', {
234
expect_equal(int_round(length(unique(knn_results$.config)), 0), 10)
235
})
236
print("Success!")
237
}
238
239
240
test_4.2 <- function(){
241
test_that('Did not create an object called accuracies', {
242
expect_true(exists("accuracies"))
243
})
244
test_that('accuracies .metric column should only contain accuracy', {
245
expect_true(unique(accuracies$.metric) == "accuracy")
246
})
247
properties <- c(accuracy_versus_k$layers[[1]]$mapping, accuracy_versus_k$mapping)
248
labels <- accuracy_versus_k$labels
249
test_that('Did not create a plot named accuracy_versus_k', {
250
expect_true(exists("accuracy_versus_k"))
251
})
252
test_that('neighbors should be on the x-axis.', {
253
expect_true("neighbors" == rlang::get_expr(properties$x))
254
})
255
test_that('mean should be on the y-axis.', {
256
expect_true("mean" == rlang::get_expr(properties$y))
257
})
258
test_that('accuracy_versus_k should be a scatter/line plot.', {
259
expect_true("GeomPoint" %in% c(class(accuracy_versus_k$layers[[1]]$geom)))
260
expect_true("GeomLine" %in% c(class(accuracy_versus_k$layers[[2]]$geom)))
261
})
262
print("Success!")
263
}
264
265