kaz-yos / tableone

R package to create "Table 1", description of baseline characteristics with or without propensity score weighting
https://cran.r-project.org/web/packages/tableone/index.html
214 stars 40 forks source link

Categorical variables missing from factorVars argument #82

Open Sirhurryup opened 3 years ago

Sirhurryup commented 3 years ago

I created an object of categorical variables that were of character type in my dataset. Then I created a Tableone object using the tutorial convention. When I execute that file, the categorical variables never return. I tried to convert my character variables to factors and run the CreateTableOne() to produce the output. Still, the categorical variables never appear.


require(tableone)
dput((names(demo)
con_vars <- c("days_los_estimate", "days_hemodialysis", "days_peritoneal_dialysis", "days_crrt","days_vent","age_estimate", "age_exact", "ed_los_in_hrs", "ed_ip_los_in_hrs")

catvars <- c("hemodialysis", "peritoneal_dialysis", "crrt", "vent","dialysis_any","race", "sex", "planning_payor_category_2010", "death_indicator", "age_65")

sep <- CreateTableOne(vars = convars, data = demo, factorVars = catvars)

sep
                               Overall        

n 2535 days_los_estimate (mean (SD)) 15.51 (27.41) days_hemodialysis (mean (SD)) 0.68 (4.32) days_peritoneal_dialysis (mean (SD)) 0.04 (0.63) days_crrt (mean (SD)) 0.38 (2.37) days_vent (mean (SD)) 2.46 (8.69) age_estimate (mean (SD)) 60.83 (16.20) age_exact (mean (SD)) 61.33 (16.20) ed_los_in_hrs (mean (SD)) 9.68 (15.86) ed_ip_los_in_hrs (mean (SD)) 440.98 (794.40)

Do the variables that require transformation need to be in another format? Unsure of how to display the categorical variables. I tried using as.factor in a mutate() to change the data type and the tableone object still only returns the continuous variables.

tszberkowitz commented 2 years ago

@Sirhurryup You need to specify all variables in the vars argument, not just the continuous variables. Try changing vars = convars to vars = c(convars, catvars) and re-running your code.

The behavior you've reported can be reproduced with the following example (modified from the?CreateTableOne documentation page):

## Load package
library(tableone)

## Load data
data(pbc, package = "survival")

## Make categorical variables factors
varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage")
pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor)

## Identify continuous variable names
convars <- c("time", "age")

## Identify categorical variable names
catvars <- c("status", "edema")

## Create Table 1 that doesn't show categorical variables when printed
CreateTableOne(vars = convars, strata = c("trt"), data = pbc, factorVars = catvars)
###                  Stratified by trt
###                   1                 2                 p      test
###  n                    158               154                      
###  time (mean (SD)) 2015.62 (1094.12) 1996.86 (1155.93)  0.883     
###  age (mean (SD))    51.42 (11.01)     48.58 (9.96)     0.018     

## Create Table 1 now showing all variables when printed
CreateTableOne(vars = c(convars, catvars), strata = c("trt"), data = pbc, factorVars = catvars)
###                  Stratified by trt
###                   1                 2                 p      test
###  n                    158               154                      
###  time (mean (SD)) 2015.62 (1094.12) 1996.86 (1155.93)  0.883     
###  age (mean (SD))    51.42 (11.01)     48.58 (9.96)     0.018     
###  status (%)                                            0.894     
###     0                  83 (52.5)         85 (55.2)               
###     1                  10 ( 6.3)          9 ( 5.8)               
###     2                  65 (41.1)         60 (39.0)               
###  edema (%)                                             0.877     
###     0                 132 (83.5)        131 (85.1)               
###     0.5                16 (10.1)         13 ( 8.4)               
###     1                  10 ( 6.3)         10 ( 6.5)