Path: blob/master/2022-spring/materials/worksheet_clustering/tests.R
2051 views
library(testthat)1library(digest)23# Round double to precise integer4#5# `int_round` works to create an integer corresponding to a number that is6# tested up to a particular decimal point of precision. This is useful when7# there is a need to compare a numeric value using hashes.8#9# @param x Double vector of length one.10# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.11#12# @return Integer vector of length one corresponding to a particular decimal point of precision.13#14# @examples15# # to get an integer up to two decimals of precision from 234.5678916# int_round(234.56789, 2)17#18# to get an integer rounded to the hundred digit from 234.5678919# int_round(234.56789, -2)20int_round <- function(x, digits){21x = x * 10^digits22xint = as.integer(x)23xint1 = xint + 1L24if (abs(xint - x) < abs(xint1 - x)){25return(xint)26}27else {28return(xint1)29}30}3132test_0.0 <- function(){33test_that('Solution is incorrect', {34expect_equal(digest(answer0.0), '01a75cb73d67b0f895ff0e61449c7bf8')35})36print("Success!")37}38test_0.1 <- function(){39test_that('Solution is incorrect', {40expect_equal(digest(answer0.1), 'd19d62a873f08af0488f0df720cfd293')41})42print("Success!")43}4445test_1.0 <- function(){46test_that('Did not create an object named beer', {47expect_true(exists("beer"))48})49test_that('beer should be a tibble.', {50expect_true('tbl' %in% class(beer))51})52test_that('beer does not contain the correct number of rows and/or columns.', {53expect_equal(dim(beer), c(2410, 8))54})55test_that('The beer tibble is missing columns.', {56expect_true("abv" %in% colnames(beer))57expect_true("ibu" %in% colnames(beer))58expect_true("id" %in% colnames(beer))59expect_true("name" %in% colnames(beer))60expect_true("style" %in% colnames(beer))61expect_true("brewery_id" %in% colnames(beer))62expect_true("ounces" %in% colnames(beer))63})64print("Success!")65}6667test_1.1 <- function(){68test_that('Did not create a plot named beer_plot', {69expect_true(exists("beer_plot"))70})71properties <- c(beer_plot$layers[[1]]$mapping, beer_plot$mapping)72labels <- beer_plot$labels73test_that('ibu should be on the x-axis.', {74expect_true("ibu" == rlang::get_expr(properties$x))75})76test_that('abv should be on the y-axis.', {77expect_true("abv" == rlang::get_expr(properties$y))78})79test_that('beer_plot should be a scatter plot.', {80expect_true("GeomPoint" %in% c(class(beer_plot$layers[[1]]$geom)))81})82test_that('Labels on the axes should be descriptive and human readable.', {83expect_false((labels$y) == 'abv')84expect_false((labels$x) == 'ibu')85})86print("Success!")87}8889test_1.2 <- function(){90test_that('Did not create an object named clean_beer', {91expect_true(exists("clean_beer"))92})93test_that('clean_beer should be a tibble.', {94expect_true('tbl' %in% class(clean_beer))95})96test_that('clean_beer should only contain the columns ibu and abv', {97expect_true("ibu" %in% colnames(clean_beer))98expect_true("abv" %in% colnames(clean_beer))99expect_false("id" %in% colnames(clean_beer))100expect_false("name" %in% colnames(clean_beer))101expect_false("style" %in% colnames(clean_beer))102expect_false("brewery_id" %in% colnames(clean_beer))103expect_false("ounces" %in% colnames(clean_beer))104})105test_that('clean_beer does not contain the correct number of rows and/or columns.', {106expect_equal(dim(clean_beer), c(1405, 2))107})108109print("Success!")110}111112test_1.3.1 <- function(){113test_that('Solution is incorrect', {114expect_equal(digest(answer1.3.1), '75f1160e72554f4270c809f041c7a776')115})116print("Success!")117}118119test_1.3.2 <- function(){120test_that('Did not create an object named scaled_beer', {121expect_true(exists("scaled_beer"))122})123test_that('scaled_beer should be a tibble.', {124expect_true('tbl' %in% class(scaled_beer))125})126test_that('scaled_beer does not contain the correct number of rows and/or columns.', {127expect_equal(dim(scaled_beer), c(1405, 2))128})129test_that('scaled_beer should only contain the columns ibu and abv', {130expect_true("ibu" %in% colnames(clean_beer))131expect_true("abv" %in% colnames(clean_beer))132expect_false("id" %in% colnames(clean_beer))133expect_false("name" %in% colnames(clean_beer))134expect_false("style" %in% colnames(clean_beer))135expect_false("brewery_id" %in% colnames(clean_beer))136expect_false("ounces" %in% colnames(clean_beer))137})138test_that('Columns in scaled_beer are not scaled correctly.', {139expect_true(min(scaled_beer$ibu) < 1)140expect_true(max(scaled_beer$ibu) < 4)141expect_true(min(scaled_beer$abv) < -2)142expect_true(max(scaled_beer$abv) < 5)143})144print("Success!")145}146147test_1.4 <- function(){148test_that('beer_cluster_k2 class should be kmeans', {149expect_equal(class(beer_cluster_k2), 'kmeans')150})151test_that('beer_cluster_k2 should have 2 centers', {152expect_equal(int_round(nrow(beer_cluster_k2$centers), 0), 2)153})154test_that('Solution is incorrect', {155expect_equal(int_round(beer_cluster_k2$tot.withinss, 0), 1110)156})157print("Success!")158}159160test_1.5 <- function(){161test_that('tidy_beer_cluster_k2 should contain the columns: abv, ibu, and .cluster', {162expect_true('abv' %in% colnames(tidy_beer_cluster_k2))163expect_true('ibu' %in% colnames(tidy_beer_cluster_k2))164expect_true('.cluster' %in% colnames(tidy_beer_cluster_k2))165})166test_that('tidy_beer_cluster_k2 contains an incorrect number of rows and/or columns.', {167expect_equal(int_round(nrow(tidy_beer_cluster_k2), 0), 1405)168expect_equal(int_round(ncol(tidy_beer_cluster_k2), 0), 3)169})170print("Success!")171}172173test_1.6 <- function(){174properties <- c(tidy_beer_cluster_k2_plot$layers[[1]]$mapping, tidy_beer_cluster_k2_plot$mapping)175labels <- tidy_beer_cluster_k2_plot$labels176test_that('Did not create a plot named tidy_beer_cluster_k2_plot', {177expect_true(exists("tidy_beer_cluster_k2_plot"))178})179test_that('tidy_beer_cluster_k2_plot should contain information from tidy_beer_cluster_k2', {180expect_equal(tidy_beer_cluster_k2_plot$data, tidy_beer_cluster_k2)181})182test_that('ibu should be on the x-axis.', {183expect_true("ibu" == rlang::get_expr(properties$x))184})185test_that('abv should be on the y-axis.', {186expect_true("abv" == rlang::get_expr(properties$y))187})188test_that('.cluster should be used to colour the points.', {189expect_true(".cluster" == rlang::get_expr(properties$colour))190})191test_that('tidy_beer_cluster_k2_plot should be a scatter plot.', {192expect_true("GeomPoint" %in% c(class(tidy_beer_cluster_k2_plot$layers[[1]]$geom)))193})194test_that('Labels on the axes should be descriptive and human readable.', {195expect_false((labels$y) == 'abv')196expect_false((labels$x) == 'ibu')197expect_false((labels$colour) == '.cluster')198})199print("Success!")200}201202test_1.7.1 <- function(){203test_that('Solution is incorrect', {204expect_equal(digest(answer1.7.1), '475bf9280aab63a82af60791302736f6')205})206print("Success!")207}208209test_1.7.2 <- function(){210test_that('beer_cluster_k2_model_stats should be a tibble.', {211expect_true('tbl' %in% class(beer_cluster_k2_model_stats))212})213test_that('beer_cluster_k2_model_stats should have 1 row of 4 different statistics.', {214expect_equal(dim(beer_cluster_k2_model_stats), c(1, 4))215})216test_that('beer_cluster_k2_model_stats should contain total within sum of squares (tot.withinss).', {217expect_true('tot.withinss' %in% colnames(beer_cluster_k2_model_stats))218})219print("Success!")220}221222test_1.8 <- function(){223test_that('beer_ks should be a tbl.', {224expect_true('tbl' %in% class(beer_ks))225})226test_that('beer_ks should have 1 column containing k values from 1 to 10.', {227expect_equal(int_round(nrow(beer_ks), 0), 10)228expect_equal(int_round(ncol(beer_ks), 0), 1)229expect_equal(colnames(beer_ks), 'k')230})231print("Success!")232}233234test_1.9 <- function(){235test_that('beer_clustering does not contain the correct number of rows and/or columns.', {236expect_equal(dim(beer_clustering), c(10, 2))237})238test_that('beer_clustering should contain the columns k and models', {239expect_true('k' %in% colnames(beer_clustering))240expect_true('models' %in% colnames(beer_clustering))241})242test_that('The models column in beer_clustering should be of class kmeans', {243expect_equal(class(beer_clustering$models[[1]]), 'kmeans')244})245print("Success!")246}247248test_2.0 <- function(){249test_that('beer_model_stats does not contain the correct number of rows and/or columns.', {250expect_equal(dim(beer_model_stats), c(10, 3))251})252test_that('beer_model_stats should contain the columns k, models, and model_statistics', {253expect_true('k' %in% colnames(beer_model_stats))254expect_true('models' %in% colnames(beer_model_stats))255expect_true('model_statistics' %in% colnames(beer_model_stats))256})257test_that('The models column in beer_model_stats should be of class kmeans', {258expect_equal(class(beer_model_stats$models[[1]]), 'kmeans')259})260test_that('The model_statistics column in beer_model_stats should be a tibble.', {261expect_true('tbl' %in% class(beer_model_stats$model_statistics[[1]]))262})263print("Success!")264}265266test_2.1 <- function(){267test_that('Solution is incorrect', {268expect_equal(int_round(nrow(beer_clustering_unnested), 0), 10)269expect_equal(int_round(ncol(beer_clustering_unnested), 0), 6)270expect_true('k' %in% colnames(beer_clustering_unnested))271expect_true('models' %in% colnames(beer_clustering_unnested))272expect_false('model_statistics' %in% colnames(beer_clustering_unnested))273expect_equal(class(beer_clustering_unnested$models[[1]]), 'kmeans')274expect_true('tot.withinss' %in% colnames(beer_clustering_unnested))275})276print("Success!")277}278279280test_2.2 <- function(){281properties <- c(choose_beer_k$layers[[1]]$mapping, choose_beer_k$mapping)282labels <- choose_beer_k$labels283test_that('Did not create a plot named choose_beer_k', {284expect_true(exists("choose_beer_k"))285})286test_that('# clusters should be on the x-axis.', {287expect_true("k" == rlang::get_expr(properties$x))288})289test_that('total within-cluster sum-of-squares should be on the y-axis.', {290expect_true("tot.withinss" == rlang::get_expr(properties$y))291})292test_that('choose_beer_k should be a line and scatter plot.', {293expect_true("GeomLine" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)))294})295test_that('choose_beer_k should be a line and scatter plot.', {296expect_true("GeomPoint" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)))297})298test_that('Labels on the axes should be descriptive and human readable.', {299expect_false((labels$y) == 'tot.withinss')300expect_false((labels$x) == 'k')301})302print("Success!")303}304305test_2.3 <- function(){306test_that('Solution is incorrect', {307expect_true(digest(answer2.3) %in% c('0e4033b8c0b56afbea35dc749ced4e1d', 'd19d62a873f08af0488f0df720cfd293'))308})309print("Success!")310}311312test_2.4 <- function(){313test_that('Solution is incorrect', {314expect_equal(digest(answer2.4), '475bf9280aab63a82af60791302736f6')315})316print("Success!")317}318319test_2.5 <- function(){320test_that('Solution is incorrect', {321expect_equal(digest(answer2.5), '3a5505c06543876fe45598b5e5e5195d')322})323print("Success!")324}325326test_2.6 <- function(){327test_that('Solution is incorrect', {328expect_equal(digest(answer2.6), '05ca18b596514af73f6880309a21b5dd')329})330print("Success!")331}332333334