compareGroups is an R package available on CRAN which performs descriptive tables displaying means, standard deviation, quantiles or frequencies of several variables. Also, p-value to test equality between groups is computed using the appropiate test.
With a very simple code, nice, compact and ready-to-publish descriptives table are displayed on R console. They can also be exported to different formats, such as Word, Excel, PDF or inserted in a R-Sweave or R-markdown document.
For those not familiar to R syntax, a Web User Interface (WUI) has been implemented using Shiny tools, which can be used remotely just accessing the compareGroups project website
You will find an extensive manual describing all compareGropus capabilities with real examples in the vignette. Also, compareGroups package has been published in Journal of Statistical Software (Subirana, Sanz, and Vila 2014).
Following, a step-by-step demo of how to use the compareGroups code when analysing a data set is described. In this example, a table containing descriptives by intervention group is built.
Install the compareGroups
package from CRAN and then load it by typing:
install.packages("compareGroups")
library(compareGroups)
Load the PREDIMED example data available in compareGroups
package:
data(predimed)
PREDIMED is a longitudinal study containing several baseline characteristics of the participants as well as events occurred during the 7 years follow-up period (event
and toevent
). Each individual has been assigned to a three intervention diet randomly (group
).
head(predimed)
group sex age smoke bmi waist wth htn diab hyperchol famhist
1 Control Male 58 Former 33.53 122 0.7530864 No No Yes No
2 Control Male 77 Current 31.05 119 0.7300614 Yes Yes No No
4 MedDiet + VOO Female 72 Former 30.86 106 0.6543210 No Yes No Yes
5 MedDiet + Nuts Male 71 Former 27.68 118 0.6941177 Yes No Yes No
6 MedDiet + VOO Female 79 Never 35.94 129 0.8062500 Yes No Yes No
8 Control Male 63 Former 41.66 143 0.8033708 Yes Yes Yes No
hormo p14 toevent event
1 No 10 5.374401 Yes
2 No 10 6.097194 No
4 No 8 5.946612 No
5 No 8 2.907598 Yes
6 No 9 4.761123 No
8 <NA> 9 3.148528 Yes
Compute all descriptives and tests from selected variables by using the compareGroups
Variables are selected by making use of the R standard formula
environment. Described variables are placed one the right sight of “~
” separated by “+
” sign, while the variable indicating the groups is placed on the left side. To select all variables use “.
”, and to remove variables, “-
”.
Note that no transformations are allowed in the formula environment. If necessary they must be made before calling compareGroups
function.
By the argument method
we set wtn
and p14
variables to be reported as median and quartiles instead of mean and standard deviation.
res <- compareGroups(group ~ . - toevent, data = predimed, method = c(wtn = 2, p14 = 2))
compareGroups package also offers the possibility to explore variable distribution by the generic function
plot(res[1:2])
Note the use of “[
” to select which variables we want to plot.
It is also possible to plot the distribution of described variables by groups
plot(res[1:2], bivar = TRUE)
By applying the generic function print
res
-------- Summary of results by groups of 'Intervention group'---------
var N p.value method selection
1 Sex 6324 <0.001** categorical ALL
2 Age 6324 0.003** continuous normal ALL
3 Smoking 6324 0.444 categorical ALL
4 Body mass index 6324 <0.001** continuous normal ALL
5 Waist circumference 6324 0.045** continuous normal ALL
6 Waist-to-height ratio 6324 <0.001** continuous normal ALL
7 Hypertension 6324 0.249 categorical ALL
8 Type-2 diabetes 6324 0.017** categorical ALL
9 Dyslipidemia 6324 0.423 categorical ALL
10 Family history of premature CHD 6324 0.581 categorical ALL
11 Hormone-replacement therapy 5661 0.850 categorical ALL
12 MeDiet Adherence score 6324 <0.001** continuous non-normal ALL
13 AMI, stroke, or CV Death 6324 0.064* categorical ALL
-----
Signif. codes: 0 '**' 0.05 '*' 0.1 ' ' 1
If you want to display descriptives of each varaible, call the generic function summary
summary(res[1:2])
--- Descriptives of each row-variable by groups of 'Intervention group' ---
-------------------
row-variable: Sex
Male Female Male (row%) Female (row%) p.overall p.trend
[ALL] 2679 3645 42.36243 57.63757
Control 812 1230 39.76494 60.23506 8.1e-05 0.388386
MedDiet + Nuts 968 1132 46.09524 53.90476
MedDiet + VOO 899 1283 41.20073 58.79927
p.Control vs MedDiet + Nuts p.Control vs MedDiet + VOO
[ALL]
Control 0.000133 0.358324
MedDiet + Nuts
MedDiet + VOO
p.MedDiet + Nuts vs MedDiet + VOO
[ALL]
Control 0.002076
MedDiet + Nuts
MedDiet + VOO
-------------------
row-variable: Age
N mean sd p.overall p.trend p.Control vs MedDiet + Nuts
[ALL] 6324 67.0117 6.17499
Control 2042 67.34231 6.27992 0.002666 0.101163 0.001672
MedDiet + Nuts 2100 66.6819 6.016395
MedDiet + VOO 2182 67.01971 6.212578
p.Control vs MedDiet + VOO p.MedDiet + Nuts vs MedDiet + VOO
[ALL]
Control 0.20596 0.172672
MedDiet + Nuts
MedDiet + VOO
Note that this is not a descriptive table yet. It will be created in the next step.
To build the descriptive table, apply creaTable
compareGroups
function (res
). Using creaTable
function you can customize how categorical variables are displayed (only percertage or absolute frequencies or both) by type
argument.
Also note the use of hide.no
category which is useful to hide “no” level for those binary variables.
If you only want to show “Female” category use hide
argument for sex
variable indicating which category is going to be hidden. This argument also applies to categorical variables with more than two categories.
To specify the number of decimal digits to show use digits
arguments. In this example p14
has no decimals and for hormo
only one.
Finally, if you want to show how many individuals have non-missing values in each described variable, set hide.n
argument to TRUE
.
restab <- createTable(res, digits = c(p14 = 0, hormo=1), type = 1,
hide = c(sex = "Male"), hide.no = "no", show.n = TRUE)
The descriptive table can be printed in the R
console using the method print
restab
--------Summary descriptives table by 'Intervention group'---------
_______________________________________________________________________________________
Control MedDiet + Nuts MedDiet + VOO p.overall N
N=2042 N=2100 N=2182
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sex: Female 60.2% 53.9% 58.8% <0.001 6324
Age 67.3 (6.28) 66.7 (6.02) 67.0 (6.21) 0.003 6324
Smoking: 0.444 6324
Never 62.8% 60.0% 61.9%
Current 13.2% 14.1% 13.4%
Former 24.0% 26.0% 24.7%
Body mass index 30.3 (3.96) 29.7 (3.77) 29.9 (3.71) <0.001 6324
Waist circumference 101 (10.8) 100 (10.6) 100 (10.4) 0.045 6324
Waist-to-height ratio 0.63 (0.07) 0.62 (0.06) 0.63 (0.06) <0.001 6324
Hypertension 83.8% 82.8% 81.9% 0.249 6324
Type-2 diabetes 47.5% 45.2% 49.6% 0.017 6324
Dyslipidemia 72.4% 73.3% 71.5% 0.423 6324
Family history of premature CHD 22.6% 21.9% 23.2% 0.581 6324
Hormone-replacement therapy 1.7% 1.6% 1.8% 0.850 5661
MeDiet Adherence score 8 [7;10] 9 [8;10] 9 [8;10] <0.001 6324
AMI, stroke, or CV Death 4.75% 3.33% 3.90% 0.064 6324
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
If you want to change some headers, such “p-value” instead of “p.overall” use header.labels
argument:
print(restab, header.labels = c(p.overall = "p-value"))
--------Summary descriptives table by 'Intervention group'---------
_____________________________________________________________________________________
Control MedDiet + Nuts MedDiet + VOO p-value N
N=2042 N=2100 N=2182
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sex: Female 60.2% 53.9% 58.8% <0.001 6324
Age 67.3 (6.28) 66.7 (6.02) 67.0 (6.21) 0.003 6324
Smoking: 0.444 6324
Never 62.8% 60.0% 61.9%
Current 13.2% 14.1% 13.4%
Former 24.0% 26.0% 24.7%
Body mass index 30.3 (3.96) 29.7 (3.77) 29.9 (3.71) <0.001 6324
Waist circumference 101 (10.8) 100 (10.6) 100 (10.4) 0.045 6324
Waist-to-height ratio 0.63 (0.07) 0.62 (0.06) 0.63 (0.06) <0.001 6324
Hypertension 83.8% 82.8% 81.9% 0.249 6324
Type-2 diabetes 47.5% 45.2% 49.6% 0.017 6324
Dyslipidemia 72.4% 73.3% 71.5% 0.423 6324
Family history of premature CHD 22.6% 21.9% 23.2% 0.581 6324
Hormone-replacement therapy 1.7% 1.6% 1.8% 0.850 5661
MeDiet Adherence score 8 [7;10] 9 [8;10] 9 [8;10] <0.001 6324
AMI, stroke, or CV Death 4.75% 3.33% 3.90% 0.064 6324
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Create a PDF document with the table in a publish-ready format
export2pdf(restab, file = "example.pdf", header.labels = c(p.overall = "p-value"))
Export the table to a Word document
export2word(restab, file = "example.docx", header.labels = c(p.overall = "p-value"))
Export the descriptive table to an Excel file
export2xls(restab, file = "example.xlsx", header.labels = c(p.overall = "p-value"))
Export the descriptive table to a plain text format, such as csv file, with the semicolon character as the column separator
export2csv(restab, file = "example.csv", header.labels = c(p.overall = "p-value"), sep = ";")
You can translate to Markdown code and insert it in a R-markdown chunk to create reproducible reports.
export2md(restab, header.labels = c(p.overall = "p-value"))
Var | Control N=2042 | MedDiet + Nuts N=2100 | MedDiet + VOO N=2182 | p-value | N |
---|---|---|---|---|---|
Sex: Female | 60.2% | 53.9% | 58.8% | <0.001 | 6324 |
Age | 67.3 (6.28) | 66.7 (6.02) | 67.0 (6.21) | 0.003 | 6324 |
Smoking: | 0.444 | 6324 | |||
Never | 62.8% | 60.0% | 61.9% | ||
Current | 13.2% | 14.1% | 13.4% | ||
Former | 24.0% | 26.0% | 24.7% | ||
Body mass index | 30.3 (3.96) | 29.7 (3.77) | 29.9 (3.71) | <0.001 | 6324 |
Waist circumference | 101 (10.8) | 100 (10.6) | 100 (10.4) | 0.045 | 6324 |
Waist-to-height ratio | 0.63 (0.07) | 0.62 (0.06) | 0.63 (0.06) | <0.001 | 6324 |
Hypertension | 83.8% | 82.8% | 81.9% | 0.249 | 6324 |
Type-2 diabetes | 47.5% | 45.2% | 49.6% | 0.017 | 6324 |
Dyslipidemia | 72.4% | 73.3% | 71.5% | 0.423 | 6324 |
Family history of premature CHD | 22.6% | 21.9% | 23.2% | 0.581 | 6324 |
Hormone-replacement therapy | 1.7% | 1.6% | 1.8% | 0.850 | 5661 |
MeDiet Adherence score | 8 [7;10] | 9 [8;10] | 9 [8;10] | <0.001 | 6324 |
AMI, stroke, or CV Death | 4.75% | 3.33% | 3.90% | 0.064 | 6324 |
Similar to Markdown file, it is possible to insert LaTeX code in a Sweave file (.tex)
export2tex(restab, header.labels = c(p.overall = "p-value"))
To compute descriptives for whole cohort, whithout stratifying or comparing groups, just leave the left hand side of “~
” empty.
resNoGroups <- compareGroups(~ . , predimed)
restabNoGroups <- createTable(resNoGroups, hide.no = "no")
print(restabNoGroups, header.labels = c("all" = "Entire cohort"))
--------Summary descriptives table ---------
__________________________________________________
Entire cohort N
N=6324
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Intervention group: 6324
Control 2042 (32.3%)
MedDiet + Nuts 2100 (33.2%)
MedDiet + VOO 2182 (34.5%)
Sex: 6324
Male 2679 (42.4%)
Female 3645 (57.6%)
Age 67.0 (6.17) 6324
Smoking: 6324
Never 3892 (61.5%)
Current 858 (13.6%)
Former 1574 (24.9%)
Body mass index 30.0 (3.82) 6324
Waist circumference 100 (10.6) 6324
Waist-to-height ratio 0.63 (0.07) 6324
Hypertension 5235 (82.8%) 6324
Type-2 diabetes 3002 (47.5%) 6324
Dyslipidemia 4578 (72.4%) 6324
Family history of premature CHD 1429 (22.6%) 6324
Hormone-replacement therapy 97 (1.71%) 5661
MeDiet Adherence score 8.68 (1.94) 6324
follow-up to main event (years) 4.36 (1.69) 6324
AMI, stroke, or CV Death 252 (3.98%) 6324
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
For case control studies, it may be interesting to compute the Odds Ratio for each variable between cases and controls. Although PREDIMED study is not a case-control, we will use event
variable as case-control status.
Note the use of fact.ratio
argument to set an increment of 10 units for its OR. Also, to set the reference cathegory to “male” when displaying the OR set the ref.ratio
argument.
Finally, also note that compute
argument must be set to TRUE
to compute ORs. By default, this argument is set to FALSE
to save time. Different type of methods to compute Odds Ratio are available by setting oddsratio.method
(For more info, see oddsratio
function from epitools
package).
resOR <- compareGroups(event ~ . - toevent, predimed, compute = TRUE,
fact.ratio = c(waist=10), ref.ratio = c(sex = "Male"))
restabOR <- createTable(resOR, show.ratio = TRUE, show.p.overall = FALSE,
hide.no = "no", hide = c(sex = "Male"), type=1)
print(restabOR, header.labels = c(p.ratio = "p-value"))
--------Summary descriptives table by 'AMI, stroke, or CV Death'---------
________________________________________________________________________________
No Yes OR p-value
N=6072 N=252
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Intervention group:
Control 32.0% 38.5% Ref. Ref.
MedDiet + Nuts 33.4% 27.8% 0.69 [0.50;0.95] 0.021
MedDiet + VOO 34.5% 33.7% 0.81 [0.60;1.09] 0.173
Sex: Female 58.4% 40.1% 0.48 [0.37;0.62] <0.001
Age 66.9 (6.14) 69.4 (6.65) 1.07 [1.04;1.09] <0.001
Smoking:
Never 62.2% 45.2% Ref. Ref.
Current 13.3% 19.4% 2.01 [1.41;2.82] <0.001
Former 24.5% 35.3% 1.99 [1.49;2.64] <0.001
Body mass index 30.0 (3.81) 29.8 (3.92) 0.98 [0.95;1.02] 0.365
Waist circumference 100 (10.6) 102 (10.6) 1.15 [1.03;1.30] 0.016
Waist-to-height ratio 0.63 (0.07) 0.63 (0.07) 3.64 [0.55;23.9] 0.178
Hypertension 82.8% 83.3% 1.04 [0.75;1.48] 0.826
Type-2 diabetes 46.8% 63.9% 2.01 [1.55;2.62] <0.001
Dyslipidemia 72.9% 59.9% 0.56 [0.43;0.72] <0.001
Family history of premature CHD 22.7% 20.2% 0.87 [0.63;1.18] 0.363
Hormone-replacement therapy 1.77% 0.45% 0.29 [0.01;1.27] 0.117
MeDiet Adherence score 8.70 (1.94) 8.24 (1.94) 0.89 [0.84;0.95] <0.001
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
When analysing a cohort study, one may be interested in computing Hazard Ratios instead of Odds Ratio, and take into account time-to-event or possible censored values. P-values are computed properly by log-rank test.
First of all, you must recode the time-to-event variable as a survival variable by using Surv
function from survival
package. The recoded variable can be labelled using the label
function from Hmisc
package.
predimed$eventSurv <- with(predimed, Surv(toevent, event == "Yes"))
label(predimed$eventSurv) <- "Event (possible right censored)"
Then, place the recoded variable in the left side of “~
”, and use a syntax similar to the one used to compute Odds Ratios, replacing event
to eventSurv
.
resHR <- compareGroups(eventSurv ~ . - toevent - event, predimed, compute = TRUE,
fact.ratio = c(waist=10), ref.ratio = c(sex = "Male"))
restabHR <- createTable(resHR, show.ratio = TRUE, show.p.overall = FALSE,
hide.no = "no", hide = c(sex = "Male"), type=1)
print(restabHR, header.labels = c(p.ratio = "p-value"))
--------Summary descriptives table by 'Event (possible right censored)'---------
________________________________________________________________________________
No event Event HR p-value
N=6072 N=252
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Intervention group:
Control 32.0% 38.5% Ref. Ref.
MedDiet + Nuts 33.4% 27.8% 0.66 [0.48;0.89] 0.008
MedDiet + VOO 34.5% 33.7% 0.70 [0.53;0.94] 0.018
Sex: Female 58.4% 40.1% 0.49 [0.38;0.63] <0.001
Age 66.9 (6.14) 69.4 (6.65) 1.06 [1.04;1.09] <0.001
Smoking:
Never 62.2% 45.2% Ref. Ref.
Current 13.3% 19.4% 1.96 [1.40;2.74] <0.001
Former 24.5% 35.3% 2.02 [1.53;2.67] <0.001
Body mass index 30.0 (3.81) 29.8 (3.92) 0.99 [0.96;1.02] 0.455
Waist circumference 100 (10.6) 102 (10.6) 1.19 [1.06;1.33] 0.003
Waist-to-height ratio 0.63 (0.07) 0.63 (0.07) 5.27 [0.83;33.6] 0.079
Hypertension 82.8% 83.3% 1.10 [0.79;1.53] 0.578
Type-2 diabetes 46.8% 63.9% 1.88 [1.46;2.44] <0.001
Dyslipidemia 72.9% 59.9% 0.62 [0.49;0.80] <0.001
Family history of premature CHD 22.7% 20.2% 0.93 [0.68;1.26] 0.640
Hormone-replacement therapy 1.77% 0.45% 0.29 [0.04;2.10] 0.223
MeDiet Adherence score 8.70 (1.94) 8.24 (1.94) 0.88 [0.83;0.94] <0.001
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Use the “[
restab[1:4]
--------Summary descriptives table by 'Intervention group'---------
_______________________________________________________________________
Control MedDiet + Nuts MedDiet + VOO p.overall N
N=2042 N=2100 N=2182
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sex: Female 60.2% 53.9% 58.8% <0.001 6324
Age 67.3 (6.28) 66.7 (6.02) 67.0 (6.21) 0.003 6324
Smoking: 0.444 6324
Never 62.8% 60.0% 61.9%
Current 13.2% 14.1% 13.4%
Former 24.0% 26.0% 24.7%
Body mass index 30.3 (3.96) 29.7 (3.77) 29.9 (3.71) <0.001 6324
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
The R generic method [
compareGroups
package. This may be useful to change some specific aspects from the descriptive table without changing the others.
For example, to hide available column (‘N’) in the previously table
update(restab, show.n = FALSE)
--------Summary descriptives table by 'Intervention group'---------
__________________________________________________________________________________
Control MedDiet + Nuts MedDiet + VOO p.overall
N=2042 N=2100 N=2182
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sex: Female 60.2% 53.9% 58.8% <0.001
Age 67.3 (6.28) 66.7 (6.02) 67.0 (6.21) 0.003
Smoking: 0.444
Never 62.8% 60.0% 61.9%
Current 13.2% 14.1% 13.4%
Former 24.0% 26.0% 24.7%
Body mass index 30.3 (3.96) 29.7 (3.77) 29.9 (3.71) <0.001
Waist circumference 101 (10.8) 100 (10.6) 100 (10.4) 0.045
Waist-to-height ratio 0.63 (0.07) 0.62 (0.06) 0.63 (0.06) <0.001
Hypertension 83.8% 82.8% 81.9% 0.249
Type-2 diabetes 47.5% 45.2% 49.6% 0.017
Dyslipidemia 72.4% 73.3% 71.5% 0.423
Family history of premature CHD 22.6% 21.9% 23.2% 0.581
Hormone-replacement therapy 1.7% 1.6% 1.8% 0.850
MeDiet Adherence score 8 [7;10] 9 [8;10] 9 [8;10] <0.001
AMI, stroke, or CV Death 4.75% 3.33% 3.90% 0.064
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Or to change the grouping variable (without changing the table format)
update(restab, x = update(res, sex ~ .))
--------Summary descriptives table by 'Sex'---------
______________________________________________________________________
Male Female p.overall N
N=2679 N=3645
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sex: Female 0.00% 100% 0.000 6324
Age 66.1 (6.54) 67.7 (5.80) <0.001 6324
Smoking: 0.000 6324
Never 26.4% 87.4%
Current 24.9% 5.24%
Former 48.7% 7.38%
Body mass index 29.3 (3.33) 30.5 (4.07) <0.001 6324
Waist circumference 103 (9.60) 98.3 (10.8) <0.001 6324
Waist-to-height ratio 0.62 (0.06) 0.64 (0.07) <0.001 6324
Hypertension 77.8% 86.4% <0.001 6324
Type-2 diabetes 52.7% 43.6% <0.001 6324
Dyslipidemia 66.2% 77.0% <0.001 6324
Family history of premature CHD 17.2% 26.5% <0.001 6324
Hormone-replacement therapy 0.0% 2.8% <0.001 5661
MeDiet Adherence score 9 [8;10] 9 [7;10] <0.001 6324
AMI, stroke, or CV Death 5.64% 2.77% <0.001 6324
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Note here we have to udpate first the object computed by compareGroups
function. To do so, we had to store the object previously. If no res
object has been stored, we can pick up it by “x” attribute from restab
object.
update(restab, x = update(attr(restab, "x")[[1]], sex ~ .))
If you want to perform diet comparisons by sex, first build the descriptive tables by men and women separately making us of subset
argument from the compareGroups
function, to select men and women, respectively. Then use the generic function cbind
restabfemale <- update(restab, x = update(res, . ~ . - sex, subset = sex == 'Female'),
show.n = FALSE)
restabmale <- update(restabfemale, x=update(res, . ~ . - sex, subset = sex == 'Male'))
restabstrat <- cbind("Females" = restabfemale, "Males" = restabmale)
print(restabstrat, header.labels = c(p.overall = "p-value"))
--------Summary descriptives table ---------
__________________________________________________________________________________________________________________________________
Females Males
________________________________________________ ________________________________________________
Control MedDiet + Nuts MedDiet + VOO p-value Control MedDiet + Nuts MedDiet + VOO p-value
N=1230 N=1132 N=1283 N=812 N=968 N=899
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Age 68.0 (5.96) 67.4 (5.57) 67.7 (5.84) 0.056 66.4 (6.62) 65.8 (6.40) 66.1 (6.61) 0.215
Smoking: 0.907 0.851
Never 87.6% 87.7% 86.9% 25.2% 27.5% 26.3%
Current 5.37% 4.77% 5.53% 25.1% 25.0% 24.6%
Former 7.07% 7.51% 7.56% 49.6% 47.5% 49.2%
Body mass index 30.8 (4.20) 30.2 (4.08) 30.4 (3.91) 0.002 29.6 (3.45) 29.1 (3.28) 29.2 (3.28) 0.018
Waist circumference 99.0 (11.0) 97.8 (11.0) 98.0 (10.5) 0.016 104 (9.82) 103 (9.36) 103 (9.65) 0.289
Waist-to-height ratio 0.64 (0.07) 0.63 (0.07) 0.63 (0.07) 0.002 0.62 (0.06) 0.62 (0.06) 0.62 (0.06) 0.191
Hypertension 86.3% 87.0% 86.0% 0.780 79.9% 77.8% 75.9% 0.130
Type-2 diabetes 43.9% 40.1% 46.5% 0.007 53.0% 51.2% 54.1% 0.468
Dyslipidemia 77.1% 78.3% 75.7% 0.319 65.4% 67.5% 65.5% 0.575
Family history of premature CHD 26.6% 25.5% 27.4% 0.596 16.6% 17.7% 17.4% 0.841
Hormone-replacement therapy 2.6% 2.8% 3.0% 0.898 100.0% 100.0% 100.0% .
MeDiet Adherence score 8 [7;10] 9 [8;10] 9 [7;10] <0.001 9 [7;10] 9 [8;10] 9 [8;10] <0.001
AMI, stroke, or CV Death 3.17% 2.56% 2.57% 0.576 7.14% 4.24% 5.78% 0.029
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
export2pdf(restabstrat, file = "examplestrat.pdf",
header.labels = c(p.overall = "p-value"),
landscape = TRUE, size = "footnotesize", )
To join some variables in the sense of placing a header just before each group of variables (rows of the descriptive tables), first select each set of variables making use of [
rbind
restabgroups <- rbind("First group" = restab[1:4], "Second group" = restab[5:8])
print(restabgroups, header.labels = c(p.overall = "p-value"))
--------Summary descriptives table by 'Intervention group'---------
_______________________________________________________________________________
Control MedDiet + Nuts MedDiet + VOO p-value N
N=2042 N=2100 N=2182
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
First group:
Sex: Female 60.2% 53.9% 58.8% <0.001 6324
Age 67.3 (6.28) 66.7 (6.02) 67.0 (6.21) 0.003 6324
Smoking: 0.444 6324
Never 62.8% 60.0% 61.9%
Current 13.2% 14.1% 13.4%
Former 24.0% 26.0% 24.7%
Body mass index 30.3 (3.96) 29.7 (3.77) 29.9 (3.71) <0.001 6324
Second group:
Waist circumference 101 (10.8) 100 (10.6) 100 (10.4) 0.045 6324
Waist-to-height ratio 0.63 (0.07) 0.62 (0.06) 0.63 (0.06) <0.001 6324
Hypertension 83.8% 82.8% 81.9% 0.249 6324
Type-2 diabetes 47.5% 45.2% 49.6% 0.017 6324
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
export2pdf(restabgroups, file = "examplegroups.pdf",
header.labels = c(p.overall = "p-value"))
When the table is exported to LaTeX, the headers are printed in bold. If these headers are very long, they span throught the table columns without making the first column wider.
Subirana, Isaac, Héctor Sanz, and Joan Vila. 2014. “Building Bivariate Tables: The compareGroups Package for R.” Journal of Statistical Software 57 (12): 1–16. http://www.jstatsoft.org/v57/i12/.