Path: blob/master/2022-spring/materials/tutorial_inference1/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_1.0 <- function(){33properties <- c(pop_dist$layers[[1]]$mapping, pop_dist$mapping)34test_that('grade should be on the x-axis.', {35expect_true("grade" == rlang::get_expr(properties$x))36})37test_that('pop_dist should be a histogram.', {38expect_true("GeomBar" %in% class(pop_dist$layers[[1]]$geom))39})40test_that('students_pop data should be used to create the histogram', {41expect_equal(int_round(nrow(pop_dist$data), 0), 10000)42expect_equal(digest(int_round(sum(pop_dist$data), 2)), '3136d0838b3ab31dbff61d1de4659826')43})44#test_that('Labels on the x axis should be descriptive and human readable.', {45#expect_that((pop_dist$labels$x) == 'grade', is_false())46# })47print("Success!")48}4950test_1.2 <- function(){51test_that('pop_parameters has 3 columns and one row, with column names pop_mean, pop_med and pop_sd.', {52expect_equal(int_round(nrow(pop_parameters), 0), 1)53expect_equal(int_round(ncol(pop_parameters), 0), 3)54expect_equal(digest(int_round(pop_parameters$pop_mean, 2)), '4eb3276e1ae5b53d7b41cd23f552d8cf')55expect_equal(digest(int_round(pop_parameters$pop_sd, 2)), '8c0a944402768b5b9b1e68abfeabbe7b')56})57print("Success!")58}5960test_1.2.1 <- function(){61test_that('ests_5 has one row, with column names mean_5, med_5 and sd_5.', {62expect_equal(int_round(nrow(ests_5), 0), 1)63expect_true("mean_5" %in% colnames(ests_5))64expect_true("med_5" %in% colnames(ests_5))65expect_true("sd_5" %in% colnames(ests_5))66expect_equal(digest(int_round(ests_5$mean_5, 1)), 'aa945d05c992fcdb745752143342acd3')67expect_equal(digest(int_round(ests_5$sd_5, 1)), '98616fd038a28bd3aac2db8f903ebb2d')68})69print("Success!")70}7172test_1.2.2 <- function(){73test_that('Solution is incorrect', {74expect_equal(digest(answer1.2.2), '3a5505c06543876fe45598b5e5e5195d')75})76print("Success!")77}7879test_1.2.3 <- function(){80test_that('ests_100 has one row, with column names mean_100, med_100 and sd_100.', {81expect_equal(int_round(nrow(ests_100), 0), 1)82expect_true("mean_100" %in% colnames(ests_100))83expect_true("med_100" %in% colnames(ests_100))84expect_true("sd_100" %in% colnames(ests_100))85expect_equal(digest(int_round(ests_100$mean_100, 1)), '13d94d89c771d00b29ac574f6a3ebaf6')86expect_equal(digest(int_round(ests_100$sd_100, 1)), 'e4ced46f028c228b02d106d874876fe8')87})88print("Success!")89}9091test_1.3 <- function(){92test_that('samples should have 7500 rows and 2 columns', {93expect_equal(int_round(ncol(samples), 0), 2)94expect_equal(int_round(nrow(samples), 0), 7500)95})96test_that('the column names of samples should be replicate and grade', {97expect_equal(digest(paste(sort(colnames(samples)), collapse = "")), '0454d7f37ea4f0b0109a37b637be0481')98})99test_that('size and rep arguments are incorrect', {100expect_equal(digest(int_round(sum(attr(samples, "groups")$replicate), 2)), '976edfa66891286b03609c35209572f5')101})102print("Success!")103}104105test_1.4 <- function(){106test_that('sample_estimates should have 1500 rows and 2 columns', {107expect_equal(int_round(ncol(sample_estimates), 0), 2)108expect_equal(int_round(nrow(sample_estimates), 0), 1500)109})110test_that('the column names of sample_estimates should be replicate and sample_mean', {111expect_equal(digest(paste(sort(colnames(sample_estimates)), collapse = "")), '7453089f8086e9a98a067f3eeac63363')112})113print("Success!")114}115116test_1.5 <- function(){117properties <- c(sampling_distribution_5$layers[[1]]$mapping, sampling_distribution_5$mapping)118labels <- sampling_distribution_5$labels119test_that('sample_mean should be on the x-axis.', {120expect_true("sample_mean" == rlang::get_expr(properties$x))121})122test_that('sampling_distribution_5 should be a histogram.', {123expect_true("GeomBar" %in% class(sampling_distribution_5$layers[[1]]$geom))124})125test_that('sampling_distribution data should be used to create the histogram', {126expect_equal(int_round(nrow(sampling_distribution_5$data), 0), 1500)127expect_equal(digest(int_round(sum(sampling_distribution_5$data), 2)), 'b4152c2ce7dc65945435c167ebaed7be')128})129test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {130expect_false((labels$x) == 'sample_mean')131expect_false(is.null(labels$title))132})133print("Success!")134}135136#test_1.8 <- function(){137# properties <- c(sampling_distribution_5$layers[[1]]$mapping, sampling_distribution_5$mapping)138# labels <- sampling_distribution_5$labels139# test_that('sample_mean should be on the x-axis.', {140# expect_true("sample_mean" == rlang::get_expr(properties$x))141# })142# test_that('sampling_distribution_5 should be a histogram.', {143# expect_true("GeomBar" %in% class(sampling_distribution_5$layers[[1]]$geom))144# })145# test_that('sampling_distribution data should be used to create the histogram', {146# expect_equal(int_round(nrow(sampling_distribution_5$data), 0), 1500)147# expect_equal(digest(int_round(sum(sampling_distribution_5$data), 2)), '18d1759ac02cbbc83c6f2d30e733a093')148# })149# test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {150# expect_false((labels$x) == 'cups')151# expect_false(is.null(labels$title))152# })153# test_that('size and rep values are incorrect', {154# test_that(digest(int_round(sum(attr(samples, "groups")$replicate), 2)), '976edfa66891286b03609c35209572f5')155# })156# print("Success!")157#}158159test_1.8 <- function(){160properties <- c(coffee_sampling_distribution_5$layers[[1]]$mapping, coffee_sampling_distribution_5$mapping)161labels <- coffee_sampling_distribution_5$labels162test_that('Did not create an object called coffee_samples_5', {163expect_true(exists('coffee_samples_5'))164})165test_that('coffee_samples_5 does not contain the correct data', {166expect_equal(dim(coffee_samples_5), c(7500, 2))167expect_equal(digest(int_round(sum(coffee_samples_5$replicate), 2)), '85af0fba15fcfe28a2f8ef66fdcccbcf')168})169test_that('Did not create an object called coffee_sample_estimates_5', {170expect_true(exists('coffee_sample_estimates_5'))171})172test_that('coffee_sample_estimates_5 does not contain a column called coffee_sample_mean_5', {173expect_true('coffee_sample_mean_5' %in% colnames(coffee_sample_estimates_5))174})175test_that('coffee_sample_estimates_5 does not contain the correct data', {176expect_equal(dim(coffee_sample_estimates_5), c(1500, 2))177expect_equal(digest(int_round(sum(coffee_sample_estimates_5$coffee_sample_mean_5), 2)), '1c69dbae0a048e8a95be34cbdd4ccb9f')178})179test_that('coffee_sample_mean_5 should be on the x-axis.', {180expect_true("coffee_sample_mean_5" == rlang::get_expr(properties$x))181})182test_that('coffee_sampling_distribution_5 should be a histogram.', {183expect_true("GeomBar" %in% class(coffee_sampling_distribution_5$layers[[1]]$geom))184})185test_that('coffee_sampling_distribution_5 data should be used to create the histogram', {186expect_equal(int_round(nrow(coffee_sampling_distribution_5$data), 0), 1500)187expect_equal(digest(int_round(sum(coffee_sampling_distribution_5$data), 2)), '0393ea270fd8d93548cce5b02d06d2cf')188})189test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {190expect_false((labels$x) == 'coffee_sample_mean_5')191expect_false(is.null(labels$title))192})193test_that('size and rep values are incorrect', {194expect_equal(digest(int_round(sum(attr(coffee_samples_5, "groups")$replicate), 2)), '976edfa66891286b03609c35209572f5')195})196print("Success!")197}198199#test_2.0 <- function(){200# properties <- c(sampling_distribution_30$layers[[1]]$mapping, sampling_distribution_30$mapping)201# labels <- sampling_distribution_30$labels202# test_that('sample_mean should be on the x-axis.', {203# expect_true("sample_mean" == rlang::get_expr(properties$x))204# })205# test_that('sampling_distribution_30 should be a histogram.', {206# expect_true("GeomBar" %in% class(sampling_distribution_30$layers[[1]]$geom))207# })208# test_that('sampling_distribution_30 data should be used to create the histogram', {209# expect_equal(int_round(nrow(sampling_distribution_30$data), 0), 1500)210# expect_equal(digest(int_round(sum(sampling_distribution_30$data), 2)), "94bb219a42d934ffd9d349ddcbfa74f2")211# })212# test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {213# expect_false((labels$x) == 'sample_mean')214# expect_false(is.null(labels$title))215# })216# print("Success!")217#}218219test_2.0 <- function(){220properties <- c(coffee_sampling_distribution_30$layers[[1]]$mapping, coffee_sampling_distribution_30$mapping)221labels <- coffee_sampling_distribution_30$labels222test_that('Did not create an object called coffee_samples_30', {223expect_true(exists('coffee_samples_30'))224})225test_that('coffee_samples_30 does not contain the correct data', {226expect_equal(dim(coffee_samples_30), c(45000, 2))227expect_equal(digest(int_round(sum(coffee_samples_30$replicate), 0)), 'e9f03592cbd42086a7a75ba269f05d5e')228})229test_that('Did not create an object called coffee_sample_estimates_30', {230expect_true(exists('coffee_sample_estimates_30'))231})232test_that('coffee_sample_estimates_30 does not contain a column called coffee_sample_mean_30', {233expect_true('coffee_sample_mean_30' %in% colnames(coffee_sample_estimates_30))234})235test_that('coffee_sample_estimates_30 does not contain the correct data', {236expect_equal(dim(coffee_sample_estimates_5), c(1500, 2))237expect_equal(digest(int_round(sum(coffee_sample_estimates_30$coffee_sample_mean_30), 2)), 'c7c6210820892e507d0173ba167d416f')238})239test_that('coffee_sample_mean_30 should be on the x-axis.', {240expect_true("coffee_sample_mean_30" == rlang::get_expr(properties$x))241})242test_that('coffee_sampling_distribution_30 should be a histogram.', {243expect_true("GeomBar" %in% class(coffee_sampling_distribution_30$layers[[1]]$geom))244})245test_that('coffee_sampling_distribution_30 data should be used to create the histogram', {246expect_equal(int_round(nrow(coffee_sampling_distribution_30$data), 0), 1500)247expect_equal(digest(int_round(sum(coffee_sampling_distribution_30$data), 2)), '87c3a46c577cba8da795a55da1f8c17b')248})249test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {250expect_false((labels$x) == 'coffee_sample_mean_30')251expect_false(is.null(labels$title))252})253test_that('size and rep values are incorrect', {254expect_equal(digest(int_round(sum(attr(coffee_samples_30, "groups")$replicate), 2)), '976edfa66891286b03609c35209572f5')255})256print("Success!")257}258259260