tableone CreateCatTable() test

What is tableone?

### Load dataset
library(survival)
data(pbc)
head(pbc)
  id time status trt   age sex ascites hepato spiders edema bili chol albumin copper alk.phos    ast trig platelet
1  1  400      2   1 58.77   f       1      1       1   1.0 14.5  261    2.60    156     1718 137.95  172      190
2  2 4500      0   1 56.45   f       0      1       1   0.0  1.1  302    4.14     54     7395 113.52   88      221
3  3 1012      2   1 70.07   m       0      0       0   0.5  1.4  176    3.48    210      516  96.10   55      151
4  4 1925      2   1 54.74   f       0      1       1   0.5  1.8  244    2.54     64     6122  60.63   92      183
5  5 1504      1   2 38.11   f       0      1       1   0.0  3.4  279    3.53    143      671 113.15   72      136
6  6 2503      2   2 66.26   f       0      1       0   0.0  0.8  248    3.98     50      944  93.00   63       NA
  protime stage
1    12.2     4
2    10.6     3
3    12.0     4
4    10.3     4
5    10.9     3
6    11.0     3
## Group by "status" (3 groups).
## Use c("time","age","bili","chol","albumin","copper","alk.phos","ast","trig","platelet","protime")
## 
## age:       in years                                                      
## albumin:   serum albumin (g/dl)                                          
## alk.phos:  alkaline phosphotase (U/liter)                                
## ascites:   presence of ascites                                           
## ast:       aspartate aminotransferase, once called SGOT (U/ml)           
## bili:      serum bilirunbin (mg/dl)                                      
## chol:      serum cholesterol (mg/dl)                                     
## copper:    urine copper (ug/day)                                         
## edema:     0 no edema, 0.5 untreated or successfully treated             
##            1 edema despite diuretic therapy                              
## hepato:    presence of hepatomegaly or enlarged liver                    
## id:        case number                                                   
## platelet:  platelet count                                                
## protime:   standardised blood clotting time                              
## sex:       m/f                                                           
## spiders:   blood vessel malformations in the skin                        
## stage:     histologic stage of disease (needs biopsy)                    
## status:    status at endpoint, 0/1/2 for censored, transplant, dead      
## time:      number of days between registration and the earlier of death, 
##            transplantion, or study analysis in July, 1986                
## trt:       1/2/NA for D-penicillmain, placebo, not randomised            
## trig:      triglycerides (mg/dl)

### Load tableone package from the local developmental repository
source("~/statistics/_source_table_one.R")

###  Define variables
vars <- c("trt","sex","ascites","hepato","spiders","edema","stage")

## Strata
strata <- "status"
strataTwoVars <- c("status","sex")
strataThreeVars <- c("sex","ascites","hepato")

## Data and variables
dataFrame <- pbc
exactVars <- c("ascites","edema")

## Create edema strata that are of sizes 1, 2, and 3
smallDataFrame <- pbc[c(2, 3,4, 1,10,14),]
smallStrata <- "edema"


### Behaviors on correct data format (non-stratified)
## Creation
catTable1 <- CreateCatTable(vars = vars, data = dataFrame)
## summary method
summary(catTable1)
strata: Overall
     var   n miss level freq percent cum.percent
     trt 418  106     1  158    50.6        50.6
                      2  154    49.4       100.0

     sex 418    0     m   44    10.5        10.5
                      f  374    89.5       100.0

 ascites 418  106     0  288    92.3        92.3
                      1   24     7.7       100.0

  hepato 418  106     0  152    48.7        48.7
                      1  160    51.3       100.0

 spiders 418  106     0  222    71.2        71.2
                      1   90    28.8       100.0

   edema 418    0     0  354    84.7        84.7
                    0.5   44    10.5        95.2
                      1   20     4.8       100.0

   stage 418    6     1   21     5.1         5.1
                      2   92    22.3        27.4
                      3  155    37.6        65.0
                      4  144    35.0       100.0

## print method by default
print(catTable1)
                Overall    
n               418        
trt = 2 (%)     154 (49.4) 
sex = f (%)     374 (89.5) 
ascites = 1 (%)  24 ( 7.7) 
hepato = 1 (%)  160 (51.3) 
spiders = 1 (%)  90 (28.8) 
edema (%)                  
   0            354 (84.7) 
   0.5           44 (10.5) 
   1             20 ( 4.8) 
stage (%)                  
   1             21 ( 5.1) 
   2             92 (22.3) 
   3            155 (37.6) 
   4            144 (35.0) 
## Format (percent only)
print(catTable1, format = "p")
                Overall
n               418    
trt = 2 (%)     49.4   
sex = f (%)     89.5   
ascites = 1 (%)  7.7   
hepato = 1 (%)  51.3   
spiders = 1 (%) 28.8   
edema (%)              
   0            84.7   
   0.5          10.5   
   1             4.8   
stage (%)              
   1             5.1   
   2            22.3   
   3            37.6   
   4            35.0   
## Format (frequency only, no explanation)
print(catTable1, format = "f", explain = FALSE)
            Overall
n           418    
trt = 2     154    
sex = f     374    
ascites = 1  24    
hepato = 1  160    
spiders = 1  90    
edema              
   0        354    
   0.5       44    
   1         20    
stage              
   1         21    
   2         92    
   3        155    
   4        144    

### Behaviors on correct data format (stratified)
## Creation
catTable2 <- CreateCatTable(vars = vars, strata = strata, data = dataFrame)
## summary method
summary(catTable2)
status: 0
     var   n miss level freq percent cum.percent
     trt 232   64     1   83    49.4        49.4
                      2   85    50.6       100.0

     sex 232    0     m   17     7.3         7.3
                      f  215    92.7       100.0

 ascites 232   64     0  167    99.4        99.4
                      1    1     0.6       100.0

  hepato 232   64     0  108    64.3        64.3
                      1   60    35.7       100.0

 spiders 232   64     0  135    80.4        80.4
                      1   33    19.6       100.0

   edema 232    0     0  216    93.1        93.1
                    0.5   15     6.5        99.6
                      1    1     0.4       100.0

   stage 232    2     1   19     8.3         8.3
                      2   64    27.8        36.1
                      3   97    42.2        78.3
                      4   50    21.7       100.0

