Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add na.rm option to remove the strata variable in rows #368

Merged
merged 11 commits into from
May 22, 2022
9 changes: 7 additions & 2 deletions R/get_tableone.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' @param strata Stratifying/Grouping variable name(s) as character vector. If NULL, only overall results are returned
#' @param overall If TRUE, the summary statistics for the overall dataset are also calculated
#' @param summary_function A function defining summary statistics for numeric and categorical values
#' @param na.rm If TRUE, the default, the `strata` variables will be eliminated from the table rows as they leads to all NAs.
#' @details It is possible to provide your own summary function. Please have a loot at summary for inspiration.
#'
#' @note All columns in the table will be summarized. If only some columns shall be used, please select only those
Expand Down Expand Up @@ -70,7 +71,7 @@
#'
#' @export

get_tableone <- function(data, strata = NULL, overall=TRUE, summary_function = summarize_short){
get_tableone <- function(data, strata = NULL, overall=TRUE, summary_function = summarize_short, na.rm = TRUE){
UseMethod("get_tableone")
}

Expand All @@ -79,12 +80,16 @@ get_tableone <- function(data, strata = NULL, overall=TRUE, summary_function = s
#' @return object of class tableone. That is a list of data specified summaries
#' for all input variables.
#' @export
get_tableone.default <- function(data, strata = NULL, overall=TRUE, summary_function = summarize_short){
get_tableone.default <- function(data, strata = NULL, overall=TRUE, summary_function = summarize_short, na.rm = TRUE){

summary_FUN <- match.fun(summary_function)

if(overall & !is.null(strata)){
overall_table1 <- get_tableone(data, strata = NULL, overall = FALSE, summary_function = summary_function)
if(na.rm){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the purpose was not to exclude NA rather than to remove everything?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove strata from the body of the table. No na.rm argument.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SHAESEN2 I removed na.rm and applied the removal of strata in the tableone.

overall_table1 <- overall_table1 %>%
dplyr::filter(!(variable %in% strata))
}
combine_dfs <- TRUE
}
else{
Expand Down
72 changes: 52 additions & 20 deletions tests/testthat/test-get_tableone.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#' T4.3 An error when the `summary_function` is a function not build for it
#' T4.4 An error when the `summary_function` is `summarize_long`
#' T4.5 No error when the `summary_function` is `summarize_short`
#' T5. The tableone removes strata variables in rows that leads NA values
#' T5.1 An error when the the table includes the strata variables if `na.rm = TRUE`
#' An error when the the table does not include any of the strata variables if `na.rm = FALSE`

# Requirement T1 ----------------------------------------------------------

Expand All @@ -39,18 +42,18 @@ testthat::test_that("T1.1. No error when `data` is of class `data.frame`", {
testthat::test_that("T1.2. No error when `data` is of class `tibble`", {

data <- dplyr::as_tibble(adtte)

testthat::expect_error(visR::get_tableone(data = data), NA)

})

testthat::test_that("T1.3 No error when `data` is of class `data.table`", {

if (nzchar(find.package("data.table"))) {

data <- data.table::as.data.table(adtte)
testthat::expect_error(visR::get_tableone(data = data), NA)

}
})

Expand Down Expand Up @@ -85,18 +88,18 @@ testthat::test_that("T2.2 An error when `strata` is a string that is not a colna

testthat::test_that("T2.3 Additional colnames in the tableone are the `strata` values (for one `strata`)", {

trtp_colnames <- colnames(visR::get_tableone(data = adtte,
trtp_colnames <- colnames(visR::get_tableone(data = adtte,
strata = c("TRTP")))[4:6]
testthat::expect_equal(trtp_colnames, levels(adtte$TRTP))

})

testthat::test_that("T2.4 Additional colnames in the tableone are the crossproduct of all `strata` values (for more than one `strata`)", {

mapply_colnames <- c(mapply(function(x, y) paste(x, y, sep = "_"),
mapply_colnames <- c(mapply(function(x, y) paste(x, y, sep = "_"),
levels(adtte$TRTP),
MoreArgs = list(levels(adtte$SEX))))
visR_colnames <- colnames(visR::get_tableone(data = adtte,
visR_colnames <- colnames(visR::get_tableone(data = adtte,
strata = c("TRTP", "SEX")))[4:9]
testthat::expect_equal(mapply_colnames, visR_colnames)

Expand All @@ -107,30 +110,30 @@ testthat::test_that("T2.4 Additional colnames in the tableone are the crossprodu
testthat::context("get_tableone - T3. The tableone includes expected columnnames")

testthat::test_that("T3.1 Tableone by default includes columns `variable`, `statistic`, and `Total`", {

tableone_colnames <- colnames(visR::get_tableone(data = adtte))

testthat::expect_equal(tableone_colnames, c("variable", "statistic", "Total"))

})

testthat::test_that("T3.2 Tableone still includes the colum `Total` if `overall` is FALSE but no `strata` is given", {

tableone_colnames <- colnames(visR::get_tableone(data = adtte, overall = FALSE))

testthat::expect_equal(tableone_colnames, c("variable", "statistic", "Total"))

})

testthat::test_that("T3.3 Tableone does not include the colum `Total` if `overall` is FALSE and a `strata` is given", {

tblone_colnames <- colnames(visR::get_tableone(data = adtte,
overall = FALSE,
tblone_colnames <- colnames(visR::get_tableone(data = adtte,
overall = FALSE,
strata = c("TRTP")))
testthat::expect_equal(tblone_colnames,

testthat::expect_equal(tblone_colnames,
c("variable", "statistic", levels(adtte$TRTP)))

})

# Requirement T4 ---------------------------------------------------------------
Expand All @@ -139,38 +142,67 @@ testthat::context("get_tableone - T4. The function only accepts suitable summary

testthat::test_that("T4.1 An error when the `summary_function` is NULL", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = NULL))

})

testthat::test_that("T4.2 An error when the `summary_function` is a string", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = "A"))

})

testthat::test_that("T4.3 An error when the `summary_function` is a function not build for it", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = sum))

})

testthat::test_that("T4.4 An error when the `summary_function` is `summarize_long`", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = summarize_long))

})


testthat::test_that("T4.5 No error when the `summary_function` is `summarize_short`", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = summarize_short), NA)

})

# Requirement T5 ---------------------------------------------------------------

testthat::context("get_tableone - T5. The tableone removes strata variables in rows that leads NA values")

testthat::test_that("T5.1 An error when the the table includes the strata variables if `na.rm = TRUE`", {

strata <- c("SEX", "RACE")

table <- adtte %>%
visR::get_tableone(strata = strata,
na.rm = TRUE)

testthat::expect_true(sum(strata %in% unique(table$variable)) == 0)

})


testthat::test_that("T5.2 An error when the the table does not include any of the strata variables if `na.rm = FALSE`", {

strata <- c("SEX", "RACE")

table <- adtte %>%
visR::get_tableone(strata = strata,
na.rm = FALSE)

testthat::expect_true(sum(strata %in% unique(table$variable)) == length(strata))

})

# END OF CODE -------------------------------------------------------------