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