Summary statistics using rpsychi package

## Settings for RMarkdown http://yihui.name/knitr/options#chunk_options
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, tidy = FALSE, 
    echo = TRUE, fig.width = 7, fig.height = 7)
options(width = 120, scipen = 10)

setwd("~/statistics/Rmedstats/")

Load rpsychi

library(rpsychi)

Data preparation

data(infert)
infert$case <- factor(infert$case, labels=c("control", "case"))
infert$induced <- factor(infert$induced, labels=c("0","1","2 or more"))
infert$spontaneous <- factor(infert$spontaneous, labels=c("0","1","2 or more"))

summary(infert)
   education        age           parity          induced         case        spontaneous     stratum    
 0-5yrs : 12   Min.   :21.0   Min.   :1.00   0        :143   control:165   0        :141   Min.   : 1.0  
 6-11yrs:120   1st Qu.:28.0   1st Qu.:1.00   1        : 68   case   : 83   1        : 71   1st Qu.:21.0  
 12+ yrs:116   Median :31.0   Median :2.00   2 or more: 37                 2 or more: 36   Median :42.0  
               Mean   :31.5   Mean   :2.09                                                 Mean   :41.9  
               3rd Qu.:35.2   3rd Qu.:3.00                                                 3rd Qu.:62.2  
               Max.   :44.0   Max.   :6.00                                                 Max.   :83.0  
 pooled.stratum
 Min.   : 1.0  
 1st Qu.:19.0  
 Median :36.0  
 Mean   :33.6  
 3rd Qu.:48.2  
 Max.   :63.0  
head(infert)
  education age parity   induced case spontaneous stratum pooled.stratum
1    0-5yrs  26      6         1 case   2 or more       1              3
2    0-5yrs  42      1         1 case           0       2              1
3    0-5yrs  39      6 2 or more case           0       3              4
4    0-5yrs  34      4 2 or more case           0       4              2
5   6-11yrs  35      3         1 case           1       5             32
6   6-11yrs  36      4 2 or more case           1       6             36

Summarize

##continuous and categorical variables
groupSummary(infert, group = "case")
                      total.m/f total.sd/pct total.n control.m/f control.sd/pct control.n case.m/f case.sd/pct case.n
education.0-5yrs         12.000        4.839     248       8.000          4.848       165    4.000       4.819     83
education.6-11yrs       120.000       48.387      NA      80.000         48.485        NA   40.000      48.193     NA
education.12+ yrs       116.000       46.774      NA      77.000         46.667        NA   39.000      46.988     NA
age                      31.504        5.241     248      31.491          5.238       165   31.530       5.247     83
parity                    2.093        1.249     248       2.085          1.238       165    2.108       1.271     83
induced.0               143.000       57.661     248      96.000         58.182       165   47.000      56.627     83
induced.1                68.000       27.419      NA      45.000         27.273        NA   23.000      27.711     NA
induced.2 or more        37.000       14.919      NA      24.000         14.545        NA   13.000      15.663     NA
spontaneous.0           141.000       56.855     248     113.000         68.485       165   28.000      33.735     83
spontaneous.1            71.000       28.629      NA      40.000         24.242        NA   31.000      37.349     NA
spontaneous.2 or more    36.000       14.516      NA      12.000          7.273        NA   24.000      28.916     NA
stratum                  41.871       23.920     248      41.806         23.901       165   42.000      23.958     83
pooled.stratum           33.581       17.237     248      33.521         17.204       165   33.699      17.303     83

##continuous variables only
groupSummary(infert[, c(2,3,7,8, 5)], group = "case")
               total.m total.sd total.n control.m control.sd control.n case.m case.sd case.n
age             31.504    5.241     248    31.491      5.238       165 31.530   5.247     83
parity           2.093    1.249     248     2.085      1.238       165  2.108   1.271     83
stratum         41.871   23.920     248    41.806     23.901       165 42.000  23.958     83
pooled.stratum  33.581   17.237     248    33.521     17.204       165 33.699  17.303     83

##categorical variables only
groupSummary(infert[, c(1,4, 6, 5)],  group = "case")    
                      total.f total.pct total.n control.f control.pct control.n case.f case.pct case.n
education.0-5yrs           12     4.839     248         8       4.848       165      4    4.819     83
education.6-11yrs         120    48.387      NA        80      48.485        NA     40   48.193     NA
education.12+ yrs         116    46.774      NA        77      46.667        NA     39   46.988     NA
induced.0                 143    57.661     248        96      58.182       165     47   56.627     83
induced.1                  68    27.419      NA        45      27.273        NA     23   27.711     NA
induced.2 or more          37    14.919      NA        24      14.545        NA     13   15.663     NA
spontaneous.0             141    56.855     248       113      68.485       165     28   33.735     83
spontaneous.1              71    28.629      NA        40      24.242        NA     31   37.349     NA
spontaneous.2 or more      36    14.516      NA        12       7.273        NA     24   28.916     NA

##total sample
groupSummary(infert[, c(1,4, 6, 5)])
                      total.f total.pct total.n
education.0-5yrs           12     4.839     248
education.6-11yrs         120    48.387      NA
education.12+ yrs         116    46.774      NA
induced.0                 143    57.661     248
induced.1                  68    27.419      NA
induced.2 or more          37    14.919      NA
spontaneous.0             141    56.855     248
spontaneous.1              71    28.629      NA
spontaneous.2 or more      36    14.516      NA
case.control              165    66.532     248
case.case                  83    33.468      NA