Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-spring/materials/tutorial_09/tests_tutorial_09.R
2051 views
1
library(testthat)
2
library(digest)
3
library(rlang)
4
5
#' Round double to precise integer
6
#'
7
#' `int_round` works to create an integer corresponding to a number that is
8
#' tested up to a particular decimal point of precision. This is useful when
9
#' there is a need to compare a numeric value using hashes.
10
#'
11
#' @param x Double vector of length one.
12
#' @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.
13
#'
14
#' @return Integer vector of length one corresponding to a particular decimal point of precision.
15
#'
16
#' @examples
17
#' # to get an integer up to two decimals of precision from 234.56789
18
#' int_round(234.56789, 2)
19
#'
20
#' to get an integer rounded to the hundred digit from 234.56789
21
#' int_round(234.56789, -2)
22
int_round <- function(x, digits){
23
x = x*10^digits
24
xint = as.integer(x)
25
xint1 = xint + 1L
26
if (abs(xint - x) < abs(xint1 - x)){
27
return(xint)
28
}
29
else {
30
return(xint1)
31
}
32
}
33
34
test_1.0 <- function() {
35
test_that('Did not create an object named credit', {
36
expect_true(exists("credit"))
37
})
38
test_that('credit should be a tibble.', {
39
expect_true('tbl' %in% class(credit))
40
})
41
test_that('credit does not contain the correct number of rows and/or columns.', {
42
expect_equal(dim(credit), c(400, 12))
43
})
44
test_that('The credit tibble is missing columns.', {
45
expect_true("Income" %in% colnames(credit))
46
expect_true("Balance" %in% colnames(credit))
47
})
48
test_that('credit does not contain the correct data.', {
49
expect_equal(digest(int_round(sum(credit$Income), 2)), '7b41cc2ef140f2cfb4b6eb86ccebf416')
50
expect_equal(digest(int_round(sum(credit$Limit), 2)), '1bc8a53a9b0cc2ea3cf99f2306872029')
51
})
52
print("Success!")
53
}
54
55
test_1.1 <- function() {
56
test_that('credit should be a tibble.', {
57
expect_true('tbl' %in% class(credit))
58
})
59
test_that('credit does not contain the correct number of rows and/or columns.', {
60
expect_equal(dim(credit), c(400, 3))
61
})
62
test_that('The credit data frame should not contain the column X1', {
63
expect_false("X1" %in% colnames(credit))
64
})
65
test_that('The credit data frame is missing columns.', {
66
expect_true("Income" %in% colnames(credit))
67
expect_true("Balance" %in% colnames(credit))
68
expect_true("Rating" %in% colnames(credit))
69
})
70
print("Success!")
71
}
72
73
test_1.2 <- function() {
74
test_that('Did not create an object called credit_split.', {
75
expect_true(exists('credit_split'))
76
})
77
test_that('credit_split is not an r_split object.', {
78
expect_true('rsplit' %in% class(credit_split))
79
})
80
test_that('Did not create an object called credit_training.', {
81
expect_true(exists('credit_training'))
82
})
83
test_that('credit_training is not a tibble.',{
84
expect_true('tbl' %in% class(credit_training))
85
})
86
test_that('credit_training does not contain 60% of the data.', {
87
expect_equal(dim(credit_training), c(241, 3))
88
expect_equal(digest(int_round(sum(credit_training$Balance), 2)), '273157f18270727a00bfb2d4bff79903')
89
expect_equal(digest(int_round(sum(credit_training$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')
90
expect_equal(digest(int_round(sum(credit_training$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')
91
})
92
test_that('Did not create an object called credit_testing.', {
93
expect_true(exists('credit_testing'))
94
})
95
test_that('credit_testing is not a tibble.', {
96
expect_true('tbl' %in% class(credit_training))
97
})
98
test_that('credit_testing does not contain the remaining 40% of the data.', {
99
expect_equal(dim(credit_testing), c(159, 3))
100
expect_equal(digest(int_round(sum(credit_testing$Balance), 2)), 'b0fe1ffce8158f92072c0c27ad1f181f')
101
expect_equal(digest(int_round(sum(credit_testing$Income), 2)), '581f089d90262775f263a2eb16c22480')
102
expect_equal(digest(int_round(sum(credit_testing$Rating), 2)), '26a325d7669edb2146d4a17935fe2636')
103
})
104
print("Success!")
105
}
106
107
test_1.3 <- function() {
108
test_that('credit_eda should be a pairwise plot matrix.', {
109
expect_true('ggmatrix' %in% c(class(credit_eda)))
110
})
111
test_that('credit_eda should be using data from the credit data frame.', {
112
expect_equal(int_round(nrow(credit_eda$data), 0), 241)
113
})
114
test_that('credit_eda should be using the Balance, Income, Rating, Limit columns.', {
115
expect_equal(int_round(ncol(credit_eda$data), 0), 3)
116
})
117
print("Success!")
118
}
119
120
test_1.4 <- function() {
121
test_that('Did not create an object called answer1.4.', {
122
expect_true(exists('answer1.4'))
123
})
124
test_that('Solution is incorrect.',{
125
expect_equal(digest(answer1.4), '475bf9280aab63a82af60791302736f6')
126
})
127
print("Success!")
128
}
129
130
test_1.5 <- function() {
131
test_that('Did not create an object called lm_spec.', {
132
expect_true(exists('lm_spec'))
133
})
134
test_that('lm_spec should be a linear regression model specification.', {
135
expect_true('linear_reg' %in% class(lm_spec))
136
})
137
test_that('Did not create an object called credit_recipe.', {
138
expect_true(exists('credit_recipe'))
139
})
140
test_that('credit_recipe should be a recipe.', {
141
expect_true('recipe' %in% class(credit_recipe))
142
})
143
test_that('credit_recipe does not contain the training data.', {
144
expect_equal(dim(credit_recipe$template), c(241, 3))
145
expect_equal(digest(int_round(sum(credit_recipe$template$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')
146
expect_equal(digest(int_round(sum(credit_recipe$template$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')
147
expect_equal(digest(int_round(sum(credit_recipe$template$Balance), 2)), '273157f18270727a00bfb2d4bff79903')
148
})
149
print("Success!")
150
}
151
152
test_1.6 <- function() {
153
test_that('Did not create an object called credit_fit.', {
154
expect_true(exists('credit_fit'))
155
})
156
test_that('credit_fit should be a workflow.', {
157
expect_true('workflow' %in% class(credit_fit))
158
})
159
test_that('credit_fit does not contain the training data.', {
160
expect_equal(digest(int_round(sum(credit_fit$pre$mold$predictors$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')
161
expect_equal(digest(int_round(sum(credit_fit$pre$mold$predictors$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')
162
})
163
print("Success!")
164
}
165
166
test_1.7 <- function(){
167
test_that('Did not create an object called answer1.7.')
168
expect_true(exists('answer1.7'))
169
test_that('Solution is incorrect.', {
170
expect_equal(digest(answer1.7), '75f1160e72554f4270c809f041c7a776')
171
})
172
}
173
174
test_1.8 <- function() {
175
test_that('Solution is incorrect.', {
176
expect_equal(digest(int_round(lm_rmse, 2)), 'fc3357631f940d71d5027270ad54a2cf')
177
})
178
print("Success!")
179
}
180
181
test_1.9 <- function() {
182
test_that('Solution is incorrect.', {
183
expect_equal(digest(int_round(lm_rmspe, 2)), 'ec726f8469b8d46ed8231dcbcca420e2')
184
})
185
print("Success!")
186
}
187
188
test_1.9.2 <- function(){
189
test_that('Did not create an object called answer1.9.2', {
190
expect_true(exists('answer1.9.2'))
191
})
192
test_that('Solution is incorrect', {
193
expect_equal(digest(answer1.9.2), '475bf9280aab63a82af60791302736f6')
194
})
195
print("Success!")
196
}
197
198