Path: blob/master/2021-fall/materials/worksheet_11/tests_worksheet_11.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}3132# function to extract attributes from cowplot objects33# source: https://stackoverflow.com/questions/54051576/extracting-individual-plot-details-from-combined-plot-in-cowplot-for-unit-test?answertab=votes#tab-top34fun <- function(p, what) {35unlist(sapply(p$layers, function(x) {36idx <- which(x$geom_params$grob$layout$name == what)37x$geom_params$grob$grobs[[idx]]$children[[1]]$label38}))39}4041test_1.0 <- function(){42test_that('Solution is incorrect', {43expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')44})45print("Success!")46}4748test_1.1 <- function(){49test_that('variables should be assigned numeric values (do not include the definition in your answer, just the number associated with the definition)', {50values <- c(point_estimate, population, random_sampling, representative_sampling, population_parameter, sample, observation, sampling_distribution)51expect_is(values, 'numeric')52})5354test_that('At least one term-definition match is incorrect', {55expect_equal(digest(int_round(point_estimate, 0)), '25e6a154090e35101d7678d6f034353a')56expect_equal(digest(int_round(population, 0)), '4b5630ee914e848e8d07221556b0a2fb')57expect_equal(digest(int_round(random_sampling, 0)), 'c01f179e4b57ab8bd9de309e6d576c48')58expect_equal(digest(int_round(representative_sampling, 0)), '7c7124efff5c7039a1b1e7cba65c5379')59expect_equal(digest(int_round(population_parameter, 0)), '11946e7a3ed5e1776e81c0f0ecd383d0')60expect_equal(digest(int_round(sample, 0)), 'dd4ad37ee474732a009111e3456e7ed7')61expect_equal(digest(int_round(observation, 0)), '9d08099943f8627959cfb8ecee0d2f5d')62expect_equal(digest(int_round(sampling_distribution, 0)), '234a2a5581872457b9fe1187d1616b13')63})64print("Success!")65}6667test_1.2 <- function(){68properties <- c(pop_dist$layers[[1]]$mapping, pop_dist$mapping)69labels <- pop_dist$labels70test_that('age should be on the x-axis.', {71expect_true("age" == rlang::get_expr(properties$x))72})73test_that('pop_dist should be a histogram.', {74expect_true("GeomBar" %in% class(pop_dist$layers[[1]]$geom))75})76test_that('can_seniors data should be used to create the histogram', {77expect_equal(int_round(nrow(pop_dist$data), 0), 1027941)78expect_equal(digest(int_round(sum(pop_dist$data$age), 0)), '0a65b77971cd131982c7117a5ab90242')79})80test_that('Labels on the x axis should be descriptive and human readable.', {81expect_false((labels$x) == 'age')82})83print("Success!")84}8586test_1.3 <- function(){87test_that('pop_parameters has 3 columns and one row, with column names pop_mean, pop_med and pop_sd.', {88expect_equal(int_round(nrow(pop_parameters), 0), 1)89expect_equal(int_round(ncol(pop_parameters), 0), 3)90expect_equal(digest(paste(sort(colnames(pop_parameters)), collapse = "")), '723d282ea6dad216da6b1074ca7cf688')91})92print("Success!")93}9495test_1.4 <- function(){96test_that('sample_1 should have 2 columns and 40 rows', {97expect_equal(int_round(nrow(sample_1), 0), 40)98expect_equal(int_round(ncol(sample_1), 0), 2)99})100test_that('the column names of sample_1 should be replicate and age', {101expect_equal(digest(paste(sort(colnames(sample_1)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')102})103print("Success!")104}105106test_1.5 <- function(){107properties <- c(sample_1_dist$layers[[1]]$mapping, sample_1_dist$mapping)108labels <- sample_1_dist$labels109test_that('age should be on the x-axis.', {110expect_true("age" == rlang::get_expr(properties$x))111})112test_that('sample_1_dist should be a histogram.', {113expect_true("GeomBar" %in% class(sample_1_dist$layers[[1]]$geom))114})115test_that('sample_1 data should be used to create the histogram', {116expect_equal(int_round(nrow(sample_1_dist$data), 0), 40)117expect_equal(digest(int_round(sum(sample_1_dist$data$age), 2)), 'f856ba7ffab8e669473a2ee7bf49de52')118})119test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {120expect_false((labels$x) == 'age')121expect_false(is.null(labels$title))122})123print("Success!")124}125126test_1.6 <- function(){127test_that('sample_1_estimates should have at least 3 columns, and 1 row', {128expect_equal(int_round(nrow(sample_1_estimates), 0), 1)129expect_true(int_round(ncol(sample_1_estimates), 0) >= 3)130})131test_that('sample_1_estimates has columns with correct names', {132expect_true("sample_1_mean" %in% colnames(sample_1_estimates))133expect_true("sample_1_med" %in% colnames(sample_1_estimates))134expect_true("sample_1_sd" %in% colnames(sample_1_estimates))135})136print("Success!")137}138139test_1.7 <- function(){140test_that('Solution is incorrect', {141expect_equal(digest(answer1.7), '475bf9280aab63a82af60791302736f6')142})143print("Success!")144}145146147test_1.8.0 <- function(){148test_that('sample_2 should have 2 columns and 40 rows', {149expect_equal(int_round(nrow(sample_2), 0), 40)150expect_equal(int_round(ncol(sample_2), 0), 2)151})152test_that('the column names of sample_2 should be replicate and age', {153expect_equal(digest(paste(sort(colnames(sample_2)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')154})155properties <- c(sample_2_dist$layers[[1]]$mapping, sample_2_dist$mapping)156labels <- sample_2_dist$labels157test_that('age should be on the x-axis.', {158expect_true("age" == rlang::get_expr(properties$x))159})160test_that('sample_2_dist should be a histogram.', {161expect_true("GeomBar" %in% class(sample_2_dist$layers[[1]]$geom))162})163test_that('sample_2 data should be used to create the histogram', {164expect_equal(int_round(nrow(sample_2_dist$data), 0), 40)165expect_equal(digest(int_round(sum(sample_2_dist$data$age), 2)), '199d472897c57c820c8c694f44d7786c')166})167test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {168expect_false((labels$x) == 'age')169expect_false(is.null(labels$title))170})171test_that('sample_2_estimates should have at least 3 columns, and 1 row', {172expect_equal(int_round(nrow(sample_2_estimates), 0), 1)173expect_true(int_round(ncol(sample_2_estimates), 0) >= 3)174})175test_that('sample_2_estimates has columns with correct names', {176expect_true("sample_2_mean" %in% colnames(sample_2_estimates))177expect_true("sample_2_med" %in% colnames(sample_2_estimates))178expect_true("sample_2_sd" %in% colnames(sample_2_estimates))179})180print("Success!")181}182183test_1.8.1 <- function(){184test_that('Solution is incorrect', {185expect_equal(digest(answer1.8.1), '475bf9280aab63a82af60791302736f6')186})187print("Success!")188}189190test_1.9 <- function(){191test_that('samples should have 60000 rows and 2 columns', {192expect_equal(int_round(ncol(samples), 0), 2)193expect_equal(int_round(nrow(samples), 0), 60000)194})195test_that('the column names of samples should be replicate and age', {196expect_equal(digest(paste(sort(colnames(samples)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')197})198print("Success!")199}200201test_2.0 <- function(){202test_that('sample_estimates should have 1500 rows and 2 columns', {203expect_equal(int_round(ncol(sample_estimates), 0), 2)204expect_equal(int_round(nrow(sample_estimates), 0), 1500)205})206test_that('the column names of sample_estimates should be replicate and sample_mean', {207expect_equal(digest(paste(sort(colnames(sample_estimates)), collapse = "")), '7453089f8086e9a98a067f3eeac63363')208})209print("Success!")210}211212test_2.1 <- function(){213properties <- c(sampling_distribution$layers[[1]]$mapping, sampling_distribution$mapping)214labels <- sampling_distribution$labels215test_that('sample_mean should be on the x-axis.', {216expect_true("sample_mean" == rlang::get_expr(properties$x))217})218test_that('sampling_distribution should be a histogram.', {219expect_true("GeomBar" %in% class(sampling_distribution$layers[[1]]$geom))220})221test_that('sampling_distribution data should be used to create the histogram', {222expect_equal(int_round(nrow(sampling_distribution$data), 0), 1500)223expect_equal(digest(int_round(sum(sampling_distribution$data$sample_mean), 2)), 'e20a3a6689ccb7122ce8aaa71bab55bf')224})225test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {226expect_false((labels$x) == 'age')227expect_false(is.null(labels$title))228})229print("Success!")230}231232test_2.2 <- function(){233test_that('Solution is incorrect', {234expect_equal(digest(int_round(answer2.2, 2)), '0ddc7e7a0d2654650cba2f2a15cbca52')235})236print("Success!")237}238239test_2.3 <- function(){240test_that('Solution is incorrect', {241expect_equal(digest(answer2.3), '3a5505c06543876fe45598b5e5e5195d')242})243print("Success!")244}245246test_2.4 <- function(){247test_that('Solution is incorrect', {248expect_equal(digest(tolower(answer2.4)), '05ca18b596514af73f6880309a21b5dd')249})250print("Success!")251}252253test_2.5 <- function(){254properties <- c(sampling_distribution_20$layers[[1]]$mapping, sampling_distribution_20$mapping)255labels <- sampling_distribution_20$labels256test_that('sample_mean should be on the x-axis.', {257expect_true("sample_mean" == rlang::get_expr(properties$x))258})259test_that('sampling_distribution should be a histogram.', {260expect_true("GeomBar" %in% class(sampling_distribution_20$layers[[1]]$geom))261})262test_that('sampling_distribution data should be used to create the histogram', {263expect_equal(int_round(nrow(sampling_distribution_20$data), 0), 1500)264expect_equal(digest(int_round(sum(sampling_distribution_20$data$sample_mean), 2)), '49a66adc63b05e7e8f90b66202de0b84')265})266test_that('Labels on the x axis should be descriptive. The plot should have the title n = 20.', {267expect_false((labels$x) == 'age')268expect_equal(labels$title, "n = 20")269})270271print("Success!")272}273274test_2.6 <- function(){275properties <- c(sampling_distribution_100$layers[[1]]$mapping, sampling_distribution_100$mapping)276labels <- sampling_distribution_100$labels277test_that('sample_mean should be on the x-axis.', {278expect_true("sample_mean" == rlang::get_expr(properties$x))279})280test_that('sampling_distribution should be a histogram.', {281expect_true("GeomBar" %in% class(sampling_distribution_100$layers[[1]]$geom))282})283test_that('sampling_distribution data should be used to create the histogram', {284expect_equal(int_round(nrow(sampling_distribution_100$data), 0), 1500)285expect_equal(digest(int_round(sum(sampling_distribution_100$data$sample_mean), 2)), '59c92b151db8f38ba93a364fd62ae7c9')286})287test_that('Labels on the x axis should be descriptive. The plot should have the title n = 100.', {288expect_false((labels$x) == 'age')289expect_equal(labels$title, "n = 100")290})291292print("Success!")293}294295test_2.7 <- function(){296test_that('object is named sampling_distribution_panel.', {297expect_true(exists("sampling_distribution_panel"))298})299test_that('sampling distributions are plotted side-by-side with the correct titles of n = 20, "n = 40, and n = 100', {300expect_equal(fun(sampling_distribution_panel, "title"), c("n = 20", "n = 40", "n = 100"))301})302print("Success!")303}304305test_2.8 <- function(){306test_that('Solution is incorrect', {307expect_equal(digest(answer2.8), 'c1f86f7430df7ddb256980ea6a3b57a4')308})309print("Success!")310}311312test_2.9 <- function(){313test_that('Solution is incorrect', {314expect_equal(digest(tolower(answer2.9)), 'd2a90307aac5ae8d0ef58e2fe730d38b')315})316print("Success!")317}318319320