Diff of /R/compClinvar.R [000000] .. [494cbf]

Switch to unified view

a b/R/compClinvar.R
1
#' @name compClinvar
2
#' @title Comparison of clinical variables
3
#' @description Create a table summarizing all baseline variables (both continuous and categorical) stratifying by current identified Subtypes and performing statistical tests. The object gives a table that is easy to use in medical research papers.
4
#' @param moic.res An object returned by `getMOIC()` with one specified algorithm or `get\%algorithm_name\%` or `getConsensusMOIC()` with a list of multiple algorithms.
5
#' @param var2comp A data.frame of clinical variables that need to compare among current subtypes with rownames for samples and columns for variable names.
6
#' @param strata A string value to indicate the stratifying variable. This function will generate an internal 'Subtype' variable which concatenates a string of 'CS' and values from 'clust' column of 'clust.res' in argument of `moic.res`. This argument is set as NULL by default and in this case using 'Subtype' variable as strata.
7
#' @param factorVars A string vectors to indicate the categorical variables. If omitted, only factors are considered categorical variables.
8
#' @param nonnormalVars A string vector to specify the variables for which the p-values should be those of nonparametric tests. By default all p-values are from normal assumption-based tests (oneway.test)., Default: NULL
9
#' @param exactVars A string vector to specify the variables for which the p-values should be those of exact tests. By default all p-values are from large sample approximation tests (chisq.test)., Default: NULL
10
#' @param includeNA A logic value to indicate if NA should be handled as a regular factor level rather than missing. NA is shown as the last factor level in the table if \code{includeNA = T}. Only effective for categorical variables., Default: FALSE
11
#' @param doWord A logic value to indicate if transformating the .txt outfile to a .docx WORD file (.txt file will be also kept).
12
#' @param tab.name A string value to indicate the name of the output table.
13
#' @param res.path A string value to indicate the path for saving the table.
14
#' @param ... Additionnal parameters pass to jstable::CreateTableOne2
15
#' @importFrom jstable CreateTableOne2
16
#' @importFrom dplyr %>%
17
#' @importFrom officer read_docx body_add_par body_add_table body_add_par
18
#' @export
19
#' @return A summarizing table with stastitical testing results.
20
#' @examples # There is no example and please refer to vignette.
21
compClinvar <- function(moic.res      = NULL,
22
                        var2comp      = NULL,
23
                        strata        = NULL,
24
                        factorVars    = NULL,
25
                        nonnormalVars = NULL,
26
                        exactVars     = NULL,
27
                        includeNA     = FALSE,
28
                        doWord        = TRUE,
29
                        tab.name      = NULL,
30
                        res.path      = getwd(),
31
                        ...){
32
33
  dat <- moic.res$clust.res
34
  colnames(dat)[which(colnames(dat) == "clust")] <- "Subtype"
35
  dat$Subtype <- paste0("CS", dat$Subtype)
36
  com_sam <- intersect(dat$samID,rownames(var2comp))
37
38
  # check data
39
  if(length(com_sam) == nrow(dat)) {
40
    message("--all samples matched.")
41
  } else {
42
    message(paste0("--",(nrow(dat)-length(com_sam))," samples mismatched from current subtypes."))
43
  }
44
  dat <- cbind.data.frame("Subtype" = dat[com_sam, "Subtype", drop = FALSE], var2comp[com_sam, , drop = FALSE])
45
46
  # summarizing
47
  if(is.null(strata)) {
48
    strata <- "Subtype"
49
  }
50
  if(!is.element(strata, colnames(dat))) {
51
    stop("fail to find this strata in var2comp. Consider using NULL by default.")
52
  }
53
54
  warn <- NULL
55
  tryCatch(stabl <-
56
             jstable::CreateTableOne2(vars = setdiff(colnames(dat), strata),
57
                                     strata = strata,
58
                                     data = dat,
59
                                     factorVars = factorVars,
60
                                     nonnormal = nonnormalVars,
61
                                     exact = exactVars,
62
                                     includeNA = includeNA,
63
                                     showAllLevels = TRUE,
64
                                     ...),
65
           warning=function(w) {
66
             warn <<- append(warn, conditionMessage(w))}
67
           )
68
  if(grepl("NA", warn, fixed = TRUE)) { # if get warning, probably due to the failure of computing exact p value in large data
69
    set.seed(19991018)
70
    stabl <- jstable::CreateTableOne2(vars = setdiff(colnames(dat), strata),
71
                                      strata = strata,
72
                                      data = dat,
73
                                      factorVars = factorVars,
74
                                      nonnormal = nonnormalVars,
75
                                      exact = exactVars,
76
                                      includeNA = includeNA,
77
                                      showAllLevels = TRUE,
78
                                      argsExact = list(simulate.p.value = T), # use simulated P values then
79
                                      ...)
80
  } else {
81
    stabl <- jstable::CreateTableOne2(vars = setdiff(colnames(dat), strata),
82
                                      strata = strata,
83
                                      data = dat,
84
                                      factorVars = factorVars,
85
                                      nonnormal = nonnormalVars,
86
                                      exact = exactVars,
87
                                      includeNA = includeNA,
88
                                      showAllLevels = TRUE,
89
                                      ...)
90
  }
91
92
  # trim output
93
  comtable <- as.data.frame(stabl)
94
  comtable <- cbind.data.frame(var = rownames(stabl), comtable)
95
  rownames(comtable) <- NULL; colnames(comtable)[1] <- " "
96
  comtable[is.na(comtable)] <- ""
97
  comtable <- comtable[,setdiff(colnames(comtable),"sig")] # remove significance
98
  #print(comtable)
99
100
  if(is.null(tab.name)) {
101
    outFile <- "summarization of clinical variables stratified by current subtype.txt"
102
  } else {
103
    outFile <- paste0(tab.name,".txt")
104
  }
105
106
  write.table(stabl, file.path(res.path,outFile), sep = "\t", quote = FALSE)
107
108
  # generate WORD format
109
  if(doWord){
110
    table_subtitle <- colnames(comtable)
111
112
    title_name <- paste0("Table *. ", gsub(".txt", "", outFile, fixed = TRUE))
113
    mynote <- "Note: ..."
114
115
    my_doc <- officer::read_docx()
116
    my_doc %>%
117
      officer::body_add_par(value = title_name, style = "table title") %>%
118
      officer::body_add_table(value = comtable, style = "table_template") %>%
119
      officer::body_add_par(value = mynote) %>%
120
      print(target = file.path(res.path,paste0("TABLE ", gsub(".txt", "", outFile, fixed = TRUE), ".docx")))
121
  }
122
  return(list(compTab = comtable))
123
}