Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 29, 2023
1 parent 5974417 commit 6039335
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
41 changes: 41 additions & 0 deletions tests/testthat/test-model_parameters.bracl.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
skip_if_not_installed("brglm2")
skip_if_not_installed("faraway")
skip_if_not(packageVersion("insight") > "0.19.1")

skip_on_cran()

test_that("model_parameters.bracl", {
data("cns", package = "faraway")
cns2 <- reshape(cns,
direction = "long", timevar = "Type",
times = names(cns)[3:5], varying = 3:5, v.names = "Freq"
)[, 3:6]
cns2$Type <- factor(cns2$Type, levels = unique(cns2$Type))

mbracl <- brglm2::bracl(Type ~ Water + Work, data = cns2, weights = Freq)
mpbracl <- model_parameters(mbracl)

expect_named(
mpbracl,
c(
"Parameter", "Coefficient", "SE", "CI", "CI_low", "CI_high",
"z", "df_error", "p", "Response"
)
)
expect_identical(
mpbracl$Parameter,
c(
"An:(Intercept)", "Sp:(Intercept)", "An:Water", "Sp:Water",
"An:WorkNonManual", "Sp:WorkNonManual"
)
)
expect_identical(
mpbracl$Response,
c("An", "Sp", "An", "Sp", "An", "Sp")
)
expect_equal(
mpbracl$Coefficient,
c(-0.37392, 1.49063, 0.00129, -0.00349, -0.11292, 0.36384),
tolerance = 1e-4
)
})
58 changes: 46 additions & 12 deletions tests/testthat/test-model_parameters.nnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ skip_if_not(packageVersion("insight") > "0.19.1")

skip_on_cran()

data("cns", package = "faraway")
cns2 <- reshape(cns,
direction = "long", timevar = "Type",
times = names(cns)[3:5], varying = 3:5, v.names = "Freq"
)[, 3:6]
cns2$Type <- factor(cns2$Type, levels = unique(cns2$Type))

mnnet1 <- nnet::multinom(Type ~ Water + Work, data = cns2, weights = Freq, trace = FALSE)
mnnet2 <- nnet::multinom(cbind(An, Sp, Other) ~ Water + Work, data = cns, trace = FALSE)

test_that("model_parameters.multinom - long and wide", {
data("cns", package = "faraway")
cns2 <- reshape(cns,
direction = "long", timevar = "Type",
times = names(cns)[3:5], varying = 3:5, v.names = "Freq"
)[, 3:6]
cns2$Type <- factor(cns2$Type, levels = unique(cns2$Type))

mnnet1 <- nnet::multinom(Type ~ Water + Work, data = cns2, weights = Freq, trace = FALSE)
mpnnet1 <- model_parameters(mnnet1)
mpnnet2 <- model_parameters(mnnet2)

expect_named(
mpnnet1,
Expand All @@ -24,7 +27,7 @@ test_that("model_parameters.multinom - long and wide", {
)
expect_identical(
mpnnet1$Parameter,
c("(Intercept)", "Water", "WorkNonManual", "(Intercept)", "Water","WorkNonManual")
c("(Intercept)", "Water", "WorkNonManual", "(Intercept)", "Water", "WorkNonManual")
)
expect_identical(
mpnnet1$Response,
Expand All @@ -36,9 +39,6 @@ test_that("model_parameters.multinom - long and wide", {
tolerance = 1e-4
)

mnnet2 <- nnet::multinom(cbind(An, Sp, Other) ~ Water + Work, data = cns, trace = FALSE)
mpnnet2 <- model_parameters(mnnet2)

expect_named(
mpnnet2,
c(
Expand All @@ -60,3 +60,37 @@ test_that("model_parameters.multinom - long and wide", {
tolerance = 1e-4
)
})


test_that("ci.multinom - long and wide", {
cinnet1 <- ci(mnnet1)
cinnet2 <- ci(mnnet2)

expect_identical(
cinnet1$Parameter,
c("(Intercept)", "Water", "WorkNonManual", "(Intercept)", "Water", "WorkNonManual")
)
expect_identical(
cinnet1$Response,
c("Sp", "Sp", "Sp", "Other", "Other", "Other")
)
# expect_equal(
# cinnet1$CI_low,
# c(-0.0083, -0.0054, -0.30539, -1.68673, -0.00366, -0.9256),
# tolerance = 1e-4
# )

expect_identical(
cinnet2$Parameter,
c("(Intercept)", "Water", "WorkNonManual", "(Intercept)", "Water", "WorkNonManual")
)
expect_identical(
cinnet2$Response,
c("Sp", "Sp", "Sp", "Other", "Other", "Other")
)
# expect_equal(
# cinnet2$CI_low,
# c(-0.0083, -0.0054, -0.30539, -1.68673, -0.00366, -0.9256),
# tolerance = 1e-4
# )
})

0 comments on commit 6039335

Please sign in to comment.