--------------------------------------------------------------------------------------- 
status: 1
     var  n miss level freq percent cum.percent
     trt 25    6     1   10    52.6        52.6
                     2    9    47.4       100.0

     sex 25    0     m    3    12.0        12.0
                     f   22    88.0       100.0

 ascites 25    6     0   19   100.0       100.0
                     1    0     0.0       100.0

  hepato 25    6     0    7    36.8        36.8
                     1   12    63.2       100.0

 spiders 25    6     0   14    73.7        73.7
                     1    5    26.3       100.0

   edema 25    0     0   22    88.0        88.0
                   0.5    3    12.0       100.0
                     1    0     0.0       100.0

   stage 25    0     1    0     0.0         0.0
                     2    5    20.0        20.0
                     3   10    40.0        60.0
                     4   10    40.0       100.0

--------------------------------------------------------------------------------------- 
status: 2
     var   n miss level freq percent cum.percent
     trt 161   36     1   65    52.0        52.0
                      2   60    48.0       100.0

     sex 161    0     m   24    14.9        14.9
                      f  137    85.1       100.0

 ascites 161   36     0  102    81.6        81.6
                      1   23    18.4       100.0

  hepato 161   36     0   37    29.6        29.6
                      1   88    70.4       100.0

 spiders 161   36     0   73    58.4        58.4
                      1   52    41.6       100.0

   edema 161    0     0  116    72.0        72.0
                    0.5   26    16.1        88.2
                      1   19    11.8       100.0

   stage 161    4     1    2     1.3         1.3
                      2   23    14.6        15.9
                      3   48    30.6        46.5
                      4   84    53.5       100.0


p-values
              pApprox         pExact
trt     0.89350974940 0.884221883654
sex     0.05344266826 0.041912591395
ascites 0.00000004847 0.000000019153
hepato  0.00000001808 0.000000007246
spiders 0.00021408606 0.000194188617
edema   0.00000003438 0.000000019547
stage   0.00000001118             NA
## print method by default
print(catTable2, exact = exactVars)
                 Stratified by status
                  0           1          2           p      test 
  n               232         25         161                     
  trt = 2 (%)      85 (50.6)   9 (47.4)   60 (48.0)   0.894      
  sex = f (%)     215 (92.7)  22 (88.0)  137 (85.1)   0.053      
  ascites = 1 (%)   1 ( 0.6)   0 ( 0.0)   23 (18.4)  <0.001 exact
  hepato = 1 (%)   60 (35.7)  12 (63.2)   88 (70.4)  <0.001      
  spiders = 1 (%)  33 (19.6)   5 (26.3)   52 (41.6)  <0.001      
  edema (%)                                          <0.001 exact
     0            216 (93.1)  22 (88.0)  116 (72.0)              
     0.5           15 ( 6.5)   3 (12.0)   26 (16.1)              
     1              1 ( 0.4)   0 ( 0.0)   19 (11.8)              
  stage (%)                                          <0.001      
     1             19 ( 8.3)   0 ( 0.0)    2 ( 1.3)              
     2             64 (27.8)   5 (20.0)   23 (14.6)              
     3             97 (42.2)  10 (40.0)   48 (30.6)              
     4             50 (21.7)  10 (40.0)   84 (53.5)              
## print method by default
print(catTable2, showAllLevels = TRUE)
             Stratified by status
              level 0           1           2           p     
  n                 232         25          161               
  trt (%)     1      83 (49.4)  10 ( 52.6)   65 (52.0)   0.894
              2      85 (50.6)   9 ( 47.4)   60 (48.0)        
  sex (%)     m      17 ( 7.3)   3 ( 12.0)   24 (14.9)   0.053
              f     215 (92.7)  22 ( 88.0)  137 (85.1)        
  ascites (%) 0     167 (99.4)  19 (100.0)  102 (81.6)  <0.001
              1       1 ( 0.6)   0 (  0.0)   23 (18.4)        
  hepato (%)  0     108 (64.3)   7 ( 36.8)   37 (29.6)  <0.001
              1      60 (35.7)  12 ( 63.2)   88 (70.4)        
  spiders (%) 0     135 (80.4)  14 ( 73.7)   73 (58.4)  <0.001
              1      33 (19.6)   5 ( 26.3)   52 (41.6)        
  edema (%)   0     216 (93.1)  22 ( 88.0)  116 (72.0)  <0.001
              0.5    15 ( 6.5)   3 ( 12.0)   26 (16.1)        
              1       1 ( 0.4)   0 (  0.0)   19 (11.8)        
  stage (%)   1      19 ( 8.3)   0 (  0.0)    2 ( 1.3)  <0.001
              2      64 (27.8)   5 ( 20.0)   23 (14.6)        
              3      97 (42.2)  10 ( 40.0)   48 (30.6)        
              4      50 (21.7)  10 ( 40.0)   84 (53.5)        

