Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-spring/materials/worksheet_09/tests_worksheet_09.R
2051 views
1
library(testthat)
2
library(digest)
3
4
#' Round double to precise integer
5
#'
6
#' `int_round` works to create an integer corresponding to a number that is
7
#' tested up to a particular decimal point of precision. This is useful when
8
#' there is a need to compare a numeric value using hashes.
9
#'
10
#' @param x Double vector of length one.
11
#' @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.
12
#'
13
#' @return Integer vector of length one corresponding to a particular decimal point of precision.
14
#'
15
#' @examples
16
#' # to get an integer up to two decimals of precision from 234.56789
17
#' int_round(234.56789, 2)
18
#'
19
#' to get an integer rounded to the hundred digit from 234.56789
20
#' int_round(234.56789, -2)
21
int_round <- function(x, digits){
22
x = x*10^digits
23
xint = as.integer(x)
24
xint1 = xint + 1
25
if (abs(xint - x) < abs(xint1 - x)){
26
return(xint)
27
}
28
else {
29
return(xint1)
30
}
31
}
32
33
test_1.0 <- function() {
34
test_that('Did not create an object named answer1.0', {
35
expect_true(exists('answer1.0'))
36
})
37
test_that('Solution is incorrect', {
38
expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')
39
})
40
print("Success!")
41
}
42
43
test_1.1 <- function() {
44
test_that('Did not create an object named answer1.1', {
45
expect_true(exists('answer1.1'))
46
})
47
test_that('Solution is incorrect', {
48
expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6')
49
})
50
print("Success!")
51
}
52
53
test_1.2 <- function() {
54
test_that('Did not create an object named answer1.2', {
55
expect_true(exists('answer1.2'))
56
})
57
test_that('Solution is incorrect', {
58
expect_equal(digest(answer1.2), '75f1160e72554f4270c809f041c7a776')
59
})
60
print("Success!")
61
}
62
63
test_2.0 <- function() {
64
test_that('Did not create an object named answer2.0', {
65
expect_true(exists('answer2.0'))
66
})
67
test_that('Solution is incorrect', {
68
expect_equal(digest(int_round(answer2.0, 2)), '9a6564e67167bff7e7cf99a541a641f1')
69
})
70
print("Success!")
71
}
72
73
test_2.1 <- function() {
74
test_that('Did not create an object named answer2.1', {
75
expect_true(exists('answer2.1'))
76
})
77
test_that('Solution is incorrect', {
78
expect_equal(digest(int_round(answer2.1, 2)), '9a6564e67167bff7e7cf99a541a641f1')
79
})
80
print("Success!")
81
}
82
83
test_2.2 <- function() {
84
test_that('Did not create an object named answer2.2', {
85
expect_true(exists('answer2.2'))
86
})
87
test_that('Solution is incorrect', {
88
expect_equal(digest(int_round(answer2.2, 2)), 'd69e7827ff0b1272336c2136df42c3f0')
89
})
90
print("Success!")
91
}
92
93
test_2.3 <- function() {
94
test_that('Did not create an object named answer2.3', {
95
expect_true(exists('answer2.3'))
96
})
97
test_that('Solution is incorrect', {
98
expect_equal(digest(answer2.3), '475bf9280aab63a82af60791302736f6')
99
})
100
print("Success!")
101
}
102
103
test_3.0 <- function() {
104
test_that('Did not create an object named marathon', {
105
expect_true(exists("marathon"))
106
})
107
test_that('marathon should be a tibble.', {
108
expect_true('tbl' %in% class(marathon))
109
})
110
test_that('marathon does not contain the correct number of rows and/or columns.', {
111
expect_equal(dim(marathon), c(929, 13))
112
})
113
test_that('The marathon tibble does not contain the correct columns.', {
114
expect_true("time_hrs" %in% colnames(marathon))
115
expect_true("max" %in% colnames(marathon))
116
})
117
test_that('marathon contains the wrong data', {
118
expect_equal(digest(int_round(sum(marathon$max), 0)), 'b64d424699e3efa872a878b15e4615fc')
119
expect_equal(digest(int_round(sum(marathon$time_hrs), 0)), '0a386b4fbb992709ee886a69c311a49c')
120
})
121
print("Success!")
122
}
123
124
test_3.1 <- function() {
125
test_that('Did not create an object named marathon_split', {
126
expect_true(exists('marathon_split'))
127
})
128
test_that('marathon_split is not a split object (not a tibble)', {
129
expect_true('rsplit' %in% class(marathon_split))
130
})
131
test_that('marathon_split does not contain marathon data', {
132
expect_equal(dim(marathon_split$data), c(929, 13))
133
expect_equal(digest(int_round(sum(marathon_split$data$max), 0)), 'b64d424699e3efa872a878b15e4615fc')
134
expect_equal(digest(int_round(sum(marathon_split$data$time_hrs), 0)), '0a386b4fbb992709ee886a69c311a49c')
135
})
136
test_that('Did not create an object named marathon_training', {
137
expect_true(exists('marathon_training'))
138
})
139
test_that('marathon_training is not a tibble', {
140
expect_true('tbl' %in% class(marathon_training))
141
})
142
test_that('marathon_training does not contain 0.75 of the marathon data', {
143
expect_equal(dim(marathon_training), c(698, 13))
144
expect_equal(digest(int_round(sum(marathon_training$max), 0)), '6e85687c32809edf13dccf228f9f84e9')
145
expect_equal(digest(int_round(sum(marathon_training$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')
146
})
147
test_that('Did not create an object named marathon_testing', {
148
expect_true(exists('marathon_testing'))
149
})
150
test_that('marathon_testing is not a tibble', {
151
expect_true('tbl' %in% class(marathon_testing))
152
})
153
test_that('marathon testing does not contain 0.25 of the marathon data', {
154
expect_equal(dim(marathon_testing), c(231, 13))
155
expect_equal(digest(int_round(sum(marathon_testing$max), 0)), 'e5032644f10cbf9a251aff4ed126d4af')
156
expect_equal(digest(int_round(sum(marathon_testing$time_hrs), 0)), 'ba825ab3d722243760b0700769f9371b')
157
})
158
print("Success!")
159
}
160
161
test_3.2 <- function() {
162
properties <- c(marathon_eda$layers[[1]]$mapping, marathon_eda$mapping)
163
labels <- marathon_eda$labels
164
layers <- marathon_eda$layers[[1]]
165
test_that('Did not create a plot named marathon_eda', {
166
expect_true(exists("marathon_eda"))
167
})
168
test_that('max should be on the x-axis.', {
169
expect_true("max" %in% c(rlang::get_expr(properties$x)))
170
})
171
test_that('time_hrs should be on the y-axis.', {
172
expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))
173
})
174
test_that('marathon_eda should be a scatter plot.', {
175
expect_equal(digest(class(rlang::get_expr(layers$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')
176
})
177
test_that('Labels on the axes should be descriptive and human readable.', {
178
expect_false((labels$y) == 'time_hrs')
179
expect_false((labels$x) == 'max')
180
})
181
test_that('Only the training data set should be used to create the plot', {
182
expect_equal(int_round(nrow(marathon_eda$data), 0), 698)
183
})
184
print("Success!")
185
}
186
187
test_3.3 <- function() {
188
test_that('Did not create an object named lm_spec', {
189
expect_true(exists("lm_spec"))
190
})
191
test_that('lm_spec is not a linear regression model', {
192
expect_true('linear_reg' %in% class(lm_spec))
193
})
194
test_that('lm_spec does not contain the correct specifications', {
195
expect_equal(digest(as.character(lm_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')
196
expect_equal(digest(as.character(lm_spec$engine)), '0995419f6f003f701c545d050292f42d')
197
})
198
print("Success!")
199
}
200
201
test_3.3.1 <- function() {
202
test_that('Did not create an object named lm_recipe', {
203
expect_true(exists('lm_recipe'))
204
})
205
test_that('lm_recipe is not a recipe', {
206
expect_true('recipe' %in% class(lm_recipe))
207
})
208
test_that('lm_recipe does not contain the correct variables', {
209
expect_equal(digest(int_round(sum(lm_recipe$template$max), 0)), '6e85687c32809edf13dccf228f9f84e9')
210
expect_equal(digest(int_round(sum(lm_recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')
211
})
212
test_that('Did not create an object named lm_fit', {
213
expect_true(exists('lm_fit'))
214
})
215
test_that('lm_fit is not a workflow', {
216
expect_true('workflow' %in% class(lm_fit))
217
})
218
test_that('lm_fit does not contain the correct data', {
219
expect_equal(digest(int_round(sum(lm_fit$pre$actions$recipe$recipe$template$max), 0)), '6e85687c32809edf13dccf228f9f84e9')
220
expect_equal(digest(int_round(sum(lm_fit$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')
221
})
222
test_that('lm_fit coefficients are incorrect', {
223
expect_equal(digest(int_round(sum(lm_fit$fit$fit$fit$coefficients), 1)), '80b0ae73fe0e882b0a24973e4e2c8203')
224
})
225
print("Success!")
226
}
227
228
test_3.4 <- function() {
229
properties <- c(lm_predictions$layers[[1]]$mapping, lm_predictions$mapping)
230
labels <- lm_predictions$labels
231
layers <- c(lm_predictions$layers[[1]], lm_predictions$layers[[2]])
232
layers2 <- c(lm_predictions$layers[[2]], lm_predictions$layers[[1]])
233
test_that('Did not create a plot named lm_predictions', {
234
expect_true(exists("lm_predictions"))
235
})
236
test_that('max should be on the x-axis.', {
237
expect_true("max" %in% c(rlang::get_expr(properties$x)))
238
})
239
test_that('time_hrs should be on the y-axis.', {
240
expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))
241
})
242
test_that('lm_predictions should be a scatter plot.', {
243
expect_true('GeomPoint' %in% c(class(rlang::get_expr(lm_predictions$layers[[1]]$geom)),
244
class(rlang::get_expr(lm_predictions$layers[[2]]$geom))))
245
246
})
247
test_that('lm_predictions should have a best fit line using a linear regression model.', {
248
expect_true('GeomSmooth' %in% c(class(rlang::get_expr(lm_predictions$layers[[2]]$geom)),
249
class(rlang::get_expr(lm_predictions$layers[[1]]$geom))))
250
})
251
test_that('Labels on the axes should be descriptive and human readable.', {
252
expect_false((labels$y) == 'time_hrs')
253
expect_false((labels$x) == 'max')
254
})
255
print("Success!")
256
}
257
258
test_3.5 <- function() {
259
test_that('Did not create an object named lm_test_results', {
260
expect_true(exists('lm_test_results'))
261
})
262
test_that('lm_test_results is not a tibble', {
263
expect_true('tbl' %in% class(lm_test_results))
264
})
265
test_that('lm_test_results does not contain the correct data', {
266
expect_equal(dim(lm_test_results), c(3, 3))
267
expect_equal(digest(int_round(sum(lm_test_results$.estimate), 1)), 'a86d0670df7fb4f1da7b38943f5ee4e7')
268
})
269
test_that('Did not create an object named lm_rmspe', {
270
expect_true(exists('lm_rmspe'))
271
})
272
test_that('lm_rmspe is not a numeric', {
273
expect_true('numeric' %in% class(lm_rmspe))
274
})
275
test_that('lm_rmspe is not correct', {
276
expect_equal(digest(int_round(lm_rmspe, 1)), '25e6a154090e35101d7678d6f034353a')
277
})
278
print("Success!")
279
}
280
281
test_3.5.1 <- function() {
282
properties <- c(lm_predictions_test$mapping, lm_predictions_test$layers[[1]]$mapping)
283
labels <- lm_predictions_test$labels
284
layers <- lm_predictions_test$layers[[1]]
285
test_that('Did not create a plot named lm_predictions_test', {
286
expect_true(exists("lm_predictions_test"))
287
})
288
test_that('max should be on the x-axis.', {
289
expect_true("max" %in% c(rlang::get_expr(properties$x)))
290
})
291
test_that('time_hrs should be on the y-axis.', {
292
expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))
293
})
294
test_that('lm_predictions_test should be a scatter plot.', {
295
expect_equal(digest(class(rlang::get_expr(layers$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')
296
})
297
test_that('Labels on the axes should be descriptive and human readable.', {
298
expect_false((labels$y) == 'time_hrs')
299
expect_false((labels$x) == 'max')
300
})
301
test_that('Only the testing data set should be used to create the plot', {
302
expect_equal(int_round(nrow(lm_predictions_test$data), 0), 231)
303
})
304
print("Success!")
305
}
306
307
test_3.6 <- function() {
308
test_that('Did not create an object named answer3.6', {
309
expect_true(exists('answer3.6'))
310
})
311
test_that('Solution is incorrect', {
312
expect_equal(digest(answer3.6), '75f1160e72554f4270c809f041c7a776')
313
})
314
print("Success!")
315
}
316
317
test_3.7 <- function() {
318
test_that('Did not create an object named answer3.7', {
319
expect_true(exists('answer3.7'))
320
})
321
test_that('Solution is incorrect', {
322
expect_equal(digest(answer3.7), '3a5505c06543876fe45598b5e5e5195d')
323
})
324
print("Success!")
325
}
326
327
test_3.8.1 <- function() {
328
test_that('Did not create an object named answer3.8.1', {
329
expect_true(exists('answer3.8.1'))
330
})
331
test_that('Solution is incorrect', {
332
expect_equal(digest(answer3.8.1), '75f1160e72554f4270c809f041c7a776')
333
})
334
print("Success!")
335
}
336
337