Using simpletable and broom together

The broom package is a great R package for neatly summarizing the results of statistical tests and models. The output of broom is always a data.frame and rownames are always included as the first column. This makes them work very seamlessly with simpletable if we want to produce an attractive looking table in a RMarkdown document.


For background information on simpletable see the introductory vignette here or the github page here.



Load packages

library(broom)
library(simpletable)




Some examples

I will first use the tidy function in broom to make some summary tables of a lm and glm model.



lmfit <- lm(mpg ~ wt, mtcars)
lmfit
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Coefficients:
## (Intercept)           wt  
##      37.285       -5.344


tidy(lmfit)
##          term  estimate std.error statistic      p.value
## 1 (Intercept) 37.285126  1.877627 19.857575 8.241799e-19
## 2          wt -5.344472  0.559101 -9.559044 1.293959e-10



This is how we can make it look using simpletable using two default styles:



tablecode(tidy(lmfit), tabletype="minimal")
term estimate std.error statistic p.value
(Intercept) 37.285126 1.877627 19.857575 8.241799e-19
wt -5.344472 0.559101 -9.559044 1.293959e-10



tablecode(tidy(lmfit), tabletype="box")
term estimate std.error statistic p.value
(Intercept) 37.285126 1.877627 19.857575 8.241799e-19
wt -5.344472 0.559101 -9.559044 1.293959e-10



Here is another example:

glmfit <- glm(am ~ wt, mtcars, family="binomial")
glmfit
## 
## Call:  glm(formula = am ~ wt, family = "binomial", data = mtcars)
## 
## Coefficients:
## (Intercept)           wt  
##      12.040       -4.024  
## 
## Degrees of Freedom: 31 Total (i.e. Null);  30 Residual
## Null Deviance:       43.23 
## Residual Deviance: 19.18     AIC: 23.18
tidy(glmfit)
##          term estimate std.error statistic     p.value
## 1 (Intercept) 12.04037  4.509706  2.669879 0.007587858
## 2          wt -4.02397  1.436416 -2.801396 0.005088198


tablecode(tidy(glmfit), tabletype="minimal")
term estimate std.error statistic p.value
(Intercept) 12.04037 4.509706 2.669879 0.007587858
wt -4.02397 1.436416 -2.801396 0.005088198


tablecode(tidy(glmfit), tabletype="box")
term estimate std.error statistic p.value
(Intercept) 12.04037 4.509706 2.669879 0.007587858
wt -4.02397 1.436416 -2.801396 0.005088198



We can also look at the fitted values and residuals with augment


lmfit.a <-augment(lmfit)
lmfit.a[,4:10] <- round(lmfit.a[,4:10],3)


which can give..



tablecode(lmfit.a, tabletype="gridtable")
.rownames mpg wt .fitted .se.fit .resid .hat .sigma .cooksd .std.resid
Mazda RX4 21.0 2.620 23.283 0.634 -2.283 0.043 3.067 0.013 -0.766
Mazda RX4 Wag 21.0 2.875 21.920 0.571 -0.920 0.035 3.093 0.002 -0.307
Datsun 710 22.8 2.320 24.886 0.736 -2.086 0.058 3.072 0.015 -0.706
Hornet 4 Drive 21.4 3.215 20.103 0.538 1.297 0.031 3.088 0.003 0.433
Hornet Sportabout 18.7 3.440 18.900 0.553 -0.200 0.033 3.098 0.000 -0.067
Valiant 18.1 3.460 18.793 0.555 -0.693 0.033 3.095 0.001 -0.231
Duster 360 14.3 3.570 18.205 0.573 -3.905 0.035 3.009 0.031 -1.306
Merc 240D 24.4 3.190 20.236 0.539 4.164 0.031 2.997 0.031 1.389
Merc 230 22.8 3.150 20.450 0.540 2.350 0.031 3.066 0.010 0.784
Merc 280 19.2 3.440 18.900 0.553 0.300 0.033 3.097 0.000 0.100
Merc 280C 17.8 3.440 18.900 0.553 -1.100 0.033 3.091 0.002 -0.367
Merc 450SE 16.4 4.070 15.533 0.719 0.867 0.056 3.094 0.003 0.293
Merc 450SL 17.3 3.730 17.350 0.610 -0.050 0.040 3.098 0.000 -0.017
Merc 450SLC 15.2 3.780 17.083 0.624 -1.883 0.042 3.077 0.009 -0.632
Cadillac Fleetwood 10.4 5.250 9.227 1.258 1.173 0.170 3.089 0.018 0.423
Lincoln Continental 10.4 5.424 8.297 1.346 2.103 0.195 3.067 0.072 0.770
Chrysler Imperial 14.7 5.345 8.719 1.306 5.981 0.184 2.844 0.532 2.174
Fiat 128 32.4 2.200 25.527 0.783 6.873 0.066 2.802 0.193 2.335
Honda Civic 30.4 1.615 28.654 1.045 1.746 0.118 3.079 0.025 0.610
Toyota Corolla 33.9 1.835 27.478 0.942 6.422 0.096 2.833 0.260 2.217
Toyota Corona 21.5 2.465 24.111 0.683 -2.611 0.050 3.058 0.020 -0.880
Dodge Challenger 15.5 3.520 18.473 0.564 -2.973 0.034 3.047 0.018 -0.993
AMC Javelin 15.2 3.435 18.927 0.552 -3.727 0.033 3.017 0.026 -1.244
Camaro Z28 13.3 3.840 16.762 0.641 -3.462 0.044 3.027 0.031 -1.163
Pontiac Firebird 19.2 3.845 16.736 0.643 2.464 0.045 3.062 0.016 0.828
Fiat X1-9 27.3 1.935 26.944 0.897 0.356 0.087 3.097 0.001 0.122
Porsche 914-2 26.0 2.140 25.848 0.808 0.152 0.070 3.098 0.000 0.052
Lotus Europa 30.4 1.513 29.199 1.094 1.201 0.129 3.089 0.013 0.423
Ford Pantera L 15.8 3.170 20.343 0.539 -4.543 0.031 2.977 0.037 -1.515
Ferrari Dino 19.7 2.770 22.481 0.594 -2.781 0.038 3.053 0.017 -0.931
Maserati Bora 15.0 3.570 18.205 0.573 -3.205 0.035 3.038 0.021 -1.072
Volvo 142E 21.4 2.780 22.427 0.591 -1.027 0.038 3.092 0.002 -0.344




