1
## svyCreate Table1 : include 2 strata
2

3

4
#' @title svyCreateTableOne2: Modified svyCreateTableOne function in tableone package
5
#' @description Combine svyCreateTableOne & print function in tableone package
6
#' @param data A data frame in which these variables exist. All variables (both vars and strata) must be in this data frame.
7
#' @param strata Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned.
8
#' @param vars Variables to be summarized given as a character vector. Factors are handled as categorical variables, whereas numeric variables are handled as continuous variables. If empty, all variables in the data frame specified in the data argument are used.
9
#' @param factorVars Numerically coded variables that should be handled as categorical variables given as a character vector. Do not include factors, unless you need to relevel them by removing empty levels. If omitted, only factors are considered categorical variables. The variables specified here must also be specified in the vars argument.
10
#' @param includeNA If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables., Default: F
11
#' @param test If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed, Default: T
12
#' @param showAllLevels Whether to show all levels. FALSE by default, i.e., for 2-level categorical variables, only the higher level is shown to avoid redundant information., Default: T
13
#' @param printToggle Whether to print the output. If FALSE, no output is created, and a matrix is invisibly returned., Default: F
14
#' @param quote Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily, Default: F
15
#' @param smd If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated, Default: F
16
#' @param Labels Use Label, Default: F
17
#' @param nonnormal A character 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
18
#' @param catDigits Number of digits to print for proportions., Default: 1
19
#' @param contDigits Number of digits to print for continuous variables. Default 2.
20
#' @param pDigits Number of digits to print for p-values (also used for standardized mean differences), Default: 3
21
#' @param labeldata labeldata to use, Default: NULL
22
#' @param minMax Whether to use [min,max] instead of [p25,p75] for nonnormal variables. The default is FALSE.
23
#' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv.
24
#' @details DETAILS
25
#' @examples 
26
#'  library(survey);data(nhanes)
27
#'  nhanes$SDMVPSU <- as.factor(nhanes$SDMVPSU)
28
#'  nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, weights = ~ WTMEC2YR, 
29
#'                         nest = TRUE, data = nhanes)
30
#'  svyCreateTableOne2(vars = c("HI_CHOL","race","agecat","RIAGENDR"), 
31
#'                     strata = "RIAGENDR", data = nhanesSvy)
32
#' @rdname svyCreateTableOne2
33
#' @importFrom data.table data.table :=
34
#' @importFrom tableone svyCreateTableOne 
35
#' @importFrom labelled var_label var_label<-
36
#' @export 
37

38
svyCreateTableOne2 <- function(data, strata, vars, factorVars, includeNA = F, test = T,
39
                           showAllLevels = T, printToggle = F, quote = F, smd = F, nonnormal = NULL, 
40
                           catDigits = 1, contDigits = 2, pDigits = 3, Labels = F, labeldata = NULL, minMax = F){
41
  
42 1
  setkey <- variable <- level <- . <- val_label <- NULL
43
  
44 1
  if (length(strata) != 1){
45 0
    stop("Please select only 1 strata")
46
  }
47

48
  
49 1
  res <- tableone::svyCreateTableOne(vars =vars, strata = strata, data = data, factorVars = factorVars, includeNA = includeNA, test = test, 
50 1
                                  smd = smd)
51
  
52 1
  factor_vars <- res[["MetaData"]][["varFactors"]]
53
  
54 1
  if (Labels & !is.null(labeldata)){
55 1
    labelled::var_label(data$variables) = sapply(names(data$variables), function(v){as.character(labeldata[get("variable") == v, "var_label"][1])}, simplify = F)
56
    #vals.tb1 <- c(NA, unlist(sapply(vars, function(v){labeldata[get("variable") == v, "val_label"]})))
57 1
    data.table::setkey(labeldata, variable, level)
58 1
    res0 <- tableone::svyCreateTableOne(vars =vars, data = data, factorVars = factorVars, includeNA = includeNA)
59 1
    for (i in seq_along(res0$CatTable)){
60 1
      for(j in factor_vars){
61 1
        lvs <- res0$CatTable[[i]][[j]]$level
62 1
        res0$CatTable[[i]][[j]]$level <- labeldata[.(j, lvs), val_label]
63
      }
64
    }
65 1
    ptb1.res0 <- print(res0, showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, varLabels = Labels, nonnormal = nonnormal,
66 1
                       catDigits = catDigits, contDigits = contDigits, minMax = minMax)
67 1
    ptb1.rn <- rownames(ptb1.res0)
68 1
    ptb1.rn <- gsub("(mean (SD))", "", ptb1.rn, fixed=T)
69

70
  }
71
  
72
  
73 1
  ptb1 <- print(res,
74 1
                showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, smd = smd, varLabels = Labels, nonnormal = nonnormal,
75 1
                catDigits = catDigits, contDigits = contDigits, pDigits = pDigits, minMax = minMax)
76 1
  rownames(ptb1) = gsub("(mean (SD))", "", rownames(ptb1), fixed=T)
77 1
  if (Labels & !is.null(labeldata)){
78 1
    rownames(ptb1) <- ptb1.rn
79 1
    if (showAllLevels == T) ptb1[, 1] <- ptb1.res0[, 1]
80
  }
81
  #cap.tb1 = paste("Table 1: Stratified by ", strata, sep="")
82
  
83 1
  if (Labels & !is.null(labeldata)){
84 1
    colname.group_var = unlist(labeldata[get("variable") == strata, "val_label"])
85 1
    if(showAllLevels == T){
86
      #colname.group_var <- unlist(labeldata[get("variable") == strata, "val_label"])
87 1
      colnames(ptb1)[1:(length(colname.group_var)+1)] <- unlist(c(labeldata[get("variable") == strata, "var_label"][1], colname.group_var))
88
    } else{
89 1
      colnames(ptb1)[1:length(colname.group_var)] <- colname.group_var
90
    }
91
    
92
  }
93
  
94 1
  sig <- ifelse(ptb1[,"p"] == "<0.001", "0", ptb1[,"p"])
95 1
  sig <- as.numeric(as.vector(sig))
96 1
  sig <- ifelse(sig <= 0.05, "**", "")
97 1
  ptb1 <- cbind(ptb1, sig)
98 1
  return(ptb1)
99
}
100