### Multiple null level handling (levels defined in the strata but have no one)
dataFrame$brokenStatus <- factor(dataFrame$status, c(-1, 0:2, 3))
## 
catTable2 <- CreateCatTable(vars = vars, strata = "brokenStatus", data = dataFrame)
## summary method
summary(catTable2)
brokenStatus: -1
NULL
--------------------------------------------------------------------------------------- 
brokenStatus: 0
     var   n miss level freq percent cum.percent
     trt 232   64     1   83    49.4        49.4
                      2   85    50.6       100.0

     sex 232    0     m   17     7.3         7.3
                      f  215    92.7       100.0

 ascites 232   64     0  167    99.4        99.4
                      1    1     0.6       100.0

  hepato 232   64     0  108    64.3        64.3
                      1   60    35.7       100.0

 spiders 232   64     0  135    80.4        80.4
                      1   33    19.6       100.0

   edema 232    0     0  216    93.1        93.1
                    0.5   15     6.5        99.6
                      1    1     0.4       100.0

   stage 232    2     1   19     8.3         8.3
                      2   64    27.8        36.1
                      3   97    42.2        78.3
                      4   50    21.7       100.0

--------------------------------------------------------------------------------------- 
brokenStatus: 1
     var  n miss level freq percent cum.percent
     trt 25    6     1   10    52.6        52.6
                     2    9    47.4       100.0

     sex 25    0     m    3    12.0        12.0
                     f   22    88.0       100.0

 ascites 25    6     0   19   100.0       100.0
                     1    0     0.0       100.0

  hepato 25    6     0    7    36.8        36.8
                     1   12    63.2       100.0

 spiders 25    6     0   14    73.7        73.7
                     1    5    26.3       100.0

   edema 25    0     0   22    88.0        88.0
                   0.5    3    12.0       100.0
                     1    0     0.0       100.0

   stage 25    0     1    0     0.0         0.0
                     2    5    20.0        20.0
                     3   10    40.0        60.0
                     4   10    40.0       100.0

--------------------------------------------------------------------------------------- 
brokenStatus: 2
     var   n miss level freq percent cum.percent
     trt 161   36     1   65    52.0        52.0
                      2   60    48.0       100.0

     sex 161    0     m   24    14.9        14.9
                      f  137    85.1       100.0

 ascites 161   36     0  102    81.6        81.6
                      1   23    18.4       100.0

  hepato 161   36     0   37    29.6        29.6
                      1   88    70.4       100.0

 spiders 161   36     0   73    58.4        58.4
                      1   52    41.6       100.0

   edema 161    0     0  116    72.0        72.0
                    0.5   26    16.1        88.2
                      1   19    11.8       100.0

   stage 161    4     1    2     1.3         1.3
                      2   23    14.6        15.9
                      3   48    30.6        46.5
                      4   84    53.5       100.0

--------------------------------------------------------------------------------------- 
brokenStatus: 3
NULL

p-values
        pApprox         pExact
trt         NaN 0.884221883654
sex         NaN 0.041912591395
ascites     NaN 0.000000019153
hepato      NaN 0.000000007246
spiders     NaN 0.000194188617
edema       NaN 0.000000019547
stage       NaN             NA
## print method by default (approximation does not work with many zero cells)
print(catTable2, exact = TRUE)
                 Stratified by brokenStatus
                  -1 0           1          2           3 p      test 
  n               0  232         25         161         0             
  trt = 2 (%)     -   85 (50.6)   9 (47.4)   60 (48.0)  -  0.884 exact
  sex = f (%)     -  215 (92.7)  22 (88.0)  137 (85.1)  -  0.042 exact
  ascites = 1 (%) -    1 ( 0.6)   0 ( 0.0)   23 (18.4)  - <0.001 exact
  hepato = 1 (%)  -   60 (35.7)  12 (63.2)   88 (70.4)  - <0.001 exact
  spiders = 1 (%) -   33 (19.6)   5 (26.3)   52 (41.6)  - <0.001 exact
  edema (%)       -                                     - <0.001 exact
     0            -  216 (93.1)  22 (88.0)  116 (72.0)  -             
     0.5          -   15 ( 6.5)   3 (12.0)   26 (16.1)  -             
     1            -    1 ( 0.4)   0 ( 0.0)   19 (11.8)  -             
  stage (%)       -                                     -  NA    exact
     1            -   19 ( 8.3)   0 ( 0.0)    2 ( 1.3)  -             
     2            -   64 (27.8)   5 (20.0)   23 (14.6)  -             
     3            -   97 (42.2)  10 (40.0)   48 (30.6)  -             
     4            -   50 (21.7)  10 (40.0)   84 (53.5)  -             
## print method by default
print(catTable2, showAllLevels = TRUE, exact = TRUE)
             Stratified by brokenStatus
              level -1 0           1           2           3 p      test 
  n                 0  232         25          161         0             
  trt (%)     1     -   83 (49.4)  10 ( 52.6)   65 (52.0)  -  0.884 exact
              2     -   85 (50.6)   9 ( 47.4)   60 (48.0)  -             
  sex (%)     m     -   17 ( 7.3)   3 ( 12.0)   24 (14.9)  -  0.042 exact
              f     -  215 (92.7)  22 ( 88.0)  137 (85.1)  -             
  ascites (%) 0     -  167 (99.4)  19 (100.0)  102 (81.6)  - <0.001 exact
              1     -    1 ( 0.6)   0 (  0.0)   23 (18.4)  -             
  hepato (%)  0     -  108 (64.3)   7 ( 36.8)   37 (29.6)  - <0.001 exact
              1     -   60 (35.7)  12 ( 63.2)   88 (70.4)  -             
  spiders (%) 0     -  135 (80.4)  14 ( 73.7)   73 (58.4)  - <0.001 exact
              1     -   33 (19.6)   5 ( 26.3)   52 (41.6)  -             
  edema (%)   0     -  216 (93.1)  22 ( 88.0)  116 (72.0)  - <0.001 exact
              0.5   -   15 ( 6.5)   3 ( 12.0)   26 (16.1)  -             
              1     -    1 ( 0.4)   0 (  0.0)   19 (11.8)  -             
  stage (%)   1     -   19 ( 8.3)   0 (  0.0)    2 ( 1.3)  -  NA    exact
              2     -   64 (27.8)   5 ( 20.0)   23 (14.6)  -             
              3     -   97 (42.2)  10 ( 40.0)   48 (30.6)  -             
              4     -   50 (21.7)  10 ( 40.0)   84 (53.5)  -             
