Skip to content

Commit

Permalink
tests anova should be complete
Browse files Browse the repository at this point in the history
  • Loading branch information
nbruder committed Nov 18, 2023
1 parent 9107390 commit d92bde1
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 39 deletions.
6 changes: 4 additions & 2 deletions R/FDistribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#' @exportClass NestedModels
setClass("NestedModels", representation(
p_inner = "numeric",
p_outer = "numeric"),
p_outer = "numeric"),
contains = "DataDistribution")


Expand Down Expand Up @@ -73,6 +73,8 @@ NestedModels <- function(p_inner, p_outer) {
#' @rdname ANOVA-class
#' @export
ANOVA <- function(n_groups) {
if (n_groups < 0 || abs(n_groups - round(n_groups)) > sqrt(.Machine$double.eps))
stop("The number of groups must be a natural number.")
new("ANOVA", p_outer = n_groups, p_inner = 1L)
}

Expand Down Expand Up @@ -158,7 +160,7 @@ setMethod("simulate", signature("NestedModels", "numeric"),

setMethod("print", signature('NestedModels'), function(x, ...) {
glue::glue(
"{class(x)[1]}<p_inner={x@p_inner}, p_outer={x@p_outer}>"
"{class(x)[1]}<p_inner={x@p_inner},p_outer={x@p_outer}>"
)
})

Expand Down
14 changes: 13 additions & 1 deletion man/ANOVA-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions man/BinomialDataDistribution-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions man/ChiSquaredDataDistribution-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions man/NestedModels-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/Pearson2xK-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ZSquared-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions man/cumulative_distribution_function.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions man/probability_density_function.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions tests/testthat/test_ANOVA.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,117 @@
context("F-distribution")

test_that("Constructors work", {

expect_true(
NestedModels(4, 5)@p_inner == 4 &
NestedModels(4, 5)@p_outer ==5
)

expect_true(
ANOVA(6)@p_inner == 1 &
ANOVA(6)@p_outer == 6
)

expect_error(
NestedModels(2.3, 1)
)

expect_error(
NestedModels(2, -1)
)

expect_error(
ANOVA(3.4)
)

expect_error(
ANOVA(-6)
)
})

test_that("thetas and ncps are correctly computed", {


# ANOVA
p_vec <- c(0.8, 0.15, 0.3, 0.96)
n <- 52
sigma <- 2
dist <- ANOVA(length(p_vec))

real_ncp <- n * (sum((p_vec - mean(p_vec))^2)) / sigma^2
ncp_calc <- (n * (dist@p_outer - dist@p_inner + 1)) * get_tau_ANOVA(p_vec, sigma)

expect_equal(
real_ncp, ncp_calc
)

})

test_that("pdf is defined correctly", {

dist <- ANOVA(3)
x <- seq(0.1, 5, by = 0.1)
n <- 42
p_vec <- c(0.3, 0.4, 0.5)
theta <- get_tau_ANOVA(p_vec)
real_ncp <- n * (sum((p_vec - mean(p_vec))^2))
expect_equal(
probability_density_function(dist, x, n, theta),
stats::df(x, 2, 3 * n - 3, ncp = real_ncp)
)

})

test_that("cdf is defined correctly", {

dist <- ANOVA(3)
x <- seq(0.1, 5, by = 0.1)
n <- 32
p_vec <- c(0.2, 0.4, 0.5)
theta <- get_tau_ANOVA(p_vec)
real_ncp <- n * (sum((p_vec - mean(p_vec))^2))
expect_equal(
cumulative_distribution_function(dist, x, n, theta),
stats::pf(x, 2, 3 * n - 3, ncp = real_ncp)
)
})

test_that("quantile is defined correctly", {

dist <- ANOVA(4)
x <- seq(0.1, 5, by = 0.1)
n <- 22
p_vec <- c(0.2, 0.4, 0.7, 0.36)
theta <- get_tau_ANOVA(p_vec)
real_ncp <- n * (sum((p_vec - mean(p_vec))^2))
expect_equal(
cumulative_distribution_function(dist, x, n, theta),
stats::pf(x, 3, 4 * n - 4, ncp = real_ncp)
)
})

test_that("simulate respects seed", {

expect_equal(
simulate(ANOVA(3), 10, 10, 0.2, seed = 42),
simulate(ANOVA(3), 10, 10, 0.2, seed = 42),
tolerance = 1e-6, scale = 1)

set.seed(42)

expect_true(
all(simulate(ANOVA(4), 10, 12, 1.1) != simulate(ANOVA(4), 10, 12, 1.1)))
})

test_that("show_method", {

expect_equal(
capture.output(show(NestedModels(4, 5))),
"NestedModels<p_inner=4,p_outer=5> "
)

expect_equal(
capture.output(show(ANOVA(4))),
"ANOVA<n_groups=4> "
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test_ChiSquared.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test_that('constructors work', {
)
})

test_that('thetas are correctly computed', {
test_that('thetas and ncps are correctly computed', {

# theoretical ncp and calculated ncp are equal
# Pearson2xK
Expand Down

0 comments on commit d92bde1

Please sign in to comment.