Skip to content

Commit

Permalink
Apply automatic stylistic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 15, 2024
1 parent 5b4d6ce commit 7876095
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 46 deletions.
2 changes: 1 addition & 1 deletion R/CreateTableOneJS.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ CreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, test
strata_count <- length(unique(data$variables[[strata]]))
comparison_columns <- colnames(ptb1)[(p_position - strata_count):(p_position - 1)]
pairwise_comparisons <- combn(
comparison_columns, 2,
comparison_columns, 2,
simplify = FALSE
)
pairwise_pvalues_list <- lapply(vars, function(x) {
Expand Down
69 changes: 42 additions & 27 deletions R/forestcox.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title count_event_by: funciton to count event, subgroup number inside TableSubgroupCox, TableSubgroupMultiCox
#' @description Function to count event, subgroup number
#' @param formula formula with survival analysis
#' @param data same data as in formula
#' @param formula formula with survival analysis
#' @param data same data as in formula
#' @param count_by_var variables to count subgroup for
#' @param var_subgroup 1 sub-group variable for analysis,
#' @param decimal.percent decimals to show percent of, Default: 1
Expand Down Expand Up @@ -31,38 +31,38 @@ count_event_by <- function(formula, data, count_by_var = NULL, var_subgroup = NU
total_count <- nrow(data)
total_event_count <- sum(data[[event_col]] == 1, na.rm = TRUE)
total_event_rate <- paste0(total_event_count, "/", total_count, " (", round(total_event_count / total_count * 100, decimal.percent), "%)")

if (!is.null(count_by_var) && !is.null(var_subgroup)) {
# count_by_var와 var_subgroup이 모두 있을 때
counts <- data %>%
dplyr::filter(!is.na(!!rlang::sym(var_subgroup))) %>%
dplyr::group_by(!!rlang::sym(count_by_var), !!rlang::sym(var_subgroup)) %>%
dplyr::summarize(Count = dplyr::n(), Event_Count = sum(!!rlang::sym(event_col) == 1, na.rm = TRUE), .groups = 'drop') %>%
dplyr::summarize(Count = dplyr::n(), Event_Count = sum(!!rlang::sym(event_col) == 1, na.rm = TRUE), .groups = "drop") %>%
dplyr::mutate(Event_Rate = paste0(Event_Count, "/", Count, " (", round(Event_Count / Count * 100, decimal.percent), "%)"))

overall_counts <- data %>%
dplyr::group_by(!!rlang::sym(count_by_var)) %>%
dplyr::summarize(Count = dplyr::n(), Event_Count = sum(!!rlang::sym(event_col) == 1, na.rm = TRUE), .groups = 'drop') %>%
dplyr::mutate(Event_Rate = paste0(Event_Count, "/", Count, " (", round(Event_Count / Count * 100, decimal.percent), "%)"),
!!rlang::sym(var_subgroup) := "Overall")

dplyr::summarize(Count = dplyr::n(), Event_Count = sum(!!rlang::sym(event_col) == 1, na.rm = TRUE), .groups = "drop") %>%
dplyr::mutate(
Event_Rate = paste0(Event_Count, "/", Count, " (", round(Event_Count / Count * 100, decimal.percent), "%)"),
!!rlang::sym(var_subgroup) := "Overall"
)

counts <- counts %>%
dplyr::bind_rows(overall_counts) %>%
dplyr::arrange(!!rlang::sym(count_by_var), !!rlang::sym(var_subgroup))

} else if (is.null(count_by_var) && !is.null(var_subgroup)) {
# var_subgroup만 있을 때
counts <- data %>%
dplyr::filter(!is.na(!!rlang::sym(var_subgroup))) %>%
dplyr::group_by(!!rlang::sym(var_subgroup)) %>%
dplyr::summarize(Count = dplyr::n(), Event_Count = sum(!!rlang::sym(event_col) == 1, na.rm = TRUE), .groups = 'drop') %>%
dplyr::summarize(Count = dplyr::n(), Event_Count = sum(!!rlang::sym(event_col) == 1, na.rm = TRUE), .groups = "drop") %>%
dplyr::mutate(Event_Rate = paste0(Event_Count, "/", Count, " (", round(Event_Count / Count * 100, decimal.percent), "%)"))

} else if (!is.null(count_by_var) && is.null(var_subgroup)) {
# count_by_var만 있을 때
counts <- data %>%
dplyr::group_by(!!rlang::sym(count_by_var)) %>%
dplyr::summarize(Count = dplyr::n(), Event_Count = sum(!!rlang::sym(event_col) == 1, na.rm = TRUE), .groups = 'drop') %>%
dplyr::summarize(Count = dplyr::n(), Event_Count = sum(!!rlang::sym(event_col) == 1, na.rm = TRUE), .groups = "drop") %>%
dplyr::mutate(Event_Rate = paste0(Event_Count, "/", Count, " (", round(Event_Count / Count * 100, decimal.percent), "%)"))
} else {
# count_by_var와 var_subgroup이 NULL일 때는 전체 데이터만 Total로 계산
Expand All @@ -74,16 +74,16 @@ count_event_by <- function(formula, data, count_by_var = NULL, var_subgroup = NU
)
return(counts)
}

# Total 행을 추가
total_row <- tibble::tibble(
Count = total_count,
Event_Count = total_event_count,
Event_Rate = total_event_rate
)

counts <- dplyr::bind_rows(counts, total_row)

return(counts)
}

Expand Down Expand Up @@ -624,7 +624,7 @@ TableSubgroupCox <- function(formula, var_subgroup = NULL, var_cov = NULL, data,
}
## add event, count_by options
if ((event) && is.null(count_by)) {
original_output <- TableSubgroupCox(formula = formula, var_subgroup = var_subgroup, var_cov = var_cov, data = data, time_eventrate = time_eventrate, decimal.hr = decimal.hr, decimal.percent = decimal.percent, decimal.pvalue = decimal.pvalue, cluster = cluster, strata = strata, weights = weights, event = FALSE, count_by = count_by, labeldata = labeldata)
original_output <- TableSubgroupCox(formula = formula, var_subgroup = var_subgroup, var_cov = var_cov, data = data, time_eventrate = time_eventrate, decimal.hr = decimal.hr, decimal.percent = decimal.percent, decimal.pvalue = decimal.pvalue, cluster = cluster, strata = strata, weights = weights, event = FALSE, count_by = count_by, labeldata = labeldata)
count_output <- count_event_by(formula = formula, data = data, count_by_var = count_by, var_subgroup = var_subgroup, decimal.percent = 1)
if (!is.null(var_subgroup)) {
for (i in 1:nrow(original_output)) {
Expand Down Expand Up @@ -654,12 +654,20 @@ TableSubgroupCox <- function(formula, var_subgroup = NULL, var_cov = NULL, data,
if (!is.null(labeldata)) {
count_by_levels <- sapply(count_by_levels, function(x) {
label <- labeldata[labeldata$variable == count_by & labeldata$level == x, "val_label"]
if (length(label) > 0) return(label) else return(x)
if (length(label) > 0) {
return(label)
} else {
return(x)
}
})

count_output[[count_by]] <- sapply(count_output[[count_by]], function(x) {
label <- labeldata[labeldata$variable == count_by & labeldata$level == x, "val_label"]
if (length(label) > 0) return(label) else return(x)
if (length(label) > 0) {
return(label)
} else {
return(x)
}
})
}
if (!is.null(var_subgroup)) {
Expand Down Expand Up @@ -716,7 +724,7 @@ TableSubgroupCox <- function(formula, var_subgroup = NULL, var_cov = NULL, data,
if (!(event) && !is.null(count_by)) {
original_output <- TableSubgroupCox(formula = formula, var_subgroup = var_subgroup, var_cov = var_cov, data = data, time_eventrate = time_eventrate, decimal.hr = decimal.hr, decimal.percent = decimal.percent, decimal.pvalue = decimal.pvalue, cluster = cluster, strata = strata, weights = weights, event = event, count_by = NULL, labeldata = labeldata)
count_output <- count_event_by(formula = formula, data = data, count_by_var = count_by, var_subgroup = var_subgroup, decimal.percent = 1)

if (inherits(data, "survey.design")) {
data <- data$variables
} else {
Expand All @@ -727,15 +735,23 @@ TableSubgroupCox <- function(formula, var_subgroup = NULL, var_cov = NULL, data,
# count_by_levels와 count_output의 count_by 값을 라벨로 변환
count_by_levels <- sapply(count_by_levels, function(x) {
label <- labeldata[labeldata$variable == count_by & labeldata$level == x, "val_label"]
if (length(label) > 0) return(label) else return(x)
if (length(label) > 0) {
return(label)
} else {
return(x)
}
})

count_output[[count_by]] <- sapply(count_output[[count_by]], function(x) {
label <- labeldata[labeldata$variable == count_by & labeldata$level == x, "val_label"]
if (length(label) > 0) return(label) else return(x)
if (length(label) > 0) {
return(label)
} else {
return(x)
}
})
}
}

if (!is.null(var_subgroup)) {
subgroup_levels <- unique(data[[var_subgroup]])
for (countlevel in count_by_levels) {
Expand Down Expand Up @@ -847,4 +863,3 @@ TableSubgroupMultiCox <- function(formula, var_subgroups = NULL, var_cov = NULL,
}
}
}

2 changes: 1 addition & 1 deletion R/svyCreateTableOneJS.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ svyCreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, te
strata_count <- length(unique(data$variables[[strata]]))
comparison_columns <- colnames(ptb1)[(p_position - strata_count):(p_position - 1)]
pairwise_comparisons <- combn(
comparison_columns, 2,
comparison_columns, 2,
simplify = FALSE
)
pairwise_pvalues_list <- list()
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-CreateTableOneJS.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ test_that("Run CreateOneTableJS", {
expect_is(CreateTableOneJS(vars = names(lung), data = lung, showAllLevels = F), "list")
expect_is(CreateTableOneJS(vars = names(lung), data = lung, labeldata = lung.label, Labels = T), "list")
expect_is(CreateTableOneJS(vars = names(lung), data = lung, labeldata = lung.label, Labels = T, showAllLevels = F), "list")

expect_is(CreateTableOneJS(vars = names(lung), strata = "sex", data = lung), "list")
expect_is(CreateTableOneJS(vars = names(lung), strata = "sex", data = lung, labeldata = lung.label, Labels = T), "list")
expect_is(CreateTableOneJS(vars = names(lung), strata = "sex", data = lung, showAllLevels = F), "list")
expect_is(CreateTableOneJS(vars = names(lung), strata = "sex", data = lung, showAllLevels = F, labeldata = lung.label, Labels = T), "list")
expect_is(CreateTableOneJS(vars = names(lung), strata = "ph.ecog", data = lung, showAllLevels = F, labeldata = lung.label, Labels = T, pairwise = T), "list")

expect_warning(CreateTableOneJS(vars = names(lung), strata = "sex", strata2 = "ph.ecog", data = lung))
expect_warning(CreateTableOneJS(vars = names(lung), strata = "sex", strata2 = "ph.ecog", data = lung, showAllLevels = F))
expect_warning(CreateTableOneJS(vars = names(lung), strata = "sex", strata2 = "ph.ecog", data = lung, psub = F))
expect_warning(CreateTableOneJS(vars = names(lung), strata = "sex", strata2 = "ph.ecog", data = lung, psub = F, showAllLevels = F))
expect_warning(CreateTableOneJS(vars = names(lung), strata = "ph.ecog", data = lung, showAllLevels = F, labeldata = lung.label, Labels = T, pairwise = T))

expect_warning(CreateTableOneJS(vars = names(lung), strata = "sex", strata2 = "ph.ecog", data = lung, labeldata = lung.label, Labels = T))
expect_warning(CreateTableOneJS(vars = names(lung), strata = "sex", strata2 = "ph.ecog", data = lung, showAllLevels = F, labeldata = lung.label, Labels = T))
expect_warning(CreateTableOneJS(vars = names(lung), strata = "sex", strata2 = "ph.ecog", data = lung, labeldata = lung.label, Labels = T, psub = F))
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-forestcox.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ test_that("Run TableSubgroupMultiCox", {
variable == "kk" & level == "1" ~ "Yes",
variable == "kk1" & level == "0" ~ "No",
variable == "kk1" & level == "1" ~ "Yes",
TRUE ~ val_label
TRUE ~ val_label
)
)
lung.label
lung.label
expect_is(TableSubgroupMultiCox(Surv(time, status) ~ sex, time_eventrate = 100, data = lung), "data.frame")
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE))
Expand All @@ -33,23 +33,23 @@ test_that("Run TableSubgroupMultiCox", {
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst"))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age"))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = T))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = T, count_by = 'sex'))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = F, count_by = 'sex'))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = T, count_by = "sex"))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = F, count_by = "sex"))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", labeldata = lung.label))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = TRUE, labeldata = lung.label))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = TRUE, count_by = 'sex', labeldata = lung.label))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = FALSE, count_by = 'sex', labeldata = lung.label))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = TRUE, count_by = "sex", labeldata = lung.label))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst", strata = "inst", weights = "age", event = FALSE, count_by = "sex", labeldata = lung.label))

