Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 27, 2024
1 parent f2344c8 commit 5b655df
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.11.0.4
Version: 0.11.0.5
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
95 changes: 95 additions & 0 deletions tests/testthat/test-demean.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,98 @@ test_that("demean shows message if some vars don't exist", {
regexp = "not found"
)
})


test_that("demean for cross-classified designs (by > 1)", {
skip_if(getRversion() < "4.1.0") # for pipe

data(efc, package = "datawizard")
dat <- na.omit(efc)
dat$e42dep <- factor(dat$e42dep)
dat$c172code <- factor(dat$c172code)

x2a <- dat |>
data_group(e42dep) |>
data_modify(
c12hour_e42dep = mean(c12hour)
) |>
data_ungroup() |>
data_group(c172code) |>
data_modify(
c12hour_c172code = mean(c12hour)
) |>
data_ungroup() |>
data_modify(
c12hour_within = c12hour - c12hour_e42dep - c12hour_c172code
)

out <- degroup(
dat,
select = "c12hour",
by = c("e42dep", "c172code"),
suffix_demean = "_within"
)

expect_equal(
out$c12hour_e42dep_between,
x2a$c12hour_e42dep,
tolerance = 1e-4,
ignore_attr = TRUE
)
expect_equal(
out$c12hour_within,
x2a$c12hour_within,
tolerance = 1e-4,
ignore_attr = TRUE
)

x2a <- dat |>
data_group(e42dep) |>
data_modify(
c12hour_e42dep = mean(c12hour, na.rm = TRUE),
neg_c_7_e42dep = mean(neg_c_7, na.rm = TRUE)
) |>
data_ungroup() |>
data_group(c172code) |>
data_modify(
c12hour_c172code = mean(c12hour, na.rm = TRUE),
neg_c_7_c172code = mean(neg_c_7, na.rm = TRUE)
) |>
data_ungroup() |>
data_modify(
c12hour_within = c12hour - c12hour_e42dep - c12hour_c172code,
neg_c_7_within = neg_c_7 - neg_c_7_e42dep - neg_c_7_c172code
)

out <- degroup(
dat,
select = c("c12hour", "neg_c_7"),
by = c("e42dep", "c172code"),
suffix_demean = "_within"
)

expect_equal(
out$c12hour_e42dep_between,
x2a$c12hour_e42dep,
tolerance = 1e-4,
ignore_attr = TRUE
)
expect_equal(
out$neg_c_7_c172code_between,
x2a$neg_c_7_c172code,
tolerance = 1e-4,
ignore_attr = TRUE
)
expect_equal(
out$neg_c_7_within,
x2a$neg_c_7_within,
tolerance = 1e-4,
ignore_attr = TRUE
)
expect_equal(
out$c12hour_within,
x2a$c12hour_within,
tolerance = 1e-4,
ignore_attr = TRUE
)
})

0 comments on commit 5b655df

Please sign in to comment.