## Fix stratifying variable if it makes sense
dataFrame$fixedStatus <- factor(dataFrame$brokenStatus)
catTable2 <- CreateCatTable(vars = vars, strata = "fixedStatus", data = dataFrame)
summary(catTable2)
fixedStatus: 0
     var   n miss level freq percent cum.percent
     trt 232   64     1   83    49.4        49.4
                      2   85    50.6       100.0

     sex 232    0     m   17     7.3         7.3
                      f  215    92.7       100.0

 ascites 232   64     0  167    99.4        99.4
                      1    1     0.6       100.0

  hepato 232   64     0  108    64.3        64.3
                      1   60    35.7       100.0

 spiders 232   64     0  135    80.4        80.4
                      1   33    19.6       100.0

   edema 232    0     0  216    93.1        93.1
                    0.5   15     6.5        99.6
                      1    1     0.4       100.0

   stage 232    2     1   19     8.3         8.3
                      2   64    27.8        36.1
                      3   97    42.2        78.3
                      4   50    21.7       100.0

--------------------------------------------------------------------------------------- 
fixedStatus: 1
     var  n miss level freq percent cum.percent
     trt 25    6     1   10    52.6        52.6
                     2    9    47.4       100.0

     sex 25    0     m    3    12.0        12.0
                     f   22    88.0       100.0

 ascites 25    6     0   19   100.0       100.0
                     1    0     0.0       100.0

  hepato 25    6     0    7    36.8        36.8
                     1   12    63.2       100.0

 spiders 25    6     0   14    73.7        73.7
                     1    5    26.3       100.0

   edema 25    0     0   22    88.0        88.0
                   0.5    3    12.0       100.0
                     1    0     0.0       100.0

   stage 25    0     1    0     0.0         0.0
                     2    5    20.0        20.0
                     3   10    40.0        60.0
                     4   10    40.0       100.0

--------------------------------------------------------------------------------------- 
fixedStatus: 2
     var   n miss level freq percent cum.percent
     trt 161   36     1   65    52.0        52.0
                      2   60    48.0       100.0

     sex 161    0     m   24    14.9        14.9
                      f  137    85.1       100.0

 ascites 161   36     0  102    81.6        81.6
                      1   23    18.4       100.0

  hepato 161   36     0   37    29.6        29.6
                      1   88    70.4       100.0

 spiders 161   36     0   73    58.4        58.4
                      1   52    41.6       100.0

   edema 161    0     0  116    72.0        72.0
                    0.5   26    16.1        88.2
                      1   19    11.8       100.0

   stage 161    4     1    2     1.3         1.3
                      2   23    14.6        15.9
                      3   48    30.6        46.5
                      4   84    53.5       100.0


p-values
              pApprox         pExact
trt     0.89350974940 0.884221883654
sex     0.05344266826 0.041912591395
ascites 0.00000004847 0.000000019153
hepato  0.00000001808 0.000000007246
spiders 0.00021408606 0.000194188617
edema   0.00000003438 0.000000019547
stage   0.00000001118             NA

### Multivariable stratification
## Two variables
catTable4 <- CreateCatTable(vars = vars, strata = strataTwoVars, data = dataFrame)
summary(catTable4)
status: 0
sex: m
     var  n miss level freq percent cum.percent
     trt 17    6     1    4    36.4        36.4
                     2    7    63.6       100.0

     sex 17    0     m   17   100.0       100.0
                     f    0     0.0       100.0

 ascites 17    6     0   10    90.9        90.9
                     1    1     9.1       100.0

  hepato 17    6     0    4    36.4        36.4
                     1    7    63.6       100.0

 spiders 17    6     0    9    81.8        81.8
                     1    2    18.2       100.0

   edema 17    0     0   16    94.1        94.1
                   0.5    1     5.9       100.0
                     1    0     0.0       100.0

   stage 17    0     1    2    11.8        11.8
                     2    3    17.6        29.4
                     3    5    29.4        58.8
                     4    7    41.2       100.0

--------------------------------------------------------------------------------------- 
status: 1
sex: m
     var n miss level freq percent cum.percent
     trt 3    0     1    3   100.0       100.0
                    2    0     0.0       100.0

     sex 3    0     m    3   100.0       100.0
                    f    0     0.0       100.0

 ascites 3    0     0    3   100.0       100.0
                    1    0     0.0       100.0

  hepato 3    0     0    2    66.7        66.7
                    1    1    33.3       100.0

 spiders 3    0     0    3   100.0       100.0
                    1    0     0.0       100.0

   edema 3    0     0    3   100.0       100.0
                  0.5    0     0.0       100.0
                    1    0     0.0       100.0

   stage 3    0     1    0     0.0         0.0
                    2    0     0.0         0.0
                    3    2    66.7        66.7
                    4    1    33.3       100.0

--------------------------------------------------------------------------------------- 
status: 2
sex: m
     var  n miss level freq percent cum.percent
     trt 24    2     1   14    63.6        63.6
                     2    8    36.4       100.0

     sex 24    0     m   24   100.0       100.0
                     f    0     0.0       100.0

 ascites 24    2     0   20    90.9        90.9
                     1    2     9.1       100.0

  hepato 24    2     0    9    40.9        40.9
                     1   13    59.1       100.0

 spiders 24    2     0   20    90.9        90.9
                     1    2     9.1       100.0

   edema 24    0     0   17    70.8        70.8
                   0.5    4    16.7        87.5
                     1    3    12.5       100.0

   stage 24    0     1    1     4.2         4.2
                     2    5    20.8        25.0
                     3    9    37.5        62.5
                     4    9    37.5       100.0