## Survey data
library(survey)
expect_warning(data.design <- svydesign(id = ~1, data = lung))
expect_is(TableSubgroupMultiCox(Surv(time, status) ~ sex, data = data.design, time_eventrate = 100), "data.frame")
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100, line = TRUE))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100, line = TRUE, event =T))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100, line = TRUE, event =F, count_by = 'sex'))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100, line = TRUE, event =T, count_by = 'sex'))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100, line = TRUE, event =T, count_by = 'sex', labeldata = lung.label))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100, line = TRUE, event = T))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100, line = TRUE, event = F, count_by = "sex"))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100, line = TRUE, event = T, count_by = "sex"))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = data.design, time_eventrate = 100, line = TRUE, event = T, count_by = "sex", labeldata = lung.label))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, strata = "inst"))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, weights = "age"))
expect_warning(TableSubgroupMultiCox(Surv(time, status) ~ sex, var_subgroups = c("kk", "kk1"), data = lung, time_eventrate = 100, line = TRUE, cluster = "inst"))
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-svyCreateTableOne.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ library(survey)
test_that("Run SvyCreateOneTableJS", {
data(nhanes)
nhanes$SDMVPSU <- as.factor(nhanes$SDMVPSU)
nhanes$race<-as.factor(nhanes$race)
nhanes$race <- as.factor(nhanes$race)
a.label <- mk.lev(nhanes)
nhanesSvy <- svydesign(ids = ~SDMVPSU, strata = ~SDMVSTRA, weights = ~WTMEC2YR, nest = TRUE, data = nhanes)

Expand All @@ -28,13 +28,13 @@ test_that("Run SvyCreateOneTableJS", {
vars = c("HI_CHOL", "race", "agecat", "RIAGENDR"),
strata = "race", data = nhanesSvy, factorVars = c("HI_CHOL", "race", "RIAGENDR"), labeldata = a.label, Labels = T, pairwise = T
), "list")

expect_is(svyCreateTableOneJS(
vars = c("HI_CHOL", "race", "agecat", "RIAGENDR"),
strata = "RIAGENDR", data = nhanesSvy, factorVars = c("HI_CHOL", "race", "RIAGENDR"), labeldata = a.label, Labels = T, showAllLevels = F
), "list")


expect_is(svyCreateTableOneJS(
vars = c("HI_CHOL", "race", "RIAGENDR"),
strata = "SDMVPSU", strata2 = "agecat", data = nhanesSvy, factorVars = c("HI_CHOL", "race", "RIAGENDR"), labeldata = a.label, Labels = T
Expand Down

0 comments on commit 7876095

Please sign in to comment.