Customizing the output

It’s still a work in process, but I’m also enabling the user to customize output of tables to change all sorts of features. This can be done with the stylecode function. A vignette is in the works. Here is a brief example:

stylecode(name="uniqueid", colh="rgba(0,0,255,0.2)", colh.text="#000000", col="rgba(0,0,255,0.1)", col.text="#000000", font.size=c(14,12), hfont.wt=900, lineheight=1.5, border.style="none", borderh.style="solid", borderh.wt=2, borderh.col="#3399FF", htext.align="center", text.align="center", borderb.style="solid", pad=c(3,1,3,3))
tablecode(lmfit.a, tabletype="uniqueid", width="35%")
.rownames mpg wt .fitted .se.fit .resid .hat .sigma .cooksd .std.resid
Mazda RX4 21.0 2.620 23.283 0.634 -2.283 0.043 3.067 0.013 -0.766
Mazda RX4 Wag 21.0 2.875 21.920 0.571 -0.920 0.035 3.093 0.002 -0.307
Datsun 710 22.8 2.320 24.886 0.736 -2.086 0.058 3.072 0.015 -0.706
Hornet 4 Drive 21.4 3.215 20.103 0.538 1.297 0.031 3.088 0.003 0.433
Hornet Sportabout 18.7 3.440 18.900 0.553 -0.200 0.033 3.098 0.000 -0.067
Valiant 18.1 3.460 18.793 0.555 -0.693 0.033 3.095 0.001 -0.231
Duster 360 14.3 3.570 18.205 0.573 -3.905 0.035 3.009 0.031 -1.306
Merc 240D 24.4 3.190 20.236 0.539 4.164 0.031 2.997 0.031 1.389
Merc 230 22.8 3.150 20.450 0.540 2.350 0.031 3.066 0.010 0.784
Merc 280 19.2 3.440 18.900 0.553 0.300 0.033 3.097 0.000 0.100
Merc 280C 17.8 3.440 18.900 0.553 -1.100 0.033 3.091 0.002 -0.367
Merc 450SE 16.4 4.070 15.533 0.719 0.867 0.056 3.094 0.003 0.293
Merc 450SL 17.3 3.730 17.350 0.610 -0.050 0.040 3.098 0.000 -0.017
Merc 450SLC 15.2 3.780 17.083 0.624 -1.883 0.042 3.077 0.009 -0.632
Cadillac Fleetwood 10.4 5.250 9.227 1.258 1.173 0.170 3.089 0.018 0.423
Lincoln Continental 10.4 5.424 8.297 1.346 2.103 0.195 3.067 0.072 0.770
Chrysler Imperial 14.7 5.345 8.719 1.306 5.981 0.184 2.844 0.532 2.174
Fiat 128 32.4 2.200 25.527 0.783 6.873 0.066 2.802 0.193 2.335
Honda Civic 30.4 1.615 28.654 1.045 1.746 0.118 3.079 0.025 0.610
Toyota Corolla 33.9 1.835 27.478 0.942 6.422 0.096 2.833 0.260 2.217
Toyota Corona 21.5 2.465 24.111 0.683 -2.611 0.050 3.058 0.020 -0.880
Dodge Challenger 15.5 3.520 18.473 0.564 -2.973 0.034 3.047 0.018 -0.993
AMC Javelin 15.2 3.435 18.927 0.552 -3.727 0.033 3.017 0.026 -1.244
Camaro Z28 13.3 3.840 16.762 0.641 -3.462 0.044 3.027 0.031 -1.163
Pontiac Firebird 19.2 3.845 16.736 0.643 2.464 0.045 3.062 0.016 0.828
Fiat X1-9 27.3 1.935 26.944 0.897 0.356 0.087 3.097 0.001 0.122
Porsche 914-2 26.0 2.140 25.848 0.808 0.152 0.070 3.098 0.000 0.052
Lotus Europa 30.4 1.513 29.199 1.094 1.201 0.129 3.089 0.013 0.423
Ford Pantera L 15.8 3.170 20.343 0.539 -4.543 0.031 2.977 0.037 -1.515
Ferrari Dino 19.7 2.770 22.481 0.594 -2.781 0.038 3.053 0.017 -0.931
Maserati Bora 15.0 3.570 18.205 0.573 -3.205 0.035 3.038 0.021 -1.072
Volvo 142E 21.4 2.780 22.427 0.591 -1.027 0.038 3.092 0.002 -0.344



I still have some features to add to this function such as the ability to manipulate the width of the first column independently of the other columns, but this gives you an idea of what’s possible.