101

102

103
#' @title svyCreateTableOneJS: Modified CreateTableOne function in tableone package
104
#' @description Combine svyCreateTableOne & print function in tableone package
105
#' @param data A data frame in which these variables exist. All variables (both vars and strata) must be in this data frame.
106
#' @param strata Stratifying grouping variable name(s) given as a character vector. If omitted, the overall results are returned.
107
#' @param strata2 Stratifying 2nd grouping variable name(s) given as a character vector. If omitted, the 1 group results are returned.
108
#' @param vars Variables to be summarized given as a character vector. Factors are handled as categorical variables, whereas numeric variables are handled as continuous variables. If empty, all variables in the data frame specified in the data argument are used.
109
#' @param factorVars Numerically coded variables that should be handled as categorical variables given as a character vector. Do not include factors, unless you need to relevel them by removing empty levels. If omitted, only factors are considered categorical variables. The variables specified here must also be specified in the vars argument.
110
#' @param includeNA If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables., Default: F
111
#' @param test If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed, Default: T
112
#' @param showAllLevels Whether to show all levels. FALSE by default, i.e., for 2-level categorical variables, only the higher level is shown to avoid redundant information., Default: T
113
#' @param printToggle Whether to print the output. If FALSE, no output is created, and a matrix is invisibly returned., Default: F
114
#' @param quote Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily, Default: F
115
#' @param smd If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated, Default: F
116
#' @param Labels Use Label, Default: F
117
#' @param nonnormal A character 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
118
#' @param catDigits Number of digits to print for proportions., Default: 1
119
#' @param contDigits Number of digits to print for continuous variables. Default 2.
120
#' @param pDigits Number of digits to print for p-values (also used for standardized mean differences), Default: 3
121
#' @param labeldata labeldata to use, Default: NULL
122
#' @param psub show sub-group p-values, Default: F
123
#' @param minMax Whether to use [min,max] instead of [p25,p75] for nonnormal variables. The default is FALSE.
124
#' @param showpm Logical, show normal distributed continuous variables as Mean ± SD. Default: T 
125
#' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via write.csv.
126
#' @details DETAILS
127
#' @examples 
128
#'  library(survey);data(nhanes)
129
#'  nhanes$SDMVPSU <- as.factor(nhanes$SDMVPSU)
130
#'  nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, weights = ~ WTMEC2YR, 
131
#'                         nest = TRUE, data = nhanes)
132
#'  svyCreateTableOneJS(vars = c("HI_CHOL","race","agecat","RIAGENDR"), 
133
#'                      strata = "RIAGENDR", data = nhanesSvy)
134
#' @rdname svyCreateTableOneJS
135
#' @importFrom data.table data.table := CJ
136
#' @importFrom tableone svyCreateTableOne 
137
#' @importFrom labelled var_label var_label<-
138
#' @export 
139

