Skip to content

Commit

Permalink
Fix for #889 (#891)
Browse files Browse the repository at this point in the history
* assigning all variables a test (even when all values are NA)

* increment version number
  • Loading branch information
ddsjoberg authored May 4, 2021
1 parent 7cac7d0 commit 2c6fa01
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: gtsummary
Title: Presentation-Ready Data Summary and Analytic Result
Tables
Version: 1.4.0.9003
Version: 1.4.0.9004
Authors@R:
c(person(given = "Daniel D.",
family = "Sjoberg",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# gtsummary (development version)

* Bug fix in `add_p.tbl_summary()` for columns that are all `NA`. These variables no longer error; rather, a message is printed indicating the p-value is not possible to calculate. (#889)

* Updated `tbl_svysummary()` to be compatible with {srvyr} package (#886)

* Updated default header when using `tbl_uvregression(x=)` to `"**Outcome**"` (#867)
Expand Down
33 changes: 15 additions & 18 deletions R/utils-add_p.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@
}

# if all obs are missing, return NULL ----------------------------------------
if (length(data[[variable]]) == sum(is.na(data[[variable]]))) {
return(NULL)
}
# if (length(data[[variable]]) == sum(is.na(data[[variable]]))) {
# return(NULL)
# }

# if no test supplied, setting defaults --------------------------------------
# if by var has 3 or more levels, return error...no default test.
Expand Down Expand Up @@ -227,16 +227,18 @@

# calculate expected counts to select between chisq and fisher
min_exp <-
expand.grid(
table(data[[variable]]) / sum(!is.na(data[[variable]])),
table(data[[by]]) / sum(!is.na(data[[by]]))
) %>%
mutate(
exp = .data$Var1 * .data$Var2 *
sum(!is.na(data[[variable]]) & !is.na(data[[by]]))
) %>%
pull(exp) %>%
min()
suppressWarnings(
expand.grid(
table(data[[variable]]) / sum(!is.na(data[[variable]])),
table(data[[by]]) / sum(!is.na(data[[by]]))
) %>%
mutate(
exp = .data$Var1 * .data$Var2 *
sum(!is.na(data[[variable]]) & !is.na(data[[by]]))
) %>%
pull(exp) %>%
min()
)

# if expected counts >= 5 for all cells, chisq, otherwise Fishers exact
if (min_exp >= 5) {
Expand Down Expand Up @@ -270,11 +272,6 @@
return(test[[variable]])
}

# if all obs are missing, return NULL ----------------------------------------
if (length(data$variables[[variable]]) == sum(is.na(data$variables[[variable]]))) {
return(NULL)
}

# for continuous data, default to non-parametric tests
if (summary_type %in% c("continuous", "continuous2")) {
test_func <-
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-add_difference.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,18 @@ test_that("row formatting of differences and CIs work", {
c("-4.6, 3.7", "-0.05, 0.44", "-18%, 9.9%", "-21%, 9.0%")
)
})

test_that("no error with missing data", {
expect_message(
t1 <-
mtcars %>%
mutate(mpg = NA, hp = NA) %>%
select(mpg, hp, am) %>%
tbl_summary(by = "am", type = hp ~ "continuous", missing = 'no') %>%
add_difference()
)
expect_equal(
t1 %>% as_tibble(col_labels = FALSE) %>% dplyr::pull(p.value),
rep_len(NA_character_, 2)
)
})
15 changes: 15 additions & 0 deletions tests/testthat/test-add_p.tbl_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,18 @@ test_that("difftime works with wolcox", {
"p=0.7"
)
})

test_that("no error with missing data", {
expect_message(
t1 <-
mtcars %>%
mutate(mpg = NA, hp = NA) %>%
select(mpg, hp, am) %>%
tbl_summary(by = "am", type = hp ~ "continuous") %>%
add_p()
)
expect_equal(
t1 %>% as_tibble(col_labels = FALSE) %>% dplyr::pull(p.value),
rep_len(NA_character_, 4)
)
})

0 comments on commit 2c6fa01

Please sign in to comment.