--------------------------------------------------------------------------------------- 
status: 0
sex: f
     var   n miss level freq percent cum.percent
     trt 215   58     1   79    50.3        50.3
                      2   78    49.7       100.0

     sex 215    0     m    0     0.0         0.0
                      f  215   100.0       100.0

 ascites 215   58     0  157   100.0       100.0
                      1    0     0.0       100.0

  hepato 215   58     0  104    66.2        66.2
                      1   53    33.8       100.0

 spiders 215   58     0  126    80.3        80.3
                      1   31    19.7       100.0

   edema 215    0     0  200    93.0        93.0
                    0.5   14     6.5        99.5
                      1    1     0.5       100.0

   stage 215    2     1   17     8.0         8.0
                      2   61    28.6        36.6
                      3   92    43.2        79.8
                      4   43    20.2       100.0

--------------------------------------------------------------------------------------- 
status: 1
sex: f
     var  n miss level freq percent cum.percent
     trt 22    6     1    7    43.8        43.8
                     2    9    56.2       100.0

     sex 22    0     m    0     0.0         0.0
                     f   22   100.0       100.0

 ascites 22    6     0   16   100.0       100.0
                     1    0     0.0       100.0

  hepato 22    6     0    5    31.2        31.2
                     1   11    68.8       100.0

 spiders 22    6     0   11    68.8        68.8
                     1    5    31.2       100.0

   edema 22    0     0   19    86.4        86.4
                   0.5    3    13.6       100.0
                     1    0     0.0       100.0

   stage 22    0     1    0     0.0         0.0
                     2    5    22.7        22.7
                     3    8    36.4        59.1
                     4    9    40.9       100.0

--------------------------------------------------------------------------------------- 
status: 2
sex: f
     var   n miss level freq percent cum.percent
     trt 137   34     1   51    49.5        49.5
                      2   52    50.5       100.0

     sex 137    0     m    0     0.0         0.0
                      f  137   100.0       100.0

 ascites 137   34     0   82    79.6        79.6
                      1   21    20.4       100.0

  hepato 137   34     0   28    27.2        27.2
                      1   75    72.8       100.0

 spiders 137   34     0   53    51.5        51.5
                      1   50    48.5       100.0

   edema 137    0     0   99    72.3        72.3
                    0.5   22    16.1        88.3
                      1   16    11.7       100.0

   stage 137    4     1    1     0.8         0.8
                      2   18    13.5        14.3
                      3   39    29.3        43.6
                      4   75    56.4       100.0


p-values
          pApprox    pExact
trt     3.396e-01 3.872e-01
sex     3.910e-88 1.293e-60
ascites 3.538e-07 3.965e-08
hepato  5.988e-08 7.539e-09
spiders 6.333e-06 4.754e-06
edema   1.099e-05        NA
stage   8.605e-07        NA
print(catTable4, exact = exactVars)
                 Stratified by status:sex
                  0:m        1:m        2:m        0:f          1:f         2:f          p      test 
  n               17         3          24         215          22          137                      
  trt = 2 (%)      7 (63.6)  0 (  0.0)   8 (36.4)   78 ( 49.7)   9 ( 56.2)   52 ( 50.5)   0.340      
  sex = f (%)      0 ( 0.0)  0 (  0.0)   0 ( 0.0)  215 (100.0)  22 (100.0)  137 (100.0)  <0.001      
  ascites = 1 (%)  1 ( 9.1)  0 (  0.0)   2 ( 9.1)    0 (  0.0)   0 (  0.0)   21 ( 20.4)  <0.001 exact
  hepato = 1 (%)   7 (63.6)  1 ( 33.3)  13 (59.1)   53 ( 33.8)  11 ( 68.8)   75 ( 72.8)  <0.001      
  spiders = 1 (%)  2 (18.2)  0 (  0.0)   2 ( 9.1)   31 ( 19.7)   5 ( 31.2)   50 ( 48.5)  <0.001      
  edema (%)                                                                               NA    exact
     0            16 (94.1)  3 (100.0)  17 (70.8)  200 ( 93.0)  19 ( 86.4)   99 ( 72.3)              
     0.5           1 ( 5.9)  0 (  0.0)   4 (16.7)   14 (  6.5)   3 ( 13.6)   22 ( 16.1)              
     1             0 ( 0.0)  0 (  0.0)   3 (12.5)    1 (  0.5)   0 (  0.0)   16 ( 11.7)              
  stage (%)                                                                              <0.001      
     1             2 (11.8)  0 (  0.0)   1 ( 4.2)   17 (  8.0)   0 (  0.0)    1 (  0.8)              
     2             3 (17.6)  0 (  0.0)   5 (20.8)   61 ( 28.6)   5 ( 22.7)   18 ( 13.5)              
     3             5 (29.4)  2 ( 66.7)   9 (37.5)   92 ( 43.2)   8 ( 36.4)   39 ( 29.3)              
     4             7 (41.2)  1 ( 33.3)   9 (37.5)   43 ( 20.2)   9 ( 40.9)   75 ( 56.4)              
