extend the modeling output

General modeling output

Here is a linear regression model as an example.

Raw dataset

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#> 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
#> 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> 4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#> 5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
#> 6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
#> 7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
#> 8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> 9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> 10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
#> # ... with 22 more rows

here we treat mpg as the dependent variable and the rest as the independent variables for regression modeling study.

Raw modeling output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#>
#> Call:
#> lm(formula = mpg ~ qsec + factor(am) + wt + factor(gear), data = mtcars)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -3.5064 -1.5220 -0.7517 1.3841 4.6345
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 9.3650 8.3730 1.118 0.27359
#> qsec 1.2449 0.3828 3.252 0.00317 **
#> factor(am)1 3.1505 1.9405 1.624 0.11654
#> wt -3.9263 0.7428 -5.286 1.58e-05 ***
#> factor(gear)4 -0.2682 1.6555 -0.162 0.87257
#> factor(gear)5 -0.2697 2.0632 -0.131 0.89698
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 2.55 on 26 degrees of freedom
#> Multiple R-squared: 0.8498, Adjusted R-squared: 0.8209
#> F-statistic: 29.43 on 5 and 26 DF, p-value: 6.379e-10

where you can see the statistical output, including the model significance, fit-of-goodness, residual and standard error, coefficients and according statistical test, and the modeling equation used. Generally that’s what we see from most of the software output. If you’re a good statistician it looks okay, but we can extend it a little further by transform the output.

Extend 1: to easy the multi-modeling comparison

1
library(broom)

tidy the output into a data table

1
2
3
4
5
6
7
#> term estimate std.error statistic p.value
#> 1 (Intercept) 9.3650443 8.3730161 1.1184792 2.735903e-01
#> 2 qsec 1.2449212 0.3828479 3.2517387 3.168128e-03
#> 3 factor(am)1 3.1505178 1.9405171 1.6235455 1.165367e-01
#> 4 wt -3.9263022 0.7427562 -5.2861251 1.581735e-05
#> 5 factor(gear)4 -0.2681630 1.6554617 -0.1619868 8.725685e-01
#> 6 factor(gear)5 -0.2697468 2.0631829 -0.1307430 8.969850e-01

Glance the modeling fit

1
2
3
4
#> r.squared adj.r.squared sigma statistic p.value df logLik
#> 1 0.8498278 0.8209485 2.550272 29.4269 6.379318e-10 6 -72.0422
#> AIC BIC deviance df.residual
#> 1 158.0844 168.3446 169.101 26

Extend 2: to prettify the output for report

1
library(pixiedust)
Term Coefficient SE T-statistic P-value
(Intercept) 9.365 8.373 1.118 0.27
qsec 1.245 0.383 3.252 0.003
factor(am)1 3.151 1.941 1.624 0.12
wt -3.926 0.743 -5.286 < 0.001
factor(gear)4 -0.268 1.655 -0.162 0.87
factor(gear)5 -0.27 2.063 -0.131 0.9

Here we round the value to specified number of decimal places, changed the column names, modified the P value to an easy reading format, and added backgroud color to highlight the significant cofficients, and you can do more in your imagination since it’s HTML table and CSS can be customized by yourself.

Does it look good 🙂

Citation

See details on the below two packages,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#>
#> To cite package 'broom' in publications use:
#>
#> David Robinson (2016). broom: Convert Statistical Analysis
#> Objects into Tidy Data Frames. R package version 0.4.1.
#> https://CRAN.R-project.org/package=broom
#>
#> A BibTeX entry for LaTeX users is
#>
#> @Manual{,
#> title = {broom: Convert Statistical Analysis Objects into Tidy Data Frames},
#> author = {David Robinson},
#> year = {2016},
#> note = {R package version 0.4.1},
#> url = {https://CRAN.R-project.org/package=broom},
#> }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#>
#> To cite package 'pixiedust' in publications use:
#>
#> Benjamin Nutter (2016). pixiedust: Tables so Beautifully
#> Fine-Tuned You Will Believe It's Magic. R package version 0.7.4.
#> https://CRAN.R-project.org/package=pixiedust
#>
#> A BibTeX entry for LaTeX users is
#>
#> @Manual{,
#> title = {pixiedust: Tables so Beautifully Fine-Tuned You Will Believe It's Magic},
#> author = {Benjamin Nutter},
#> year = {2016},
#> note = {R package version 0.7.4},
#> url = {https://CRAN.R-project.org/package=pixiedust},
#> }