Created
April 28, 2020 15:13
-
-
Save PatrickRWright/4e38dd04f7ff15d25c1558af32181cf5 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Create Table One" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The `CreateTableOne()` function from the `tableone` package is a handy tool to get an overview of your data while allowing stratification (i.e. grouping)\n", | |
"based on certrain categorical variables. Lets have a look at the `mtcars` data set and stratify by transmssion type (`am`: 0 = automatic, 1 = manual) and engine type (`vs`: 0 = V-shaped, 1 = straight)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"library(tableone)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table>\n", | |
"<caption>A data.frame: 3 × 11</caption>\n", | |
"<thead>\n", | |
"\t<tr><th></th><th scope=col>mpg</th><th scope=col>cyl</th><th scope=col>disp</th><th scope=col>hp</th><th scope=col>drat</th><th scope=col>wt</th><th scope=col>qsec</th><th scope=col>vs</th><th scope=col>am</th><th scope=col>gear</th><th scope=col>carb</th></tr>\n", | |
"\t<tr><th></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", | |
"</thead>\n", | |
"<tbody>\n", | |
"\t<tr><th scope=row>Mazda RX4</th><td>21.0</td><td>6</td><td>160</td><td>110</td><td>3.90</td><td>2.620</td><td>16.46</td><td>0</td><td>1</td><td>4</td><td>4</td></tr>\n", | |
"\t<tr><th scope=row>Mazda RX4 Wag</th><td>21.0</td><td>6</td><td>160</td><td>110</td><td>3.90</td><td>2.875</td><td>17.02</td><td>0</td><td>1</td><td>4</td><td>4</td></tr>\n", | |
"\t<tr><th scope=row>Datsun 710</th><td>22.8</td><td>4</td><td>108</td><td> 93</td><td>3.85</td><td>2.320</td><td>18.61</td><td>1</td><td>1</td><td>4</td><td>1</td></tr>\n", | |
"</tbody>\n", | |
"</table>\n" | |
], | |
"text/latex": [ | |
"A data.frame: 3 × 11\n", | |
"\\begin{tabular}{r|lllllllllll}\n", | |
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n", | |
" & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl>\\\\\n", | |
"\\hline\n", | |
"\tMazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\\\\n", | |
"\tMazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\\\\n", | |
"\tDatsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1\\\\\n", | |
"\\end{tabular}\n" | |
], | |
"text/markdown": [ | |
"\n", | |
"A data.frame: 3 × 11\n", | |
"\n", | |
"| <!--/--> | mpg <dbl> | cyl <dbl> | disp <dbl> | hp <dbl> | drat <dbl> | wt <dbl> | qsec <dbl> | vs <dbl> | am <dbl> | gear <dbl> | carb <dbl> |\n", | |
"|---|---|---|---|---|---|---|---|---|---|---|---|\n", | |
"| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |\n", | |
"| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |\n", | |
"| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |\n", | |
"\n" | |
], | |
"text/plain": [ | |
" mpg cyl disp hp drat wt qsec vs am gear carb\n", | |
"Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 \n", | |
"Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 \n", | |
"Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 " | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"# show the mtcars structure\n", | |
"head(mtcars, n = 3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"vars <- c(\"mpg\", \"cyl\", \"disp\", \"hp\", \"drat\", \"wt\", \"qsec\", \"gear\", \"carb\")\n", | |
"# specify stratification variables\n", | |
"strata <- c(\"am\", \"vs\")\n", | |
"# specify categorical variables\n", | |
"cat_vars <- c(\"cyl\", \"gear\", \"carb\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
" Stratified by am:vs\n", | |
" 0:0 1:0 0:1 1:1 \n", | |
" n 12 6 7 7 \n", | |
" mpg (mean (SD)) 15.05 (2.77) 19.75 (4.01) 20.74 (2.47) 28.37 (4.76) \n", | |
" cyl (%) \n", | |
" 4 0 ( 0.0) 1 (16.7) 3 (42.9) 7 (100.0) \n", | |
" 6 0 ( 0.0) 3 (50.0) 4 (57.1) 0 ( 0.0) \n", | |
" 8 12 (100.0) 2 (33.3) 0 ( 0.0) 0 ( 0.0) \n", | |
" disp (mean (SD)) 357.62 (71.82) 206.22 (95.23) 175.11 (49.13) 89.80 (18.80) \n", | |
" hp (mean (SD)) 194.17 (33.36) 180.83 (98.82) 102.14 (20.93) 80.57 (24.14) \n", | |
" drat (mean (SD)) 3.12 (0.23) 3.94 (0.34) 3.57 (0.46) 4.15 (0.38) \n", | |
" wt (mean (SD)) 4.10 (0.77) 2.86 (0.49) 3.19 (0.35) 2.03 (0.44) \n", | |
" qsec (mean (SD)) 17.14 (0.80) 15.80 (1.09) 19.97 (1.46) 18.70 (0.95) \n", | |
" gear (%) \n", | |
" 3 12 (100.0) 0 ( 0.0) 3 (42.9) 0 ( 0.0) \n", | |
" 4 0 ( 0.0) 2 (33.3) 4 (57.1) 6 ( 85.7) \n", | |
" 5 0 ( 0.0) 4 (66.7) 0 ( 0.0) 1 ( 14.3) \n", | |
" carb (%) \n", | |
" 1 0 ( 0.0) 0 ( 0.0) 3 (42.9) 4 ( 57.1) \n", | |
" 2 4 ( 33.3) 1 (16.7) 2 (28.6) 3 ( 42.9) \n", | |
" 3 3 ( 25.0) 0 ( 0.0) 0 ( 0.0) 0 ( 0.0) \n", | |
" 4 5 ( 41.7) 3 (50.0) 2 (28.6) 0 ( 0.0) \n", | |
" 6 0 ( 0.0) 1 (16.7) 0 ( 0.0) 0 ( 0.0) \n", | |
" 8 0 ( 0.0) 1 (16.7) 0 ( 0.0) 0 ( 0.0) " | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"# CreateTableOne\n", | |
"CreateTableOne(data = mtcars, vars = vars, strata = strata,\n", | |
" factorVars = cat_vars, test = FALSE)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## From this summary table you can draw preliminary conclusions such as:\n", | |
"* cars with automatic transmission and V-shaped engines exhibit the lowest milage per gallon (`mpg`)\n", | |
"* cars with straight engines and manual transmission are likely designed to have less cylinders (`cyl`)\n", | |
"* the fastest cars on the quarter mile speed test (`qsec`) have a manual transmission and a V-shaped motor" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Assume you would like the have the median an IQR displayed for the horsepower (`hp`) and weight (`wt`)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"For this you need to adjust the print options `TableOne` object. Please note that the table is wrapped into two \"lines\" because of the size of the returned table." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" Stratified by am:vs\n", | |
" 0:0 1:0 \n", | |
" n 12 6 \n", | |
" mpg (mean (SD)) 15.05 (2.77) 19.75 (4.01) \n", | |
" cyl (%) \n", | |
" 4 0 ( 0.0) 1 (16.7) \n", | |
" 6 0 ( 0.0) 3 (50.0) \n", | |
" 8 12 (100.0) 2 (33.3) \n", | |
" disp (mean (SD)) 357.62 (71.82) 206.22 (95.23) \n", | |
" hp (median [IQR]) 180.00 [175.00, 218.75] 142.50 [110.00, 241.75]\n", | |
" drat (mean (SD)) 3.12 (0.23) 3.94 (0.34) \n", | |
" wt (median [IQR]) 3.81 [3.56, 4.37] 2.82 [2.66, 3.10] \n", | |
" qsec (mean (SD)) 17.14 (0.80) 15.80 (1.09) \n", | |
" gear (%) \n", | |
" 3 12 (100.0) 0 ( 0.0) \n", | |
" 4 0 ( 0.0) 2 (33.3) \n", | |
" 5 0 ( 0.0) 4 (66.7) \n", | |
" carb (%) \n", | |
" 1 0 ( 0.0) 0 ( 0.0) \n", | |
" 2 4 ( 33.3) 1 (16.7) \n", | |
" 3 3 ( 25.0) 0 ( 0.0) \n", | |
" 4 5 ( 41.7) 3 (50.0) \n", | |
" 6 0 ( 0.0) 1 (16.7) \n", | |
" 8 0 ( 0.0) 1 (16.7) \n", | |
" Stratified by am:vs\n", | |
" 0:1 1:1 \n", | |
" n 7 7 \n", | |
" mpg (mean (SD)) 20.74 (2.47) 28.37 (4.76) \n", | |
" cyl (%) \n", | |
" 4 3 (42.9) 7 (100.0) \n", | |
" 6 4 (57.1) 0 ( 0.0) \n", | |
" 8 0 ( 0.0) 0 ( 0.0) \n", | |
" disp (mean (SD)) 175.11 (49.13) 89.80 (18.80) \n", | |
" hp (median [IQR]) 105.00 [96.00, 116.50] 66.00 [65.50, 101.00]\n", | |
" drat (mean (SD)) 3.57 (0.46) 4.15 (0.38) \n", | |
" wt (median [IQR]) 3.21 [3.17, 3.44] 1.94 [1.73, 2.26] \n", | |
" qsec (mean (SD)) 19.97 (1.46) 18.70 (0.95) \n", | |
" gear (%) \n", | |
" 3 3 (42.9) 0 ( 0.0) \n", | |
" 4 4 (57.1) 6 ( 85.7) \n", | |
" 5 0 ( 0.0) 1 ( 14.3) \n", | |
" carb (%) \n", | |
" 1 3 (42.9) 4 ( 57.1) \n", | |
" 2 2 (28.6) 3 ( 42.9) \n", | |
" 3 0 ( 0.0) 0 ( 0.0) \n", | |
" 4 2 (28.6) 0 ( 0.0) \n", | |
" 6 0 ( 0.0) 0 ( 0.0) \n", | |
" 8 0 ( 0.0) 0 ( 0.0) \n" | |
] | |
} | |
], | |
"source": [ | |
"# store the object in a variable (t1)\n", | |
"t1 <- CreateTableOne(data = mtcars, vars = vars, strata = strata,\n", | |
" factorVars = cat_vars, test = FALSE)\n", | |
"\n", | |
"# adjust print options\n", | |
"print(t1, nonnormal = c(\"hp\", \"wt\"), test = FALSE)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"A more comprehensive resource can be found [here](https://cran.r-project.org/web/packages/tableone/vignettes/introduction.html)." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "R", | |
"language": "R", | |
"name": "conda-env-r-r" | |
}, | |
"language_info": { | |
"codemirror_mode": "r", | |
"file_extension": ".r", | |
"mimetype": "text/x-r-source", | |
"name": "R", | |
"pygments_lexer": "r", | |
"version": "3.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment