Path: blob/master/2020-fall/materials/worksheet_11/tests_worksheet_11.R
2710 views
# +1library(testthat)2library(digest)34int_round <- function(x, digits){5x = x*10^digits6xint = as.integer(x)7xint1 = xint + 1L8if (abs(xint - x) < abs(xint1 - x)){9return(xint)10}11else {12return(xint1)13}14}15# -1617test_1.0 <- function(){18test_that('Solution is incorrect', {19expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')20})21print("Success!")22}2324test_1.1 <- function(){25test_that('variables should be assigned numeric values (do not include the definition in your answer, just the number associated with the definition)', {26values <- c(point_estimate, population, random_sampling, representative_sampling, population_parameter, sample, observation, sampling_distribution)27expect_is(values, 'numeric')28})2930test_that('At least one term-definition match is incorrect', {31expect_equal(digest(int_round(point_estimate, 0)), '25e6a154090e35101d7678d6f034353a')32expect_equal(digest(int_round(population, 0)), '4b5630ee914e848e8d07221556b0a2fb')33expect_equal(digest(int_round(random_sampling, 0)), 'c01f179e4b57ab8bd9de309e6d576c48')34expect_equal(digest(int_round(representative_sampling, 0)), '7c7124efff5c7039a1b1e7cba65c5379')35expect_equal(digest(int_round(population_parameter, 0)), '11946e7a3ed5e1776e81c0f0ecd383d0')36expect_equal(digest(int_round(sample, 0)), 'dd4ad37ee474732a009111e3456e7ed7')37expect_equal(digest(int_round(observation, 0)), '9d08099943f8627959cfb8ecee0d2f5d')38expect_equal(digest(int_round(sampling_distribution, 0)), '234a2a5581872457b9fe1187d1616b13')39})40print("Success!")41}4243test_1.2 <- function(){44properties <- c(pop_dist$layers[[1]]$mapping, pop_dist$mapping)45labels <- pop_dist$labels46test_that('age should be on the x-axis.', {47expect_true("age" == rlang::get_expr(properties$x))48})49test_that('pop_dist should be a histogram.', {50expect_true("GeomBar" %in% class(pop_dist$layers[[1]]$geom))51})52test_that('can_seniors data should be used to create the histogram', {53expect_equal(int_round(nrow(pop_dist$data), 0), 1027941)54expect_equal(digest(int_round(sum(pop_dist$data$age), 0)), '0a65b77971cd131982c7117a5ab90242')55})56test_that('Labels on the x axis should be descriptive and human readable.', {57expect_false((labels$x) == 'age')58})59print("Success!")60}6162test_1.3 <- function(){63test_that('pop_parameters has 3 columns and one row, with column names pop_mean, pop_med and pop_sd.', {64expect_equal(int_round(nrow(pop_parameters), 0), 1)65expect_equal(int_round(ncol(pop_parameters), 0), 3)66expect_equal(digest(paste(sort(colnames(pop_parameters)), collapse = "")), '723d282ea6dad216da6b1074ca7cf688')67})68print("Success!")69}7071test_1.4 <- function(){72test_that('sample_1 should have 2 columns and 40 rows', {73expect_equal(int_round(nrow(sample_1), 0), 40)74expect_equal(int_round(ncol(sample_1), 0), 2)75})76test_that('the column names of sample_1 should be replicate and age', {77expect_equal(digest(paste(sort(colnames(sample_1)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')78})79print("Success!")80}8182test_1.5 <- function(){83properties <- c(sample_1_dist$layers[[1]]$mapping, sample_1_dist$mapping)84labels <- sample_1_dist$labels85test_that('age should be on the x-axis.', {86expect_true("age" == rlang::get_expr(properties$x))87})88test_that('sample_1_dist should be a histogram.', {89expect_true("GeomBar" %in% class(sample_1_dist$layers[[1]]$geom))90})91test_that('sample_1 data should be used to create the histogram', {92expect_equal(int_round(nrow(sample_1_dist$data), 0), 40)93expect_equal(digest(int_round(sum(sample_1_dist$data$age), 2)), 'f856ba7ffab8e669473a2ee7bf49de52')94})95test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {96expect_false((labels$x) == 'age')97expect_false(is.null(labels$title))98})99print("Success!")100}101102test_1.6 <- function(){103test_that('sample_1_estimates should have at least 3 columns, and 1 row', {104expect_equal(int_round(nrow(sample_1_estimates), 0), 1)105expect_true(int_round(ncol(sample_1_estimates), 0) >= 3)106})107test_that('sample_1_estimates has columns with correct names', {108expect_true("sample_1_mean" %in% colnames(sample_1_estimates))109expect_true("sample_1_med" %in% colnames(sample_1_estimates))110expect_true("sample_1_sd" %in% colnames(sample_1_estimates))111})112print("Success!")113}114115test_1.7 <- function(){116test_that('Solution is incorrect', {117expect_equal(digest(answer1.7), '475bf9280aab63a82af60791302736f6')118})119print("Success!")120}121122123test_1.8.0 <- function(){124test_that('sample_2 should have 2 columns and 40 rows', {125expect_equal(int_round(nrow(sample_2), 0), 40)126expect_equal(int_round(ncol(sample_2), 0), 2)127})128test_that('the column names of sample_2 should be replicate and age', {129expect_equal(digest(paste(sort(colnames(sample_2)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')130})131properties <- c(sample_2_dist$layers[[1]]$mapping, sample_2_dist$mapping)132labels <- sample_2_dist$labels133test_that('age should be on the x-axis.', {134expect_true("age" == rlang::get_expr(properties$x))135})136test_that('sample_2_dist should be a histogram.', {137expect_true("GeomBar" %in% class(sample_2_dist$layers[[1]]$geom))138})139test_that('sample_2 data should be used to create the histogram', {140expect_equal(int_round(nrow(sample_2_dist$data), 0), 40)141expect_equal(digest(int_round(sum(sample_2_dist$data$age), 2)), '199d472897c57c820c8c694f44d7786c')142})143test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {144expect_false((labels$x) == 'age')145expect_false(is.null(labels$title))146})147test_that('sample_2_estimates should have at least 3 columns, and 1 row', {148expect_equal(int_round(nrow(sample_2_estimates), 0), 1)149expect_true(int_round(ncol(sample_2_estimates), 0) >= 3)150})151test_that('sample_2_estimates has columns with correct names', {152expect_true("sample_2_mean" %in% colnames(sample_2_estimates))153expect_true("sample_2_med" %in% colnames(sample_2_estimates))154expect_true("sample_2_sd" %in% colnames(sample_2_estimates))155})156print("Success!")157}158159test_1.8.1 <- function(){160test_that('Solution is incorrect', {161expect_equal(digest(answer1.8.1), '475bf9280aab63a82af60791302736f6')162})163print("Success!")164}165166test_1.9 <- function(){167test_that('samples should have 60000 rows and 2 columns', {168expect_equal(int_round(ncol(samples), 0), 2)169expect_equal(int_round(nrow(samples), 0), 60000)170})171test_that('the column names of samples should be replicate and age', {172expect_equal(digest(paste(sort(colnames(samples)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')173})174print("Success!")175}176177test_2.0 <- function(){178test_that('sample_estimates should have 1500 rows and 2 columns', {179expect_equal(int_round(ncol(sample_estimates), 0), 2)180expect_equal(int_round(nrow(sample_estimates), 0), 1500)181})182test_that('the column names of sample_estimates should be replicate and sample_mean', {183expect_equal(digest(paste(sort(colnames(sample_estimates)), collapse = "")), '7453089f8086e9a98a067f3eeac63363')184})185print("Success!")186}187188test_2.1 <- function(){189properties <- c(sampling_distribution$layers[[1]]$mapping, sampling_distribution$mapping)190labels <- sampling_distribution$labels191test_that('sample_mean should be on the x-axis.', {192expect_true("sample_mean" == rlang::get_expr(properties$x))193})194test_that('sampling_distribution should be a histogram.', {195expect_true("GeomBar" %in% class(sampling_distribution$layers[[1]]$geom))196})197test_that('sampling_distribution data should be used to create the histogram', {198expect_equal(int_round(nrow(sampling_distribution$data), 0), 1500)199expect_equal(digest(int_round(sum(sampling_distribution$data$sample_mean), 2)), 'e20a3a6689ccb7122ce8aaa71bab55bf')200})201test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {202expect_false((labels$x) == 'age')203expect_false(is.null(labels$title))204})205print("Success!")206}207208test_2.2 <- function(){209test_that('Solution is incorrect', {210expect_equal(digest(int_round(answer2.2, 2)), '0ddc7e7a0d2654650cba2f2a15cbca52')211})212print("Success!")213}214215test_2.3 <- function(){216test_that('Solution is incorrect', {217expect_equal(digest(answer2.3), '3a5505c06543876fe45598b5e5e5195d')218})219print("Success!")220}221222test_2.4 <- function(){223test_that('Solution is incorrect', {224expect_equal(digest(tolower(answer2.4)), '05ca18b596514af73f6880309a21b5dd')225})226print("Success!")227}228229test_2.5 <- function(){230properties <- c(sampling_distribution_20$layers[[1]]$mapping, sampling_distribution_20$mapping)231labels <- sampling_distribution_20$labels232test_that('sample_mean should be on the x-axis.', {233expect_true("sample_mean" == rlang::get_expr(properties$x))234})235test_that('sampling_distribution should be a histogram.', {236expect_true("GeomBar" %in% class(sampling_distribution_20$layers[[1]]$geom))237})238test_that('sampling_distribution data should be used to create the histogram', {239expect_equal(int_round(nrow(sampling_distribution_20$data), 0), 1500)240expect_equal(digest(int_round(sum(sampling_distribution_20$data$sample_mean), 2)), '49a66adc63b05e7e8f90b66202de0b84')241})242test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {243expect_false((labels$x) == 'age')244expect_equal(labels$title, "n = 20")245})246247print("Success!")248}249250test_2.6 <- function(){251properties <- c(sampling_distribution_100$layers[[1]]$mapping, sampling_distribution_100$mapping)252labels <- sampling_distribution_100$labels253test_that('sample_mean should be on the x-axis.', {254expect_true("sample_mean" == rlang::get_expr(properties$x))255})256test_that('sampling_distribution should be a histogram.', {257expect_true("GeomBar" %in% class(sampling_distribution_100$layers[[1]]$geom))258})259test_that('sampling_distribution data should be used to create the histogram', {260expect_equal(int_round(nrow(sampling_distribution_100$data), 0), 1500)261expect_equal(digest(int_round(sum(sampling_distribution_100$data$sample_mean), 2)), '59c92b151db8f38ba93a364fd62ae7c9')262})263test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {264expect_false((labels$x) == 'age')265expect_equal(labels$title, "n = 100")266})267268print("Success!")269}270271test_2.7 <- function(){272test_that('object is named sampling_distribution_panel.', {273expect_true(exists("sampling_distribution_panel"))274})275test_that('sampling distributions are plotted side-by-side', {276expect_equal(int_round(ncol(sampling_distribution_panel), 0), 3)277})278print("Success!")279}280281test_2.8 <- function(){282test_that('Solution is incorrect', {283expect_equal(digest(answer2.8), 'c1f86f7430df7ddb256980ea6a3b57a4')284})285print("Success!")286}287288test_2.9 <- function(){289test_that('Solution is incorrect', {290expect_equal(digest(tolower(answer2.9)), 'd2a90307aac5ae8d0ef58e2fe730d38b')291})292print("Success!")293}294295296