tableone CreateCatTable() test (raw code)

What is tableone?

### Load dataset
library(survival)
data(pbc)
head(pbc)
## 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)

## Download necessary scripts
repoUrl <- "https://raw.github.com/kaz-yos/tableone/master/R/"
fileNames <- c("CreateContTable.R","print.ContTable.R","summary.ContTable.R")
download.file(url = paste0(repoUrl, fileNames[1]), destfile = fileNames[1], method = "wget")
download.file(url = paste0(repoUrl, fileNames[2]), destfile = fileNames[2], method = "wget")
download.file(url = paste0(repoUrl, fileNames[3]), destfile = fileNames[3], method = "wget")

## Source them
source("./CreateCatTable.R")
source("./print.CatTable.R")
source("./summary.CatTable.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)
## print method by default
print(catTable1)
## Format (percent only)
print(catTable1, format = "p")
## Format (frequency only, no explanation)
print(catTable1, format = "f", explain = FALSE)

### Behaviors on correct data format (stratified)
## Creation
catTable2 <- CreateCatTable(vars = vars, strata = strata, data = dataFrame)
## summary method
summary(catTable2)
## print method by default
print(catTable2, exact = exactVars)


### Multivariable stratification
## Two variables
catTable4 <- CreateCatTable(vars = vars, strata = strataTwoVars, data = dataFrame)
summary(catTable4)
print(catTable4, exact = exactVars)
## Three variables
catTable5 <- CreateCatTable(vars = vars, strata = strataThreeVars, data = dataFrame)
summary(catTable5)
print(catTable5, exact = exactVars)