R modelsummary: Custom balance table using datasummary()

25 Views Asked by At

I would like to build my own balance table table using datasummary(). The reason is that I would like to get rid of some columns and add a second version of the table with the balanced sample of the table. However I am puzzled with how I can define my own function that calculates the difference in means for each group of a factor so that I can get something like this:

library(modelsummary)
library(estimatr)

data("ToothGrowth")

datasummary_balance(~ supp, data = ToothGrowth)
datasummary(Mean ~ supp, data = ToothGrowth)
|      | OJ (N=30) / Mean | OJ (N=30) / Std. Dev. | VC (N=30) / Mean | VC (N=30) / Std. Dev. | Diff. in Means | Std. Error | 
|:-----|-----------------:|----------------------:|-----------------:|----------------------:|---------------:|-----------:|
| len  |             20.7 |                   6.6 |             17.0 |                   8.3 |           -3.7 |        1.9 | 
| dose |              1.2 |                   0.6 |              1.2 |                   0.6 |            0.0 |        0.2 | 

But when using

datasummary(Mean ~ supp, data = ToothGrowth, output = "markdown")

I get

|      | OJ | VC | 
|:-----|---:|---:|
| Mean |    |    | 
0

There are 0 best solutions below