print(catTable4, showAllLevels = TRUE)
             Stratified by status:sex
              level 0:m         1:m        2:m         0:f          1:f         2:f          p     
  n                 17          3          24          215          22          137                
  trt (%)     1      4 ( 36.4)  3 (100.0)  14 ( 63.6)   79 ( 50.3)   7 ( 43.8)   51 ( 49.5)   0.340
              2      7 ( 63.6)  0 (  0.0)   8 ( 36.4)   78 ( 49.7)   9 ( 56.2)   52 ( 50.5)        
  sex (%)     m     17 (100.0)  3 (100.0)  24 (100.0)    0 (  0.0)   0 (  0.0)    0 (  0.0)  <0.001
              f      0 (  0.0)  0 (  0.0)   0 (  0.0)  215 (100.0)  22 (100.0)  137 (100.0)        
  ascites (%) 0     10 ( 90.9)  3 (100.0)  20 ( 90.9)  157 (100.0)  16 (100.0)   82 ( 79.6)  <0.001
              1      1 (  9.1)  0 (  0.0)   2 (  9.1)    0 (  0.0)   0 (  0.0)   21 ( 20.4)        
  hepato (%)  0      4 ( 36.4)  2 ( 66.7)   9 ( 40.9)  104 ( 66.2)   5 ( 31.2)   28 ( 27.2)  <0.001
              1      7 ( 63.6)  1 ( 33.3)  13 ( 59.1)   53 ( 33.8)  11 ( 68.8)   75 ( 72.8)        
  spiders (%) 0      9 ( 81.8)  3 (100.0)  20 ( 90.9)  126 ( 80.3)  11 ( 68.8)   53 ( 51.5)  <0.001
              1      2 ( 18.2)  0 (  0.0)   2 (  9.1)   31 ( 19.7)   5 ( 31.2)   50 ( 48.5)        
  edema (%)   0     16 ( 94.1)  3 (100.0)  17 ( 70.8)  200 ( 93.0)  19 ( 86.4)   99 ( 72.3)  <0.001
              0.5    1 (  5.9)  0 (  0.0)   4 ( 16.7)   14 (  6.5)   3 ( 13.6)   22 ( 16.1)        
              1      0 (  0.0)  0 (  0.0)   3 ( 12.5)    1 (  0.5)   0 (  0.0)   16 ( 11.7)        
  stage (%)   1      2 ( 11.8)  0 (  0.0)   1 (  4.2)   17 (  8.0)   0 (  0.0)    1 (  0.8)  <0.001
              2      3 ( 17.6)  0 (  0.0)   5 ( 20.8)   61 ( 28.6)   5 ( 22.7)   18 ( 13.5)        
              3      5 ( 29.4)  2 ( 66.7)   9 ( 37.5)   92 ( 43.2)   8 ( 36.4)   39 ( 29.3)        
              4      7 ( 41.2)  1 ( 33.3)   9 ( 37.5)   43 ( 20.2)   9 ( 40.9)   75 ( 56.4)        
## Three variables
catTable5 <- CreateCatTable(vars = vars, strata = strataThreeVars, data = dataFrame)
summary(catTable5)
sex: m
ascites: 0
hepato: 0
     var  n miss level freq percent cum.percent
     trt 15    0     1    9    60.0        60.0
                     2    6    40.0       100.0

     sex 15    0     m   15   100.0       100.0
                     f    0     0.0       100.0

 ascites 15    0     0   15   100.0       100.0
                     1    0     0.0       100.0

  hepato 15    0     0   15   100.0       100.0
                     1    0     0.0       100.0

 spiders 15    0     0   14    93.3        93.3
                     1    1     6.7       100.0

   edema 15    0     0   13    86.7        86.7
                   0.5    1     6.7        93.3
                     1    1     6.7       100.0

   stage 15    0     1    3    20.0        20.0
                     2    3    20.0        40.0
                     3    7    46.7        86.7
                     4    2    13.3       100.0

--------------------------------------------------------------------------------------- 
sex: f
ascites: 0
hepato: 0
     var   n miss level freq percent cum.percent
     trt 132    0     1   73    55.3        55.3
                      2   59    44.7       100.0

     sex 132    0     m    0     0.0         0.0
                      f  132   100.0       100.0

 ascites 132    0     0  132   100.0       100.0
                      1    0     0.0       100.0

  hepato 132    0     0  132   100.0       100.0
                      1    0     0.0       100.0

 spiders 132    0     0  111    84.1        84.1
                      1   21    15.9       100.0

   edema 132    0     0  123    93.2        93.2
                    0.5    8     6.1        99.2
                      1    1     0.8       100.0

   stage 132    0     1   13     9.8         9.8
                      2   45    34.1        43.9
                      3   59    44.7        88.6
                      4   15    11.4       100.0

--------------------------------------------------------------------------------------- 
sex: m
ascites: 1
hepato: 0
NULL
--------------------------------------------------------------------------------------- 
sex: f
ascites: 1
hepato: 0
     var n miss level freq percent cum.percent
     trt 5    0     1    3    60.0        60.0
                    2    2    40.0       100.0

     sex 5    0     m    0     0.0         0.0
                    f    5   100.0       100.0

 ascites 5    0     0    0     0.0         0.0
                    1    5   100.0       100.0

  hepato 5    0     0    5   100.0       100.0
                    1    0     0.0       100.0

 spiders 5    0     0    4    80.0        80.0
                    1    1    20.0       100.0

   edema 5    0     0    2    40.0        40.0
                  0.5    0     0.0        40.0
                    1    3    60.0       100.0

   stage 5    0     1    0     0.0         0.0
                    2    0     0.0         0.0
                    3    1    20.0        20.0
                    4    4    80.0       100.0

--------------------------------------------------------------------------------------- 
sex: m
ascites: 0
hepato: 1
     var  n miss level freq percent cum.percent
     trt 18    0     1   11    61.1        61.1
                     2    7    38.9       100.0

     sex 18    0     m   18   100.0       100.0
                     f    0     0.0       100.0

 ascites 18    0     0   18   100.0       100.0
                     1    0     0.0       100.0

  hepato 18    0     0    0     0.0         0.0
                     1   18   100.0       100.0

 spiders 18    0     0   16    88.9        88.9
                     1    2    11.1       100.0

   edema 18    0     0   15    83.3        83.3
                   0.5    3    16.7       100.0
                     1    0     0.0       100.0

   stage 18    0     1    0     0.0         0.0
                     2    3    16.7        16.7
                     3    5    27.8        44.4
                     4   10    55.6       100.0

