From 49cb2b0a9419e82301718cd618679adb885bdcb9 Mon Sep 17 00:00:00 2001 From: VisruthSK <67435125+VisruthSK@users.noreply.github.com> Date: Tue, 30 Sep 2025 10:52:14 -0700 Subject: [PATCH 1/4] LLM generated tests --- tests/testthat/_snaps/binomial-suite.md | 213 ++++++++++++++++++ tests/testthat/_snaps/chisq-hyper.md | 113 ++++++++++ tests/testthat/_snaps/normal-suite.md | 118 ++++++++++ .../{addexp-plot.svg => iscamaddexp-plot.svg} | 130 +++++------ ...dlnorm-plot.svg => iscamaddlnorm-plot.svg} | 144 ++++++------ ...addnorm-plot.svg => iscamaddnorm-plot.svg} | 140 ++++++------ .../{addt-plot.svg => iscamaddt-plot.svg} | 138 ++++++------ ...dtnorm-plot.svg => iscamaddtnorm-plot.svg} | 148 ++++++------ ...t-one-var.svg => iscamboxplot-one-var.svg} | 104 ++++----- ...two-vars.svg => iscamboxplot-two-vars.svg} | 130 +++++------ ...t-one-var.svg => iscamdotplot-one-var.svg} | 156 ++++++------- ...two-vars.svg => iscamdotplot-two-vars.svg} | 170 +++++++------- tests/testthat/_snaps/prop-tests.md | 104 +++++++++ tests/testthat/_snaps/t-suite.md | 187 +++++++++++++++ tests/testthat/helper-capture.R | 17 ++ tests/testthat/test-binomial-suite.R | 95 ++++++++ tests/testthat/test-chisq-hyper.R | 51 +++++ tests/testthat/test-normal-suite.R | 65 ++++++ tests/testthat/test-prop-tests.R | 63 ++++++ tests/testthat/test-t-suite.R | 131 +++++++++++ 20 files changed, 1787 insertions(+), 630 deletions(-) create mode 100644 tests/testthat/_snaps/binomial-suite.md create mode 100644 tests/testthat/_snaps/chisq-hyper.md create mode 100644 tests/testthat/_snaps/normal-suite.md rename tests/testthat/_snaps/overlayDensities/{addexp-plot.svg => iscamaddexp-plot.svg} (99%) rename tests/testthat/_snaps/overlayDensities/{addlnorm-plot.svg => iscamaddlnorm-plot.svg} (99%) rename tests/testthat/_snaps/overlayDensities/{addnorm-plot.svg => iscamaddnorm-plot.svg} (99%) rename tests/testthat/_snaps/overlayDensities/{addt-plot.svg => iscamaddt-plot.svg} (99%) rename tests/testthat/_snaps/overlayDensities/{addtnorm-plot.svg => iscamaddtnorm-plot.svg} (99%) rename tests/testthat/_snaps/plots/{boxplot-one-var.svg => iscamboxplot-one-var.svg} (94%) rename tests/testthat/_snaps/plots/{boxplot-two-vars.svg => iscamboxplot-two-vars.svg} (98%) rename tests/testthat/_snaps/plots/{dotplot-one-var.svg => iscamdotplot-one-var.svg} (96%) rename tests/testthat/_snaps/plots/{dotplot-two-vars.svg => iscamdotplot-two-vars.svg} (98%) create mode 100644 tests/testthat/_snaps/prop-tests.md create mode 100644 tests/testthat/_snaps/t-suite.md create mode 100644 tests/testthat/helper-capture.R create mode 100644 tests/testthat/test-binomial-suite.R create mode 100644 tests/testthat/test-chisq-hyper.R create mode 100644 tests/testthat/test-normal-suite.R create mode 100644 tests/testthat/test-prop-tests.R create mode 100644 tests/testthat/test-t-suite.R diff --git a/tests/testthat/_snaps/binomial-suite.md b/tests/testthat/_snaps/binomial-suite.md new file mode 100644 index 0000000..538f572 --- /dev/null +++ b/tests/testthat/_snaps/binomial-suite.md @@ -0,0 +1,213 @@ +# iscambinomprob returns expected tail probabilities + + Code + cat("lower_value:\n") + Output + lower_value: + Code + print(res_lower$value) + Output + [1] 0.3822806 + Code + cat("lower_expected:", pbinom(3, 10, 0.4), "\n") + Output + lower_expected: 0.3822806 + Code + cat("lower_output:\n") + Output + lower_output: + Code + cat(collapse_output(res_lower$output), "\n") + Output + Probability 3 and below = 0.3822806 + Code + cat("---\n") + Output + --- + Code + cat("upper_value:\n") + Output + upper_value: + Code + print(res_upper$value) + Output + [1] 0.05476188 + Code + cat("upper_expected:", 1 - pbinom(6, 10, 0.4), "\n") + Output + upper_expected: 0.05476188 + Code + cat("upper_output:\n") + Output + upper_output: + Code + cat(collapse_output(res_upper$output), "\n") + Output + Probability 7 and above = 0.05476188 + +# iscaminvbinom solves the correct quantile + + Code + cat("lower_value:\n") + Output + lower_value: + Code + print(res_lower$value) + Output + [1] 4 + Code + cat("lower_expected:", qbinom(0.1, 20, 0.4, lower.tail = TRUE) - 1, "\n") + Output + lower_expected: 4 + Code + cat("lower_output:\n") + Output + lower_output: + Code + cat(collapse_output(res_lower$output), "\n") + Output + The observation with at most 0.1 probability at or below is 4 + Code + cat("---\n") + Output + --- + Code + cat("upper_value:\n") + Output + upper_value: + Code + print(res_upper$value) + Output + [1] 12 + Code + cat("upper_expected:", qbinom(0.1, 20, 0.4, lower.tail = FALSE) + 1, "\n") + Output + upper_expected: 12 + Code + cat("upper_output:\n") + Output + upper_output: + Code + cat(collapse_output(res_upper$output), "\n") + Output + The observation with at most 0.1 probability at or above is 12 + +# iscambinomnorm executes for each direction + + Code + cat("below:\n") + Output + below: + Code + cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "below"))$ + output), "\n") + Output + binomial: 0.5881 + normal approx: 0.5 + normal approx with continuity: 0.5885 + Code + cat("---\n") + Output + --- + Code + cat("above:\n") + Output + above: + Code + cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "above"))$ + output), "\n") + Output + binomial: 0.5881 + normal approx: 0.5 + normal approx with continuity: 0.5885 + Code + cat("---\n") + Output + --- + Code + cat("two-sided:\n") + Output + two-sided: + Code + cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "two.sided"))$ + output), "\n") + Output + binomial: 1.176 + normal approx: 1 + normal approx with continuity: 1.177 + +# iscambinompower reports rejection probabilities + + Code + cat("critical_rr:", rr, "\n") + Output + critical_rr: 15 + Code + cat("null_prob:", null_prob, "\n") + Output + null_prob: 0.02069473 + Code + cat("alt_prob:", alt_prob, "\n") + Output + alt_prob: 0.125599 + Code + cat("value:\n") + Output + value: + Code + print(res$value) + Output + NULL + Code + cat("output:\n") + Output + output: + Code + cat(collapse_output(res$output), "\n") + Output + Null: Probability 15 and above = 0.02069473 + Alternative: Probability 15 and above = 0.125599 + +# iscambinomtest matches binom.test results + + Code + cat("value:\n") + Output + value: + Code + print(res$value) + Output + $pvalue + [1] 0.36159 + + $lower + [1] 0.40603 + + $upper + [1] 0.77344 + + Code + cat("expected_p: ", signif(bt$p.value, 5), "\n", sep = "") + Output + expected_p: 0.36159 + Code + cat("expected_ci:", bt$conf.int[1], bt$conf.int[2], "\n") + Output + expected_ci: 0.4060349 0.7734424 + Code + cat("output:\n") + Output + output: + Code + cat(collapse_output(res$output), "\n") + Output + + Exact Binomial Test + + Data: observed successes = 18, sample size = 30, sample proportion = 0.6 + + Null hypothesis : pi = 0.5 + Alternative hypothesis: pi <> 0.5 + p-value: 0.36159 + 95 % Confidence interval for pi: ( 0.40603 , 0.77344 ) + diff --git a/tests/testthat/_snaps/chisq-hyper.md b/tests/testthat/_snaps/chisq-hyper.md new file mode 100644 index 0000000..eed4ace --- /dev/null +++ b/tests/testthat/_snaps/chisq-hyper.md @@ -0,0 +1,113 @@ +# iscamchisqprob returns formatted upper-tail probability + + Code + cat("value:\n") + Output + value: + Code + print(res$value) + Output + [1] "0.1718" + Code + cat("expected:", pchisq(5, 3, lower.tail = FALSE), "\n") + Output + expected: 0.1717971 + Code + cat("output:\n") + Output + output: + Code + cat(collapse_output(res$output), "\n") + Output + probability: 0.1718 + +# iscamhyperprob matches hypergeometric tails + + Code + cat("lower_value:\n") + Output + lower_value: + Code + print(res_lower$value) + Output + [1] 0.7038184 + Code + cat("lower_expected:", phyper(2, 5, fail, 8), "\n") + Output + lower_expected: 0.7038184 + Code + cat("lower_output:\n") + Output + lower_output: + Code + cat(collapse_output(res_lower$output), "\n") + Output + Probability 2 and below = 0.7038184 + Code + cat("---\n") + Output + --- + Code + cat("upper_value:\n") + Output + upper_value: + Code + print(res_upper$value) + Output + [1] 0.2961816 + Code + cat("upper_expected:", 1 - phyper(2, 5, fail, 8), "\n") + Output + upper_expected: 0.2961816 + Code + cat("upper_output:\n") + Output + upper_output: + Code + cat(collapse_output(res_upper$output), "\n") + Output + Probability 3 and above = 0.2961816 + +# iscamhypernorm reports tail probabilities and normal approximations + + Code + cat("lower_value:\n") + Output + lower_value: + Code + print(res_lower$value) + Output + NULL + Code + cat("lower_output:\n") + Output + lower_output: + Code + cat(collapse_output(res_lower$output), "\n") + Output + hypergeometric: 0.7038 + normal approx: 0.5 + normal approx with continuity: 0.6963 + Code + cat("---\n") + Output + --- + Code + cat("upper_value:\n") + Output + upper_value: + Code + print(res_upper$value) + Output + NULL + Code + cat("upper_output:\n") + Output + upper_output: + Code + cat(collapse_output(res_upper$output), "\n") + Output + hypergeometric: 0.2962 + normal approx: 0.1521 + normal approx with continuity: 0.3037 + diff --git a/tests/testthat/_snaps/normal-suite.md b/tests/testthat/_snaps/normal-suite.md new file mode 100644 index 0000000..677d84b --- /dev/null +++ b/tests/testthat/_snaps/normal-suite.md @@ -0,0 +1,118 @@ +# iscamnormprob returns formatted probabilities + + Code + cat("above_value:\n") + Output + above_value: + Code + print(res_above$value) + Output + [1] "0.025" + Code + cat("above_expected:", pnorm(1.96, lower.tail = FALSE), "\n") + Output + above_expected: 0.0249979 + Code + cat("above_output:\n") + Output + above_output: + Code + cat(collapse_output(res_above$output), "\n") + Output + probability: 0.025 + Code + cat("---\n") + Output + --- + Code + cat("between_value:\n") + Output + between_value: + Code + print(res_between$value) + Output + [1] "0.6827" + Code + cat("between_expected:", pnorm(1) - pnorm(-1), "\n") + Output + between_expected: 0.6826895 + Code + cat("between_output:\n") + Output + between_output: + Code + cat(collapse_output(res_between$output), "\n") + Output + probability: 0.6827 + +# iscamnormpower reports null and alternative rejection rates + + Code + cat("rr:", rr, "\n") + Output + rr: 0.5919501 + Code + cat("null_prob:", null_prob, "\n") + Output + null_prob: 0.05 + Code + cat("alt_prob:", alt_prob, "\n") + Output + alt_prob: 0.2253625 + Code + cat("value:\n") + Output + value: + Code + print(res$value) + Output + NULL + Code + cat("output:\n") + Output + output: + Code + cat(collapse_output(res$output), "\n") + Output + Null: Probability 0.592 and above = 0.05 + Alt: Probability 0.592 and above = 0.2253625 + +# iscaminvnorm reports requested quantiles + + Code + cat("below_value:\n") + Output + below_value: + Code + print(res_below$value) + Output + NULL + Code + cat("below_output:\n") + Output + below_output: + Code + cat(collapse_output(res_below$output), "\n") + Output + The observation with 0.05 probability below is -1.645 + Code + cat("---\n") + Output + --- + Code + cat("outside_value:\n") + Output + outside_value: + Code + print(res_outside$value) + Output + NULL + Code + cat("outside_output:\n") + Output + outside_output: + Code + cat(collapse_output(res_outside$output), "\n") + Output + There is 0.1 probability outside -1.645 and 1.645 + diff --git a/tests/testthat/_snaps/overlayDensities/addexp-plot.svg b/tests/testthat/_snaps/overlayDensities/iscamaddexp-plot.svg similarity index 99% rename from tests/testthat/_snaps/overlayDensities/addexp-plot.svg rename to tests/testthat/_snaps/overlayDensities/iscamaddexp-plot.svg index 5db6b70..dc6a208 100644 --- a/tests/testthat/_snaps/overlayDensities/addexp-plot.svg +++ b/tests/testthat/_snaps/overlayDensities/iscamaddexp-plot.svg @@ -1,65 +1,65 @@ - - - - - - - - - - - - - - - - - - - -0.0 -0.5 -1.0 -1.5 -2.0 -2.5 - - - - - - -0.0 -0.5 -1.0 -1.5 -2.0 - - - - - - - - - - - - - - - - -x -density -Histogram with exponential curve - - + + + + + + + + + + + + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 +2.5 + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 + + + + + + + + + + + + + + + + +x +density +Histogram with exponential curve + + diff --git a/tests/testthat/_snaps/overlayDensities/addlnorm-plot.svg b/tests/testthat/_snaps/overlayDensities/iscamaddlnorm-plot.svg similarity index 99% rename from tests/testthat/_snaps/overlayDensities/addlnorm-plot.svg rename to tests/testthat/_snaps/overlayDensities/iscamaddlnorm-plot.svg index 3b8e2fd..40e628e 100644 --- a/tests/testthat/_snaps/overlayDensities/addlnorm-plot.svg +++ b/tests/testthat/_snaps/overlayDensities/iscamaddlnorm-plot.svg @@ -1,72 +1,72 @@ - - - - - - - - - - - - - - - - - - - -0 -2 -4 -6 -8 -10 - - - - - - - - - -0.0 -0.1 -0.2 -0.3 -0.4 -0.5 -0.6 -0.7 - - - - - - - - - - - - - - - - - -x -density -Histogram with log-normal curve - - + + + + + + + + + + + + + + + + + + + +0 +2 +4 +6 +8 +10 + + + + + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 +0.5 +0.6 +0.7 + + + + + + + + + + + + + + + + + +x +density +Histogram with log-normal curve + + diff --git a/tests/testthat/_snaps/overlayDensities/addnorm-plot.svg b/tests/testthat/_snaps/overlayDensities/iscamaddnorm-plot.svg similarity index 99% rename from tests/testthat/_snaps/overlayDensities/addnorm-plot.svg rename to tests/testthat/_snaps/overlayDensities/iscamaddnorm-plot.svg index 79dda94..8ec0cad 100644 --- a/tests/testthat/_snaps/overlayDensities/addnorm-plot.svg +++ b/tests/testthat/_snaps/overlayDensities/iscamaddnorm-plot.svg @@ -1,70 +1,70 @@ - - - - - - - - - - - - - - - - - - --2 --1 -0 -1 -2 - - - - - - - -0.0 -0.1 -0.2 -0.3 -0.4 -0.5 - - - - - - - - - - - - - - - - - - - - - -x -density -Histogram with normal curve - - + + + + + + + + + + + + + + + + + + +-2 +-1 +0 +1 +2 + + + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 +0.5 + + + + + + + + + + + + + + + + + + + + + +x +density +Histogram with normal curve + + diff --git a/tests/testthat/_snaps/overlayDensities/addt-plot.svg b/tests/testthat/_snaps/overlayDensities/iscamaddt-plot.svg similarity index 99% rename from tests/testthat/_snaps/overlayDensities/addt-plot.svg rename to tests/testthat/_snaps/overlayDensities/iscamaddt-plot.svg index 68480c3..904112e 100644 --- a/tests/testthat/_snaps/overlayDensities/addt-plot.svg +++ b/tests/testthat/_snaps/overlayDensities/iscamaddt-plot.svg @@ -1,69 +1,69 @@ - - - - - - - - - - - - - - - - - - --2 --1 -0 -1 -2 - - - - - - - -0.0 -0.1 -0.2 -0.3 -0.4 -0.5 - - - - - - - - - - - - - - - - - - - - -x -density -Histogram with t curve - - + + + + + + + + + + + + + + + + + + +-2 +-1 +0 +1 +2 + + + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 +0.5 + + + + + + + + + + + + + + + + + + + + +x +density +Histogram with t curve + + diff --git a/tests/testthat/_snaps/overlayDensities/addtnorm-plot.svg b/tests/testthat/_snaps/overlayDensities/iscamaddtnorm-plot.svg similarity index 99% rename from tests/testthat/_snaps/overlayDensities/addtnorm-plot.svg rename to tests/testthat/_snaps/overlayDensities/iscamaddtnorm-plot.svg index 3f866b0..3f1023f 100644 --- a/tests/testthat/_snaps/overlayDensities/addtnorm-plot.svg +++ b/tests/testthat/_snaps/overlayDensities/iscamaddtnorm-plot.svg @@ -1,74 +1,74 @@ - - - - - - - - - - - - - - - - - --4 --2 -0 -2 - - - - - - -0.0 -0.1 -0.2 -0.3 -0.4 - - - - - - - - - - - - - - - - - - - - - - -x -density -Histogram with t and normal curve - - - - - -normal -t, df=5 - - + + + + + + + + + + + + + + + + + +-4 +-2 +0 +2 + + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 + + + + + + + + + + + + + + + + + + + + + + +x +density +Histogram with t and normal curve + + + + + +normal +t, df=5 + + diff --git a/tests/testthat/_snaps/plots/boxplot-one-var.svg b/tests/testthat/_snaps/plots/iscamboxplot-one-var.svg similarity index 94% rename from tests/testthat/_snaps/plots/boxplot-one-var.svg rename to tests/testthat/_snaps/plots/iscamboxplot-one-var.svg index d84e3da..3ee8a42 100644 --- a/tests/testthat/_snaps/plots/boxplot-one-var.svg +++ b/tests/testthat/_snaps/plots/iscamboxplot-one-var.svg @@ -1,52 +1,52 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -10 -15 -20 -25 -30 - -mtcars Cylinders Dotplot -Number of Cylinders - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +10 +15 +20 +25 +30 + +mtcars Cylinders iscamdotplot +Number of Cylinders + + diff --git a/tests/testthat/_snaps/plots/boxplot-two-vars.svg b/tests/testthat/_snaps/plots/iscamboxplot-two-vars.svg similarity index 98% rename from tests/testthat/_snaps/plots/boxplot-two-vars.svg rename to tests/testthat/_snaps/plots/iscamboxplot-two-vars.svg index fc2a9d9..6e4d4a5 100644 --- a/tests/testthat/_snaps/plots/boxplot-two-vars.svg +++ b/tests/testthat/_snaps/plots/iscamboxplot-two-vars.svg @@ -1,65 +1,65 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -0 -1 - - - - - - -10 -15 -20 -25 -30 - -Automatic Cars Have Better Mileage on Average -Mileage (miles per gallon) -Automatic (yes coded as 1) - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +1 + + + + + + +10 +15 +20 +25 +30 + +Automatic Cars Have Better Mileage on Average +Mileage (miles per gallon) +Automatic (yes coded as 1) + + diff --git a/tests/testthat/_snaps/plots/dotplot-one-var.svg b/tests/testthat/_snaps/plots/iscamdotplot-one-var.svg similarity index 96% rename from tests/testthat/_snaps/plots/dotplot-one-var.svg rename to tests/testthat/_snaps/plots/iscamdotplot-one-var.svg index 99fe84e..3838c88 100644 --- a/tests/testthat/_snaps/plots/dotplot-one-var.svg +++ b/tests/testthat/_snaps/plots/iscamdotplot-one-var.svg @@ -1,78 +1,78 @@ - - - - - - - - - - - - - - - - - - - -4 -5 -6 -7 -8 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -mtcars Cylinders Dotplot -Number of Cylinders - - + + + + + + + + + + + + + + + + + + + +4 +5 +6 +7 +8 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +mtcars Cylinders iscamdotplot +Number of Cylinders + + diff --git a/tests/testthat/_snaps/plots/dotplot-two-vars.svg b/tests/testthat/_snaps/plots/iscamdotplot-two-vars.svg similarity index 98% rename from tests/testthat/_snaps/plots/dotplot-two-vars.svg rename to tests/testthat/_snaps/plots/iscamdotplot-two-vars.svg index c218074..feef04e 100644 --- a/tests/testthat/_snaps/plots/dotplot-two-vars.svg +++ b/tests/testthat/_snaps/plots/iscamdotplot-two-vars.svg @@ -1,85 +1,85 @@ - - - - - - - - - - - - - - - - - - - -10 -15 -20 -25 -30 - - - -0 -1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Automatic Cars Have Better Mileage on Average -Mileage (miles per gallon) -Automatic (yes coded as 1) - - + + + + + + + + + + + + + + + + + + + +10 +15 +20 +25 +30 + + + +0 +1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Automatic Cars Have Better Mileage on Average +Mileage (miles per gallon) +Automatic (yes coded as 1) + + diff --git a/tests/testthat/_snaps/prop-tests.md b/tests/testthat/_snaps/prop-tests.md new file mode 100644 index 0000000..74a7dc6 --- /dev/null +++ b/tests/testthat/_snaps/prop-tests.md @@ -0,0 +1,104 @@ +# iscamonepropztest agrees with prop.test + + Code + cat("value:\n") + Output + value: + Code + print(res$value) + Output + $zvalue + [1] 2.828427 + + $pvalue + [1] 0.002339 + + $lower + [1] 0.5729798 + + $upper + [1] 0.8270202 + + Code + cat("expected_z:", z_expected, "\n") + Output + expected_z: 2.828427 + Code + cat("expected_p:", p_expected, "\n") + Output + expected_p: 0.002339 + Code + cat("expected_ci:", lower, upper, "\n") + Output + expected_ci: 0.5729798 0.8270202 + Code + cat("output:\n") + Output + output: + Code + cat(collapse_output(res$output), "\n") + Output + + One Proportion z test + + Data: observed successes = 35, sample size = 50, sample proportion = 0.7 + + Null hypothesis : pi = 0.5 + Alternative hypothesis: pi > 0.5 + z-statistic: 2.828 + p-value: 0.002339 + 95 % Confidence interval for pi: ( 0.5729798 , 0.8270202 ) + +# iscamtwopropztest matches two-sample z test calculations + + Code + cat("value:\n") + Output + value: + Code + print(res$value) + Output + $zvalue + [1] 0.8008724 + + $pvalue + [1] 0.2116 + + $lower + [1] -0.1124861 + + $upper + [1] 0.2680417 + + Code + cat("expected_z:", z_expected, "\n") + Output + expected_z: 0.8008724 + Code + cat("expected_p:", p_expected, "\n") + Output + expected_p: 0.2116 + Code + cat("expected_ci:", lower, upper, "\n") + Output + expected_ci: -0.1124861 0.2680417 + Code + cat("output:\n") + Output + output: + Code + cat(collapse_output(res$output), "\n") + Output + + Two Proportion z test + + Group1: observed successes = 35, sample size = 50, sample proportion = 0.7 + + Group2: observed successes = 28, sample size = 45, sample proportion = 0.6222 + + Null hypothesis : pi1-pi2 = 0 + Alternative hypothesis: pi1-pi2 > 0 + z-statistic: 0.8009 + 95 % Confidence interval for pi1-pi2: ( -0.1124861 , 0.2680417 ) + p-value: 0.2116 + diff --git a/tests/testthat/_snaps/t-suite.md b/tests/testthat/_snaps/t-suite.md new file mode 100644 index 0000000..c367881 --- /dev/null +++ b/tests/testthat/_snaps/t-suite.md @@ -0,0 +1,187 @@ +# iscaminvt reports requested t quantiles + + Code + cat("below_answer:\n") + Output + below_answer: + Code + print(res_below$value) + Output + $answer + [1] -1.753 + + Code + cat("expected_below:", qb, "\n") + Output + expected_below: -1.75305 + Code + cat("below_output:\n") + Output + below_output: + Code + cat(collapse_output(res_below$output), "\n") + Output + The observation with 0.05 probability below is -1.753 + Code + cat("---\n") + Output + --- + Code + cat("outside_answer:\n") + Output + outside_answer: + Code + print(res_outside$value) + Output + $answer1 + [1] -1.812 + + $answer2 + [1] 1.812 + + Code + cat("expected_outside:", ql, qu, "\n") + Output + expected_outside: -1.812461 1.812461 + Code + cat("outside_output:\n") + Output + outside_output: + Code + cat(collapse_output(res_outside$output), "\n") + Output + There is 0.1 probability outside -1.812 and 1.812 + +# iscamtprob matches t tail probabilities + + Code + cat("below_value:\n") + Output + below_value: + Code + print(res_below$value) + Output + NULL + Code + cat("below_expected:", pt(-2.05, 10), "\n") + Output + below_expected: 0.03375415 + Code + cat("below_output:\n") + Output + below_output: + Code + cat(collapse_output(res_below$output), "\n") + Output + probability: 0.03375 + Code + cat("---\n") + Output + --- + Code + cat("between_value:\n") + Output + between_value: + Code + print(res_between$value) + Output + NULL + Code + cat("between_expected:", pt(2, 12) - pt(-2, 12), "\n") + Output + between_expected: 0.931345 + Code + cat("between_output:\n") + Output + between_output: + Code + cat(collapse_output(res_between$output), "\n") + Output + probability: 0.9313 + +# iscamonesamplet returns Welch statistics + + Code + cat("value:\n") + Output + value: + Code + print(res$value) + Output + NULL + Code + cat("expected_t:", t_expected, "\n") + Output + expected_t: 2.282177 + Code + cat("expected_p:", p_expected, "\n") + Output + expected_p: 0.01499991 + Code + cat("expected_ci:", lower, upper, "\n") + Output + expected_ci: 2.051913 2.948087 + Code + cat("output:\n") + Output + output: + Code + cat(collapse_output(res$output), "\n") + Output + + One Sample t test + + mean = 2.5, sd = 1.2, sample size = 30 + Null hypothesis : mu = 2 + Alternative hypothesis: mu > 2 + t-statistic: 2.282 + 95 % Confidence interval for mu: ( 2.051913 , 2.948087 ) + p-value: 0.01499991 + +# iscamtwosamplet returns Welch two-sample results + + Code + cat("value:\n") + Output + value: + Code + print(res$value) + Output + NULL + Code + cat("expected_df:", df_expected, "\n") + Output + expected_df: 53.58 + Code + cat("expected_t:", t_expected, "\n") + Output + expected_t: 2.163254 + Code + cat("expected_p:", p_expected, "\n") + Output + expected_p: 0.03500158 + Code + cat("expected_ci:", lower, upper, "\n") + Output + expected_ci: 0.07304471 1.926955 + Code + cat("output:\n") + Output + output: + Code + cat(collapse_output(res$output), "\n") + Output + + Two Sample t test + + Group1: mean = 5, sd = 2, sample size = 30 + Group2: mean = 4, sd = 1.5, sample size = 28 + diff:1 + + Null hypothesis : mu1-mu2 = 0 + Alternative hypothesis: mu1-mu2 <> 0 + t-statistic: 2.163 + df: 53.58 + 95 % Confidence interval for mu1-mu2: ( 0.07304471 , 1.926955 ) + p-value: 0.035 + diff --git a/tests/testthat/helper-capture.R b/tests/testthat/helper-capture.R new file mode 100644 index 0000000..4f80521 --- /dev/null +++ b/tests/testthat/helper-capture.R @@ -0,0 +1,17 @@ +capture_plot_result <- function(expr) { + tmp <- tempfile(fileext = ".pdf") + grDevices::pdf(tmp) + on.exit({ + grDevices::dev.off() + unlink(tmp) + }, add = TRUE) + value <- NULL + output <- capture.output({ + value <- withVisible(force(expr)) + }) + list(output = output, value = value$value, visible = value$visible) +} + +collapse_output <- function(lines) { + paste(lines, collapse = "\n") +} diff --git a/tests/testthat/test-binomial-suite.R b/tests/testthat/test-binomial-suite.R new file mode 100644 index 0000000..739aac4 --- /dev/null +++ b/tests/testthat/test-binomial-suite.R @@ -0,0 +1,95 @@ +library(stats) + +test_that("iscambinomprob returns expected tail probabilities", { + res_lower <- capture_plot_result(iscambinomprob(k = 3, n = 10, prob = 0.4, lower.tail = TRUE)) + res_upper <- capture_plot_result(iscambinomprob(k = 7, n = 10, prob = 0.4, lower.tail = FALSE)) + + expect_snapshot({ + cat("lower_value:\n") + print(res_lower$value) + cat("lower_expected:", pbinom(3, 10, 0.4), "\n") + cat("lower_output:\n") + cat(collapse_output(res_lower$output), "\n") + cat("---\n") + cat("upper_value:\n") + print(res_upper$value) + cat("upper_expected:", 1 - pbinom(6, 10, 0.4), "\n") + cat("upper_output:\n") + cat(collapse_output(res_upper$output), "\n") + }) +}) + +test_that("iscaminvbinom solves the correct quantile", { + res_lower <- capture_plot_result(iscaminvbinom(alpha = 0.1, n = 20, prob = 0.4, lower.tail = TRUE)) + res_upper <- capture_plot_result(iscaminvbinom(alpha = 0.1, n = 20, prob = 0.4, lower.tail = FALSE)) + + expect_snapshot({ + cat("lower_value:\n") + print(res_lower$value) + cat("lower_expected:", qbinom(0.1, 20, 0.4, lower.tail = TRUE) - 1, "\n") + cat("lower_output:\n") + cat(collapse_output(res_lower$output), "\n") + cat("---\n") + cat("upper_value:\n") + print(res_upper$value) + cat("upper_expected:", qbinom(0.1, 20, 0.4, lower.tail = FALSE) + 1, "\n") + cat("upper_output:\n") + cat(collapse_output(res_upper$output), "\n") + }) +}) + +test_that("iscambinomnorm executes for each direction", { + expect_snapshot({ + cat("below:\n") + cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "below"))$output), "\n") + cat("---\n") + cat("above:\n") + cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "above"))$output), "\n") + cat("---\n") + cat("two-sided:\n") + cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "two.sided"))$output), "\n") + }) +}) + +test_that("iscambinompower reports rejection probabilities", { + los <- 0.05 + n <- 20 + prob1 <- 0.5 + prob2 <- 0.6 + + res <- capture_plot_result(suppressWarnings(iscambinompower(LOS = los, n = n, prob1 = prob1, alternative = "greater", prob2 = prob2))) + rr <- qbinom(los, n, prob1, lower.tail = FALSE) + 1 + null_prob <- 1 - pbinom(rr - 1, n, prob1) + alt_prob <- 1 - pbinom(rr - 1, n, prob2) + + expect_snapshot({ + cat("critical_rr:", rr, "\n") + cat("null_prob:", null_prob, "\n") + cat("alt_prob:", alt_prob, "\n") + cat("value:\n") + print(res$value) + cat("output:\n") + cat(collapse_output(res$output), "\n") + }) +}) + +test_that("iscambinomtest matches binom.test results", { + res <- capture_plot_result(iscambinomtest( + observed = 18, + n = 30, + hypothesized = 0.5, + alternative = "two.sided", + conf.level = 0.95 + )) + + bt <- binom.test(18, 30, p = 0.5, alternative = "two.sided") + + expect_snapshot({ + cat("value:\n") + print(res$value) + cat("expected_p: ", signif(bt$p.value, 5), "\n", sep = "") + cat("expected_ci:", bt$conf.int[1], bt$conf.int[2], "\n") + cat("output:\n") + cat(collapse_output(res$output), "\n") + }) +}) diff --git a/tests/testthat/test-chisq-hyper.R b/tests/testthat/test-chisq-hyper.R new file mode 100644 index 0000000..327584a --- /dev/null +++ b/tests/testthat/test-chisq-hyper.R @@ -0,0 +1,51 @@ +library(stats) + +test_that("iscamchisqprob returns formatted upper-tail probability", { + res <- capture_plot_result(iscamchisqprob(5, 3)) + + expect_snapshot({ + cat("value:\n") + print(res$value) + cat("expected:", pchisq(5, 3, lower.tail = FALSE), "\n") + cat("output:\n") + cat(collapse_output(res$output), "\n") + }) +}) + +test_that("iscamhyperprob matches hypergeometric tails", { + res_lower <- capture_plot_result(suppressWarnings(iscamhyperprob(k = 2, total = 20, succ = 5, n = 8, lower.tail = TRUE))) + res_upper <- capture_plot_result(suppressWarnings(iscamhyperprob(k = 3, total = 20, succ = 5, n = 8, lower.tail = FALSE))) + + fail <- 20 - 5 + + expect_snapshot({ + cat("lower_value:\n") + print(res_lower$value) + cat("lower_expected:", phyper(2, 5, fail, 8), "\n") + cat("lower_output:\n") + cat(collapse_output(res_lower$output), "\n") + cat("---\n") + cat("upper_value:\n") + print(res_upper$value) + cat("upper_expected:", 1 - phyper(2, 5, fail, 8), "\n") + cat("upper_output:\n") + cat(collapse_output(res_upper$output), "\n") + }) +}) + +test_that("iscamhypernorm reports tail probabilities and normal approximations", { + res_lower <- capture_plot_result(suppressWarnings(iscamhypernorm(k = 2, total = 20, succ = 5, n = 8, lower.tail = TRUE))) + res_upper <- capture_plot_result(suppressWarnings(iscamhypernorm(k = 3, total = 20, succ = 5, n = 8, lower.tail = FALSE))) + + expect_snapshot({ + cat("lower_value:\n") + print(res_lower$value) + cat("lower_output:\n") + cat(collapse_output(res_lower$output), "\n") + cat("---\n") + cat("upper_value:\n") + print(res_upper$value) + cat("upper_output:\n") + cat(collapse_output(res_upper$output), "\n") + }) +}) diff --git a/tests/testthat/test-normal-suite.R b/tests/testthat/test-normal-suite.R new file mode 100644 index 0000000..8624502 --- /dev/null +++ b/tests/testthat/test-normal-suite.R @@ -0,0 +1,65 @@ +library(stats) + +test_that("iscamnormprob returns formatted probabilities", { + res_above <- capture_plot_result(suppressWarnings(iscamnormprob(1.96, direction = "above"))) + res_between <- capture_plot_result(suppressWarnings(iscamnormprob(-1, xval2 = 1, direction = "between"))) + + expect_snapshot({ + cat("above_value:\n") + print(res_above$value) + cat("above_expected:", pnorm(1.96, lower.tail = FALSE), "\n") + cat("above_output:\n") + cat(collapse_output(res_above$output), "\n") + cat("---\n") + cat("between_value:\n") + print(res_between$value) + cat("between_expected:", pnorm(1) - pnorm(-1), "\n") + cat("between_output:\n") + cat(collapse_output(res_between$output), "\n") + }) +}) + +test_that("iscamnormpower reports null and alternative rejection rates", { + los <- 0.05 + n <- 80 + prob1 <- 0.5 + prob2 <- 0.55 + + res <- capture_plot_result(suppressWarnings(iscamnormpower(LOS = los, n = n, prob1 = prob1, alternative = "greater", prob2 = prob2))) + + mean1 <- prob1 + sd1 <- sqrt(prob1 * (1 - prob1) / n) + rr <- qnorm(los, mean1, sd1, lower.tail = FALSE) + null_prob <- 1 - pnorm(rr, mean1, sd1) + + mean2 <- prob2 + sd2 <- sqrt(prob2 * (1 - prob2) / n) + alt_prob <- 1 - pnorm(rr, mean2, sd2) + + expect_snapshot({ + cat("rr:", rr, "\n") + cat("null_prob:", null_prob, "\n") + cat("alt_prob:", alt_prob, "\n") + cat("value:\n") + print(res$value) + cat("output:\n") + cat(collapse_output(res$output), "\n") + }) +}) + +test_that("iscaminvnorm reports requested quantiles", { + res_below <- capture_plot_result(suppressWarnings(iscaminvnorm(0.05, direction = "below"))) + res_outside <- capture_plot_result(suppressWarnings(iscaminvnorm(0.1, direction = "outside"))) + + expect_snapshot({ + cat("below_value:\n") + print(res_below$value) + cat("below_output:\n") + cat(collapse_output(res_below$output), "\n") + cat("---\n") + cat("outside_value:\n") + print(res_outside$value) + cat("outside_output:\n") + cat(collapse_output(res_outside$output), "\n") + }) +}) diff --git a/tests/testthat/test-prop-tests.R b/tests/testthat/test-prop-tests.R new file mode 100644 index 0000000..8ec6958 --- /dev/null +++ b/tests/testthat/test-prop-tests.R @@ -0,0 +1,63 @@ +library(stats) + +test_that("iscamonepropztest agrees with prop.test", { + res <- capture_plot_result(suppressWarnings(iscamonepropztest( + observed = 35, + n = 50, + hypothesized = 0.5, + alternative = "greater", + conf.level = 0.95 + ))) + + statistic <- 35 / 50 + z_expected <- (statistic - 0.5) / sqrt(0.5 * (1 - 0.5) / 50) + p_expected <- signif(prop.test(35, 50, p = 0.5, alternative = "greater", correct = FALSE)$p.value, 4) + critical <- qnorm((1 - 0.95) / 2) + se_stat <- sqrt(statistic * (1 - statistic) / 50) + lower <- statistic + critical * se_stat + upper <- statistic - critical * se_stat + + expect_snapshot({ + cat("value:\n") + print(res$value) + cat("expected_z:", z_expected, "\n") + cat("expected_p:", p_expected, "\n") + cat("expected_ci:", lower, upper, "\n") + cat("output:\n") + cat(collapse_output(res$output), "\n") + }) +}) + +test_that("iscamtwopropztest matches two-sample z test calculations", { + res <- capture_plot_result(suppressWarnings(iscamtwopropztest( + observed1 = 35, + n1 = 50, + observed2 = 28, + n2 = 45, + hypothesized = 0, + alternative = "greater", + conf.level = 0.95 + ))) + + p1 <- 35 / 50 + p2 <- 28 / 45 + diff_est <- p1 - p2 + pooled <- (35 + 28) / (50 + 45) + z_expected <- (diff_est - 0) / sqrt(pooled * (1 - pooled) * (1 / 50 + 1 / 45)) + p_expected <- signif(1 - pnorm(z_expected), 4) + + se_diff <- sqrt(p1 * (1 - p1) / 50 + p2 * (1 - p2) / 45) + critical <- qnorm((1 - 0.95) / 2) + lower <- diff_est + critical * se_diff + upper <- diff_est - critical * se_diff + + expect_snapshot({ + cat("value:\n") + print(res$value) + cat("expected_z:", z_expected, "\n") + cat("expected_p:", p_expected, "\n") + cat("expected_ci:", lower, upper, "\n") + cat("output:\n") + cat(collapse_output(res$output), "\n") + }) +}) diff --git a/tests/testthat/test-t-suite.R b/tests/testthat/test-t-suite.R new file mode 100644 index 0000000..54286f1 --- /dev/null +++ b/tests/testthat/test-t-suite.R @@ -0,0 +1,131 @@ +library(stats) + +test_that("iscaminvt reports requested t quantiles", { + res_below <- capture_plot_result(iscaminvt( + 0.05, + df = 15, + direction = "below" + )) + res_outside <- capture_plot_result(iscaminvt( + 0.1, + df = 10, + direction = "outside" + )) + + qb <- qt(0.05, 15, lower.tail = TRUE) + ql <- qt(0.05, 10, lower.tail = TRUE) + qu <- qt(0.95, 10, lower.tail = TRUE) + + expect_snapshot({ + cat("below_answer:\n") + print(res_below$value) + cat("expected_below:", qb, "\n") + cat("below_output:\n") + cat(collapse_output(res_below$output), "\n") + cat("---\n") + cat("outside_answer:\n") + print(res_outside$value) + cat("expected_outside:", ql, qu, "\n") + cat("outside_output:\n") + cat(collapse_output(res_outside$output), "\n") + }) +}) + +test_that("iscamtprob matches t tail probabilities", { + res_below <- capture_plot_result(iscamtprob( + xval = -2.05, + df = 10, + direction = "below" + )) + res_between <- capture_plot_result(iscamtprob( + xval = -2, + xval2 = 2, + df = 12, + direction = "between" + )) + + expect_snapshot({ + cat("below_value:\n") + print(res_below$value) + cat("below_expected:", pt(-2.05, 10), "\n") + cat("below_output:\n") + cat(collapse_output(res_below$output), "\n") + cat("---\n") + cat("between_value:\n") + print(res_between$value) + cat("between_expected:", pt(2, 12) - pt(-2, 12), "\n") + cat("between_output:\n") + cat(collapse_output(res_between$output), "\n") + }) +}) + +test_that("iscamonesamplet returns Welch statistics", { + res <- capture_plot_result(suppressWarnings(iscamonesamplet( + xbar = 2.5, + sd = 1.2, + n = 30, + alternative = "greater", + hypothesized = 2, + conf.level = 0.95 + ))) + + se <- 1.2 / sqrt(30) + t_expected <- (2.5 - 2) / se + p_expected <- pt(t_expected, df = 29, lower.tail = FALSE) + critical <- qt((1 - 0.95) / 2, df = 29) + lower <- 2.5 + critical * se + upper <- 2.5 - critical * se + + expect_snapshot({ + cat("value:\n") + print(res$value) + cat("expected_t:", t_expected, "\n") + cat("expected_p:", p_expected, "\n") + cat("expected_ci:", lower, upper, "\n") + cat("output:\n") + cat(collapse_output(res$output), "\n") + }) +}) + +test_that("iscamtwosamplet returns Welch two-sample results", { + x1 <- 5 + sd1 <- 2 + n1 <- 30 + x2 <- 4 + sd2 <- 1.5 + n2 <- 28 + conf <- 0.95 + + res <- capture_plot_result(suppressWarnings(iscamtwosamplet( + x1 = x1, + sd1 = sd1, + n1 = n1, + x2 = x2, + sd2 = sd2, + n2 = n2, + hypothesized = 0, + alternative = "two.sided", + conf.level = conf + ))) + + se <- sqrt(sd1^2 / n1 + sd2^2 / n2) + df_calc <- (sd1^2 / n1 + sd2^2 / n2)^2 / + ((sd1^2 / n1)^2 / (n1 - 1) + (sd2^2 / n2)^2 / (n2 - 1)) + df_expected <- signif(df_calc, 4) + t_expected <- (x1 - x2) / se + p_expected <- 2 * pt(-abs(t_expected), df_expected) + critical <- qt((1 - conf) / 2, df_expected) + lower <- (x1 - x2) + critical * se + upper <- (x1 - x2) - critical * se + + expect_snapshot({ + cat("value:\n") + print(res$value) + cat("expected_df:", df_expected, "\n") + cat("expected_t:", t_expected, "\n") + cat("expected_p:", p_expected, "\n") + cat("expected_ci:", lower, upper, "\n") + cat("output:\n") + cat(collapse_output(res$output), "\n") + }) +}) From 577b7cc4df583f1c0c4f70bfac6b7e5a3a3e68df Mon Sep 17 00:00:00 2001 From: VisruthSK <67435125+VisruthSK@users.noreply.github.com> Date: Tue, 30 Sep 2025 11:17:44 -0700 Subject: [PATCH 2/4] Updated tests --- DESCRIPTION | 2 +- tests/testthat/_snaps/binomial-suite.md | 213 ---------------------- tests/testthat/_snaps/chisq-hyper.md | 113 ------------ tests/testthat/_snaps/iscamsummary.md | 37 ---- tests/testthat/_snaps/normal-suite.md | 118 ------------ tests/testthat/_snaps/prop-tests.md | 104 ----------- tests/testthat/_snaps/t-suite.md | 187 ------------------- tests/testthat/helper-capture.R | 11 +- tests/testthat/test-binomial-suite.R | 230 +++++++++++++++++------- tests/testthat/test-chisq-hyper.R | 93 ++++++---- tests/testthat/test-iscamsummary.R | 6 - tests/testthat/test-normal-suite.R | 88 ++++----- tests/testthat/test-prop-tests.R | 93 +++++++--- tests/testthat/test-t-suite.R | 157 ++++++++++------ 14 files changed, 457 insertions(+), 995 deletions(-) delete mode 100644 tests/testthat/_snaps/binomial-suite.md delete mode 100644 tests/testthat/_snaps/chisq-hyper.md delete mode 100644 tests/testthat/_snaps/iscamsummary.md delete mode 100644 tests/testthat/_snaps/normal-suite.md delete mode 100644 tests/testthat/_snaps/prop-tests.md delete mode 100644 tests/testthat/_snaps/t-suite.md diff --git a/DESCRIPTION b/DESCRIPTION index 4ad79bd..6648411 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,4 +34,4 @@ Config/testthat/edition: 3 Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.2 +RoxygenNote: 7.3.3 diff --git a/tests/testthat/_snaps/binomial-suite.md b/tests/testthat/_snaps/binomial-suite.md deleted file mode 100644 index 538f572..0000000 --- a/tests/testthat/_snaps/binomial-suite.md +++ /dev/null @@ -1,213 +0,0 @@ -# iscambinomprob returns expected tail probabilities - - Code - cat("lower_value:\n") - Output - lower_value: - Code - print(res_lower$value) - Output - [1] 0.3822806 - Code - cat("lower_expected:", pbinom(3, 10, 0.4), "\n") - Output - lower_expected: 0.3822806 - Code - cat("lower_output:\n") - Output - lower_output: - Code - cat(collapse_output(res_lower$output), "\n") - Output - Probability 3 and below = 0.3822806 - Code - cat("---\n") - Output - --- - Code - cat("upper_value:\n") - Output - upper_value: - Code - print(res_upper$value) - Output - [1] 0.05476188 - Code - cat("upper_expected:", 1 - pbinom(6, 10, 0.4), "\n") - Output - upper_expected: 0.05476188 - Code - cat("upper_output:\n") - Output - upper_output: - Code - cat(collapse_output(res_upper$output), "\n") - Output - Probability 7 and above = 0.05476188 - -# iscaminvbinom solves the correct quantile - - Code - cat("lower_value:\n") - Output - lower_value: - Code - print(res_lower$value) - Output - [1] 4 - Code - cat("lower_expected:", qbinom(0.1, 20, 0.4, lower.tail = TRUE) - 1, "\n") - Output - lower_expected: 4 - Code - cat("lower_output:\n") - Output - lower_output: - Code - cat(collapse_output(res_lower$output), "\n") - Output - The observation with at most 0.1 probability at or below is 4 - Code - cat("---\n") - Output - --- - Code - cat("upper_value:\n") - Output - upper_value: - Code - print(res_upper$value) - Output - [1] 12 - Code - cat("upper_expected:", qbinom(0.1, 20, 0.4, lower.tail = FALSE) + 1, "\n") - Output - upper_expected: 12 - Code - cat("upper_output:\n") - Output - upper_output: - Code - cat(collapse_output(res_upper$output), "\n") - Output - The observation with at most 0.1 probability at or above is 12 - -# iscambinomnorm executes for each direction - - Code - cat("below:\n") - Output - below: - Code - cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "below"))$ - output), "\n") - Output - binomial: 0.5881 - normal approx: 0.5 - normal approx with continuity: 0.5885 - Code - cat("---\n") - Output - --- - Code - cat("above:\n") - Output - above: - Code - cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "above"))$ - output), "\n") - Output - binomial: 0.5881 - normal approx: 0.5 - normal approx with continuity: 0.5885 - Code - cat("---\n") - Output - --- - Code - cat("two-sided:\n") - Output - two-sided: - Code - cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "two.sided"))$ - output), "\n") - Output - binomial: 1.176 - normal approx: 1 - normal approx with continuity: 1.177 - -# iscambinompower reports rejection probabilities - - Code - cat("critical_rr:", rr, "\n") - Output - critical_rr: 15 - Code - cat("null_prob:", null_prob, "\n") - Output - null_prob: 0.02069473 - Code - cat("alt_prob:", alt_prob, "\n") - Output - alt_prob: 0.125599 - Code - cat("value:\n") - Output - value: - Code - print(res$value) - Output - NULL - Code - cat("output:\n") - Output - output: - Code - cat(collapse_output(res$output), "\n") - Output - Null: Probability 15 and above = 0.02069473 - Alternative: Probability 15 and above = 0.125599 - -# iscambinomtest matches binom.test results - - Code - cat("value:\n") - Output - value: - Code - print(res$value) - Output - $pvalue - [1] 0.36159 - - $lower - [1] 0.40603 - - $upper - [1] 0.77344 - - Code - cat("expected_p: ", signif(bt$p.value, 5), "\n", sep = "") - Output - expected_p: 0.36159 - Code - cat("expected_ci:", bt$conf.int[1], bt$conf.int[2], "\n") - Output - expected_ci: 0.4060349 0.7734424 - Code - cat("output:\n") - Output - output: - Code - cat(collapse_output(res$output), "\n") - Output - - Exact Binomial Test - - Data: observed successes = 18, sample size = 30, sample proportion = 0.6 - - Null hypothesis : pi = 0.5 - Alternative hypothesis: pi <> 0.5 - p-value: 0.36159 - 95 % Confidence interval for pi: ( 0.40603 , 0.77344 ) - diff --git a/tests/testthat/_snaps/chisq-hyper.md b/tests/testthat/_snaps/chisq-hyper.md deleted file mode 100644 index eed4ace..0000000 --- a/tests/testthat/_snaps/chisq-hyper.md +++ /dev/null @@ -1,113 +0,0 @@ -# iscamchisqprob returns formatted upper-tail probability - - Code - cat("value:\n") - Output - value: - Code - print(res$value) - Output - [1] "0.1718" - Code - cat("expected:", pchisq(5, 3, lower.tail = FALSE), "\n") - Output - expected: 0.1717971 - Code - cat("output:\n") - Output - output: - Code - cat(collapse_output(res$output), "\n") - Output - probability: 0.1718 - -# iscamhyperprob matches hypergeometric tails - - Code - cat("lower_value:\n") - Output - lower_value: - Code - print(res_lower$value) - Output - [1] 0.7038184 - Code - cat("lower_expected:", phyper(2, 5, fail, 8), "\n") - Output - lower_expected: 0.7038184 - Code - cat("lower_output:\n") - Output - lower_output: - Code - cat(collapse_output(res_lower$output), "\n") - Output - Probability 2 and below = 0.7038184 - Code - cat("---\n") - Output - --- - Code - cat("upper_value:\n") - Output - upper_value: - Code - print(res_upper$value) - Output - [1] 0.2961816 - Code - cat("upper_expected:", 1 - phyper(2, 5, fail, 8), "\n") - Output - upper_expected: 0.2961816 - Code - cat("upper_output:\n") - Output - upper_output: - Code - cat(collapse_output(res_upper$output), "\n") - Output - Probability 3 and above = 0.2961816 - -# iscamhypernorm reports tail probabilities and normal approximations - - Code - cat("lower_value:\n") - Output - lower_value: - Code - print(res_lower$value) - Output - NULL - Code - cat("lower_output:\n") - Output - lower_output: - Code - cat(collapse_output(res_lower$output), "\n") - Output - hypergeometric: 0.7038 - normal approx: 0.5 - normal approx with continuity: 0.6963 - Code - cat("---\n") - Output - --- - Code - cat("upper_value:\n") - Output - upper_value: - Code - print(res_upper$value) - Output - NULL - Code - cat("upper_output:\n") - Output - upper_output: - Code - cat(collapse_output(res_upper$output), "\n") - Output - hypergeometric: 0.2962 - normal approx: 0.1521 - normal approx with continuity: 0.3037 - diff --git a/tests/testthat/_snaps/iscamsummary.md b/tests/testthat/_snaps/iscamsummary.md deleted file mode 100644 index b4e3bcc..0000000 --- a/tests/testthat/_snaps/iscamsummary.md +++ /dev/null @@ -1,37 +0,0 @@ -# summary without explanatory works - - Code - iscamsummary(fake_data) - Output - Missing n Min Q1 Median Q3 Max Mean SD Skewness - 1 0 30 -1.54 -0.621 -0.031 0.487 2.405 0.022 0.914 0.441 - ---- - - Code - iscamsummary(fake_data, digits = 5) - Output - Missing n Min Q1 Median Q3 Max Mean SD - 1 0 30 -1.53995 -0.62109 -0.03144 0.48663 2.40465 0.02195 0.91414 - Skewness - 1 0.44133 - -# summary with explanatory works - - Code - iscamsummary(fake_data, groups) - Output - Missing n Min Q1 Median Q3 Max Mean SD Skewness - group1 0 13 -1.238 -0.326 -0.057 0.436 1.263 -0.020 0.655 0.210 - group2 0 17 -1.540 -0.892 0.133 0.764 2.405 0.054 1.091 0.384 - ---- - - Code - iscamsummary(fake_data, groups) - Output - Missing n Min Q1 Median Q3 Max Mean SD Skewness - group1 0 10 -1.540 -0.298 -0.148 0.676 2.405 0.173 1.103 0.582 - group2 0 10 -1.285 -1.093 -0.089 0.360 1.330 -0.207 0.919 0.217 - group3 0 10 -0.892 -0.390 0.098 0.472 1.272 0.101 0.736 0.216 - diff --git a/tests/testthat/_snaps/normal-suite.md b/tests/testthat/_snaps/normal-suite.md deleted file mode 100644 index 677d84b..0000000 --- a/tests/testthat/_snaps/normal-suite.md +++ /dev/null @@ -1,118 +0,0 @@ -# iscamnormprob returns formatted probabilities - - Code - cat("above_value:\n") - Output - above_value: - Code - print(res_above$value) - Output - [1] "0.025" - Code - cat("above_expected:", pnorm(1.96, lower.tail = FALSE), "\n") - Output - above_expected: 0.0249979 - Code - cat("above_output:\n") - Output - above_output: - Code - cat(collapse_output(res_above$output), "\n") - Output - probability: 0.025 - Code - cat("---\n") - Output - --- - Code - cat("between_value:\n") - Output - between_value: - Code - print(res_between$value) - Output - [1] "0.6827" - Code - cat("between_expected:", pnorm(1) - pnorm(-1), "\n") - Output - between_expected: 0.6826895 - Code - cat("between_output:\n") - Output - between_output: - Code - cat(collapse_output(res_between$output), "\n") - Output - probability: 0.6827 - -# iscamnormpower reports null and alternative rejection rates - - Code - cat("rr:", rr, "\n") - Output - rr: 0.5919501 - Code - cat("null_prob:", null_prob, "\n") - Output - null_prob: 0.05 - Code - cat("alt_prob:", alt_prob, "\n") - Output - alt_prob: 0.2253625 - Code - cat("value:\n") - Output - value: - Code - print(res$value) - Output - NULL - Code - cat("output:\n") - Output - output: - Code - cat(collapse_output(res$output), "\n") - Output - Null: Probability 0.592 and above = 0.05 - Alt: Probability 0.592 and above = 0.2253625 - -# iscaminvnorm reports requested quantiles - - Code - cat("below_value:\n") - Output - below_value: - Code - print(res_below$value) - Output - NULL - Code - cat("below_output:\n") - Output - below_output: - Code - cat(collapse_output(res_below$output), "\n") - Output - The observation with 0.05 probability below is -1.645 - Code - cat("---\n") - Output - --- - Code - cat("outside_value:\n") - Output - outside_value: - Code - print(res_outside$value) - Output - NULL - Code - cat("outside_output:\n") - Output - outside_output: - Code - cat(collapse_output(res_outside$output), "\n") - Output - There is 0.1 probability outside -1.645 and 1.645 - diff --git a/tests/testthat/_snaps/prop-tests.md b/tests/testthat/_snaps/prop-tests.md deleted file mode 100644 index 74a7dc6..0000000 --- a/tests/testthat/_snaps/prop-tests.md +++ /dev/null @@ -1,104 +0,0 @@ -# iscamonepropztest agrees with prop.test - - Code - cat("value:\n") - Output - value: - Code - print(res$value) - Output - $zvalue - [1] 2.828427 - - $pvalue - [1] 0.002339 - - $lower - [1] 0.5729798 - - $upper - [1] 0.8270202 - - Code - cat("expected_z:", z_expected, "\n") - Output - expected_z: 2.828427 - Code - cat("expected_p:", p_expected, "\n") - Output - expected_p: 0.002339 - Code - cat("expected_ci:", lower, upper, "\n") - Output - expected_ci: 0.5729798 0.8270202 - Code - cat("output:\n") - Output - output: - Code - cat(collapse_output(res$output), "\n") - Output - - One Proportion z test - - Data: observed successes = 35, sample size = 50, sample proportion = 0.7 - - Null hypothesis : pi = 0.5 - Alternative hypothesis: pi > 0.5 - z-statistic: 2.828 - p-value: 0.002339 - 95 % Confidence interval for pi: ( 0.5729798 , 0.8270202 ) - -# iscamtwopropztest matches two-sample z test calculations - - Code - cat("value:\n") - Output - value: - Code - print(res$value) - Output - $zvalue - [1] 0.8008724 - - $pvalue - [1] 0.2116 - - $lower - [1] -0.1124861 - - $upper - [1] 0.2680417 - - Code - cat("expected_z:", z_expected, "\n") - Output - expected_z: 0.8008724 - Code - cat("expected_p:", p_expected, "\n") - Output - expected_p: 0.2116 - Code - cat("expected_ci:", lower, upper, "\n") - Output - expected_ci: -0.1124861 0.2680417 - Code - cat("output:\n") - Output - output: - Code - cat(collapse_output(res$output), "\n") - Output - - Two Proportion z test - - Group1: observed successes = 35, sample size = 50, sample proportion = 0.7 - - Group2: observed successes = 28, sample size = 45, sample proportion = 0.6222 - - Null hypothesis : pi1-pi2 = 0 - Alternative hypothesis: pi1-pi2 > 0 - z-statistic: 0.8009 - 95 % Confidence interval for pi1-pi2: ( -0.1124861 , 0.2680417 ) - p-value: 0.2116 - diff --git a/tests/testthat/_snaps/t-suite.md b/tests/testthat/_snaps/t-suite.md deleted file mode 100644 index c367881..0000000 --- a/tests/testthat/_snaps/t-suite.md +++ /dev/null @@ -1,187 +0,0 @@ -# iscaminvt reports requested t quantiles - - Code - cat("below_answer:\n") - Output - below_answer: - Code - print(res_below$value) - Output - $answer - [1] -1.753 - - Code - cat("expected_below:", qb, "\n") - Output - expected_below: -1.75305 - Code - cat("below_output:\n") - Output - below_output: - Code - cat(collapse_output(res_below$output), "\n") - Output - The observation with 0.05 probability below is -1.753 - Code - cat("---\n") - Output - --- - Code - cat("outside_answer:\n") - Output - outside_answer: - Code - print(res_outside$value) - Output - $answer1 - [1] -1.812 - - $answer2 - [1] 1.812 - - Code - cat("expected_outside:", ql, qu, "\n") - Output - expected_outside: -1.812461 1.812461 - Code - cat("outside_output:\n") - Output - outside_output: - Code - cat(collapse_output(res_outside$output), "\n") - Output - There is 0.1 probability outside -1.812 and 1.812 - -# iscamtprob matches t tail probabilities - - Code - cat("below_value:\n") - Output - below_value: - Code - print(res_below$value) - Output - NULL - Code - cat("below_expected:", pt(-2.05, 10), "\n") - Output - below_expected: 0.03375415 - Code - cat("below_output:\n") - Output - below_output: - Code - cat(collapse_output(res_below$output), "\n") - Output - probability: 0.03375 - Code - cat("---\n") - Output - --- - Code - cat("between_value:\n") - Output - between_value: - Code - print(res_between$value) - Output - NULL - Code - cat("between_expected:", pt(2, 12) - pt(-2, 12), "\n") - Output - between_expected: 0.931345 - Code - cat("between_output:\n") - Output - between_output: - Code - cat(collapse_output(res_between$output), "\n") - Output - probability: 0.9313 - -# iscamonesamplet returns Welch statistics - - Code - cat("value:\n") - Output - value: - Code - print(res$value) - Output - NULL - Code - cat("expected_t:", t_expected, "\n") - Output - expected_t: 2.282177 - Code - cat("expected_p:", p_expected, "\n") - Output - expected_p: 0.01499991 - Code - cat("expected_ci:", lower, upper, "\n") - Output - expected_ci: 2.051913 2.948087 - Code - cat("output:\n") - Output - output: - Code - cat(collapse_output(res$output), "\n") - Output - - One Sample t test - - mean = 2.5, sd = 1.2, sample size = 30 - Null hypothesis : mu = 2 - Alternative hypothesis: mu > 2 - t-statistic: 2.282 - 95 % Confidence interval for mu: ( 2.051913 , 2.948087 ) - p-value: 0.01499991 - -# iscamtwosamplet returns Welch two-sample results - - Code - cat("value:\n") - Output - value: - Code - print(res$value) - Output - NULL - Code - cat("expected_df:", df_expected, "\n") - Output - expected_df: 53.58 - Code - cat("expected_t:", t_expected, "\n") - Output - expected_t: 2.163254 - Code - cat("expected_p:", p_expected, "\n") - Output - expected_p: 0.03500158 - Code - cat("expected_ci:", lower, upper, "\n") - Output - expected_ci: 0.07304471 1.926955 - Code - cat("output:\n") - Output - output: - Code - cat(collapse_output(res$output), "\n") - Output - - Two Sample t test - - Group1: mean = 5, sd = 2, sample size = 30 - Group2: mean = 4, sd = 1.5, sample size = 28 - diff:1 - - Null hypothesis : mu1-mu2 = 0 - Alternative hypothesis: mu1-mu2 <> 0 - t-statistic: 2.163 - df: 53.58 - 95 % Confidence interval for mu1-mu2: ( 0.07304471 , 1.926955 ) - p-value: 0.035 - diff --git a/tests/testthat/helper-capture.R b/tests/testthat/helper-capture.R index 4f80521..0c7a71b 100644 --- a/tests/testthat/helper-capture.R +++ b/tests/testthat/helper-capture.R @@ -1,10 +1,13 @@ capture_plot_result <- function(expr) { tmp <- tempfile(fileext = ".pdf") grDevices::pdf(tmp) - on.exit({ - grDevices::dev.off() - unlink(tmp) - }, add = TRUE) + on.exit( + { + grDevices::dev.off() + unlink(tmp) + }, + add = TRUE + ) value <- NULL output <- capture.output({ value <- withVisible(force(expr)) diff --git a/tests/testthat/test-binomial-suite.R b/tests/testthat/test-binomial-suite.R index 739aac4..33aea7f 100644 --- a/tests/testthat/test-binomial-suite.R +++ b/tests/testthat/test-binomial-suite.R @@ -1,54 +1,120 @@ -library(stats) - test_that("iscambinomprob returns expected tail probabilities", { - res_lower <- capture_plot_result(iscambinomprob(k = 3, n = 10, prob = 0.4, lower.tail = TRUE)) - res_upper <- capture_plot_result(iscambinomprob(k = 7, n = 10, prob = 0.4, lower.tail = FALSE)) - - expect_snapshot({ - cat("lower_value:\n") - print(res_lower$value) - cat("lower_expected:", pbinom(3, 10, 0.4), "\n") - cat("lower_output:\n") - cat(collapse_output(res_lower$output), "\n") - cat("---\n") - cat("upper_value:\n") - print(res_upper$value) - cat("upper_expected:", 1 - pbinom(6, 10, 0.4), "\n") - cat("upper_output:\n") - cat(collapse_output(res_upper$output), "\n") - }) + res_lower <- capture_plot_result(iscambinomprob( + k = 3, + n = 10, + prob = 0.4, + lower.tail = TRUE + )) + res_upper <- capture_plot_result(iscambinomprob( + k = 7, + n = 10, + prob = 0.4, + lower.tail = FALSE + )) + + expect_equal(res_lower$value, pbinom(3, 10, 0.4), tolerance = 1e-6) + expect_equal(res_upper$value, 1 - pbinom(6, 10, 0.4), tolerance = 1e-6) + + lower_lines <- trimws(res_lower$output) + expect_true(any(grepl( + "Probability 3 and below =", + lower_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("= %.7f", res_lower$value), + lower_lines, + fixed = TRUE + ))) + + upper_lines <- trimws(res_upper$output) + expect_true(any(grepl( + "Probability 7 and above =", + upper_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("= %.8f", res_upper$value), + upper_lines, + fixed = TRUE + ))) }) test_that("iscaminvbinom solves the correct quantile", { - res_lower <- capture_plot_result(iscaminvbinom(alpha = 0.1, n = 20, prob = 0.4, lower.tail = TRUE)) - res_upper <- capture_plot_result(iscaminvbinom(alpha = 0.1, n = 20, prob = 0.4, lower.tail = FALSE)) - - expect_snapshot({ - cat("lower_value:\n") - print(res_lower$value) - cat("lower_expected:", qbinom(0.1, 20, 0.4, lower.tail = TRUE) - 1, "\n") - cat("lower_output:\n") - cat(collapse_output(res_lower$output), "\n") - cat("---\n") - cat("upper_value:\n") - print(res_upper$value) - cat("upper_expected:", qbinom(0.1, 20, 0.4, lower.tail = FALSE) + 1, "\n") - cat("upper_output:\n") - cat(collapse_output(res_upper$output), "\n") - }) + res_lower <- capture_plot_result(iscaminvbinom( + alpha = 0.1, + n = 20, + prob = 0.4, + lower.tail = TRUE + )) + res_upper <- capture_plot_result(iscaminvbinom( + alpha = 0.1, + n = 20, + prob = 0.4, + lower.tail = FALSE + )) + + expect_equal(res_lower$value, qbinom(0.1, 20, 0.4, lower.tail = TRUE) - 1) + expect_equal(res_upper$value, qbinom(0.1, 20, 0.4, lower.tail = FALSE) + 1) + + lower_lines <- trimws(res_lower$output) + expect_true(any(grepl( + "The observation with at most 0.1 probability at or below", + lower_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("is %d", res_lower$value), + lower_lines, + fixed = TRUE + ))) + + upper_lines <- trimws(res_upper$output) + expect_true(any(grepl( + "The observation with at most 0.1 probability at or above", + upper_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("is %d", res_upper$value), + upper_lines, + fixed = TRUE + ))) }) test_that("iscambinomnorm executes for each direction", { - expect_snapshot({ - cat("below:\n") - cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "below"))$output), "\n") - cat("---\n") - cat("above:\n") - cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "above"))$output), "\n") - cat("---\n") - cat("two-sided:\n") - cat(collapse_output(capture_plot_result(iscambinomnorm(10, 20, 0.5, "two.sided"))$output), "\n") - }) + below_lines <- trimws( + capture_plot_result(iscambinomnorm(10, 20, 0.5, "below"))$output + ) + expect_true(any(grepl("binomial: 0.5881", below_lines, fixed = TRUE))) + expect_true(any(grepl("normal approx: 0.5", below_lines, fixed = TRUE))) + expect_true(any(grepl( + "normal approx with continuity: 0.5885", + below_lines, + fixed = TRUE + ))) + + above_lines <- trimws( + capture_plot_result(iscambinomnorm(10, 20, 0.5, "above"))$output + ) + expect_true(any(grepl("binomial: 0.5881", above_lines, fixed = TRUE))) + expect_true(any(grepl("normal approx: 0.5", above_lines, fixed = TRUE))) + expect_true(any(grepl( + "normal approx with continuity: 0.5885", + above_lines, + fixed = TRUE + ))) + + two_sided_lines <- trimws( + capture_plot_result(iscambinomnorm(10, 20, 0.5, "two.sided"))$output + ) + expect_true(any(grepl("binomial: 1.176", two_sided_lines, fixed = TRUE))) + expect_true(any(grepl("normal approx: 1", two_sided_lines, fixed = TRUE))) + expect_true(any(grepl( + "normal approx with continuity: 1.177", + two_sided_lines, + fixed = TRUE + ))) }) test_that("iscambinompower reports rejection probabilities", { @@ -57,20 +123,33 @@ test_that("iscambinompower reports rejection probabilities", { prob1 <- 0.5 prob2 <- 0.6 - res <- capture_plot_result(suppressWarnings(iscambinompower(LOS = los, n = n, prob1 = prob1, alternative = "greater", prob2 = prob2))) + res <- capture_plot_result(suppressWarnings(iscambinompower( + LOS = los, + n = n, + prob1 = prob1, + alternative = "greater", + prob2 = prob2 + ))) rr <- qbinom(los, n, prob1, lower.tail = FALSE) + 1 null_prob <- 1 - pbinom(rr - 1, n, prob1) alt_prob <- 1 - pbinom(rr - 1, n, prob2) - expect_snapshot({ - cat("critical_rr:", rr, "\n") - cat("null_prob:", null_prob, "\n") - cat("alt_prob:", alt_prob, "\n") - cat("value:\n") - print(res$value) - cat("output:\n") - cat(collapse_output(res$output), "\n") - }) + expect_equal(rr, 15) + expect_equal(null_prob, 0.02069473, tolerance = 1e-6) + expect_equal(alt_prob, 0.125599, tolerance = 1e-6) + expect_null(res$value) + + output_lines <- trimws(res$output) + expect_true(any(grepl( + sprintf("Null: Probability %d and above = %.8f", rr, null_prob), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("Alternative: Probability %d and above = %.6f", rr, alt_prob), + output_lines, + fixed = TRUE + ))) }) test_that("iscambinomtest matches binom.test results", { @@ -84,12 +163,39 @@ test_that("iscambinomtest matches binom.test results", { bt <- binom.test(18, 30, p = 0.5, alternative = "two.sided") - expect_snapshot({ - cat("value:\n") - print(res$value) - cat("expected_p: ", signif(bt$p.value, 5), "\n", sep = "") - cat("expected_ci:", bt$conf.int[1], bt$conf.int[2], "\n") - cat("output:\n") - cat(collapse_output(res$output), "\n") - }) + expect_equal(res$value$pvalue, signif(bt$p.value, 5), tolerance = 1e-6) + expect_equal(res$value$lower, bt$conf.int[1], tolerance = 1e-4) + expect_equal(res$value$upper, bt$conf.int[2], tolerance = 1e-4) + + output_lines <- trimws(res$output) + expect_true("Exact Binomial Test" %in% output_lines) + expect_true(any(grepl( + "Data: observed successes = 18, sample size = 30, sample proportion = 0.6", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("Null hypothesis : pi = %.1f", 0.5), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + "Alternative hypothesis: pi <> 0.5", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("p-value: %.5f", res$value$pvalue), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf( + "95 %% Confidence interval for pi: ( %.5f , %.5f )", + res$value$lower, + res$value$upper + ), + output_lines, + fixed = TRUE + ))) }) diff --git a/tests/testthat/test-chisq-hyper.R b/tests/testthat/test-chisq-hyper.R index 327584a..302afc5 100644 --- a/tests/testthat/test-chisq-hyper.R +++ b/tests/testthat/test-chisq-hyper.R @@ -1,51 +1,68 @@ -library(stats) - test_that("iscamchisqprob returns formatted upper-tail probability", { res <- capture_plot_result(iscamchisqprob(5, 3)) - expect_snapshot({ - cat("value:\n") - print(res$value) - cat("expected:", pchisq(5, 3, lower.tail = FALSE), "\n") - cat("output:\n") - cat(collapse_output(res$output), "\n") - }) + expect_equal(as.numeric(res$value), pchisq(5, 3, lower.tail = FALSE), tolerance = 1e-4) + + output_lines <- trimws(res$output) + expect_true(any(grepl("probability: 0.1718", output_lines, fixed = TRUE))) }) test_that("iscamhyperprob matches hypergeometric tails", { - res_lower <- capture_plot_result(suppressWarnings(iscamhyperprob(k = 2, total = 20, succ = 5, n = 8, lower.tail = TRUE))) - res_upper <- capture_plot_result(suppressWarnings(iscamhyperprob(k = 3, total = 20, succ = 5, n = 8, lower.tail = FALSE))) + res_lower <- capture_plot_result(suppressWarnings(iscamhyperprob( + k = 2, + total = 20, + succ = 5, + n = 8, + lower.tail = TRUE + ))) + res_upper <- capture_plot_result(suppressWarnings(iscamhyperprob( + k = 3, + total = 20, + succ = 5, + n = 8, + lower.tail = FALSE + ))) fail <- 20 - 5 - expect_snapshot({ - cat("lower_value:\n") - print(res_lower$value) - cat("lower_expected:", phyper(2, 5, fail, 8), "\n") - cat("lower_output:\n") - cat(collapse_output(res_lower$output), "\n") - cat("---\n") - cat("upper_value:\n") - print(res_upper$value) - cat("upper_expected:", 1 - phyper(2, 5, fail, 8), "\n") - cat("upper_output:\n") - cat(collapse_output(res_upper$output), "\n") - }) + expect_equal(res_lower$value, phyper(2, 5, fail, 8), tolerance = 1e-6) + expect_equal(res_upper$value, 1 - phyper(2, 5, fail, 8), tolerance = 1e-6) + + lower_lines <- trimws(res_lower$output) + expect_true(any(grepl("Probability 2 and below =", lower_lines, fixed = TRUE))) + expect_true(any(grepl(sprintf("= %.7f", res_lower$value), lower_lines, fixed = TRUE))) + + upper_lines <- trimws(res_upper$output) + expect_true(any(grepl("Probability 3 and above =", upper_lines, fixed = TRUE))) + expect_true(any(grepl(sprintf("= %.7f", res_upper$value), upper_lines, fixed = TRUE))) }) test_that("iscamhypernorm reports tail probabilities and normal approximations", { - res_lower <- capture_plot_result(suppressWarnings(iscamhypernorm(k = 2, total = 20, succ = 5, n = 8, lower.tail = TRUE))) - res_upper <- capture_plot_result(suppressWarnings(iscamhypernorm(k = 3, total = 20, succ = 5, n = 8, lower.tail = FALSE))) - - expect_snapshot({ - cat("lower_value:\n") - print(res_lower$value) - cat("lower_output:\n") - cat(collapse_output(res_lower$output), "\n") - cat("---\n") - cat("upper_value:\n") - print(res_upper$value) - cat("upper_output:\n") - cat(collapse_output(res_upper$output), "\n") - }) + res_lower <- capture_plot_result(suppressWarnings(iscamhypernorm( + k = 2, + total = 20, + succ = 5, + n = 8, + lower.tail = TRUE + ))) + res_upper <- capture_plot_result(suppressWarnings(iscamhypernorm( + k = 3, + total = 20, + succ = 5, + n = 8, + lower.tail = FALSE + ))) + + expect_null(res_lower$value) + expect_null(res_upper$value) + + lower_lines <- trimws(res_lower$output) + expect_true(any(grepl("hypergeometric: 0.7038", lower_lines, fixed = TRUE))) + expect_true(any(grepl("normal approx: 0.5", lower_lines, fixed = TRUE))) + expect_true(any(grepl("normal approx with continuity: 0.6963", lower_lines, fixed = TRUE))) + + upper_lines <- trimws(res_upper$output) + expect_true(any(grepl("hypergeometric: 0.2962", upper_lines, fixed = TRUE))) + expect_true(any(grepl("normal approx: 0.1521", upper_lines, fixed = TRUE))) + expect_true(any(grepl("normal approx with continuity: 0.3037", upper_lines, fixed = TRUE))) }) diff --git a/tests/testthat/test-iscamsummary.R b/tests/testthat/test-iscamsummary.R index 94d9ba8..c7e3ee4 100644 --- a/tests/testthat/test-iscamsummary.R +++ b/tests/testthat/test-iscamsummary.R @@ -28,9 +28,6 @@ test_that("summary without explanatory works", { iscamsummary(fake_data, digits = 5), raw_summary |> round(5) ) - - expect_snapshot(iscamsummary(fake_data)) - expect_snapshot(iscamsummary(fake_data, digits = 5)) }) test_that("summary with explanatory works", { @@ -42,8 +39,6 @@ test_that("summary with explanatory works", { rbind(iscamsummary(group1), iscamsummary(group2)) |> structure(row.names = c("group1", "group2")) ) - expect_snapshot(iscamsummary(fake_data, groups)) - groups <- sample(c("group1", "group2", "group3"), 30, TRUE) group1 <- fake_data[groups == "group1"] group2 <- fake_data[groups == "group2"] @@ -54,5 +49,4 @@ test_that("summary with explanatory works", { rbind(iscamsummary(group1), iscamsummary(group2), iscamsummary(group3)) |> structure(row.names = c("group1", "group2", "group3")) ) - expect_snapshot(iscamsummary(fake_data, groups)) }) diff --git a/tests/testthat/test-normal-suite.R b/tests/testthat/test-normal-suite.R index 8624502..a1edea7 100644 --- a/tests/testthat/test-normal-suite.R +++ b/tests/testthat/test-normal-suite.R @@ -1,22 +1,22 @@ -library(stats) - test_that("iscamnormprob returns formatted probabilities", { - res_above <- capture_plot_result(suppressWarnings(iscamnormprob(1.96, direction = "above"))) - res_between <- capture_plot_result(suppressWarnings(iscamnormprob(-1, xval2 = 1, direction = "between"))) + res_above <- capture_plot_result(suppressWarnings(iscamnormprob( + 1.96, + direction = "above" + ))) + res_between <- capture_plot_result(suppressWarnings(iscamnormprob( + -1, + xval2 = 1, + direction = "between" + ))) + + expect_equal(as.numeric(res_above$value), round(pnorm(1.96, lower.tail = FALSE), 3), tolerance = 1e-6) + expect_equal(as.numeric(res_between$value), round(pnorm(1) - pnorm(-1), 4), tolerance = 1e-6) + + above_lines <- trimws(res_above$output) + expect_true(any(grepl("probability: 0.025", above_lines, fixed = TRUE))) - expect_snapshot({ - cat("above_value:\n") - print(res_above$value) - cat("above_expected:", pnorm(1.96, lower.tail = FALSE), "\n") - cat("above_output:\n") - cat(collapse_output(res_above$output), "\n") - cat("---\n") - cat("between_value:\n") - print(res_between$value) - cat("between_expected:", pnorm(1) - pnorm(-1), "\n") - cat("between_output:\n") - cat(collapse_output(res_between$output), "\n") - }) + between_lines <- trimws(res_between$output) + expect_true(any(grepl("probability: 0.6827", between_lines, fixed = TRUE))) }) test_that("iscamnormpower reports null and alternative rejection rates", { @@ -25,7 +25,13 @@ test_that("iscamnormpower reports null and alternative rejection rates", { prob1 <- 0.5 prob2 <- 0.55 - res <- capture_plot_result(suppressWarnings(iscamnormpower(LOS = los, n = n, prob1 = prob1, alternative = "greater", prob2 = prob2))) + res <- capture_plot_result(suppressWarnings(iscamnormpower( + LOS = los, + n = n, + prob1 = prob1, + alternative = "greater", + prob2 = prob2 + ))) mean1 <- prob1 sd1 <- sqrt(prob1 * (1 - prob1) / n) @@ -36,30 +42,32 @@ test_that("iscamnormpower reports null and alternative rejection rates", { sd2 <- sqrt(prob2 * (1 - prob2) / n) alt_prob <- 1 - pnorm(rr, mean2, sd2) - expect_snapshot({ - cat("rr:", rr, "\n") - cat("null_prob:", null_prob, "\n") - cat("alt_prob:", alt_prob, "\n") - cat("value:\n") - print(res$value) - cat("output:\n") - cat(collapse_output(res$output), "\n") - }) + expect_equal(rr, 0.5919501, tolerance = 1e-6) + expect_equal(null_prob, 0.05, tolerance = 1e-7) + expect_equal(alt_prob, 0.2253625, tolerance = 1e-7) + expect_null(res$value) + + output_lines <- trimws(res$output) + expect_true(any(grepl("Null: Probability 0.592 and above = 0.05", output_lines, fixed = TRUE))) + expect_true(any(grepl("Alt: Probability 0.592 and above = 0.2253625", output_lines, fixed = TRUE))) }) test_that("iscaminvnorm reports requested quantiles", { - res_below <- capture_plot_result(suppressWarnings(iscaminvnorm(0.05, direction = "below"))) - res_outside <- capture_plot_result(suppressWarnings(iscaminvnorm(0.1, direction = "outside"))) + res_below <- capture_plot_result(suppressWarnings(iscaminvnorm( + 0.05, + direction = "below" + ))) + res_outside <- capture_plot_result(suppressWarnings(iscaminvnorm( + 0.1, + direction = "outside" + ))) + + expect_null(res_below$value) + expect_null(res_outside$value) + + below_lines <- trimws(res_below$output) + expect_true(any(grepl("The observation with 0.05 probability below is -1.645", below_lines, fixed = TRUE))) - expect_snapshot({ - cat("below_value:\n") - print(res_below$value) - cat("below_output:\n") - cat(collapse_output(res_below$output), "\n") - cat("---\n") - cat("outside_value:\n") - print(res_outside$value) - cat("outside_output:\n") - cat(collapse_output(res_outside$output), "\n") - }) + outside_lines <- trimws(res_outside$output) + expect_true(any(grepl("There is 0.1 probability outside -1.645 and 1.645", outside_lines, fixed = TRUE))) }) diff --git a/tests/testthat/test-prop-tests.R b/tests/testthat/test-prop-tests.R index 8ec6958..15dde8f 100644 --- a/tests/testthat/test-prop-tests.R +++ b/tests/testthat/test-prop-tests.R @@ -1,5 +1,3 @@ -library(stats) - test_that("iscamonepropztest agrees with prop.test", { res <- capture_plot_result(suppressWarnings(iscamonepropztest( observed = 35, @@ -11,21 +9,52 @@ test_that("iscamonepropztest agrees with prop.test", { statistic <- 35 / 50 z_expected <- (statistic - 0.5) / sqrt(0.5 * (1 - 0.5) / 50) - p_expected <- signif(prop.test(35, 50, p = 0.5, alternative = "greater", correct = FALSE)$p.value, 4) + p_expected <- signif( + prop.test( + 35, + 50, + p = 0.5, + alternative = "greater", + correct = FALSE + )$p.value, + 4 + ) critical <- qnorm((1 - 0.95) / 2) se_stat <- sqrt(statistic * (1 - statistic) / 50) lower <- statistic + critical * se_stat upper <- statistic - critical * se_stat - expect_snapshot({ - cat("value:\n") - print(res$value) - cat("expected_z:", z_expected, "\n") - cat("expected_p:", p_expected, "\n") - cat("expected_ci:", lower, upper, "\n") - cat("output:\n") - cat(collapse_output(res$output), "\n") - }) + expect_equal(res$value$zvalue, z_expected, tolerance = 1e-6) + expect_equal(res$value$pvalue, p_expected, tolerance = 1e-6) + expect_equal(res$value$lower, lower, tolerance = 1e-6) + expect_equal(res$value$upper, upper, tolerance = 1e-6) + + output_lines <- trimws(res$output) + expect_true("One Proportion z test" %in% output_lines) + expect_true(any(grepl( + "Null hypothesis : pi = 0.5", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("z-statistic: %.3f", res$value$zvalue), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("p-value: %.6f", res$value$pvalue), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf( + "95 %% Confidence interval for pi: ( %.7f , %.7f )", + res$value$lower, + res$value$upper + ), + output_lines, + fixed = TRUE + ))) }) test_that("iscamtwopropztest matches two-sample z test calculations", { @@ -51,13 +80,35 @@ test_that("iscamtwopropztest matches two-sample z test calculations", { lower <- diff_est + critical * se_diff upper <- diff_est - critical * se_diff - expect_snapshot({ - cat("value:\n") - print(res$value) - cat("expected_z:", z_expected, "\n") - cat("expected_p:", p_expected, "\n") - cat("expected_ci:", lower, upper, "\n") - cat("output:\n") - cat(collapse_output(res$output), "\n") - }) + expect_equal(res$value$zvalue, z_expected, tolerance = 1e-6) + expect_equal(res$value$pvalue, p_expected, tolerance = 1e-6) + expect_equal(res$value$lower, lower, tolerance = 1e-6) + expect_equal(res$value$upper, upper, tolerance = 1e-6) + + output_lines <- trimws(res$output) + expect_true("Two Proportion z test" %in% output_lines) + expect_true(any(grepl( + "Null hypothesis : pi1-pi2 = 0", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("z-statistic: %.4f", res$value$zvalue), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("p-value: %.4f", res$value$pvalue), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf( + "95 %% Confidence interval for pi1-pi2: ( %.7f , %.7f )", + res$value$lower, + res$value$upper + ), + output_lines, + fixed = TRUE + ))) }) diff --git a/tests/testthat/test-t-suite.R b/tests/testthat/test-t-suite.R index 54286f1..b91e2f6 100644 --- a/tests/testthat/test-t-suite.R +++ b/tests/testthat/test-t-suite.R @@ -1,5 +1,3 @@ -library(stats) - test_that("iscaminvt reports requested t quantiles", { res_below <- capture_plot_result(iscaminvt( 0.05, @@ -12,23 +10,21 @@ test_that("iscaminvt reports requested t quantiles", { direction = "outside" )) - qb <- qt(0.05, 15, lower.tail = TRUE) - ql <- qt(0.05, 10, lower.tail = TRUE) - qu <- qt(0.95, 10, lower.tail = TRUE) - - expect_snapshot({ - cat("below_answer:\n") - print(res_below$value) - cat("expected_below:", qb, "\n") - cat("below_output:\n") - cat(collapse_output(res_below$output), "\n") - cat("---\n") - cat("outside_answer:\n") - print(res_outside$value) - cat("expected_outside:", ql, qu, "\n") - cat("outside_output:\n") - cat(collapse_output(res_outside$output), "\n") - }) + expect_equal( + res_below$value$answer, + round(qt(0.05, 15, lower.tail = TRUE), 3), + tolerance = 1e-6 + ) + expect_equal( + res_outside$value$answer1, + round(qt(0.05, 10, lower.tail = TRUE), 3), + tolerance = 1e-6 + ) + expect_equal( + res_outside$value$answer2, + round(qt(0.95, 10, lower.tail = TRUE), 3), + tolerance = 1e-6 + ) }) test_that("iscamtprob matches t tail probabilities", { @@ -44,19 +40,14 @@ test_that("iscamtprob matches t tail probabilities", { direction = "between" )) - expect_snapshot({ - cat("below_value:\n") - print(res_below$value) - cat("below_expected:", pt(-2.05, 10), "\n") - cat("below_output:\n") - cat(collapse_output(res_below$output), "\n") - cat("---\n") - cat("between_value:\n") - print(res_between$value) - cat("between_expected:", pt(2, 12) - pt(-2, 12), "\n") - cat("between_output:\n") - cat(collapse_output(res_between$output), "\n") - }) + expect_null(res_below$value) + expect_null(res_between$value) + + below_lines <- trimws(res_below$output) + expect_true(any(grepl("probability: 0.03375", below_lines, fixed = TRUE))) + + between_lines <- trimws(res_between$output) + expect_true(any(grepl("probability: 0.9313", between_lines, fixed = TRUE))) }) test_that("iscamonesamplet returns Welch statistics", { @@ -76,15 +67,40 @@ test_that("iscamonesamplet returns Welch statistics", { lower <- 2.5 + critical * se upper <- 2.5 - critical * se - expect_snapshot({ - cat("value:\n") - print(res$value) - cat("expected_t:", t_expected, "\n") - cat("expected_p:", p_expected, "\n") - cat("expected_ci:", lower, upper, "\n") - cat("output:\n") - cat(collapse_output(res$output), "\n") - }) + expect_null(res$value) + + output_lines <- trimws(res$output) + expect_true("One Sample t test" %in% output_lines) + expect_true(any(grepl( + "mean = 2.5, sd = 1.2, sample size = 30", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + "Null hypothesis : mu = 2", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + "Alternative hypothesis: mu > 2", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("t-statistic: %.3f", t_expected), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("p-value: %.8f", p_expected), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("95 %% Confidence interval for mu: ( %.6f , %.6f )", lower, upper), + output_lines, + fixed = TRUE + ))) }) test_that("iscamtwosamplet returns Welch two-sample results", { @@ -118,14 +134,53 @@ test_that("iscamtwosamplet returns Welch two-sample results", { lower <- (x1 - x2) + critical * se upper <- (x1 - x2) - critical * se - expect_snapshot({ - cat("value:\n") - print(res$value) - cat("expected_df:", df_expected, "\n") - cat("expected_t:", t_expected, "\n") - cat("expected_p:", p_expected, "\n") - cat("expected_ci:", lower, upper, "\n") - cat("output:\n") - cat(collapse_output(res$output), "\n") - }) + expect_null(res$value) + + output_lines <- trimws(res$output) + expect_true("Two Sample t test" %in% output_lines) + expect_true(any(grepl( + "Group1: mean = 5, sd = 2, sample size = 30", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + "Group2: mean = 4, sd = 1.5, sample size = 28", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl("diff:1", output_lines, fixed = TRUE))) + expect_true(any(grepl( + "Null hypothesis : mu1-mu2 = 0", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + "Alternative hypothesis: mu1-mu2 <> 0", + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("t-statistic: %.3f", t_expected), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("df: %.2f", df_expected), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf("p-value: %.3f", p_expected), + output_lines, + fixed = TRUE + ))) + expect_true(any(grepl( + sprintf( + "95 %% Confidence interval for mu1-mu2: ( %.8f , %.6f )", + lower, + upper + ), + output_lines, + fixed = TRUE + ))) }) From 6b7c9ce29bfe1d626257b877b6bb20e38aa376af Mon Sep 17 00:00:00 2001 From: VisruthSK <67435125+VisruthSK@users.noreply.github.com> Date: Tue, 30 Sep 2025 12:18:38 -0700 Subject: [PATCH 3/4] Cleaned up tests --- tests/testthat/_snaps/binomial-suite.md | 69 +++++++ tests/testthat/_snaps/chisq-hyper.md | 39 ++++ tests/testthat/_snaps/normal-suite.md | 36 ++++ ...-parameters-plot.svg => custom-params.svg} | 174 +++++++++--------- .../{iscamaddexp-plot.svg => exponential.svg} | 2 +- .../overlayDensities/iscamaddt-plot.svg | 69 ------- .../overlayDensities/iscamaddtnorm-plot.svg | 74 -------- ...{iscamaddlnorm-plot.svg => log-normal.svg} | 2 +- ...{iscamaddnorm-plot.svg => normal-plot.svg} | 2 +- .../_snaps/overlayDensities/t-and-normal.svg | 83 +++++++++ .../overlayDensities/t-distribution.svg | 69 +++++++ ...-var.svg => iscamboxplot-one-variable.svg} | 0 ...ars.svg => iscamboxplot-two-variables.svg} | 0 ...-var.svg => iscamdotplot-one-variable.svg} | 0 ...ars.svg => iscamdotplot-two-variables.svg} | 0 tests/testthat/_snaps/prop-tests.md | 34 ++++ tests/testthat/_snaps/t-suite.md | 48 +++++ tests/testthat/test-binomial-suite.R | 124 +------------ tests/testthat/test-chisq-hyper.R | 29 +-- tests/testthat/test-iscamsummary.R | 1 - tests/testthat/test-normal-suite.R | 30 +-- tests/testthat/test-overlayDensities.R | 49 +++-- tests/testthat/test-plots.R | 8 +- tests/testthat/test-prop-tests.R | 54 +----- tests/testthat/test-t-suite.R | 88 +-------- 25 files changed, 541 insertions(+), 543 deletions(-) create mode 100644 tests/testthat/_snaps/binomial-suite.md create mode 100644 tests/testthat/_snaps/chisq-hyper.md create mode 100644 tests/testthat/_snaps/normal-suite.md rename tests/testthat/_snaps/overlayDensities/{custom-parameters-plot.svg => custom-params.svg} (99%) rename tests/testthat/_snaps/overlayDensities/{iscamaddexp-plot.svg => exponential.svg} (99%) delete mode 100644 tests/testthat/_snaps/overlayDensities/iscamaddt-plot.svg delete mode 100644 tests/testthat/_snaps/overlayDensities/iscamaddtnorm-plot.svg rename tests/testthat/_snaps/overlayDensities/{iscamaddlnorm-plot.svg => log-normal.svg} (99%) rename tests/testthat/_snaps/overlayDensities/{iscamaddnorm-plot.svg => normal-plot.svg} (99%) create mode 100644 tests/testthat/_snaps/overlayDensities/t-and-normal.svg create mode 100644 tests/testthat/_snaps/overlayDensities/t-distribution.svg rename tests/testthat/_snaps/plots/{iscamboxplot-one-var.svg => iscamboxplot-one-variable.svg} (100%) rename tests/testthat/_snaps/plots/{iscamboxplot-two-vars.svg => iscamboxplot-two-variables.svg} (100%) rename tests/testthat/_snaps/plots/{iscamdotplot-one-var.svg => iscamdotplot-one-variable.svg} (100%) rename tests/testthat/_snaps/plots/{iscamdotplot-two-vars.svg => iscamdotplot-two-variables.svg} (100%) create mode 100644 tests/testthat/_snaps/prop-tests.md create mode 100644 tests/testthat/_snaps/t-suite.md diff --git a/tests/testthat/_snaps/binomial-suite.md b/tests/testthat/_snaps/binomial-suite.md new file mode 100644 index 0000000..10af668 --- /dev/null +++ b/tests/testthat/_snaps/binomial-suite.md @@ -0,0 +1,69 @@ +# iscambinomprob returns expected tail probabilities + + Code + res_lower$output + Output + [1] "Probability 3 and below = 0.3822806 " + +--- + + Code + res_upper$output + Output + [1] "Probability 7 and above = 0.05476188 " + +# iscaminvbinom solves the correct quantile + + Code + res_lower$output + Output + [1] "The observation with at most 0.1 probability at or below is 4 " + +--- + + Code + res_upper$output + Output + [1] "The observation with at most 0.1 probability at or above is 12 " + +# iscambinomnorm executes for each direction + + Code + capture_plot_result(iscambinomnorm(10, 20, 0.5, "below"))$output + Output + [1] " binomial: 0.5881 " + [2] " normal approx: 0.5 " + [3] " normal approx with continuity: 0.5885 " + +--- + + Code + capture_plot_result(iscambinomnorm(10, 20, 0.5, "above"))$output + Output + [1] " binomial: 0.5881 " + [2] " normal approx: 0.5 " + [3] " normal approx with continuity: 0.5885 " + +# iscambinompower reports rejection probabilities + + Code + res$output + Output + [1] "Null: Probability 15 and above = 0.02069473 " + [2] "Alternative: Probability 15 and above = 0.125599 " + +# iscambinomtest matches binom.test results + + Code + res$output + Output + [1] "" + [2] "Exact Binomial Test" + [3] "" + [4] "Data: observed successes = 18, sample size = 30, sample proportion = 0.6" + [5] "" + [6] "Null hypothesis : pi = 0.5 " + [7] "Alternative hypothesis: pi <> 0.5 " + [8] "p-value: 0.36159 " + [9] "95 % Confidence interval for pi: ( 0.40603 , 0.77344 ) " + diff --git a/tests/testthat/_snaps/chisq-hyper.md b/tests/testthat/_snaps/chisq-hyper.md new file mode 100644 index 0000000..8ac671e --- /dev/null +++ b/tests/testthat/_snaps/chisq-hyper.md @@ -0,0 +1,39 @@ +# iscamchisqprob returns formatted upper-tail probability + + Code + res$output + Output + [1] "probability: 0.1718 " + +# iscamhyperprob matches hypergeometric tails + + Code + res_lower$output + Output + [1] "Probability 2 and below = 0.7038184 " + +--- + + Code + res_upper$output + Output + [1] "Probability 3 and above = 0.2961816 " + +# iscamhypernorm reports tail probabilities and normal approximations + + Code + res_lower$output + Output + [1] " hypergeometric: 0.7038 " + [2] " normal approx: 0.5 " + [3] " normal approx with continuity: 0.6963 " + +--- + + Code + res_upper$output + Output + [1] " hypergeometric: 0.2962 " + [2] " normal approx: 0.1521 " + [3] " normal approx with continuity: 0.3037 " + diff --git a/tests/testthat/_snaps/normal-suite.md b/tests/testthat/_snaps/normal-suite.md new file mode 100644 index 0000000..6374734 --- /dev/null +++ b/tests/testthat/_snaps/normal-suite.md @@ -0,0 +1,36 @@ +# iscamnormprob returns formatted probabilities + + Code + res_above$output + Output + [1] "probability: 0.025 " + +--- + + Code + res_between$output + Output + [1] "probability: 0.6827 " + +# iscamnormpower reports null and alternative rejection rates + + Code + res$output + Output + [1] "Null: Probability 0.592 and above = 0.05 " + [2] "Alt: Probability 0.592 and above = 0.2253625 " + +# iscaminvnorm reports requested quantiles + + Code + res_below$output + Output + [1] "The observation with 0.05 probability below is -1.645 " + +--- + + Code + res_outside$output + Output + [1] "There is 0.1 probability outside -1.645 and 1.645 " + diff --git a/tests/testthat/_snaps/overlayDensities/custom-parameters-plot.svg b/tests/testthat/_snaps/overlayDensities/custom-params.svg similarity index 99% rename from tests/testthat/_snaps/overlayDensities/custom-parameters-plot.svg rename to tests/testthat/_snaps/overlayDensities/custom-params.svg index 8a4bc55..3f8bad9 100644 --- a/tests/testthat/_snaps/overlayDensities/custom-parameters-plot.svg +++ b/tests/testthat/_snaps/overlayDensities/custom-params.svg @@ -1,87 +1,87 @@ - - - - - - - - - - - - - - - - - - --2 --1 -0 -1 -2 - - - - - - - - -0.0 -0.1 -0.2 -0.3 -0.4 -0.5 -0.6 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -My Data -density -Custom Title - - + + + + + + + + + + + + + + + + + + +-2 +-1 +0 +1 +2 + + + + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 +0.5 +0.6 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +My Data +density +Custom Title + + diff --git a/tests/testthat/_snaps/overlayDensities/iscamaddexp-plot.svg b/tests/testthat/_snaps/overlayDensities/exponential.svg similarity index 99% rename from tests/testthat/_snaps/overlayDensities/iscamaddexp-plot.svg rename to tests/testthat/_snaps/overlayDensities/exponential.svg index dc6a208..636e5c4 100644 --- a/tests/testthat/_snaps/overlayDensities/iscamaddexp-plot.svg +++ b/tests/testthat/_snaps/overlayDensities/exponential.svg @@ -58,7 +58,7 @@ -x +generate_data(dist = "exp", rate = 2) density Histogram with exponential curve diff --git a/tests/testthat/_snaps/overlayDensities/iscamaddt-plot.svg b/tests/testthat/_snaps/overlayDensities/iscamaddt-plot.svg deleted file mode 100644 index 904112e..0000000 --- a/tests/testthat/_snaps/overlayDensities/iscamaddt-plot.svg +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - - - - - - - - - - - - - --2 --1 -0 -1 -2 - - - - - - - -0.0 -0.1 -0.2 -0.3 -0.4 -0.5 - - - - - - - - - - - - - - - - - - - - -x -density -Histogram with t curve - - diff --git a/tests/testthat/_snaps/overlayDensities/iscamaddtnorm-plot.svg b/tests/testthat/_snaps/overlayDensities/iscamaddtnorm-plot.svg deleted file mode 100644 index 3f1023f..0000000 --- a/tests/testthat/_snaps/overlayDensities/iscamaddtnorm-plot.svg +++ /dev/null @@ -1,74 +0,0 @@ - - - - - - - - - - - - - - - - - --4 --2 -0 -2 - - - - - - -0.0 -0.1 -0.2 -0.3 -0.4 - - - - - - - - - - - - - - - - - - - - - - -x -density -Histogram with t and normal curve - - - - - -normal -t, df=5 - - diff --git a/tests/testthat/_snaps/overlayDensities/iscamaddlnorm-plot.svg b/tests/testthat/_snaps/overlayDensities/log-normal.svg similarity index 99% rename from tests/testthat/_snaps/overlayDensities/iscamaddlnorm-plot.svg rename to tests/testthat/_snaps/overlayDensities/log-normal.svg index 40e628e..e5ec3ce 100644 --- a/tests/testthat/_snaps/overlayDensities/iscamaddlnorm-plot.svg +++ b/tests/testthat/_snaps/overlayDensities/log-normal.svg @@ -65,7 +65,7 @@ -x +generate_data(dist = "lnorm") density Histogram with log-normal curve diff --git a/tests/testthat/_snaps/overlayDensities/iscamaddnorm-plot.svg b/tests/testthat/_snaps/overlayDensities/normal-plot.svg similarity index 99% rename from tests/testthat/_snaps/overlayDensities/iscamaddnorm-plot.svg rename to tests/testthat/_snaps/overlayDensities/normal-plot.svg index 8ec0cad..a606938 100644 --- a/tests/testthat/_snaps/overlayDensities/iscamaddnorm-plot.svg +++ b/tests/testthat/_snaps/overlayDensities/normal-plot.svg @@ -63,7 +63,7 @@ -x +generate_data(dist = "norm") density Histogram with normal curve diff --git a/tests/testthat/_snaps/overlayDensities/t-and-normal.svg b/tests/testthat/_snaps/overlayDensities/t-and-normal.svg new file mode 100644 index 0000000..cf734af --- /dev/null +++ b/tests/testthat/_snaps/overlayDensities/t-and-normal.svg @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + +-15 +-10 +-5 +0 +5 +10 +15 + + + + + + + + +0.00 +0.02 +0.04 +0.06 +0.08 +0.10 +0.12 + + + + + + + + + + + + + + + + + + + + + +generate_data(dist = "t", df = 1) +density +Histogram with t and normal curve + + + + + +normal +t, df=1 + + diff --git a/tests/testthat/_snaps/overlayDensities/t-distribution.svg b/tests/testthat/_snaps/overlayDensities/t-distribution.svg new file mode 100644 index 0000000..032091d --- /dev/null +++ b/tests/testthat/_snaps/overlayDensities/t-distribution.svg @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + +-2 +-1 +0 +1 +2 + + + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 +0.5 + + + + + + + + + + + + + + + + + + + + +generate_data(dist = "t", df = 15) +density +Histogram with t curve + + diff --git a/tests/testthat/_snaps/plots/iscamboxplot-one-var.svg b/tests/testthat/_snaps/plots/iscamboxplot-one-variable.svg similarity index 100% rename from tests/testthat/_snaps/plots/iscamboxplot-one-var.svg rename to tests/testthat/_snaps/plots/iscamboxplot-one-variable.svg diff --git a/tests/testthat/_snaps/plots/iscamboxplot-two-vars.svg b/tests/testthat/_snaps/plots/iscamboxplot-two-variables.svg similarity index 100% rename from tests/testthat/_snaps/plots/iscamboxplot-two-vars.svg rename to tests/testthat/_snaps/plots/iscamboxplot-two-variables.svg diff --git a/tests/testthat/_snaps/plots/iscamdotplot-one-var.svg b/tests/testthat/_snaps/plots/iscamdotplot-one-variable.svg similarity index 100% rename from tests/testthat/_snaps/plots/iscamdotplot-one-var.svg rename to tests/testthat/_snaps/plots/iscamdotplot-one-variable.svg diff --git a/tests/testthat/_snaps/plots/iscamdotplot-two-vars.svg b/tests/testthat/_snaps/plots/iscamdotplot-two-variables.svg similarity index 100% rename from tests/testthat/_snaps/plots/iscamdotplot-two-vars.svg rename to tests/testthat/_snaps/plots/iscamdotplot-two-variables.svg diff --git a/tests/testthat/_snaps/prop-tests.md b/tests/testthat/_snaps/prop-tests.md new file mode 100644 index 0000000..8649ad5 --- /dev/null +++ b/tests/testthat/_snaps/prop-tests.md @@ -0,0 +1,34 @@ +# iscamonepropztest agrees with prop.test + + Code + res$output + Output + [1] "" + [2] "One Proportion z test" + [3] "" + [4] "Data: observed successes = 35, sample size = 50, sample proportion = 0.7" + [5] "" + [6] "Null hypothesis : pi = 0.5 " + [7] "Alternative hypothesis: pi > 0.5 " + [8] "z-statistic: 2.828 " + [9] "p-value: 0.002339 " + [10] "95 % Confidence interval for pi: ( 0.5729798 , 0.8270202 ) " + +# iscamtwopropztest matches two-sample z test calculations + + Code + res$output + Output + [1] "" + [2] "Two Proportion z test" + [3] "" + [4] "Group1: observed successes = 35, sample size = 50, sample proportion = 0.7" + [5] "" + [6] "Group2: observed successes = 28, sample size = 45, sample proportion = 0.6222" + [7] "" + [8] "Null hypothesis : pi1-pi2 = 0 " + [9] "Alternative hypothesis: pi1-pi2 > 0 " + [10] "z-statistic: 0.8009 " + [11] "95 % Confidence interval for pi1-pi2: ( -0.1124861 , 0.2680417 ) " + [12] "p-value: 0.2116 " + diff --git a/tests/testthat/_snaps/t-suite.md b/tests/testthat/_snaps/t-suite.md new file mode 100644 index 0000000..701401e --- /dev/null +++ b/tests/testthat/_snaps/t-suite.md @@ -0,0 +1,48 @@ +# iscamtprob matches t tail probabilities + + Code + res_below$output + Output + [1] "probability: 0.03375 " + +--- + + Code + res_between$output + Output + [1] "probability: 0.9313 " + +# iscamonesamplet returns Welch statistics + + Code + res$output + Output + [1] "" + [2] "One Sample t test" + [3] "" + [4] "mean = 2.5, sd = 1.2, sample size = 30" + [5] "Null hypothesis : mu = 2 " + [6] "Alternative hypothesis: mu > 2 " + [7] "t-statistic: 2.282 " + [8] "95 % Confidence interval for mu: ( 2.051913 , 2.948087 ) " + [9] "p-value: 0.01499991 " + +# iscamtwosamplet returns Welch two-sample results + + Code + res$output + Output + [1] "" + [2] "Two Sample t test" + [3] "" + [4] "Group1: mean = 5, sd = 2, sample size = 30" + [5] "Group2: mean = 4, sd = 1.5, sample size = 28" + [6] "diff:1" + [7] "" + [8] "Null hypothesis : mu1-mu2 = 0 " + [9] "Alternative hypothesis: mu1-mu2 <> 0 " + [10] "t-statistic: 2.163 " + [11] "df: 53.58 " + [12] "95 % Confidence interval for mu1-mu2: ( 0.07304471 , 1.926955 ) " + [13] "p-value: 0.035 " + diff --git a/tests/testthat/test-binomial-suite.R b/tests/testthat/test-binomial-suite.R index 33aea7f..597526f 100644 --- a/tests/testthat/test-binomial-suite.R +++ b/tests/testthat/test-binomial-suite.R @@ -15,29 +15,8 @@ test_that("iscambinomprob returns expected tail probabilities", { expect_equal(res_lower$value, pbinom(3, 10, 0.4), tolerance = 1e-6) expect_equal(res_upper$value, 1 - pbinom(6, 10, 0.4), tolerance = 1e-6) - lower_lines <- trimws(res_lower$output) - expect_true(any(grepl( - "Probability 3 and below =", - lower_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("= %.7f", res_lower$value), - lower_lines, - fixed = TRUE - ))) - - upper_lines <- trimws(res_upper$output) - expect_true(any(grepl( - "Probability 7 and above =", - upper_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("= %.8f", res_upper$value), - upper_lines, - fixed = TRUE - ))) + expect_snapshot(res_lower$output) + expect_snapshot(res_upper$output) }) test_that("iscaminvbinom solves the correct quantile", { @@ -57,64 +36,17 @@ test_that("iscaminvbinom solves the correct quantile", { expect_equal(res_lower$value, qbinom(0.1, 20, 0.4, lower.tail = TRUE) - 1) expect_equal(res_upper$value, qbinom(0.1, 20, 0.4, lower.tail = FALSE) + 1) - lower_lines <- trimws(res_lower$output) - expect_true(any(grepl( - "The observation with at most 0.1 probability at or below", - lower_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("is %d", res_lower$value), - lower_lines, - fixed = TRUE - ))) - - upper_lines <- trimws(res_upper$output) - expect_true(any(grepl( - "The observation with at most 0.1 probability at or above", - upper_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("is %d", res_upper$value), - upper_lines, - fixed = TRUE - ))) + expect_snapshot(res_lower$output) + expect_snapshot(res_upper$output) }) test_that("iscambinomnorm executes for each direction", { - below_lines <- trimws( + expect_snapshot( capture_plot_result(iscambinomnorm(10, 20, 0.5, "below"))$output ) - expect_true(any(grepl("binomial: 0.5881", below_lines, fixed = TRUE))) - expect_true(any(grepl("normal approx: 0.5", below_lines, fixed = TRUE))) - expect_true(any(grepl( - "normal approx with continuity: 0.5885", - below_lines, - fixed = TRUE - ))) - - above_lines <- trimws( + expect_snapshot( capture_plot_result(iscambinomnorm(10, 20, 0.5, "above"))$output ) - expect_true(any(grepl("binomial: 0.5881", above_lines, fixed = TRUE))) - expect_true(any(grepl("normal approx: 0.5", above_lines, fixed = TRUE))) - expect_true(any(grepl( - "normal approx with continuity: 0.5885", - above_lines, - fixed = TRUE - ))) - - two_sided_lines <- trimws( - capture_plot_result(iscambinomnorm(10, 20, 0.5, "two.sided"))$output - ) - expect_true(any(grepl("binomial: 1.176", two_sided_lines, fixed = TRUE))) - expect_true(any(grepl("normal approx: 1", two_sided_lines, fixed = TRUE))) - expect_true(any(grepl( - "normal approx with continuity: 1.177", - two_sided_lines, - fixed = TRUE - ))) }) test_that("iscambinompower reports rejection probabilities", { @@ -139,17 +71,7 @@ test_that("iscambinompower reports rejection probabilities", { expect_equal(alt_prob, 0.125599, tolerance = 1e-6) expect_null(res$value) - output_lines <- trimws(res$output) - expect_true(any(grepl( - sprintf("Null: Probability %d and above = %.8f", rr, null_prob), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("Alternative: Probability %d and above = %.6f", rr, alt_prob), - output_lines, - fixed = TRUE - ))) + expect_snapshot(res$output) }) test_that("iscambinomtest matches binom.test results", { @@ -167,35 +89,5 @@ test_that("iscambinomtest matches binom.test results", { expect_equal(res$value$lower, bt$conf.int[1], tolerance = 1e-4) expect_equal(res$value$upper, bt$conf.int[2], tolerance = 1e-4) - output_lines <- trimws(res$output) - expect_true("Exact Binomial Test" %in% output_lines) - expect_true(any(grepl( - "Data: observed successes = 18, sample size = 30, sample proportion = 0.6", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("Null hypothesis : pi = %.1f", 0.5), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - "Alternative hypothesis: pi <> 0.5", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("p-value: %.5f", res$value$pvalue), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf( - "95 %% Confidence interval for pi: ( %.5f , %.5f )", - res$value$lower, - res$value$upper - ), - output_lines, - fixed = TRUE - ))) + expect_snapshot(res$output) }) diff --git a/tests/testthat/test-chisq-hyper.R b/tests/testthat/test-chisq-hyper.R index 302afc5..1da0272 100644 --- a/tests/testthat/test-chisq-hyper.R +++ b/tests/testthat/test-chisq-hyper.R @@ -1,10 +1,13 @@ test_that("iscamchisqprob returns formatted upper-tail probability", { res <- capture_plot_result(iscamchisqprob(5, 3)) - expect_equal(as.numeric(res$value), pchisq(5, 3, lower.tail = FALSE), tolerance = 1e-4) + expect_equal( + as.numeric(res$value), + pchisq(5, 3, lower.tail = FALSE), + tolerance = 1e-4 + ) - output_lines <- trimws(res$output) - expect_true(any(grepl("probability: 0.1718", output_lines, fixed = TRUE))) + expect_snapshot(res$output) }) test_that("iscamhyperprob matches hypergeometric tails", { @@ -28,13 +31,8 @@ test_that("iscamhyperprob matches hypergeometric tails", { expect_equal(res_lower$value, phyper(2, 5, fail, 8), tolerance = 1e-6) expect_equal(res_upper$value, 1 - phyper(2, 5, fail, 8), tolerance = 1e-6) - lower_lines <- trimws(res_lower$output) - expect_true(any(grepl("Probability 2 and below =", lower_lines, fixed = TRUE))) - expect_true(any(grepl(sprintf("= %.7f", res_lower$value), lower_lines, fixed = TRUE))) - - upper_lines <- trimws(res_upper$output) - expect_true(any(grepl("Probability 3 and above =", upper_lines, fixed = TRUE))) - expect_true(any(grepl(sprintf("= %.7f", res_upper$value), upper_lines, fixed = TRUE))) + expect_snapshot(res_lower$output) + expect_snapshot(res_upper$output) }) test_that("iscamhypernorm reports tail probabilities and normal approximations", { @@ -56,13 +54,6 @@ test_that("iscamhypernorm reports tail probabilities and normal approximations", expect_null(res_lower$value) expect_null(res_upper$value) - lower_lines <- trimws(res_lower$output) - expect_true(any(grepl("hypergeometric: 0.7038", lower_lines, fixed = TRUE))) - expect_true(any(grepl("normal approx: 0.5", lower_lines, fixed = TRUE))) - expect_true(any(grepl("normal approx with continuity: 0.6963", lower_lines, fixed = TRUE))) - - upper_lines <- trimws(res_upper$output) - expect_true(any(grepl("hypergeometric: 0.2962", upper_lines, fixed = TRUE))) - expect_true(any(grepl("normal approx: 0.1521", upper_lines, fixed = TRUE))) - expect_true(any(grepl("normal approx with continuity: 0.3037", upper_lines, fixed = TRUE))) + expect_snapshot(res_lower$output) + expect_snapshot(res_upper$output) }) diff --git a/tests/testthat/test-iscamsummary.R b/tests/testthat/test-iscamsummary.R index c7e3ee4..fe39721 100644 --- a/tests/testthat/test-iscamsummary.R +++ b/tests/testthat/test-iscamsummary.R @@ -17,7 +17,6 @@ raw_summary <- data.frame( row.names = NULL ) - test_that("summary without explanatory works", { expect_equal( iscamsummary(fake_data), diff --git a/tests/testthat/test-normal-suite.R b/tests/testthat/test-normal-suite.R index a1edea7..b769e1e 100644 --- a/tests/testthat/test-normal-suite.R +++ b/tests/testthat/test-normal-suite.R @@ -9,14 +9,19 @@ test_that("iscamnormprob returns formatted probabilities", { direction = "between" ))) - expect_equal(as.numeric(res_above$value), round(pnorm(1.96, lower.tail = FALSE), 3), tolerance = 1e-6) - expect_equal(as.numeric(res_between$value), round(pnorm(1) - pnorm(-1), 4), tolerance = 1e-6) + expect_equal( + as.numeric(res_above$value), + round(pnorm(1.96, lower.tail = FALSE), 3), + tolerance = 1e-6 + ) + expect_equal( + as.numeric(res_between$value), + round(pnorm(1) - pnorm(-1), 4), + tolerance = 1e-6 + ) - above_lines <- trimws(res_above$output) - expect_true(any(grepl("probability: 0.025", above_lines, fixed = TRUE))) - - between_lines <- trimws(res_between$output) - expect_true(any(grepl("probability: 0.6827", between_lines, fixed = TRUE))) + expect_snapshot(res_above$output) + expect_snapshot(res_between$output) }) test_that("iscamnormpower reports null and alternative rejection rates", { @@ -47,9 +52,7 @@ test_that("iscamnormpower reports null and alternative rejection rates", { expect_equal(alt_prob, 0.2253625, tolerance = 1e-7) expect_null(res$value) - output_lines <- trimws(res$output) - expect_true(any(grepl("Null: Probability 0.592 and above = 0.05", output_lines, fixed = TRUE))) - expect_true(any(grepl("Alt: Probability 0.592 and above = 0.2253625", output_lines, fixed = TRUE))) + expect_snapshot(res$output) }) test_that("iscaminvnorm reports requested quantiles", { @@ -65,9 +68,6 @@ test_that("iscaminvnorm reports requested quantiles", { expect_null(res_below$value) expect_null(res_outside$value) - below_lines <- trimws(res_below$output) - expect_true(any(grepl("The observation with 0.05 probability below is -1.645", below_lines, fixed = TRUE))) - - outside_lines <- trimws(res_outside$output) - expect_true(any(grepl("There is 0.1 probability outside -1.645 and 1.645", outside_lines, fixed = TRUE))) + expect_snapshot(res_below$output) + expect_snapshot(res_outside$output) }) diff --git a/tests/testthat/test-overlayDensities.R b/tests/testthat/test-overlayDensities.R index 988db8c..d9d5251 100644 --- a/tests/testthat/test-overlayDensities.R +++ b/tests/testthat/test-overlayDensities.R @@ -11,40 +11,51 @@ generate_data <- function(n = 100, dist = c("norm", "exp", "lnorm", "t"), ...) { } test_that("iscamaddexp creates an exponential plot", { - x <- generate_data(dist = "exp", rate = 2) - vdiffr::expect_doppelganger("iscamaddexp plot", function() iscamaddexp(x)) + vdiffr::expect_doppelganger("exponential", function() { + iscamaddexp(generate_data( + dist = "exp", + rate = 2 + )) + }) }) test_that("iscamaddlnorm creates a log-normal plot", { - x <- generate_data(dist = "lnorm") - vdiffr::expect_doppelganger("iscamaddlnorm plot", function() iscamaddlnorm(x)) + vdiffr::expect_doppelganger("log-normal", function() { + iscamaddlnorm(generate_data(dist = "lnorm")) + }) }) test_that("iscamaddnorm creates a normal plot", { - x <- generate_data(dist = "norm") - vdiffr::expect_doppelganger("iscamaddnorm plot", function() iscamaddnorm(x)) + vdiffr::expect_doppelganger("Normal plot", function() { + iscamaddnorm(generate_data(dist = "norm")) + }) }) test_that("iscamaddt creates a t-distribution plot", { - x <- generate_data(dist = "t", df = 15) - vdiffr::expect_doppelganger("iscamaddt plot", function() { - iscamaddt(x, df = 10) + vdiffr::expect_doppelganger("t-distribution", function() { + iscamaddt( + generate_data(dist = "t", df = 15), + df = 15 + ) }) }) test_that("iscamaddtnorm creates a t and normal plot", { - x <- generate_data(dist = "t", df = 5) - vdiffr::expect_doppelganger("iscamaddtnorm plot", function() { - iscamaddtnorm(x, df = 5) + vdiffr::expect_doppelganger("t and normal", function() { + iscamaddtnorm( + generate_data(dist = "t", df = 1), + df = 1 + ) }) }) test_that("Plotting with custom parameters works", { - x <- generate_data(dist = "norm") - vdiffr::expect_doppelganger( - "Custom parameters plot", - function() { - iscamaddnorm(x, main = "Custom Title", xlab = "My Data", bins = 20) - } - ) + vdiffr::expect_doppelganger("Custom params", function() { + iscamaddnorm( + generate_data(dist = "norm"), + main = "Custom Title", + xlab = "My Data", + bins = 20 + ) + }) }) diff --git a/tests/testthat/test-plots.R b/tests/testthat/test-plots.R index 611962a..73d764a 100644 --- a/tests/testthat/test-plots.R +++ b/tests/testthat/test-plots.R @@ -1,5 +1,5 @@ test_that("iscamboxplot works correctly with one variable", { - vdiffr::expect_doppelganger("iscamboxplot-one-var", function() { + vdiffr::expect_doppelganger("iscamboxplot-one-variable", function() { iscamboxplot( mtcars$mpg, main = "mtcars Cylinders iscamdotplot", @@ -9,7 +9,7 @@ test_that("iscamboxplot works correctly with one variable", { }) test_that("iscamboxplot works correctly with two variables", { - vdiffr::expect_doppelganger("iscamboxplot-two-vars", function() { + vdiffr::expect_doppelganger("iscamboxplot-two-variables", function() { iscamboxplot( mtcars$mpg, mtcars$am, @@ -21,7 +21,7 @@ test_that("iscamboxplot works correctly with two variables", { }) test_that("iscamdotplot works correctly with one variable", { - vdiffr::expect_doppelganger("iscamdotplot-one-var", function() { + vdiffr::expect_doppelganger("iscamdotplot-one-variable", function() { iscamdotplot( mtcars$cyl, main = "mtcars Cylinders iscamdotplot", @@ -31,7 +31,7 @@ test_that("iscamdotplot works correctly with one variable", { }) test_that("iscamdotplot works correctly with two variables", { - vdiffr::expect_doppelganger("iscamdotplot-two-vars", function() { + vdiffr::expect_doppelganger("iscamdotplot-two-variables", function() { iscamdotplot( mtcars$mpg, mtcars$am, diff --git a/tests/testthat/test-prop-tests.R b/tests/testthat/test-prop-tests.R index 15dde8f..414b1db 100644 --- a/tests/testthat/test-prop-tests.R +++ b/tests/testthat/test-prop-tests.R @@ -29,32 +29,7 @@ test_that("iscamonepropztest agrees with prop.test", { expect_equal(res$value$lower, lower, tolerance = 1e-6) expect_equal(res$value$upper, upper, tolerance = 1e-6) - output_lines <- trimws(res$output) - expect_true("One Proportion z test" %in% output_lines) - expect_true(any(grepl( - "Null hypothesis : pi = 0.5", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("z-statistic: %.3f", res$value$zvalue), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("p-value: %.6f", res$value$pvalue), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf( - "95 %% Confidence interval for pi: ( %.7f , %.7f )", - res$value$lower, - res$value$upper - ), - output_lines, - fixed = TRUE - ))) + expect_snapshot(res$output) }) test_that("iscamtwopropztest matches two-sample z test calculations", { @@ -85,30 +60,5 @@ test_that("iscamtwopropztest matches two-sample z test calculations", { expect_equal(res$value$lower, lower, tolerance = 1e-6) expect_equal(res$value$upper, upper, tolerance = 1e-6) - output_lines <- trimws(res$output) - expect_true("Two Proportion z test" %in% output_lines) - expect_true(any(grepl( - "Null hypothesis : pi1-pi2 = 0", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("z-statistic: %.4f", res$value$zvalue), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("p-value: %.4f", res$value$pvalue), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf( - "95 %% Confidence interval for pi1-pi2: ( %.7f , %.7f )", - res$value$lower, - res$value$upper - ), - output_lines, - fixed = TRUE - ))) + expect_snapshot(res$output) }) diff --git a/tests/testthat/test-t-suite.R b/tests/testthat/test-t-suite.R index b91e2f6..94efc9f 100644 --- a/tests/testthat/test-t-suite.R +++ b/tests/testthat/test-t-suite.R @@ -43,11 +43,8 @@ test_that("iscamtprob matches t tail probabilities", { expect_null(res_below$value) expect_null(res_between$value) - below_lines <- trimws(res_below$output) - expect_true(any(grepl("probability: 0.03375", below_lines, fixed = TRUE))) - - between_lines <- trimws(res_between$output) - expect_true(any(grepl("probability: 0.9313", between_lines, fixed = TRUE))) + expect_snapshot(res_below$output) + expect_snapshot(res_between$output) }) test_that("iscamonesamplet returns Welch statistics", { @@ -69,38 +66,7 @@ test_that("iscamonesamplet returns Welch statistics", { expect_null(res$value) - output_lines <- trimws(res$output) - expect_true("One Sample t test" %in% output_lines) - expect_true(any(grepl( - "mean = 2.5, sd = 1.2, sample size = 30", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - "Null hypothesis : mu = 2", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - "Alternative hypothesis: mu > 2", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("t-statistic: %.3f", t_expected), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("p-value: %.8f", p_expected), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("95 %% Confidence interval for mu: ( %.6f , %.6f )", lower, upper), - output_lines, - fixed = TRUE - ))) + expect_snapshot(res$output) }) test_that("iscamtwosamplet returns Welch two-sample results", { @@ -136,51 +102,5 @@ test_that("iscamtwosamplet returns Welch two-sample results", { expect_null(res$value) - output_lines <- trimws(res$output) - expect_true("Two Sample t test" %in% output_lines) - expect_true(any(grepl( - "Group1: mean = 5, sd = 2, sample size = 30", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - "Group2: mean = 4, sd = 1.5, sample size = 28", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl("diff:1", output_lines, fixed = TRUE))) - expect_true(any(grepl( - "Null hypothesis : mu1-mu2 = 0", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - "Alternative hypothesis: mu1-mu2 <> 0", - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("t-statistic: %.3f", t_expected), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("df: %.2f", df_expected), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf("p-value: %.3f", p_expected), - output_lines, - fixed = TRUE - ))) - expect_true(any(grepl( - sprintf( - "95 %% Confidence interval for mu1-mu2: ( %.8f , %.6f )", - lower, - upper - ), - output_lines, - fixed = TRUE - ))) + expect_snapshot(res$output) }) From edf525da353f136d70e475ad484d6ccdc3a8b664 Mon Sep 17 00:00:00 2001 From: VisruthSK <67435125+VisruthSK@users.noreply.github.com> Date: Mon, 6 Oct 2025 17:17:12 -0700 Subject: [PATCH 4/4] Bumped version to v0.1.0 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6648411..218a7e3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: ISCAM Title: Companion to the Book "Investigating Statistical Concepts, Applications, and Methods" -Version: 0.0.0.9000 +Version: 0.1.0 Authors@R: c( person("Beth", "Chance", , "bchance@calpoly.edu", role = c("cre", "aut", "cph")), person("Visruth", "Srimath Kandali", , "visruth@gmail.com", role = "aut",