140

141
svyCreateTableOneJS <- function(vars, strata = NULL, strata2 = NULL, data, factorVars = NULL, includeNA = F, test = T,
142
                            showAllLevels = T, printToggle = F, quote = F, smd = F, Labels = F, nonnormal = NULL, 
143
                            catDigits = 1, contDigits = 2, pDigits = 3, labeldata = NULL, psub = T, minMax = F, showpm = T){
144
  
145 1
  . <- level <- variable <- val_label <- V1 <- V2 <- NULL
146
  
147
  #if (Labels & !is.null(labeldata)){
148
  #  var_label(data) = sapply(names(data), function(v){as.character(labeldata[get("variable") == v, "var_label"][1])}, simplify = F)
149
  #  vals.tb1 = c(NA, unlist(sapply(vars, function(v){labeldata[get("variable") == v, "val_label"]})))
150
  #}
151 1
  data <- data
152
  
153 1
  if (is.null(strata)){
154
    
155 1
    if (Labels & !is.null(labeldata)){
156 1
      labelled::var_label(data$variables) = sapply(names(data$variables), function(v){as.character(labeldata[get("variable") == v, "var_label"][1])}, simplify = F)
157
      #vals.tb1 <- c(NA, unlist(sapply(vars, function(v){labeldata[get("variable") == v, "val_label"]})))
158 1
      data.table::setkey(labeldata, variable, level)
159
    }
160
    
161 1
    res <- tableone::svyCreateTableOne(vars =vars, data = data, factorVars = factorVars, includeNA = includeNA)
162 1
    factor_vars <- res[["MetaData"]][["varFactors"]]
163
    
164 1
    if (Labels & !is.null(labeldata)){
165 1
      for (i in seq_along(res$CatTable)){
166 1
        for(j in factor_vars){
167 1
          lvs <- res$CatTable[[i]][[j]]$level
168 1
          res$CatTable[[i]][[j]]$level <- labeldata[.(j, lvs), val_label]
169
        }
170
      }
171
    }
172
    
173 1
    ptb1 <- print(res,
174 1
                  showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, varLabels = Labels, nonnormal = nonnormal,
175 1
                  catDigits = catDigits, contDigits = contDigits, minMax = minMax)
176
    
177 1
    rownames(ptb1) <- gsub("(mean (SD))", "", rownames(ptb1), fixed=T)
178
    
179 1
    if (showpm){
180 1
      ptb1[!grepl("(%)", rownames(ptb1)) & rownames(ptb1) != "", ] <- gsub("\\(", "\u00B1 ", ptb1[!grepl("(%)", rownames(ptb1)) & rownames(ptb1) != "", ] )
181 1
      ptb1[!grepl("(%)", rownames(ptb1)) & rownames(ptb1) != "", ] <- gsub("\\)", "", ptb1[!grepl("(%)", rownames(ptb1)) & rownames(ptb1) != "", ] )
182
    }
183
    
184 1
    cap.tb1 <- "Total - weighted data"
185
    #if (Labels & !is.null(labeldata)){
186
    #  ptb1[,1] <- vals.tb1
187
    #}
188 1
    return(list(table = ptb1, caption = cap.tb1))
189 1
  } else if (is.null(strata2)){
190 1
    ptb1 <- svyCreateTableOne2(strata = strata, vars =vars, data = data, factorVars = factorVars, includeNA = includeNA, test = test, smd = smd,
191 1
                            showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, Labels = Labels, nonnormal = nonnormal,
192 1
                            catDigits = catDigits, contDigits = contDigits, pDigits = pDigits, labeldata = labeldata, minMax = minMax)
193
    
194 1
    cap.tb1 <- paste("Stratified by ", strata, "- weighted data", sep="")
195 1
    if (showpm){
196 1
      ptb1[!grepl("(%)", rownames(ptb1)) & ptb1[, "p"] != "", ] <- gsub("\\(", "\u00B1 ", ptb1[!grepl("(%)", rownames(ptb1)) & ptb1[, "p"] != "", ] )
197 1
      ptb1[!grepl("(%)", rownames(ptb1)) & ptb1[, "p"] != "", ] <- gsub("\\)", "", ptb1[!grepl("(%)", rownames(ptb1)) & ptb1[, "p"] != "", ] )
198
    }
199
    
200 1
    if (Labels & !is.null(labeldata)){
201 1
      cap.tb1 <- paste("Stratified by ", labeldata[get("variable") == strata, "var_label"][1], "- weighted data", sep="")
202
      #ptb1[,1] = vals.tb1
203
      
204
    }
205 1
    return(list(table = ptb1, caption = cap.tb1))
206 1
  } else if (psub ==T){
207 1
    data.strata <-  lapply(levels(data$variable[[strata]]), function(x){subset(data, get(strata) == x)})
208 1
    ptb1.list <- lapply(data.strata, svyCreateTableOne2,
209 1
                        vars =vars, strata = strata2, factorVars = factorVars, includeNA = includeNA, test = test, smd = smd,
210 1
                        showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, Labels = F, nonnormal = nonnormal, 
211 1
                        catDigits = catDigits, contDigits = contDigits, pDigits = pDigits, minMax = minMax)
212
    
213
    
214 1
    if (showAllLevels == T){
215 1
      ptb1.cbind <- Reduce(cbind, c(list(ptb1.list[[1]]), lapply(2:length(ptb1.list), function(x){ptb1.list[[x]][,-1]})))
216
    } else{
217 1
      ptb1.cbind <- Reduce(cbind, ptb1.list)
218
    }
219 1
    if (showpm){
220 1
      ptb1.cbind[!grepl("(%)", rownames(ptb1.cbind)) & ptb1.cbind[, "p"] != "", ] <- gsub("\\(", "\u00B1 ", ptb1.cbind[!grepl("(%)", rownames(ptb1.cbind)) & ptb1.cbind[, "p"] != "", ] )
221 1
      ptb1.cbind[!grepl("(%)", rownames(ptb1.cbind)) & ptb1.cbind[, "p"] != "", ] <- gsub("\\)", "", ptb1.cbind[!grepl("(%)", rownames(ptb1.cbind)) & ptb1.cbind[, "p"] != "", ] )
222
    }
223
    
224
    #colnum.test = which(colnames(ptb1.cbind) == "test")
225
    #ptb1.2group = ptb1.cbind[, c(setdiff(1:ncol(ptb1.cbind), colnum.test), colnum.test[1])]
226 1
    cap.tb1 <- paste("Stratified by ", strata, "(", paste(levels(data[[strata]]), collapse=", "), ") & ", strata2, "- weighted data", sep="")
227 1
    if (Labels & !is.null(labeldata)){
228 1
      labelled::var_label(data.strata[[1]]$variables) = sapply(names(data.strata[[1]]$variables), function(v){as.character(labeldata[get("variable") == v, "var_label"][1])}, simplify = F)
229
      #vals.tb1 <- c(NA, unlist(sapply(vars, function(v){labeldata[get("variable") == v, "val_label"]})))
230 1
      data.table::setkey(labeldata, variable, level)
231
      
232 1
      res <- tableone::svyCreateTableOne(vars =vars, data = data.strata[[1]], factorVars = factorVars, includeNA = includeNA)
233 1
      factor_vars <- res[["MetaData"]][["varFactors"]]
234 1
      for (i in seq_along(res$CatTable)){
235 0
        for(j in factor_vars){
236 0
          lvs <- res$CatTable[[i]][[j]]$level
237 0
          res$CatTable[[i]][[j]]$level <- labeldata[.(j, lvs), val_label]
238
        }
239
      }
240
      
241 1
      ptb1.res <- print(res, showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, varLabels = Labels, nonnormal = nonnormal,
242 1
                        catDigits = catDigits, contDigits = contDigits, minMax = minMax)
243 1
      ptb1.rn <- rownames(ptb1.res)
244 1
      rownames(ptb1.cbind) <- gsub("(mean (SD))", "", ptb1.rn, fixed=T)
245 1
      if (showAllLevels == T) {ptb1.cbind[, 1] <- ptb1.res[, 1]}
246
      
247 1
      cap.tb1 <- paste("Stratified by ", labeldata[get("variable") == strata, "var_label"][1], "(", paste(unlist(labeldata[get("variable") == strata, "val_label"]), collapse=", "), ") & ", labeldata[get("variable") == strata2, "var_label"][1], "- weighted data", sep="")
248
    }
249
    
250 1
    return(list(table = ptb1.cbind, caption = cap.tb1))
251
  } else{
252 0
    res <- tableone::svyCreateTableOne(vars = vars, strata = c(strata2, strata), data = data, factorVars = factorVars, includeNA = F, test = T) 
253 0
    factor_vars <- res[["MetaData"]][["varFactors"]]
254
    
255 0
    if (Labels & !is.null(labeldata)){
256 0
      labelled::var_label(data$variable) <- sapply(names(data$variable), function(v){as.character(labeldata[get("variable") == v, "var_label"][1])}, simplify = F)
257 0
      data.table::setkey(labeldata, variable, level)
258 0
      res0 <- tableone::svyCreateTableOne(vars =vars, data = data, factorVars = factorVars, includeNA = includeNA)
259 0
      for (i in seq_along(res0$CatTable)){
260 0
        for(j in factor_vars){
261 0
          lvs <- res0$CatTable[[i]][[j]]$level
262 0
          res0$CatTable[[i]][[j]]$level <- labeldata[.(j, lvs), val_label]
263
        }
264
      }
265
      
266 0
      ptb1.res0 <- print(res0, showAllLevels = showAllLevels, printToggle = printToggle, quote = quote, varLabels = Labels, nonnormal = nonnormal,
267 0
                         catDigits = catDigits, contDigits = contDigits, minMax = minMax)
268 0
      ptb1.rn <- rownames(ptb1.res0)
269 0
      ptb1.rn <- gsub("(mean (SD))", "", ptb1.rn, fixed=T)
270
      
271
      #vals.tb1 <- c(NA, unlist(sapply(vars, function(v){labeldata[get("variable") == v, "val_label"]})))
272
    }
273
    
274 0
    ptb1 <- print(res, 
275 0
                  showAllLevels=showAllLevels,
276 0
                  printToggle=F, quote=F, smd = smd, varLabels = T,  nonnormal = nonnormal,
277 0
                  catDigits = catDigits, contDigits = contDigits, pDigits = pDigits, minMax = minMax)
278
    
279 0
    rownames(ptb1) <- gsub("(mean (SD))", "", rownames(ptb1), fixed=T)
280 0
    if (Labels & !is.null(labeldata)){
281 0
      rownames(ptb1) <- ptb1.rn
282 0
      if (showAllLevels == T) {ptb1[, 1] <- ptb1.res0[, 1]}
283
      }
284
    
285 0
    sig <- ifelse(ptb1[,"p"] == "<0.001", "0", ptb1[,"p"])
286 0
    sig <- as.numeric(as.vector(sig))
287 0
    sig <- ifelse(sig <= 0.05, "**", "")
288 0
    ptb1 <- cbind(ptb1, sig)
289 0
    cap.tb1 <- paste("Stratified by ", strata, " and ",strata2, "- weighted data",  sep="")
290
    
291 0
    if (showpm){
292 0
      ptb1[!grepl("(%)", rownames(ptb1)) & ptb1[, "p"] != "", ] <- gsub("\\(", "\u00B1 ", ptb1[!grepl("(%)", rownames(ptb1)) & ptb1[, "p"] != "", ] )
293 0
      ptb1[!grepl("(%)", rownames(ptb1)) & ptb1[, "p"] != "", ] <- gsub("\\)", "", ptb1[!grepl("(%)", rownames(ptb1)) & ptb1[, "p"] != "", ] )
294
    }
295
    
296
    # Column name
297 0
    if (Labels & !is.null(labeldata)){
298 0
      val_combination <- data.table::CJ(labeldata[variable == strata, val_label], labeldata[variable == strata2, val_label], sorted = F)
299 0
      colname.group_var <- val_combination[, paste(V1, ":", V2, sep="")] 
300 0
      colname.group_index <- paste(labeldata[variable == strata, var_label][1], ":", labeldata[variable == strata2, var_label][1], sep = "")
301 0
      if (showAllLevels == T){
302 0
        colnames(ptb1)[1:(length(colname.group_var)+1)] <- c(colname.group_index, colname.group_var)
303
      } else{
304 0
        colnames(ptb1)[1:length(colname.group_var)] <- colname.group_var
305
      }
306
      # caption
307 0
      cap.tb1 <- paste("Stratified by ", labeldata[variable == strata, var_label][1], " and ", labeldata[variable == strata2, var_label][1], "- weighted data",  sep="")
308
      # val_label
309
      #vals.tb1 <- c(NA, unlist(sapply(vars, function(v){labeldata[variable == v, val_label]})))
310
      #ptb1[,1] <- vals.tb1
311
    }
312 0
    return(list(table = ptb1, caption = cap.tb1))
313
    
314
  }
315
  
316
} 

Read our documentation on viewing source code .

Loading