From 2868ab3510fe93da9883657d842533c1df8746b4 Mon Sep 17 00:00:00 2001 From: sl-eeper Date: Tue, 19 Nov 2024 07:10:27 +0000 Subject: [PATCH] fix pairwise option to deal with label and add pairwise.showtest --- R/CreateTableOneJS.R | 155 +++++++++++----------- R/svyCreateTableOneJS.R | 165 +++++++++++++----------- man/CreateTableOne2.Rd | 5 +- man/CreateTableOneJS.Rd | 5 +- man/svyCreateTableOne2.Rd | 5 +- man/svyCreateTableOneJS.Rd | 5 +- tests/testthat/test-CreateTableOneJS.R | 14 ++ tests/testthat/test-svyCreateTableOne.R | 11 +- 8 files changed, 213 insertions(+), 152 deletions(-) diff --git a/R/CreateTableOneJS.R b/R/CreateTableOneJS.R index f0bb9f5..88a3489 100644 --- a/R/CreateTableOneJS.R +++ b/R/CreateTableOneJS.R @@ -32,6 +32,7 @@ #' @param showpm Logical, show normal distributed continuous variables as Mean ± SD. Default: T #' @param addOverall (optional, only used if strata are supplied) Adds an overall column to the table. Smd and p-value calculations are performed using only the stratifed clolumns. Default: F #' @param pairwise (optional, only used if strata are supplied) When there are three or more strata, it displays the p-values for pairwise comparisons. Default: F +#' @param pairwise.showtest (optional, only used if strata are supplied) When using pairwise comparison, it displays the test used to calculate p-values for pairwise comparisons. Default: F #' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv. #' @details DETAILS #' @examples @@ -52,7 +53,7 @@ CreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, test testNormal = oneway.test, argsNormal = list(var.equal = F), testNonNormal = kruskal.test, argsNonNormal = list(NULL), showAllLevels = T, printToggle = F, quote = F, smd = F, Labels = F, exact = NULL, nonnormal = NULL, - catDigits = 1, contDigits = 2, pDigits = 3, labeldata = NULL, minMax = F, showpm = T, addOverall = F, pairwise = F) { + catDigits = 1, contDigits = 2, pDigits = 3, labeldata = NULL, minMax = F, showpm = T, addOverall = F, pairwise = F, pairwise.showtest = F) { setkey <- variable <- level <- . <- val_label <- NULL if (length(strata) != 1) { @@ -143,72 +144,40 @@ CreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, test # cap.tb1 = paste("Table 1: Stratified by ", labeldata[variable == strata, "var_label"][1], sep="") } + # Pairwise option if (pairwise && length(unique(data[[strata]])) > 2) { - # 정규성 테스트 수행 - normality_results <- sapply(vars, function(x) { - is_continuous <- is.numeric(data[[x]]) || is.integer(data[[x]]) - if (is_continuous && nrow(data) <= 5000) { - shapiro_test_result <- tryCatch(stats::shapiro.test(data[[x]])$p.value, error = function(e) NA) - return(!is.na(shapiro_test_result) && shapiro_test_result >= 0.05) # p >= 0.05면 정규성 만족 - } else { - return(TRUE) # 큰 샘플이거나 정규성 검사가 불가할 때는 정규성 만족으로 처리 - } - }) - - p_position <- which(colnames(ptb1) == "p") - strata_count <- length(unique(data$variables[[strata]])) - comparison_columns <- colnames(ptb1)[(p_position - strata_count):(p_position - 1)] - pairwise_comparisons <- combn( - comparison_columns, 2, - simplify = FALSE - ) - pairwise_pvalues_list <- lapply(vars, function(x) { - sapply(pairwise_comparisons, function(pair) { - subset_data <- data[data[[strata]] %in% pair, ] - is_continuous <- is.numeric(data[[x]]) || is.integer(data[[x]]) - test_result <- if (is_continuous) { - if (normality_results[x]) { - tryCatch( - { - test <- t.test(subset_data[[x]] ~ subset_data[[strata]], var.equal = FALSE) # Welch's t-test - list(p_value = test$p.value, test_used = "t-test") - }, - error = function(e) { - list(p_value = NA, test_used = NA) - } - ) - } else { - tryCatch( - { - test <- wilcox.test(subset_data[[x]] ~ subset_data[[strata]]) - list(p_value = test$p.value, test_used = "wilcox") - }, - error = function(e) { - list(p_value = NA, test_used = NA) - } - ) - } - } else { - # 범주형 데이터에 대해 Chi-square 또는 Fisher's exact test 수행 + unique_strata <- sort(unique(stats::na.omit(data[[strata]]))) + pairwise_comparisons <- combn(unique_strata, 2, simplify = FALSE) + pairwise_names <- sapply(pairwise_comparisons, function(pair) { + paste0("p(", pair[1], " vs ", pair[2], ")") + }) + pairwise_pvalues <- stats::setNames( + sapply(pairwise_comparisons, function(pair) { + subset_data <- data[data[[strata]] %in% pair, ] + subset_data[[strata]] <- droplevels(subset_data[[strata]]) tryCatch( { - test <- chisq.test(table(subset_data[[strata]], subset_data[[x]])) - list(p_value = test$p.value, test_used = "chisq") - }, - warning = function(w) { - test <- fisher.test(table(subset_data[[strata]], subset_data[[x]])) - list(p_value = test$p.value, test_used = "exact") + table_result <- CreateTableOne2( + data = subset_data, strata = strata, vars = vars, factorVars = factorVars, includeNA = includeNA, test = test, + testApprox = testApprox, argsApprox = argsApprox, testExact = testExact, argsExact = argsExact, + testNormal = testNormal, argsNormal = argsNormal, testNonNormal = testNonNormal, argsNonNormal = argsNonNormal, + showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, smd = smd, Labels = Labels, + exact = NULL, nonnormal = nonnormal, catDigits = catDigits, contDigits = contDigits, pDigits = pDigits, + labeldata = labeldata, minMax = minMax, showpm = showpm, addOverall = addOverall, pairwise = F + ) + p_values <- table_result[, "p"] + test_used <- table_result[, "test"] + list(p_value = p_values, + test_used = test_used) }, error = function(e) { - list(p_value = NA, test_used = NA) + list(p_value = stats::setNames(rep(NA, length(vars)), vars), + test_used = stats::setNames(rep(NA, length(vars)), vars)) } ) - } - return(test_result) - }, simplify = FALSE) - }) - names(pairwise_pvalues_list) <- vars - + }, simplify = FALSE), + nm = pairwise_names + ) for (i in seq_along(pairwise_comparisons)) { col_name <- paste0("p(", pairwise_comparisons[[i]][1], "vs", pairwise_comparisons[[i]][2], ")") test_name <- paste0("test(", pairwise_comparisons[[i]][1], "vs", pairwise_comparisons[[i]][2], ")") @@ -216,23 +185,60 @@ CreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, test colnames(ptb1)[ncol(ptb1) - 1] <- col_name colnames(ptb1)[ncol(ptb1)] <- test_name } - for (i in seq_along(pairwise_comparisons)) { col_name <- paste0("p(", pairwise_comparisons[[i]][1], "vs", pairwise_comparisons[[i]][2], ")") test_name <- paste0("test(", pairwise_comparisons[[i]][1], "vs", pairwise_comparisons[[i]][2], ")") - for (x in vars) { - p_value <- pairwise_pvalues_list[[x]][[i]]$p_value - test_used <- pairwise_pvalues_list[[x]][[i]]$test_used - cleaned_row_names <- gsub("\\s+|\\(\\%\\)", "", rownames(ptb1)) - cleaned_var_name <- gsub("\\s+|\\(\\%\\)", "", x) - first_row <- which(cleaned_row_names == cleaned_var_name)[1] - p_value <- ifelse(p_value < 0.001, "<0.001", as.character(round(p_value, 2))) - ptb1[first_row, col_name] <- p_value - ptb1[first_row, test_name] <- test_used + pairwise_key <- paste0("p(", pairwise_comparisons[[i]][1], " vs ", pairwise_comparisons[[i]][2], ")") + p_value <- pairwise_pvalues[[pairwise_key]]$p_value + test_used <- pairwise_pvalues[[pairwise_key]]$test_used + p_value_names <- names(p_value) + for (x in p_value_names) { + if (x != "") { + matched_rows <- match(x, rownames(ptb1)) + if (!is.na(matched_rows)) { + ptb1[matched_rows, col_name] <- p_value[x] + ptb1[matched_rows, test_name] <- test_used[x] + } + } + } + } + if (!is.null(labeldata) && Labels) { + pairwise_p_cols <- grep("^p\\(", colnames(ptb1), value = TRUE) + pairwise_test_cols <- grep("^test\\(", colnames(ptb1), value = TRUE) + strata_labels <- stats::setNames(labeldata[labeldata$variable == strata, val_label], labeldata[labeldata$variable == strata, level]) + updated_p_colnames <- sapply(pairwise_p_cols, function(col_name) { + match <- regmatches(col_name, regexec("^p\\(([^vs]+)vs([^\\)]+)\\)", col_name)) + if (length(match[[1]]) == 3) { + group1 <- match[[1]][2] + group2 <- match[[1]][3] + label1 <- strata_labels[as.character(group1)] + label2 <- strata_labels[as.character(group2)] + if (!is.na(label1) && !is.na(label2)) { + return(paste0("p(", label1, " vs ", label2, ")")) + } + } + return(col_name) + }) + updated_test_colnames <- sapply(pairwise_test_cols, function(col_name) { + match <- regmatches(col_name, regexec("^test\\(([^vs]+)vs([^\\)]+)\\)", col_name)) + if (length(match[[1]]) == 3) { + group1 <- match[[1]][2] + group2 <- match[[1]][3] + label1 <- strata_labels[as.character(group1)] + label2 <- strata_labels[as.character(group2)] + if (!is.na(label1) && !is.na(label2)) { + return(paste0("test(", label1, " vs ", label2, ")")) + } + } + return(col_name) + }) + colnames(ptb1)[colnames(ptb1) %in% pairwise_p_cols] <- updated_p_colnames + colnames(ptb1)[colnames(ptb1) %in% pairwise_test_cols] <- updated_test_colnames } + if(!pairwise.showtest){ + cols_to_remove <- grep("^test\\(", colnames(ptb1)) + ptb1 <- ptb1[, -cols_to_remove] } - cols_to_remove <- grep("^test\\(", colnames(ptb1)) - ptb1 <- ptb1[, -cols_to_remove] } sig <- ifelse(ptb1[, "p"] == "<0.001", "0", ptb1[, "p"]) sig <- as.numeric(as.vector(sig)) @@ -277,6 +283,7 @@ CreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, test #' @param addOverall (optional, only used if strata are supplied) Adds an overall column to the table. Smd and p-value calculations are performed using only the stratifed clolumns. Default: F #' @param normalityTest Logical, perform the Shapiro test for all variables. Default: F #' @param pairwise (optional, only used if strata are supplied) When there are three or more strata, it displays the p-values for pairwise comparisons. Default: F#' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv. +#' @param pairwise.showtest (optional, only used if strata are supplied) When using pairwise comparison, it displays the test used to calculate p-values for pairwise comparisons. Default: F #' @details DETAILS #' @examples #' library(survival) @@ -297,7 +304,7 @@ CreateTableOneJS <- function(vars, strata = NULL, strata2 = NULL, data, factorVa testNonNormal = kruskal.test, argsNonNormal = list(NULL), showAllLevels = T, printToggle = F, quote = F, smd = F, Labels = F, exact = NULL, nonnormal = NULL, catDigits = 1, contDigits = 2, pDigits = 3, labeldata = NULL, psub = T, minMax = F, showpm = T, - addOverall = F, normalityTest = F, pairwise = F) { + addOverall = F, normalityTest = F, pairwise = F, pairwise.showtest = F) { . <- level <- variable <- val_label <- V1 <- V2 <- NULL # if (Labels & !is.null(labeldata)){ # var_label(data) = sapply(names(data), function(v){as.character(labeldata[get("variable") == v, "var_label"][1])}, simplify = F) @@ -372,7 +379,7 @@ CreateTableOneJS <- function(vars, strata = NULL, strata2 = NULL, data, factorVa testNormal = testNormal, argsNormal = argsNormal, testNonNormal = testNonNormal, argsNonNormal = argsNonNormal, smd = smd, showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, Labels = Labels, nonnormal = nonnormal, exact = exact, - catDigits = catDigits, contDigits = contDigits, pDigits = pDigits, labeldata = labeldata, minMax = minMax, showpm = showpm, addOverall = addOverall, pairwise = pairwise + catDigits = catDigits, contDigits = contDigits, pDigits = pDigits, labeldata = labeldata, minMax = minMax, showpm = showpm, addOverall = addOverall, pairwise = pairwise, pairwise.showtest= pairwise.showtest ) cap.tb1 <- paste("Stratified by ", strata, sep = "") diff --git a/R/svyCreateTableOneJS.R b/R/svyCreateTableOneJS.R index 67be2ea..1c39b27 100644 --- a/R/svyCreateTableOneJS.R +++ b/R/svyCreateTableOneJS.R @@ -23,6 +23,7 @@ #' @param showpm Logical, show normal distributed continuous variables as Mean ± SD. Default: T #' @param addOverall (optional, only used if strata are supplied) Adds an overall column to the table. Smd and p-value calculations are performed using only the stratifed clolumns. Default: F #' @param pairwise (optional, only used if strata are supplied) When there are three or more strata, it displays the p-values for pairwise comparisons. Default: F +#' @param pairwise.showtest (optional, only used if strata are supplied) When using pairwise comparison, it displays the test used to calculate p-values for pairwise comparisons. Default: F #' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv. #' @details DETAILS #' @examples @@ -48,7 +49,7 @@ svyCreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, test = T, showAllLevels = T, printToggle = F, quote = F, smd = F, nonnormal = NULL, catDigits = 1, contDigits = 2, pDigits = 3, Labels = F, labeldata = NULL, minMax = F, showpm = T, - addOverall = F, pairwise = F) { + addOverall = F, pairwise = F, pairwise.showtest = F) { setkey <- variable <- level <- . <- val_label <- NULL if (length(strata) != 1) { @@ -112,9 +113,9 @@ svyCreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, te # cap.tb1 = paste("Table 1: Stratified by ", strata, sep="") if (Labels & !is.null(labeldata)) { - colname.group_var <- unlist(labeldata[get("variable") == strata, "val_label"]) - if (is.na(colname.group_var[1]) & addOverall) { - colname.group_var[1] <- "Overall" + colname.group_var <- unlist(labeldata[get("variable") == strata & get("level") %in% unique(data$variables[[strata]]), "val_label"]) + if (length(colname.group_var) == 0 & addOverall) { + colname.group_var <- c("Overall") } if (showAllLevels == T) { # colname.group_var <- unlist(labeldata[get("variable") == strata, "val_label"]) @@ -123,84 +124,101 @@ svyCreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, te colnames(ptb1)[1:length(colname.group_var)] <- colname.group_var } } - + if (pairwise && length(unique(data$variables[[strata]])) > 2) { - p_position <- which(colnames(ptb1) == "p") - strata_count <- length(unique(data$variables[[strata]])) - comparison_columns <- colnames(ptb1)[(p_position - strata_count):(p_position - 1)] - pairwise_comparisons <- combn( - comparison_columns, 2, - simplify = FALSE - ) - pairwise_pvalues_list <- list() - for (x in vars) { - pairwise_pvalues_list[[x]] <- list() - is_continuous <- !(x %in% factorVars) && !is.factor(data$variables[[x]]) - for (pair in pairwise_comparisons) { + unique_strata <- sort(unique(stats::na.omit(data$variables[[strata]]))) + pairwise_comparisons <- combn(unique_strata, 2, simplify = FALSE) + pairwise_names <- sapply(pairwise_comparisons, function(pair) { + paste0("p(", pair[1], " vs ", pair[2], ")") + }) + pairwise_pvalues <- stats::setNames( + sapply(pairwise_comparisons, function(pair) { subset_data <- subset(data, data$variables[[strata]] %in% pair) - if (is_continuous) { - test_result <- if (x %in% nonnormal) { - tryCatch( - { - test <- survey::svyranktest(as.formula(paste(x, "~", strata)), design = subset_data) - list(p_value = test$p.value, test_used = "svyranktest") - }, - error = function(e) { - list(p_value = NA, test_used = NA) - } - ) - } else { - tryCatch( - { - test <- survey::svyttest(as.formula(paste(x, "~", strata)), design = subset_data) - list(p_value = test$p.value, test_used = "svyttest") - }, - error = function(e) { - list(p_value = NA, test_used = NA) - } - ) + subset_data$variables[[strata]] <- droplevels(subset_data$variables[[strata]]) + tryCatch( + { + table_result <- svyCreateTableOne2( + data = subset_data, strata = strata, vars = vars, factorVars = factorVars, includeNA = includeNA, test = test, + showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, smd = smd, nonnormal = nonnormal, + catDigits = catDigits, contDigits = contDigits, pDigits = pDigits, Labels = Labels, labeldata = labeldata, minMax = minMax, showpm = showpm, + addOverall = addOverall, pairwise = F) + p_values <- table_result[, "p"] + test_used <- table_result[, "test"] + list(p_value = p_values, + test_used = test_used) + }, + error = function(e) { + list(p_value = stats::setNames(rep(NA, length(vars)), vars), + test_used = stats::setNames(rep(NA, length(vars)), vars)) } - } else { - test_result <- tryCatch( - { - test <- survey::svychisq(as.formula(paste("~", x, "+", strata)), design = subset_data, method = "RaoScott") - list(p_value = test$p.value, test_used = "svychisq") - }, - error = function(e) { - list(p_value = NA, test_used = NA) - } - ) - } - pairwise_pvalues_list[[x]][[paste(pair, collapse = "_")]] <- test_result - } - } + ) + }, simplify = FALSE), + nm = pairwise_names + ) for (i in seq_along(pairwise_comparisons)) { - col_name <- paste0("p(", pairwise_comparisons[[i]][1], " vs ", pairwise_comparisons[[i]][2], ")") - test_name <- paste0("test(", pairwise_comparisons[[i]][1], " vs ", pairwise_comparisons[[i]][2], ")") + col_name <- paste0("p(", pairwise_comparisons[[i]][1], "vs", pairwise_comparisons[[i]][2], ")") + test_name <- paste0("test(", pairwise_comparisons[[i]][1], "vs", pairwise_comparisons[[i]][2], ")") ptb1 <- cbind(ptb1, col_name = "", test_name = "") colnames(ptb1)[ncol(ptb1) - 1] <- col_name colnames(ptb1)[ncol(ptb1)] <- test_name } - for (x in vars) { - cleaned_var_name <- gsub("\\s+|\\(\\%\\)", "", x) - first_row <- which(gsub("\\s+|\\(\\%\\)", "", rownames(ptb1)) == cleaned_var_name)[1] - - for (i in seq_along(pairwise_comparisons)) { - pair_key <- paste(pairwise_comparisons[[i]], collapse = "_") - p_value <- pairwise_pvalues_list[[x]][[pair_key]]$p_value - test_used <- pairwise_pvalues_list[[x]][[pair_key]]$test_used - col_name <- paste0("p(", pairwise_comparisons[[i]][1], " vs ", pairwise_comparisons[[i]][2], ")") - test_name <- paste0("test(", pairwise_comparisons[[i]][1], " vs ", pairwise_comparisons[[i]][2], ")") - p_value <- ifelse(p_value < 0.001, "<0.001", as.character(round(p_value, 2))) - ptb1[first_row, col_name] <- p_value - ptb1[first_row, test_name] <- test_used + for (i in seq_along(pairwise_comparisons)) { + col_name <- paste0("p(", pairwise_comparisons[[i]][1], "vs", pairwise_comparisons[[i]][2], ")") + test_name <- paste0("test(", pairwise_comparisons[[i]][1], "vs", pairwise_comparisons[[i]][2], ")") + pairwise_key <- paste0("p(", pairwise_comparisons[[i]][1], " vs ", pairwise_comparisons[[i]][2], ")") + p_value <- pairwise_pvalues[[pairwise_key]]$p_value + test_used <- pairwise_pvalues[[pairwise_key]]$test_used + p_value_names <- names(p_value) + for (x in p_value_names) { + if (x != "") { + matched_rows <- match(x, rownames(ptb1)) + if (!is.na(matched_rows)) { + ptb1[matched_rows, col_name] <- p_value[x] + ptb1[matched_rows, test_name] <- test_used[x] + } + } } } - cols_to_remove <- grep("^test\\(", colnames(ptb1)) - ptb1 <- ptb1[, -cols_to_remove] + if (!is.null(labeldata) && Labels) { + pairwise_p_cols <- grep("^p\\(", colnames(ptb1), value = TRUE) + pairwise_test_cols <- grep("^test\\(", colnames(ptb1), value = TRUE) + + strata_labels <- stats::setNames(labeldata[labeldata$variable == strata, val_label], labeldata[labeldata$variable == strata, level]) + updated_p_colnames <- sapply(pairwise_p_cols, function(col_name) { + match <- regmatches(col_name, regexec("^p\\(([^vs]+)vs([^\\)]+)\\)", col_name)) + if (length(match[[1]]) == 3) { + group1 <- match[[1]][2] + group2 <- match[[1]][3] + label1 <- strata_labels[as.character(group1)] + label2 <- strata_labels[as.character(group2)] + if (!is.na(label1) && !is.na(label2)) { + return(paste0("p(", label1, " vs ", label2, ")")) + } + } + return(col_name) + }) + updated_test_colnames <- sapply(pairwise_test_cols, function(col_name) { + match <- regmatches(col_name, regexec("^test\\(([^vs]+)vs([^\\)]+)\\)", col_name)) + if (length(match[[1]]) == 3) { + group1 <- match[[1]][2] + group2 <- match[[1]][3] + label1 <- strata_labels[as.character(group1)] + label2 <- strata_labels[as.character(group2)] + if (!is.na(label1) && !is.na(label2)) { + return(paste0("test(", label1, " vs ", label2, ")")) + } + } + return(col_name) + }) + + colnames(ptb1)[colnames(ptb1) %in% pairwise_p_cols] <- updated_p_colnames + colnames(ptb1)[colnames(ptb1) %in% pairwise_test_cols] <- updated_test_colnames + } + if(!pairwise.showtest){ + cols_to_remove <- grep("^test\\(", colnames(ptb1)) + ptb1 <- ptb1[, -cols_to_remove] + } } - - sig <- ifelse(ptb1[, "p"] == "<0.001", "0", ptb1[, "p"]) sig <- as.numeric(as.vector(sig)) sig <- ifelse(sig <= 0.05, "**", "") @@ -233,6 +251,7 @@ svyCreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, te #' @param showpm Logical, show normal distributed continuous variables as Mean ± SD. Default: T #' @param addOverall (optional, only used if strata are supplied) Adds an overall column to the table. Smd and p-value calculations are performed using only the stratifed clolumns. Default: F #' @param pairwise (optional, only used if strata are supplied) When there are three or more strata, it displays the p-values for pairwise comparisons. Default: F +#' @param pairwise.showtest (optional, only used if strata are supplied) When using pairwise comparison, it displays the test used to calculate p-values for pairwise comparisons. Default: F #' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv. #' @details DETAILS #' @examples @@ -258,7 +277,7 @@ svyCreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, te svyCreateTableOneJS <- function(vars, strata = NULL, strata2 = NULL, data, factorVars = NULL, includeNA = F, test = T, showAllLevels = T, printToggle = F, quote = F, smd = F, Labels = F, nonnormal = NULL, catDigits = 1, contDigits = 2, pDigits = 3, labeldata = NULL, psub = T, minMax = F, showpm = T, - addOverall = F, pairwise = F) { + addOverall = F, pairwise = F, pairwise.showtest = F) { . <- level <- variable <- val_label <- V1 <- V2 <- NULL # if (Labels & !is.null(labeldata)){ @@ -310,7 +329,7 @@ svyCreateTableOneJS <- function(vars, strata = NULL, strata2 = NULL, data, facto strata = strata, vars = vars, data = data, factorVars = factorVars, includeNA = includeNA, test = test, smd = smd, showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, Labels = Labels, nonnormal = nonnormal, catDigits = catDigits, contDigits = contDigits, pDigits = pDigits, labeldata = labeldata, minMax = minMax, showpm = showpm, - addOverall = addOverall, pairwise = pairwise + addOverall = addOverall, pairwise = pairwise, pairwise.showtest = pairwise.showtest ) cap.tb1 <- paste("Stratified by ", strata, "- weighted data", sep = "") diff --git a/man/CreateTableOne2.Rd b/man/CreateTableOne2.Rd index c8c0363..11f5e0e 100644 --- a/man/CreateTableOne2.Rd +++ b/man/CreateTableOne2.Rd @@ -33,7 +33,8 @@ CreateTableOne2( minMax = F, showpm = T, addOverall = F, - pairwise = F + pairwise = F, + pairwise.showtest = F ) } \arguments{ @@ -94,6 +95,8 @@ CreateTableOne2( \item{addOverall}{(optional, only used if strata are supplied) Adds an overall column to the table. Smd and p-value calculations are performed using only the stratifed clolumns. Default: F} \item{pairwise}{(optional, only used if strata are supplied) When there are three or more strata, it displays the p-values for pairwise comparisons. Default: F} + +\item{pairwise.showtest}{(optional, only used if strata are supplied) When using pairwise comparison, it displays the test used to calculate p-values for pairwise comparisons. Default: F} } \value{ A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv. diff --git a/man/CreateTableOneJS.Rd b/man/CreateTableOneJS.Rd index 8206147..786d0a3 100644 --- a/man/CreateTableOneJS.Rd +++ b/man/CreateTableOneJS.Rd @@ -36,7 +36,8 @@ CreateTableOneJS( showpm = T, addOverall = F, normalityTest = F, - pairwise = F + pairwise = F, + pairwise.showtest = F ) } \arguments{ @@ -103,6 +104,8 @@ CreateTableOneJS( \item{normalityTest}{Logical, perform the Shapiro test for all variables. Default: F} \item{pairwise}{(optional, only used if strata are supplied) When there are three or more strata, it displays the p-values for pairwise comparisons. Default: F#' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv.} + +\item{pairwise.showtest}{(optional, only used if strata are supplied) When using pairwise comparison, it displays the test used to calculate p-values for pairwise comparisons. Default: F} } \description{ Combine CreateTableOne & print function in tableone package diff --git a/man/svyCreateTableOne2.Rd b/man/svyCreateTableOne2.Rd index 13dfc90..46bda8f 100644 --- a/man/svyCreateTableOne2.Rd +++ b/man/svyCreateTableOne2.Rd @@ -24,7 +24,8 @@ svyCreateTableOne2( minMax = F, showpm = T, addOverall = F, - pairwise = F + pairwise = F, + pairwise.showtest = F ) } \arguments{ @@ -67,6 +68,8 @@ svyCreateTableOne2( \item{addOverall}{(optional, only used if strata are supplied) Adds an overall column to the table. Smd and p-value calculations are performed using only the stratifed clolumns. Default: F} \item{pairwise}{(optional, only used if strata are supplied) When there are three or more strata, it displays the p-values for pairwise comparisons. Default: F} + +\item{pairwise.showtest}{(optional, only used if strata are supplied) When using pairwise comparison, it displays the test used to calculate p-values for pairwise comparisons. Default: F} } \value{ A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv. diff --git a/man/svyCreateTableOneJS.Rd b/man/svyCreateTableOneJS.Rd index 8c2853d..db2be2a 100644 --- a/man/svyCreateTableOneJS.Rd +++ b/man/svyCreateTableOneJS.Rd @@ -26,7 +26,8 @@ svyCreateTableOneJS( minMax = F, showpm = T, addOverall = F, - pairwise = F + pairwise = F, + pairwise.showtest = F ) } \arguments{ @@ -73,6 +74,8 @@ svyCreateTableOneJS( \item{addOverall}{(optional, only used if strata are supplied) Adds an overall column to the table. Smd and p-value calculations are performed using only the stratifed clolumns. Default: F} \item{pairwise}{(optional, only used if strata are supplied) When there are three or more strata, it displays the p-values for pairwise comparisons. Default: F} + +\item{pairwise.showtest}{(optional, only used if strata are supplied) When using pairwise comparison, it displays the test used to calculate p-values for pairwise comparisons. Default: F} } \value{ A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv. diff --git a/tests/testthat/test-CreateTableOneJS.R b/tests/testthat/test-CreateTableOneJS.R index e180fbc..3342771 100644 --- a/tests/testthat/test-CreateTableOneJS.R +++ b/tests/testthat/test-CreateTableOneJS.R @@ -6,6 +6,20 @@ test_that("Run CreateOneTableJS", { lung$status <- as.factor(lung$status) lung$ph.ecog <- as.factor(lung$ph.ecog) lung.label <- mk.lev(lung) + lung.label <- lung.label %>% + dplyr::mutate(val_label = ifelse( + variable == "ph.ecog" & level == "0", "Excellent", + ifelse( + variable == "ph.ecog" & level == "1", "Good", + ifelse( + variable == "ph.ecog" & level == "2", "Fair", + ifelse( + variable == "ph.ecog" & level == "3", "Poor", + val_label + ) + ) + ) + )) expect_is(CreateTableOneJS(vars = names(lung), data = lung), "list") 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") diff --git a/tests/testthat/test-svyCreateTableOne.R b/tests/testthat/test-svyCreateTableOne.R index 716850f..9bd6ae9 100644 --- a/tests/testthat/test-svyCreateTableOne.R +++ b/tests/testthat/test-svyCreateTableOne.R @@ -4,8 +4,17 @@ 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) + nhanes$RIAGENDR<-as.factor(nhanes$RIAGENDR) a.label <- mk.lev(nhanes) + a.label <- a.label %>% + dplyr::mutate(val_label = case_when( + variable == "race" & level == "1" ~ "White", + variable == "race" & level == "2" ~ "Black", + variable == "race" & level == "3" ~ "Hispanic", + variable == "race" & level == "4" ~ "Asian", + TRUE ~ val_label + )) nhanesSvy <- svydesign(ids = ~SDMVPSU, strata = ~SDMVSTRA, weights = ~WTMEC2YR, nest = TRUE, data = nhanes) expect_is(svyCreateTableOneJS(