--------------------------------------------------------------------------------------- 
sex: f
ascites: 0
hepato: 1
     var   n miss level freq percent cum.percent
     trt 123    0     1   51    41.5        41.5
                      2   72    58.5       100.0

     sex 123    0     m    0     0.0         0.0
                      f  123   100.0       100.0

 ascites 123    0     0  123   100.0       100.0
                      1    0     0.0       100.0

  hepato 123    0     0    0     0.0         0.0
                      1  123   100.0       100.0

 spiders 123    0     0   70    56.9        56.9
                      1   53    43.1       100.0

   edema 123    0     0  106    86.2        86.2
                    0.5   13    10.6        96.7
                      1    4     3.3       100.0

   stage 123    0     1    0     0.0         0.0
                      2   14    11.4        11.4
                      3   48    39.0        50.4
                      4   61    49.6       100.0

--------------------------------------------------------------------------------------- 
sex: m
ascites: 1
hepato: 1
     var n miss level freq percent cum.percent
     trt 3    0     1    1    33.3        33.3
                    2    2    66.7       100.0

     sex 3    0     m    3   100.0       100.0
                    f    0     0.0       100.0

 ascites 3    0     0    0     0.0         0.0
                    1    3   100.0       100.0

  hepato 3    0     0    0     0.0         0.0
                    1    3   100.0       100.0

 spiders 3    0     0    2    66.7        66.7
                    1    1    33.3       100.0

   edema 3    0     0    1    33.3        33.3
                  0.5    0     0.0        33.3
                    1    2    66.7       100.0

   stage 3    0     1    0     0.0         0.0
                    2    0     0.0         0.0
                    3    0     0.0         0.0
                    4    3   100.0       100.0

--------------------------------------------------------------------------------------- 
sex: f
ascites: 1
hepato: 1
     var  n miss level freq percent cum.percent
     trt 16    0     1   10    62.5        62.5
                     2    6    37.5       100.0

     sex 16    0     m    0     0.0         0.0
                     f   16   100.0       100.0

 ascites 16    0     0    0     0.0         0.0
                     1   16   100.0       100.0

  hepato 16    0     0    0     0.0         0.0
                     1   16   100.0       100.0

 spiders 16    0     0    5    31.2        31.2
                     1   11    68.8       100.0

   edema 16    0     0    3    18.8        18.8
                   0.5    4    25.0        43.8
                     1    9    56.2       100.0

   stage 16    0     1    0     0.0         0.0
                     2    2    12.5        12.5
                     3    0     0.0        12.5
                     4   14    87.5       100.0


p-values
        pApprox    pExact
trt         NaN 2.142e-01
sex         NaN 9.868e-48
ascites     NaN 2.124e-36
hepato      NaN 2.941e-93
spiders     NaN        NA
edema       NaN        NA
stage       NaN        NA
print(catTable5, exact = exactVars)
                 Stratified by sex:ascites:hepato
                  m:0:0      f:0:0        m:1:0 f:1:0      m:0:1       f:0:1        m:1:1      f:1:1       p     
  n               15         132          0     5          18          123          3          16                
  trt = 2 (%)      6 (40.0)   59 ( 44.7)  -     2 ( 40.0)   7 ( 38.9)   72 ( 58.5)  2 ( 66.7)   6 ( 37.5)   NaN  
  sex = f (%)      0 ( 0.0)  132 (100.0)  -     5 (100.0)   0 (  0.0)  123 (100.0)  0 (  0.0)  16 (100.0)   NaN  
  ascites = 1 (%)  0 ( 0.0)    0 (  0.0)  -     5 (100.0)   0 (  0.0)    0 (  0.0)  3 (100.0)  16 (100.0)  <0.001
  hepato = 1 (%)   0 ( 0.0)    0 (  0.0)  -     0 (  0.0)  18 (100.0)  123 (100.0)  3 (100.0)  16 (100.0)   NaN  
  spiders = 1 (%)  1 ( 6.7)   21 ( 15.9)  -     1 ( 20.0)   2 ( 11.1)   53 ( 43.1)  1 ( 33.3)  11 ( 68.8)   NaN  
  edema (%)                               -                                                                 NA   
     0            13 (86.7)  123 ( 93.2)  -     2 ( 40.0)  15 ( 83.3)  106 ( 86.2)  1 ( 33.3)   3 ( 18.8)        
     0.5           1 ( 6.7)    8 (  6.1)  -     0 (  0.0)   3 ( 16.7)   13 ( 10.6)  0 (  0.0)   4 ( 25.0)        
     1             1 ( 6.7)    1 (  0.8)  -     3 ( 60.0)   0 (  0.0)    4 (  3.3)  2 ( 66.7)   9 ( 56.2)        
  stage (%)                               -                                                                 NaN  
     1             3 (20.0)   13 (  9.8)  -     0 (  0.0)   0 (  0.0)    0 (  0.0)  0 (  0.0)   0 (  0.0)        
     2             3 (20.0)   45 ( 34.1)  -     0 (  0.0)   3 ( 16.7)   14 ( 11.4)  0 (  0.0)   2 ( 12.5)        
     3             7 (46.7)   59 ( 44.7)  -     1 ( 20.0)   5 ( 27.8)   48 ( 39.0)  0 (  0.0)   0 (  0.0)        
     4             2 (13.3)   15 ( 11.4)  -     4 ( 80.0)  10 ( 55.6)   61 ( 49.6)  3 (100.0)  14 ( 87.5)        
                 Stratified by sex:ascites:hepato
                  test 
  n                    
  trt = 2 (%)          
  sex = f (%)          
  ascites = 1 (%) exact
  hepato = 1 (%)       
  spiders = 1 (%)      
  edema (%)       exact
     0                 
     0.5               
     1                 
  stage (%)            
     1                 
     2                 
     3                 
     4                 

