Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2019-fall/materials/worksheet_08/tests_worksheet_08.R
2707 views
1
library(testthat)
2
library(digest)
3
4
test_0.0 <- function(){
5
test_that('Solution is incorrect', {
6
expect_equal(digest(answer0.0), '3a5505c06543876fe45598b5e5e5195d')
7
})
8
print("Success!")
9
}
10
11
test_0.1 <- function(){
12
test_that('Solution is incorrect', {
13
expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6')
14
})
15
print("Success!")
16
}
17
18
test_0.2 <- function(){
19
test_that('Solution is incorrect', {
20
expect_true(digest(round(answer0.2, 2)) %in% c('651ba44efc6a75d694ff482aae958ccc', '2a1ea47875e195a421d56ae3f6621d32'))
21
})
22
print("Success!")
23
}
24
25
test_0.3 <- function(){
26
test_that('Solution is incorrect', {
27
expect_equal(digest(answer0.3), '75f1160e72554f4270c809f041c7a776')
28
})
29
print("Success!")
30
}
31
32
test_1.0 <- function(){
33
test_that('Did not create an object named marathon', {
34
expect_true(exists("marathon"))
35
})
36
test_that('marathon should be a data frame.', {
37
expect_true('data.frame' %in% class(marathon))
38
})
39
test_that('marathon does not contain the correct number of rows and/or columns.', {
40
expect_equal(dim(marathon), c(929, 13))
41
})
42
test_that('The marathon data frame is missing columns.', {
43
expect_that("time_hrs" %in% colnames(marathon), is_true())
44
expect_that("max" %in% colnames(marathon), is_true())
45
})
46
print("Success!")
47
}
48
49
test_2.0 <- function(){
50
test_that('Did not create a plot named answer2', {
51
expect_true(exists("answer2"))
52
})
53
test_that('marathon_50 does not contain the correct number of rows and/or columns.', {
54
expect_equal(dim(marathon_50), c(50, 13))
55
})
56
test_that('answer2 should use information from marathon_50', {
57
expect_equal(answer2$data, marathon_50)
58
})
59
test_that('max should be on the x-axis.', {
60
expect_that("max" %in% c(rlang::get_expr(answer2$mapping$x),
61
rlang::get_expr(answer2$layers[[1]]$mapping$x)), is_true())
62
})
63
test_that('time_hrs should be on the y-axis.', {
64
expect_that("time_hrs" %in% c(rlang::get_expr(answer2$mapping$y),
65
rlang::get_expr(answer2$layers[[1]]$mapping$y)) , is_true())
66
})
67
test_that('answer2 should be a scatter plot.', {
68
expect_equal(digest(class(rlang::get_expr(answer2$layers[[1]]$geom))[1]),
69
'911e5b9debfb523f25ad2ccc01a4b2dd')
70
})
71
test_that('Labels on the axes should be descriptive and human readable.', {
72
expect_that((answer2$labels$y) == 'time_hrs', is_false())
73
expect_that((answer2$labels$x) == 'max', is_false())
74
})
75
print("Success!")
76
}
77
78
test_3.0 <- function(){
79
test_that('answer3 is incorrect', {
80
expect_true(exists('answer3'))
81
expect_equal(digest(round(answer3[[1]], 1)), 'b0370aa0feee1e438e74c77cd361ae86')
82
})
83
print("Success!")
84
}
85
86
test_4.0 <- function(){
87
test_that('answer4 is incorrect', {
88
expect_true(exists('answer4'))
89
expect_equal(digest(round(answer4[[1]], 1)), '316c6aa53246a0e994e472fb4af30fc0')
90
})
91
print("Success!")
92
}
93
94
test_5.0 <- function(){
95
test_that('Solution is incorrect', {
96
expect_that(exists('answer5'), is_true())
97
expect_equal(digest(answer5), '475bf9280aab63a82af60791302736f6')
98
})
99
print("Success!")
100
}
101
102
test_6.0 <- function(){
103
test_that('Did not create an object named training_rows', {
104
expect_true(exists("training_rows"))
105
})
106
test_that('training_rows should be a matrix (not a data frame)', {
107
expect_true('matrix' %in% class(training_rows))
108
})
109
test_that('training_rows is not the correct length.', {
110
expect_equal(length(training_rows), 698)
111
})
112
print("Success!")
113
}
114
115
test_7.0 <- function(){
116
test_that('Did not create an object named X_train', {
117
expect_true(exists("X_train"))
118
})
119
test_that('X_train should be a data frame.', {
120
expect_true('data.frame' %in% class(X_train))
121
})
122
test_that('X_train does not contain the correct number of rows and/or columns.', {
123
expect_equal(dim(X_train), c(698, 1))
124
})
125
test_that('X_train does not contain the column max', {
126
expect_true('max' %in% colnames(X_train))
127
})
128
test_that('Did not create an object named X_test', {
129
expect_true(exists("X_test"))
130
})
131
test_that('X_test should be a data frame.', {
132
expect_true('data.frame' %in% class(X_test))
133
})
134
test_that('X_test does not contain the correct number of rows and/or columns.', {
135
expect_equal(dim(X_test), c(231, 1))
136
})
137
test_that('X_test does not contain the column max', {
138
expect_true('max' %in% colnames(X_test))
139
})
140
test_that('Did not create an object named Y_train', {
141
expect_true(exists("Y_train"))
142
})
143
test_that('Y_train should be a numeric list', {
144
expect_true('numeric' %in% class(Y_train))
145
})
146
test_that('Y_train is not the correct length.', {
147
expect_equal(length(Y_train), 698)
148
})
149
test_that('Did not create an object named Y_test', {
150
expect_true(exists("Y_test"))
151
})
152
test_that('Y_test should be a numeric list', {
153
expect_true('numeric' %in% class(Y_test))
154
})
155
test_that('Y_test is not the correct length.', {
156
expect_equal(length(Y_test), 231)
157
})
158
print("Success!")
159
}
160
161
test_8.0 <- function(){
162
test_that('Did not create an object named knn_cv', {
163
expect_true(exists("knn_cv"))
164
})
165
test_that('x in knn_cv should be X_train', {
166
expect_equal(as.character(knn_cv$call$x), 'X_train')
167
})
168
test_that('y in knn_cv should be Y_train', {
169
expect_equal(as.character(knn_cv$call$y), 'Y_train')
170
})
171
test_that('method should be knn', {
172
expect_equal(as.character(knn_cv$method), 'knn')
173
})
174
test_that('k should be chosen from k_lots', {
175
expect_equal(as.numeric(sum(knn_cv$results$k)), 31375)
176
})
177
test_that('knn_cv should use 3 fold cross-validation.', {
178
expect_equal(knn_cv$control$method, 'cv')
179
expect_equal(knn_cv$control$number, 3)
180
})
181
print("Success!")
182
}
183
184
185
test_8.1 <- function(){
186
test_that('Did not create a plot named choosing_k', {
187
expect_true(exists("choosing_k"))
188
})
189
test_that('choosing_k contains incorrect information.', {
190
expect_equal(choosing_k$data, knn_cv$results)
191
})
192
test_that('k should be on the x-axis.', {
193
expect_that("k" %in% c(rlang::get_expr(choosing_k$mapping$x),rlang::get_expr(choosing_k$layers[[1]]$mapping$x)), is_true())
194
})
195
test_that('RMSE should be on the y-axis.', {
196
expect_that("RMSE" %in% c(rlang::get_expr(choosing_k$mapping$y), rlang::get_expr(choosing_k$layers[[1]]$mapping$y)) , is_true())
197
})
198
test_that('choosing_k should be a scatter plot and a line plot.', {
199
expect_that('GeomPoint' %in% c(class(rlang::get_expr(choosing_k$layers[[1]]$geom)), class(rlang::get_expr(choosing_k$layers[[2]]$geom))), is_true())
200
expect_that('GeomLine' %in% c(class(rlang::get_expr(choosing_k$layers[[1]]$geom)), class(rlang::get_expr(choosing_k$layers[[2]]$geom))), is_true())
201
})
202
print("Success!")
203
}
204
205
test_8.2 <- function(){
206
test_that('best_k is incorrect', {
207
expect_equal(digest(as.numeric(best_k[[1]])), 'dc65e64a0fb0e6b4c47b0ae79cff0459')
208
})
209
print("Success!")
210
}
211
212
test_9.0 <- function(){
213
test_that('Did not create an object named knn_model', {
214
expect_true(exists("knn_model"))
215
})
216
test_that('x in knn_model should be X_train', {
217
expect_equal(as.character(knn_model$call$x), 'X_train')
218
})
219
test_that('y in knn_model should be Y_train', {
220
expect_equal(as.character(knn_model$call$y), 'Y_train')
221
})
222
test_that('method should be knn', {
223
expect_equal(as.character(knn_model$method), 'knn')
224
})
225
test_that('k should be 75', {
226
expect_equal(as.integer(knn_model$results$k), 51)
227
})
228
test_that('knn_model contains incorrect trainingData.', {
229
expect_equal(colnames(knn_model$trainingData), c('max', '.outcome'))
230
expect_equal(dim(knn_model$trainingData), c(698, 2))
231
})
232
print("Success!")
233
}
234
235
test_10.0 <- function(){
236
test_that('predictions should be a numeric vector or length 231', {
237
expect_true(class(predictions) == 'numeric')
238
expect_equal(length(predictions), 231)
239
expect_equal(digest(mean(predictions)), '71ba098ce630c0890084dbe1ae18b8e8')
240
})
241
test_that('values of predictions are incorrect', {
242
expect_equal(digest(mean(predictions)), '71ba098ce630c0890084dbe1ae18b8e8')
243
})
244
print("Success!")
245
}
246
247
test_11.0 <- function(){
248
test_that('test_error is incorrect', {
249
expect_true('e2a1b82452e5d815ae7be864f8ba1ef1' %in% map(round(test_error, 3), digest) )
250
})
251
print("Success!")
252
}
253
254
test_11.1 <- function(){
255
test_that('Solution is incorrect', {
256
expect_equal(digest(answer11.1), '05ca18b596514af73f6880309a21b5dd')
257
})
258
print("Success!")
259
}
260
261
test_11.2 <- function(){
262
test_that('Solution is incorrect', {
263
expect_equal(digest(answer11.2), 'd2a90307aac5ae8d0ef58e2fe730d38b')
264
})
265
print("Success!")
266
}
267
268
test_12.0 <- function(){
269
test_that('Did not create an object named full_predictions', {
270
expect_true(exists("full_predictions"))
271
})
272
test_that('full_predictions should be a data frame.', {
273
expect_true('data.frame' %in% class(full_predictions))
274
})
275
test_that('full_predictions does not contain time_hrs and/or max', {
276
expect_true('time_hrs' %in% colnames(full_predictions))
277
expect_true('max' %in% colnames(full_predictions))
278
})
279
test_that('full_predictions contains incorrect information.', {
280
expect_equal(round(sum(full_predictions$time_hrs)), 434)
281
})
282
print("Success!")
283
}
284
285
test_13.0 <- function(){
286
test_that('Did not create a plot named predict_plot', {
287
expect_true(exists("predict_plot"))
288
})
289
test_that('max should be on the x-axis.', {
290
expect_that("max" %in% c(rlang::get_expr(predict_plot$mapping$x),rlang::get_expr(predict_plot$layers[[1]]$mapping$x)), is_true())
291
})
292
test_that('time_hrs should be on the y-axis.', {
293
expect_that("time_hrs" %in% c(rlang::get_expr(predict_plot$mapping$y), rlang::get_expr(predict_plot$layers[[1]]$mapping$y)) , is_true())
294
})
295
test_that('predict_plot should be a scatter plot.', {
296
expect_true('GeomPoint' %in% c(class(rlang::get_expr(predict_plot$layers[[1]]$geom)), class(rlang::get_expr(predict_plot$layers[[2]]$geom))))
297
})
298
test_that('predict_plot should have full_predictions plotted as a blue line over the data points.', {
299
expect_true('GeomLine' %in% c(class(rlang::get_expr(predict_plot$layers[[1]]$geom)), class(rlang::get_expr(predict_plot$layers[[2]]$geom))))
300
})
301
test_that('Labels on the axes should be descriptive and human readable.', {
302
expect_that((answer2$labels$y) == 'time_hrs', is_false())
303
expect_that((answer2$labels$x) == 'max', is_false())
304
})
305
print("Success!")
306
}
307
308
309
310
311
312
313
314
315