Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-fall/materials/worksheet_11/tests_worksheet_11.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 + 1L
25
if (abs(xint - x) < abs(xint1 - x)){
26
return(xint)
27
}
28
else {
29
return(xint1)
30
}
31
}
32
33
# function to extract attributes from cowplot objects
34
# source: https://stackoverflow.com/questions/54051576/extracting-individual-plot-details-from-combined-plot-in-cowplot-for-unit-test?answertab=votes#tab-top
35
fun <- function(p, what) {
36
unlist(sapply(p$layers, function(x) {
37
idx <- which(x$geom_params$grob$layout$name == what)
38
x$geom_params$grob$grobs[[idx]]$children[[1]]$label
39
}))
40
}
41
42
test_1.0 <- function(){
43
test_that('Solution is incorrect', {
44
expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')
45
})
46
print("Success!")
47
}
48
49
test_1.1 <- function(){
50
test_that('variables should be assigned numeric values (do not include the definition in your answer, just the number associated with the definition)', {
51
values <- c(point_estimate, population, random_sampling, representative_sampling, population_parameter, sample, observation, sampling_distribution)
52
expect_is(values, 'numeric')
53
})
54
55
test_that('At least one term-definition match is incorrect', {
56
expect_equal(digest(int_round(point_estimate, 0)), '25e6a154090e35101d7678d6f034353a')
57
expect_equal(digest(int_round(population, 0)), '4b5630ee914e848e8d07221556b0a2fb')
58
expect_equal(digest(int_round(random_sampling, 0)), 'c01f179e4b57ab8bd9de309e6d576c48')
59
expect_equal(digest(int_round(representative_sampling, 0)), '7c7124efff5c7039a1b1e7cba65c5379')
60
expect_equal(digest(int_round(population_parameter, 0)), '11946e7a3ed5e1776e81c0f0ecd383d0')
61
expect_equal(digest(int_round(sample, 0)), 'dd4ad37ee474732a009111e3456e7ed7')
62
expect_equal(digest(int_round(observation, 0)), '9d08099943f8627959cfb8ecee0d2f5d')
63
expect_equal(digest(int_round(sampling_distribution, 0)), '234a2a5581872457b9fe1187d1616b13')
64
})
65
print("Success!")
66
}
67
68
test_1.2 <- function(){
69
properties <- c(pop_dist$layers[[1]]$mapping, pop_dist$mapping)
70
labels <- pop_dist$labels
71
test_that('age should be on the x-axis.', {
72
expect_true("age" == rlang::get_expr(properties$x))
73
})
74
test_that('pop_dist should be a histogram.', {
75
expect_true("GeomBar" %in% class(pop_dist$layers[[1]]$geom))
76
})
77
test_that('can_seniors data should be used to create the histogram', {
78
expect_equal(int_round(nrow(pop_dist$data), 0), 1027941)
79
expect_equal(digest(int_round(sum(pop_dist$data$age), 0)), '0a65b77971cd131982c7117a5ab90242')
80
})
81
test_that('Labels on the x axis should be descriptive and human readable.', {
82
expect_false((labels$x) == 'age')
83
})
84
print("Success!")
85
}
86
87
test_1.3 <- function(){
88
test_that('pop_parameters has 3 columns and one row, with column names pop_mean, pop_med and pop_sd.', {
89
expect_equal(int_round(nrow(pop_parameters), 0), 1)
90
expect_equal(int_round(ncol(pop_parameters), 0), 3)
91
expect_equal(digest(paste(sort(colnames(pop_parameters)), collapse = "")), '723d282ea6dad216da6b1074ca7cf688')
92
})
93
print("Success!")
94
}
95
96
test_1.4 <- function(){
97
test_that('sample_1 should have 2 columns and 40 rows', {
98
expect_equal(int_round(nrow(sample_1), 0), 40)
99
expect_equal(int_round(ncol(sample_1), 0), 2)
100
})
101
test_that('the column names of sample_1 should be replicate and age', {
102
expect_equal(digest(paste(sort(colnames(sample_1)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')
103
})
104
print("Success!")
105
}
106
107
test_1.5 <- function(){
108
properties <- c(sample_1_dist$layers[[1]]$mapping, sample_1_dist$mapping)
109
labels <- sample_1_dist$labels
110
test_that('age should be on the x-axis.', {
111
expect_true("age" == rlang::get_expr(properties$x))
112
})
113
test_that('sample_1_dist should be a histogram.', {
114
expect_true("GeomBar" %in% class(sample_1_dist$layers[[1]]$geom))
115
})
116
test_that('sample_1 data should be used to create the histogram', {
117
expect_equal(int_round(nrow(sample_1_dist$data), 0), 40)
118
expect_equal(digest(int_round(sum(sample_1_dist$data$age), 2)), 'f856ba7ffab8e669473a2ee7bf49de52')
119
})
120
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
121
expect_false((labels$x) == 'age')
122
expect_false(is.null(labels$title))
123
})
124
print("Success!")
125
}
126
127
test_1.6 <- function(){
128
test_that('sample_1_estimates should have at least 3 columns, and 1 row', {
129
expect_equal(int_round(nrow(sample_1_estimates), 0), 1)
130
expect_true(int_round(ncol(sample_1_estimates), 0) >= 3)
131
})
132
test_that('sample_1_estimates has columns with correct names', {
133
expect_true("sample_1_mean" %in% colnames(sample_1_estimates))
134
expect_true("sample_1_med" %in% colnames(sample_1_estimates))
135
expect_true("sample_1_sd" %in% colnames(sample_1_estimates))
136
})
137
print("Success!")
138
}
139
140
test_1.7 <- function(){
141
test_that('Solution is incorrect', {
142
expect_equal(digest(answer1.7), '475bf9280aab63a82af60791302736f6')
143
})
144
print("Success!")
145
}
146
147
148
test_1.8.0 <- function(){
149
test_that('sample_2 should have 2 columns and 40 rows', {
150
expect_equal(int_round(nrow(sample_2), 0), 40)
151
expect_equal(int_round(ncol(sample_2), 0), 2)
152
})
153
test_that('the column names of sample_2 should be replicate and age', {
154
expect_equal(digest(paste(sort(colnames(sample_2)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')
155
})
156
properties <- c(sample_2_dist$layers[[1]]$mapping, sample_2_dist$mapping)
157
labels <- sample_2_dist$labels
158
test_that('age should be on the x-axis.', {
159
expect_true("age" == rlang::get_expr(properties$x))
160
})
161
test_that('sample_2_dist should be a histogram.', {
162
expect_true("GeomBar" %in% class(sample_2_dist$layers[[1]]$geom))
163
})
164
test_that('sample_2 data should be used to create the histogram', {
165
expect_equal(int_round(nrow(sample_2_dist$data), 0), 40)
166
expect_equal(digest(int_round(sum(sample_2_dist$data$age), 2)), '199d472897c57c820c8c694f44d7786c')
167
})
168
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
169
expect_false((labels$x) == 'age')
170
expect_false(is.null(labels$title))
171
})
172
test_that('sample_2_estimates should have at least 3 columns, and 1 row', {
173
expect_equal(int_round(nrow(sample_2_estimates), 0), 1)
174
expect_true(int_round(ncol(sample_2_estimates), 0) >= 3)
175
})
176
test_that('sample_2_estimates has columns with correct names', {
177
expect_true("sample_2_mean" %in% colnames(sample_2_estimates))
178
expect_true("sample_2_med" %in% colnames(sample_2_estimates))
179
expect_true("sample_2_sd" %in% colnames(sample_2_estimates))
180
})
181
print("Success!")
182
}
183
184
test_1.8.1 <- function(){
185
test_that('Solution is incorrect', {
186
expect_equal(digest(answer1.8.1), '475bf9280aab63a82af60791302736f6')
187
})
188
print("Success!")
189
}
190
191
test_1.9 <- function(){
192
test_that('samples should have 60000 rows and 2 columns', {
193
expect_equal(int_round(ncol(samples), 0), 2)
194
expect_equal(int_round(nrow(samples), 0), 60000)
195
})
196
test_that('the column names of samples should be replicate and age', {
197
expect_equal(digest(paste(sort(colnames(samples)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')
198
})
199
print("Success!")
200
}
201
202
test_2.0 <- function(){
203
test_that('sample_estimates should have 1500 rows and 2 columns', {
204
expect_equal(int_round(ncol(sample_estimates), 0), 2)
205
expect_equal(int_round(nrow(sample_estimates), 0), 1500)
206
})
207
test_that('the column names of sample_estimates should be replicate and sample_mean', {
208
expect_equal(digest(paste(sort(colnames(sample_estimates)), collapse = "")), '7453089f8086e9a98a067f3eeac63363')
209
})
210
print("Success!")
211
}
212
213
test_2.1 <- function(){
214
properties <- c(sampling_distribution$layers[[1]]$mapping, sampling_distribution$mapping)
215
labels <- sampling_distribution$labels
216
test_that('sample_mean should be on the x-axis.', {
217
expect_true("sample_mean" == rlang::get_expr(properties$x))
218
})
219
test_that('sampling_distribution should be a histogram.', {
220
expect_true("GeomBar" %in% class(sampling_distribution$layers[[1]]$geom))
221
})
222
test_that('sampling_distribution data should be used to create the histogram', {
223
expect_equal(int_round(nrow(sampling_distribution$data), 0), 1500)
224
expect_equal(digest(int_round(sum(sampling_distribution$data$sample_mean), 2)), 'e20a3a6689ccb7122ce8aaa71bab55bf')
225
})
226
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
227
expect_false((labels$x) == 'age')
228
expect_false(is.null(labels$title))
229
})
230
print("Success!")
231
}
232
233
test_2.2 <- function(){
234
test_that('Solution is incorrect', {
235
expect_equal(digest(int_round(answer2.2, 2)), '0ddc7e7a0d2654650cba2f2a15cbca52')
236
})
237
print("Success!")
238
}
239
240
test_2.3 <- function(){
241
test_that('Solution is incorrect', {
242
expect_equal(digest(answer2.3), '3a5505c06543876fe45598b5e5e5195d')
243
})
244
print("Success!")
245
}
246
247
test_2.4 <- function(){
248
test_that('Solution is incorrect', {
249
expect_equal(digest(tolower(answer2.4)), '05ca18b596514af73f6880309a21b5dd')
250
})
251
print("Success!")
252
}
253
254
test_2.5 <- function(){
255
properties <- c(sampling_distribution_20$layers[[1]]$mapping, sampling_distribution_20$mapping)
256
labels <- sampling_distribution_20$labels
257
test_that('sample_mean should be on the x-axis.', {
258
expect_true("sample_mean" == rlang::get_expr(properties$x))
259
})
260
test_that('sampling_distribution should be a histogram.', {
261
expect_true("GeomBar" %in% class(sampling_distribution_20$layers[[1]]$geom))
262
})
263
test_that('sampling_distribution data should be used to create the histogram', {
264
expect_equal(int_round(nrow(sampling_distribution_20$data), 0), 1500)
265
expect_equal(digest(int_round(sum(sampling_distribution_20$data$sample_mean), 2)), '49a66adc63b05e7e8f90b66202de0b84')
266
})
267
test_that('Labels on the x axis should be descriptive. The plot should have the title n = 20.', {
268
expect_false((labels$x) == 'age')
269
expect_equal(labels$title, "n = 20")
270
})
271
272
print("Success!")
273
}
274
275
test_2.6 <- function(){
276
properties <- c(sampling_distribution_100$layers[[1]]$mapping, sampling_distribution_100$mapping)
277
labels <- sampling_distribution_100$labels
278
test_that('sample_mean should be on the x-axis.', {
279
expect_true("sample_mean" == rlang::get_expr(properties$x))
280
})
281
test_that('sampling_distribution should be a histogram.', {
282
expect_true("GeomBar" %in% class(sampling_distribution_100$layers[[1]]$geom))
283
})
284
test_that('sampling_distribution data should be used to create the histogram', {
285
expect_equal(int_round(nrow(sampling_distribution_100$data), 0), 1500)
286
expect_equal(digest(int_round(sum(sampling_distribution_100$data$sample_mean), 2)), '59c92b151db8f38ba93a364fd62ae7c9')
287
})
288
test_that('Labels on the x axis should be descriptive. The plot should have the title n = 100.', {
289
expect_false((labels$x) == 'age')
290
expect_equal(labels$title, "n = 100")
291
})
292
293
print("Success!")
294
}
295
296
test_2.7 <- function(){
297
test_that('object is named sampling_distribution_panel.', {
298
expect_true(exists("sampling_distribution_panel"))
299
})
300
test_that('sampling distributions are plotted side-by-side with the correct titles of n = 20, "n = 40, and n = 100', {
301
expect_equal(fun(sampling_distribution_panel, "title"), c("n = 20", "n = 40", "n = 100"))
302
})
303
print("Success!")
304
}
305
306
test_2.8 <- function(){
307
test_that('Solution is incorrect', {
308
expect_equal(digest(answer2.8), 'c1f86f7430df7ddb256980ea6a3b57a4')
309
})
310
print("Success!")
311
}
312
313
test_2.9 <- function(){
314
test_that('Solution is incorrect', {
315
expect_equal(digest(tolower(answer2.9)), 'd2a90307aac5ae8d0ef58e2fe730d38b')
316
})
317
print("Success!")
318
}
319
320