Skip to content

Commit

Permalink
fix, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Aug 12, 2023
1 parent 37330f3 commit 11e2f83
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
4 changes: 2 additions & 2 deletions R/means_by_group.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ means_by_group.data.frame <- function(x,
# p-values of contrast-means
if (insight::check_if_installed("emmeans", quietly = TRUE)) {
# create summary table of contrasts, for p-values and confidence intervals
predicted <- emmeans::emmeans(fit, specs = "group")
predicted <- emmeans::emmeans(fit, specs = "group", level = ci)
contrasts <- emmeans::contrast(predicted, method = "eff")
# add p-values and confidence intervals to "out"
if (!is.null(ci) && !is.na(ci)) {
Expand Down Expand Up @@ -247,7 +247,7 @@ format.dw_groupmeans <- function(x, digits = NULL, ...) {
digits <- 2
}
x$N <- insight::format_value(x$N, digits = 0)
insight::format_table(x, digits = digits, ...)
insight::format_table(remove_empty_columns(x), digits = digits, ...)
}

#' @export
Expand Down
36 changes: 19 additions & 17 deletions tests/testthat/test-labelled_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ data(efc, package = "datawizard")

test_that("reverse, labels preserved", {
# factor, label
expect_equal(
expect_identical(
attr(reverse(efc$e42dep), "label", exact = TRUE),
"elder's dependency"
)
# factor, labels
expect_equal(
names(attr(reverse(efc$e42dep), "labels", exact = TRUE)),
expect_named(
attr(reverse(efc$e42dep), "labels", exact = TRUE),
names(attr(efc$e42dep, "labels", exact = TRUE))
)
expect_equal(
Expand All @@ -19,13 +19,13 @@ test_that("reverse, labels preserved", {
ignore_attr = TRUE
)
# numeric
expect_equal(
names(attr(reverse(efc$c12hour), "labels", exact = TRUE)),
expect_named(
attr(reverse(efc$c12hour), "labels", exact = TRUE),
names(attr(efc$c12hour, "labels", exact = TRUE))
)
# data frame
labels <- sapply(reverse(efc), function(i) attr(i, "label", exact = TRUE))
expect_equal(
labels <- sapply(reverse(efc), attr, which = "label", exact = TRUE)
expect_identical(
labels,
c(
c12hour = "average number of hours of care per week",
Expand All @@ -42,8 +42,8 @@ test_that("reverse, labels preserved", {
# data_merge -----------------------------------

test_that("data_merge, labels preserved", {
labels <- sapply(data_merge(efc[c(1:2)], efc[c(3:4)], verbose = FALSE), function(i) attr(i, "label", exact = TRUE))
expect_equal(
labels <- sapply(data_merge(efc[1:2], efc[3:4], verbose = FALSE), attr, which = "label", exact = TRUE)
expect_identical(
labels,
c(
c12hour = "average number of hours of care per week",
Expand Down Expand Up @@ -72,8 +72,8 @@ test_that("data_extract, labels preserved", {
ignore_attr = TRUE
)
# data frame
labels <- sapply(data_extract(efc, select = c("e42dep", "c172code")), function(i) attr(i, "label", exact = TRUE))
expect_equal(
labels <- sapply(data_extract(efc, select = c("e42dep", "c172code")), attr, which = "label", exact = TRUE)
expect_identical(
labels,
c(e42dep = "elder's dependency", c172code = "carer's level of education")
)
Expand Down Expand Up @@ -142,8 +142,8 @@ test_that("data_rename, labels preserved", {
ignore_attr = TRUE
)
# data frame
labels <- sapply(data_remove(efc, starts_with("c1")), function(i) attr(i, "label", exact = TRUE))
expect_equal(
labels <- sapply(data_remove(efc, starts_with("c1")), attr, which = "label", exact = TRUE)
expect_identical(
labels,
c(e16sex = "elder's gender", e42dep = "elder's dependency", neg_c_7 = "Negative impact with 7 items")
)
Expand Down Expand Up @@ -255,12 +255,12 @@ test_that("data_match, labels preserved", {
test_that("data_filter, labels preserved", {
x <- data_filter(efc, c172code == 1 & c12hour > 40)
# factor
expect_equal(
expect_identical(
attr(x$e42dep, "label", exact = TRUE),
attr(efc$e42dep, "label", exact = TRUE)
)
# numeric
expect_equal(
expect_identical(
attr(x$c12hour, "label", exact = TRUE),
attr(efc$c12hour, "label", exact = TRUE)
)
Expand All @@ -271,7 +271,9 @@ test_that("data_filter, labels preserved", {
# convert_to_na -----------------------------------

test_that("convert_to_na, labels preserved", {
expect_message(x <- convert_to_na(efc, na = c(2, "2"), select = starts_with("e")))
expect_message({
x <- convert_to_na(efc, na = c(2, "2"), select = starts_with("e"))
})
# factor
expect_equal(
attr(x$e42dep, "label", exact = TRUE),
Expand Down Expand Up @@ -301,7 +303,7 @@ test_that("convert_to_na, labels preserved", {
)
# drop unused value labels
x <- convert_to_na(efc$c172code, na = 2)
expect_equal(
expect_identical(
attr(x, "labels", exact = TRUE),
c(`low level of education` = 1, `high level of education` = 3)
)
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-means_by_group.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test_that("meany_by_group", {
data(efc)
expect_snapshot(means_by_group(efc, "c12hour", "e42dep"))
expect_snapshot(means_by_group(efc, "c12hour", "e42dep", ci = 0.99))
expect_snapshot(means_by_group(efc, "c12hour", "e42dep", ci = NA))
expect_snapshot(means_by_group(efc$c12hour, efc$e42dep))
expect_snapshot(means_by_group(efc$c12hour, efc$e42dep, ci = NA))
})

0 comments on commit 11e2f83

Please sign in to comment.