Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 12, 2023
1 parent a8fa07d commit 43cff24
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions tests/testthat/test-standardize_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ test_that("standardize.lm", {
m0 <- lm(Sepal.Length ~ Species * Petal.Width, data = iris_z)
m1 <- lm(Sepal.Length ~ Species * Petal.Width, data = iris2)
model <- standardize(m1)
expect_identical(coef(m0), coef(model))
expect_equal(coef(m0), coef(model), tolerance = 1e-5)
})

test_that("standardize.lm, edge case (intercept only)", {
iris2 <- na.omit(iris)
iris_z <- standardize(iris2)
m0 <- lm(Sepal.Length ~ 1, data = iris_z)
m1 <- lm(Sepal.Length ~ 1, data = iris2)
model <- standardize(m1)
expect_equal(coef(m0), coef(model), tolerance = 1e-5)
})

test_that("standardize, mlm", {
Expand Down Expand Up @@ -47,12 +56,14 @@ test_that("transformations", {
fit_scale2 <- lm(scale(mpg) ~ scale(exp(hp_100)), mt)
expect_equal(
effectsize::standardize_parameters(fit_exp, method = "refit")[2, 2],
unname(coef(fit_scale1)[2])
unname(coef(fit_scale1)[2]),
tolerance = 1e-4
)

expect_equal(
effectsize::standardize_parameters(fit_exp, method = "basic")[2, 2],
unname(coef(fit_scale2)[2])
unname(coef(fit_scale2)[2]),
tolerance = 1e-4
)

skip_if_not_installed("insight", minimum_version = "0.10.0")
Expand All @@ -64,7 +75,9 @@ test_that("transformations", {
m <- lm(log(sum + 1) ~ as.numeric(time) * group, data = d)


expect_message(out <- standardize(m))
expect_message({
out <- standardize(m)
})
expect_identical(coef(m), c(
`(Intercept)` = -0.4575, `as.numeric(time)` = 0.5492, group = 0.3379,
`as.numeric(time):group` = 0.15779
Expand Down Expand Up @@ -98,12 +111,14 @@ test_that("weights", {
stdREFIT <- effectsize::standardize_parameters(m, method = "refit")
expect_equal(
stdREFIT[[2]],
effectsize::standardize_parameters(m, method = "posthoc")[[2]]
effectsize::standardize_parameters(m, method = "posthoc")[[2]],
tolerance = 1e-4
)

expect_equal(
stdREFIT[[2]],
effectsize::standardize_parameters(m, method = "basic")[[2]]
effectsize::standardize_parameters(m, method = "basic")[[2]],
tolerance = 1e-4
)
})

Expand Down Expand Up @@ -230,7 +245,9 @@ test_that("standardize mediation", {
)

out1 <- summary(standardize(med1))
expect_message(out2 <- summary(standardize(med2)))
expect_message({
out2 <- summary(standardize(med2))
})
expect_identical(unlist(out1[c("d0", "d1", "z0", "z1", "n0", "n1", "tau.coef")]),
unlist(out2[c("d0", "d1", "z0", "z1", "n0", "n1", "tau.coef")]),
tolerance = 0.1
Expand Down Expand Up @@ -266,13 +283,19 @@ test_that("offsets", {

m <- lm(mpg ~ hp + offset(wt), data = mtcars)

expect_warning(mz1 <- standardize(m))
expect_warning(mz2 <- standardize(m, two_sd = TRUE))
expect_warning({
mz1 <- standardize(m)
})
expect_warning({
mz2 <- standardize(m, two_sd = TRUE)
})
expect_identical(c(1, 2) * coef(mz1), coef(mz2))


m <- glm(cyl ~ hp + offset(wt), family = poisson(), data = mtcars)
expect_warning(mz <- standardize(m), regexp = NA)
expect_warning({
mz <- standardize(m)
}, regexp = NA)

par1 <- parameters::model_parameters(mz)
par2 <- effectsize::standardize_parameters(m, method = "basic")
Expand All @@ -288,10 +311,15 @@ test_that("brms", {
skip_if_not_installed("brms")

invisible(
capture.output(mod <- brms::brm(mpg ~ hp,
data = mtcars,
refresh = 0, chains = 1, silent = 2
))
capture.output(
{
mod <- brms::brm(
mpg ~ hp,
data = mtcars,
refresh = 0, chains = 1, silent = 2
)
}
)
)

expect_warning(
Expand Down

0 comments on commit 43cff24

Please sign in to comment.