Path: blob/master/2022-spring/materials/tutorial_regression2/tests.R
2051 views
library(testthat)1library(digest)2library(rlang)34# Round double to precise integer5#6# `int_round` works to create an integer corresponding to a number that is7# tested up to a particular decimal point of precision. This is useful when8# 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# @examples16# # to get an integer up to two decimals of precision from 234.5678917# int_round(234.56789, 2)18#19# to get an integer rounded to the hundred digit from 234.5678920# int_round(234.56789, -2)21int_round <- function(x, digits){22x = x*10^digits23xint = as.integer(x)24xint1 = xint + 1L25if (abs(xint - x) < abs(xint1 - x)){26return(xint)27}28else {29return(xint1)30}31}3233test_1.0 <- function() {34test_that('Did not create an object named credit', {35expect_true(exists("credit"))36})37test_that('credit should be a tibble.', {38expect_true('tbl' %in% class(credit))39})40test_that('credit does not contain the correct number of rows and/or columns.', {41expect_equal(dim(credit), c(400, 12))42})43test_that('The credit tibble is missing columns.', {44expect_true("Income" %in% colnames(credit))45expect_true("Balance" %in% colnames(credit))46})47test_that('credit does not contain the correct data.', {48expect_equal(digest(int_round(sum(credit$Income), 2)), '7b41cc2ef140f2cfb4b6eb86ccebf416')49expect_equal(digest(int_round(sum(credit$Limit), 2)), '1bc8a53a9b0cc2ea3cf99f2306872029')50})51print("Success!")52}5354test_1.1 <- function() {55test_that('credit should be a tibble.', {56expect_true('tbl' %in% class(credit))57})58test_that('credit does not contain the correct number of rows and/or columns.', {59expect_equal(dim(credit), c(400, 3))60})61test_that('The credit data frame should not contain the column X1', {62expect_false("X1" %in% colnames(credit))63})64test_that('The credit data frame is missing columns.', {65expect_true("Income" %in% colnames(credit))66expect_true("Balance" %in% colnames(credit))67expect_true("Rating" %in% colnames(credit))68})69print("Success!")70}7172test_1.2 <- function() {73test_that('Did not create an object called credit_split.', {74expect_true(exists('credit_split'))75})76test_that('credit_split is not an r_split object.', {77expect_true('rsplit' %in% class(credit_split))78})79test_that('Did not create an object called credit_training.', {80expect_true(exists('credit_training'))81})82test_that('credit_training is not a tibble.',{83expect_true('tbl' %in% class(credit_training))84})85test_that('credit_training does not contain 60% of the data.', {86expect_equal(dim(credit_training), c(241, 3))87expect_equal(digest(int_round(sum(credit_training$Balance), 2)), '273157f18270727a00bfb2d4bff79903')88expect_equal(digest(int_round(sum(credit_training$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')89expect_equal(digest(int_round(sum(credit_training$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')90})91test_that('Did not create an object called credit_testing.', {92expect_true(exists('credit_testing'))93})94test_that('credit_testing is not a tibble.', {95expect_true('tbl' %in% class(credit_training))96})97test_that('credit_testing does not contain the remaining 40% of the data.', {98expect_equal(dim(credit_testing), c(159, 3))99expect_equal(digest(int_round(sum(credit_testing$Balance), 2)), 'b0fe1ffce8158f92072c0c27ad1f181f')100expect_equal(digest(int_round(sum(credit_testing$Income), 2)), '581f089d90262775f263a2eb16c22480')101expect_equal(digest(int_round(sum(credit_testing$Rating), 2)), '26a325d7669edb2146d4a17935fe2636')102})103print("Success!")104}105106test_1.3 <- function() {107test_that('credit_eda should be a pairwise plot matrix.', {108expect_true('ggmatrix' %in% c(class(credit_eda)))109})110test_that('credit_eda should be using data from the credit data frame.', {111expect_equal(int_round(nrow(credit_eda$data), 0), 241)112})113test_that('credit_eda should be using the Balance, Income, Rating, Limit columns.', {114expect_equal(int_round(ncol(credit_eda$data), 0), 3)115})116print("Success!")117}118119test_1.4 <- function() {120test_that('Did not create an object called answer1.4.', {121expect_true(exists('answer1.4'))122})123test_that('Solution is incorrect.',{124expect_equal(digest(answer1.4), '475bf9280aab63a82af60791302736f6')125})126print("Success!")127}128129test_1.5 <- function() {130test_that('Did not create an object called lm_spec.', {131expect_true(exists('lm_spec'))132})133test_that('lm_spec should be a linear regression model specification.', {134expect_true('linear_reg' %in% class(lm_spec))135})136test_that('Did not create an object called credit_recipe.', {137expect_true(exists('credit_recipe'))138})139test_that('credit_recipe should be a recipe.', {140expect_true('recipe' %in% class(credit_recipe))141})142test_that('credit_recipe does not contain the training data.', {143expect_equal(dim(credit_recipe$template), c(241, 3))144expect_equal(digest(int_round(sum(credit_recipe$template$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')145expect_equal(digest(int_round(sum(credit_recipe$template$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')146expect_equal(digest(int_round(sum(credit_recipe$template$Balance), 2)), '273157f18270727a00bfb2d4bff79903')147})148print("Success!")149}150151test_1.6 <- function() {152test_that('Did not create an object called credit_fit.', {153expect_true(exists('credit_fit'))154})155test_that('credit_fit should be a workflow.', {156expect_true('workflow' %in% class(credit_fit))157})158test_that('credit_fit does not contain the training data.', {159expect_equal(digest(int_round(sum(credit_fit$pre$mold$predictors$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')160expect_equal(digest(int_round(sum(credit_fit$pre$mold$predictors$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')161})162print("Success!")163}164165test_1.7 <- function(){166test_that('Did not create an object called answer1.7.', {167expect_true(exists('answer1.7'))168})169test_that('Solution is incorrect.', {170expect_equal(digest(answer1.7), '75f1160e72554f4270c809f041c7a776')171})172print("Success!")173}174175test_1.8 <- function() {176test_that('Solution is incorrect.', {177expect_equal(digest(int_round(lm_rmse, 2)), 'fc3357631f940d71d5027270ad54a2cf')178})179print("Success!")180}181182test_1.9 <- function() {183test_that('Solution is incorrect.', {184expect_equal(digest(int_round(lm_rmspe, 2)), 'ec726f8469b8d46ed8231dcbcca420e2')185})186print("Success!")187}188189test_1.9.2 <- function(){190test_that('Did not create an object called answer1.9.2', {191expect_true(exists('answer1.9.2'))192})193test_that('Solution is incorrect', {194expect_equal(digest(answer1.9.2), '475bf9280aab63a82af60791302736f6')195})196print("Success!")197}198199200