### CrossTable option to use gmodels::CrossTable()
catTable6 <- CreateCatTable(vars = vars[1:2], strata = strataTwoVars, data = dataFrame)
print(catTable6, CrossTable = TRUE, showAllLevels = TRUE)
         Stratified by status:sex
          level 0:m         1:m        2:m         0:f          1:f         2:f          p     
  n             17          3          24          215          22          137                
  trt (%) 1      4 ( 36.4)  3 (100.0)  14 ( 63.6)   79 ( 50.3)   7 ( 43.8)   51 ( 49.5)   0.340
          2      7 ( 63.6)  0 (  0.0)   8 ( 36.4)   78 ( 49.7)   9 ( 56.2)   52 ( 50.5)        
  sex (%) m     17 (100.0)  3 (100.0)  24 (100.0)    0 (  0.0)   0 (  0.0)    0 (  0.0)  <0.001
          f      0 (  0.0)  0 (  0.0)   0 (  0.0)  215 (100.0)  22 (100.0)  137 (100.0)        


   Cell Contents
|-------------------------|
|                       N |
| Chi-square contribution |
|           N / Row Total |
|           N / Col Total |
|         N / Table Total |
|-------------------------|


Total Observations in Table:  312 


             | status:sex 
         trt |       0:m |       1:m |       2:m |       0:f |       1:f |       2:f | Row Total | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|
           1 |         4 |         3 |        14 |        79 |         7 |        51 |       158 | 
             |     0.443 |     1.443 |     0.734 |     0.003 |     0.150 |     0.026 |           | 
             |     0.025 |     0.019 |     0.089 |     0.500 |     0.044 |     0.323 |     0.506 | 
             |     0.364 |     1.000 |     0.636 |     0.503 |     0.438 |     0.495 |           | 
             |     0.013 |     0.010 |     0.045 |     0.253 |     0.022 |     0.163 |           | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|
           2 |         7 |         0 |         8 |        78 |         9 |        52 |       154 | 
             |     0.454 |     1.481 |     0.753 |     0.003 |     0.154 |     0.026 |           | 
             |     0.045 |     0.000 |     0.052 |     0.506 |     0.058 |     0.338 |     0.494 | 
             |     0.636 |     0.000 |     0.364 |     0.497 |     0.562 |     0.505 |           | 
             |     0.022 |     0.000 |     0.026 |     0.250 |     0.029 |     0.167 |           | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|
Column Total |        11 |         3 |        22 |       157 |        16 |       103 |       312 | 
             |     0.035 |     0.010 |     0.071 |     0.503 |     0.051 |     0.330 |           | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|




   Cell Contents
|-------------------------|
|                       N |
| Chi-square contribution |
|           N / Row Total |
|           N / Col Total |
|         N / Table Total |
|-------------------------|


Total Observations in Table:  418 


             | status:sex 
         sex |       0:m |       1:m |       2:m |       0:f |       1:f |       2:f | Row Total | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|
           m |        17 |         3 |        24 |         0 |         0 |         0 |        44 | 
             |   129.289 |    22.816 |   182.526 |    22.632 |     2.316 |    14.421 |           | 
             |     0.386 |     0.068 |     0.545 |     0.000 |     0.000 |     0.000 |     0.105 | 
             |     1.000 |     1.000 |     1.000 |     0.000 |     0.000 |     0.000 |           | 
             |     0.041 |     0.007 |     0.057 |     0.000 |     0.000 |     0.000 |           | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|
           f |         0 |         0 |         0 |       215 |        22 |       137 |       374 | 
             |    15.211 |     2.684 |    21.474 |     2.663 |     0.272 |     1.697 |           | 
             |     0.000 |     0.000 |     0.000 |     0.575 |     0.059 |     0.366 |     0.895 | 
             |     0.000 |     0.000 |     0.000 |     1.000 |     1.000 |     1.000 |           | 
             |     0.000 |     0.000 |     0.000 |     0.514 |     0.053 |     0.328 |           | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|
Column Total |        17 |         3 |        24 |       215 |        22 |       137 |       418 | 
             |     0.041 |     0.007 |     0.057 |     0.514 |     0.053 |     0.328 |           | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|


print(catTable6, showAllLevels = TRUE)
         Stratified by status:sex
          level 0:m         1:m        2:m         0:f          1:f         2:f          p     
  n             17          3          24          215          22          137                
  trt (%) 1      4 ( 36.4)  3 (100.0)  14 ( 63.6)   79 ( 50.3)   7 ( 43.8)   51 ( 49.5)   0.340
          2      7 ( 63.6)  0 (  0.0)   8 ( 36.4)   78 ( 49.7)   9 ( 56.2)   52 ( 50.5)        
  sex (%) m     17 (100.0)  3 (100.0)  24 (100.0)    0 (  0.0)   0 (  0.0)    0 (  0.0)  <0.001
          f      0 (  0.0)  0 (  0.0)   0 (  0.0)  215 (100.0)  22 (100.0)  137 (100.0)        

### Check attributes
attributes(catTable6)
$dim
[1] 3 2

$dimnames
$dimnames$status
[1] "0" "1" "2"

$dimnames$sex
[1] "m" "f"


$call
by.data.frame(data = dat, INDICES = strata, FUN = function(dfStrataDat) {
    sapply(dfStrataDat, FUN = CreateTableForOneVar, simplify = FALSE)
}, simplify = FALSE)

$class
[1] "CatTable" "by"      

$strataVarName
[1] "status:sex"

$pValues
      pApprox    pExact
trt 3.396e-01 3.872e-01
sex 3.910e-88 1.293e-60

$xtabs
$xtabs$trt
   status:sex
trt 0:m 1:m 2:m 0:f 1:f 2:f
  1   4   3  14  79   7  51
  2   7   0   8  78   9  52

$xtabs$sex
   status:sex
sex 0:m 1:m 2:m 0:f 1:f 2:f
  m  17   3  24   0   0   0
  f   0   0   0 215  22 137