Skip to content

Commit

Permalink
adjust newly failing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Jan 3, 2021
1 parent 99b5f21 commit 508eefa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
25 changes: 7 additions & 18 deletions tests/testthat/test-calculate.R
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,14 @@ test_that("specify() %>% calculate() works", {
expect_silent(
gss_tbl %>% specify(hours ~ NULL) %>% calculate(stat = "mean")
)
expect_error(
expect_message(
gss_tbl %>%
specify(hours ~ NULL) %>%
hypothesize(null = "point", mu = 4) %>%
calculate(stat = "mean")
)

expect_error(
expect_warning(
gss_tbl %>% specify(partyid ~ NULL) %>% calculate(stat = "Chisq")
)
})
Expand All @@ -401,11 +401,11 @@ test_that("One sample t hypothesis test is working", {
calculate(stat = "t")
)

expect_message(
expect_warning(
gss_tbl %>%
specify(response = hours) %>%
calculate(stat = "t"),
"the t-test will assume a null hypothesis"
"A t statistic requires"
)

gss_tbl %>%
Expand All @@ -426,7 +426,7 @@ test_that("specify done before calculate", {

test_that("chisq GoF has params specified for observed stat", {
no_params <- gss_tbl %>% specify(response = partyid)
expect_error(calculate(no_params, stat = "Chisq"))
expect_warning(calculate(no_params, stat = "Chisq"))

params <- gss_tbl %>%
specify(response = partyid) %>%
Expand All @@ -437,22 +437,11 @@ test_that("chisq GoF has params specified for observed stat", {
expect_silent(calculate(params, stat = "Chisq"))
})

test_that("generate not done before calculate", {
gss_tbl_hyp <- gss_tbl %>%
specify(hours ~ college) %>%
hypothesize(null = "independence")
attr(gss_tbl_hyp, "generated") <- TRUE
expect_warning(calculate(gss_tbl_hyp,
stat = "t",
order = c("no degree", "degree")
))
})

test_that("One sample t bootstrap is working", {
expect_message(
expect_warning(
gss_tbl %>%
specify(hours ~ NULL) %>%
generate(reps = 10) %>%
generate(reps = 10, type = "bootstrap") %>%
calculate(stat = "t")
)
})
Expand Down
13 changes: 9 additions & 4 deletions tests/testthat/test-get_confidence_interval.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
context("get_confidence_interval")

set.seed(2018)
test_df <- gss_calc[1:20,]
test_df$stat <- c(
-5, -4, -4, -4, -1, -0.5, rep(0, 6), 1, 1, 3.999, 4, 4, 4.001, 5, 5
)
point <- mean(test_df[["stat"]])

perc_def_out <- tibble::tibble(
Expand Down Expand Up @@ -35,7 +40,7 @@ test_that("get_confidence_interval works with `type = 'se'`", {
expect_message(
expect_equal(
test_df %>% get_confidence_interval(type = "se", point_estimate = point),
tibble::tibble(lower_ci = -1.965, upper_ci = 2.008),
tibble::tibble(lower_ci = -5.653, upper_ci = 6.603),
tolerance = 1e-3
),
"Using `level = 0.95`"
Expand All @@ -44,7 +49,7 @@ test_that("get_confidence_interval works with `type = 'se'`", {
expect_equal(
test_df %>%
get_confidence_interval(level = 0.5, type = "se", point_estimate = point),
tibble::tibble(lower_ci = -0.662, upper_ci = 0.705),
tibble::tibble(lower_ci = -1.633, upper_ci = 2.583),
tolerance = 1e-3
)
})
Expand All @@ -56,7 +61,7 @@ test_that("get_confidence_interval works with `type = 'bias-corrected'`", {
get_confidence_interval(
type = "bias-corrected", point_estimate = point
),
tibble::tibble(lower_ci = -1.692, upper_ci = 2.276),
tibble::tibble(lower_ci = -4.00, upper_ci = 5),
tolerance = 1e-3
),
"Using `level = 0.95`"
Expand All @@ -67,7 +72,7 @@ test_that("get_confidence_interval works with `type = 'bias-corrected'`", {
get_confidence_interval(
level = 0.5, type = "bias-corrected", point_estimate = point
),
tibble::tibble(lower_ci = -0.594, upper_ci = 0.815),
tibble::tibble(lower_ci = 0, upper_ci = 4.0007),
tolerance = 1e-3
)
})
Expand Down
28 changes: 11 additions & 17 deletions tests/testthat/test-get_p_value.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
context("get_p_value")

set.seed(2018)
test_df <- tibble::tibble(
stat = sample(c(
-5, -4, -4, -4, -1, -0.5, rep(0, 6), 1, 1, 3.999, 4, 4, 4.001, 5, 5
))
)
test_df <- gss_calc[1:20,]
test_df$stat <- sample(c(
-5, -4, -4, -4, -1, -0.5, rep(0, 6), 1, 1, 3.999, 4, 4, 4.001, 5, 5
))

test_that("direction is appropriate", {
expect_error(test_df %>% get_p_value(obs_stat = 0.5, direction = "righ"))
Expand Down Expand Up @@ -71,31 +70,26 @@ test_that("theoretical p-value not supported error", {
})

test_that("get_p_value warns in case of zero p-value", {
stat_df <- tibble::tibble(stat = 1:10)

expect_warning(
get_p_value(stat_df, obs_stat = -10, direction = "left"),
get_p_value(gss_calc, obs_stat = -10, direction = "left"),
"be cautious"
)
})

test_that("get_p_value throws error in case of `NaN` stat", {
stat_df <- tibble::tibble(stat = 1:10)
obs_stat <- 2.71

stat_df$stat[1] <- NaN
gss_calc$stat[1] <- NaN
expect_error(
get_p_value(stat_df, obs_stat, "both"),
get_p_value(gss_calc, 0, "both"),
"1 calculated statistic was `NaN`.*not well-defined"
)

stat_df$stat[2] <- NaN
gss_calc$stat[2] <- NaN
expect_error(
get_p_value(stat_df, obs_stat, "both"),
get_p_value(gss_calc, 0, "both"),
"2 calculated statistics were `NaN`.*not well-defined"
)

# In the case that _all_ values are NaN, error should have different text
stat_df$stat <- NaN
expect_error(get_p_value(stat_df, obs_stat, "both"), "All calculated stat")
gss_calc$stat <- NaN
expect_error(get_p_value(gss_calc, 0, "both"), "All calculated stat")
})

0 comments on commit 508eefa

Please sign in to comment.