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_11/tests_worksheet_11.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_1.0 <- function(){
5
test_that('Solution is incorrect', {
6
expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')
7
})
8
print("Success!")
9
}
10
11
test_1.1 <- function(){
12
test_that('variables should be assigned numeric values (do not include the definition in your answer, just the number associated with the definition)', {
13
values <- c(point_estimate, population, random_sampling, representative_sampling, population_quantity, sample, observation, sampling_distribution)
14
expect_is(values, 'numeric')
15
})
16
17
test_that('At least one term-definition match is incorrect', {
18
expect_equal(digest(point_estimate), '0aee9b78301d7ec8998971363be87c03')
19
expect_equal(digest(population), '6717f2823d3202449301145073ab8719')
20
expect_equal(digest(random_sampling), 'db8e490a925a60e62212cefc7674ca02')
21
expect_equal(digest(representative_sampling), '90a7653d717dc1553ee564aa27b749b9')
22
expect_equal(digest(population_quantity), 'e5b57f323c7b3719bbaaf9f96b260d39')
23
expect_equal(digest(sample), '5e338704a8e069ebd8b38ca71991cf94')
24
expect_equal(digest(observation), '4a5d7d50676e6d0ea065f445d8a5539d')
25
expect_equal(digest(sampling_distribution), 'dbc09cba9fe2583fb01d63c70e1555a8')
26
})
27
print("Success!")
28
}
29
30
test_1.2 <- function(){
31
properties <- c(pop_dist$layers[[1]]$mapping, pop_dist$mapping)
32
test_that('age should be on the x-axis.', {
33
expect_true("age" == rlang::get_expr(properties$x))
34
})
35
test_that('pop_dist should be a histogram.', {
36
expect_that("GeomBar" %in% class(pop_dist$layers[[1]]$geom), is_true())
37
})
38
test_that('can_seniors data should be used to create the histogram', {
39
expect_equal(nrow(pop_dist$data), 1027941)
40
expect_equal(round(as.numeric(sum(pop_dist$data))), 81516771)
41
})
42
test_that('Labels on the x axis should be descriptive and human readable.', {
43
expect_that((pop_dist$labels$x) == 'age', is_false())
44
})
45
print("Success!")
46
}
47
48
test_1.3 <- function(){
49
test_that('pop_quantities has 3 columns and one row, with column names pop_mean, pop_med and pop_sd.', {
50
expect_equal(nrow(pop_quantities), 1)
51
expect_equal(ncol(pop_quantities), 3)
52
expect_equal(digest(paste(sort(colnames(pop_quantities)), collapse = "")), '723d282ea6dad216da6b1074ca7cf688')
53
})
54
print("Success!")
55
}
56
57
test_1.4 <- function(){
58
test_that('sample_1 should have 2 columns and 40 rows', {
59
expect_equal(nrow(sample_1), 40)
60
expect_equal(ncol(sample_1), 2)
61
})
62
test_that('the column names of sample_1 should be replicate and age', {
63
expect_equal(digest(paste(sort(colnames(sample_1)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')
64
})
65
print("Success!")
66
}
67
68
test_1.5 <- function(){
69
properties <- c(sample_1_dist$layers[[1]]$mapping, sample_1_dist$mapping)
70
test_that('age should be on the x-axis.', {
71
expect_true("age" == rlang::get_expr(properties$x))
72
})
73
test_that('sample_1_dist should be a histogram.', {
74
expect_that("GeomBar" %in% class(sample_1_dist$layers[[1]]$geom), is_true())
75
})
76
test_that('sample_1 data should be used to create the histogram', {
77
expect_equal(nrow(sample_1_dist$data), 40)
78
expect_equal(round(as.numeric(sum(sample_1_dist$data))), 3265)
79
})
80
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
81
expect_that((sample_1_dist$labels$x) == 'age', is_false())
82
expect_false(is.null(sample_1_dist$labels$title))
83
})
84
print("Success!")
85
}
86
87
test_1.6 <- function(){
88
test_that('sample_1_estimates should have at least 3 columns, and 1 row', {
89
expect_equal(nrow(sample_1_estimates), 1)
90
expect_true(ncol(sample_1_estimates) >= 3)
91
})
92
test_that('sample_1_estimates has columns with correct names', {
93
expect_true("sample_1_mean" %in% colnames(sample_1_estimates))
94
expect_true("sample_1_med" %in% colnames(sample_1_estimates))
95
expect_true("sample_1_sd" %in% colnames(sample_1_estimates))
96
})
97
print("Success!")
98
}
99
100
test_1.7 <- function(){
101
test_that('Solution is incorrect', {
102
expect_equal(digest(answer1.7), '475bf9280aab63a82af60791302736f6')
103
})
104
print("Success!")
105
}
106
107
test_1.8.1 <- function(){
108
test_that('Solution is incorrect', {
109
expect_equal(digest(answer1.8.1), '475bf9280aab63a82af60791302736f6')
110
})
111
print("Success!")
112
}
113
114
test_1.9 <- function(){
115
test_that('samples should have 60000 rows and 2 columns', {
116
expect_equal(ncol(samples), 2)
117
expect_equal(nrow(samples), 60000)
118
})
119
test_that('the column names of samples should be replicate and age', {
120
expect_equal(digest(paste(sort(colnames(samples)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')
121
})
122
print("Success!")
123
}
124
125
test_2.0 <- function(){
126
test_that('sample_estimates should have 1500 rows and 2 columns', {
127
expect_equal(ncol(sample_estimates), 2)
128
expect_equal(nrow(sample_estimates), 1500)
129
})
130
test_that('the column names of sample_estimates should be replicate and sample_mean', {
131
expect_equal(digest(paste(sort(colnames(sample_estimates)), collapse = "")), '7453089f8086e9a98a067f3eeac63363')
132
})
133
print("Success!")
134
}
135
136
test_2.1 <- function(){
137
properties <- c(sampling_distribution$layers[[1]]$mapping, sampling_distribution$mapping)
138
test_that('sample_mean should be on the x-axis.', {
139
expect_true("sample_mean" == rlang::get_expr(properties$x))
140
})
141
test_that('sampling_distribution should be a histogram.', {
142
expect_that("GeomBar" %in% class(sampling_distribution$layers[[1]]$geom), is_true())
143
})
144
test_that('sampling_distribution data should be used to create the histogram', {
145
expect_equal(nrow(sampling_distribution$data), 1500)
146
expect_equal(round(as.numeric(sum(sampling_distribution$data))), 1244931)
147
})
148
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
149
expect_that((sampling_distribution$labels$x) == 'age', is_false())
150
expect_false(is.null(sampling_distribution$labels$title))
151
})
152
print("Success!")
153
}
154
155
test_2.2 <- function(){
156
test_that('Solution is incorrect', {
157
expect_equal(digest(round(answer2.2)), '49df39a554b7960907ee669ebb6c2071')
158
})
159
print("Success!")
160
}
161
162
test_2.3 <- function(){
163
test_that('Solution is incorrect', {
164
expect_equal(digest(answer2.3), '3a5505c06543876fe45598b5e5e5195d')
165
})
166
print("Success!")
167
}
168
169
test_2.4 <- function(){
170
test_that('Solution is incorrect', {
171
expect_equal(digest(tolower(answer2.4)), '05ca18b596514af73f6880309a21b5dd')
172
})
173
print("Success!")
174
}
175
176
test_2.5 <- function(){
177
properties <- c(sampling_distribution_20$layers[[1]]$mapping, sampling_distribution_20$mapping)
178
test_that('sample_mean should be on the x-axis.', {
179
expect_true("sample_mean" == rlang::get_expr(properties$x))
180
})
181
test_that('sampling_distribution should be a histogram.', {
182
expect_that("GeomBar" %in% class(sampling_distribution_20$layers[[1]]$geom), is_true())
183
})
184
test_that('sampling_distribution data should be used to create the histogram', {
185
expect_equal(nrow(sampling_distribution_20$data), 1500)
186
expect_equal(round(as.numeric(sum(sampling_distribution_20$data))), 1244837)
187
})
188
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
189
expect_that((sampling_distribution_20$labels$x) == 'age', is_false())
190
expect_equal(sampling_distribution_20$labels$title, "n = 20")
191
})
192
193
print("Success!")
194
}
195
196
test_2.6 <- function(){
197
properties <- c(sampling_distribution_100$layers[[1]]$mapping, sampling_distribution_100$mapping)
198
test_that('sample_mean should be on the x-axis.', {
199
expect_true("sample_mean" == rlang::get_expr(properties$x))
200
})
201
test_that('sampling_distribution should be a histogram.', {
202
expect_that("GeomBar" %in% class(sampling_distribution_100$layers[[1]]$geom), is_true())
203
})
204
test_that('sampling_distribution data should be used to create the histogram', {
205
expect_equal(nrow(sampling_distribution_100$data), 1500)
206
expect_equal(round(as.numeric(sum(sampling_distribution_100$data))), 1244833)
207
})
208
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
209
expect_that((sampling_distribution_100$labels$x) == 'age', is_false())
210
expect_equal(sampling_distribution_100$labels$title, "n = 100")
211
})
212
213
print("Success!")
214
}
215
216
test_2.8 <- function(){
217
test_that('Solution is incorrect', {
218
expect_equal(digest(answer2.8), 'c1f86f7430df7ddb256980ea6a3b57a4')
219
})
220
print("Success!")
221
}
222
223
test_2.9 <- function(){
224
test_that('Solution is incorrect', {
225
expect_equal(digest(tolower(answer2.9)), 'd2a90307aac5ae8d0ef58e2fe730d38b')
226
})
227
print("Success!")
228
}
229