The output generated by R commands is usually not formated for including in reports and so while building Markdown or Latex reports it will be useful to pipe them through one of these packages to make them publication quality
library("knitr")
library("xtable")
library("stargazer")
the useful packages and commands are
This is a function from knitr that produces tables using dataframe or matrix as input
kable(head(mtcars[,1:4]),
format = "pandoc", # default
digits = 2, # specify decimal places
caption = "Title of the table",
col.names = c("miles","cylinders","displacement","horsepower"),
row.names = TRUE,
align = c("c"), # align = c("c","c","c","r")
# padding = 2 # inner spacing
)
| miles | cylinders | displacement | horsepower | |
|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 |
| Datsun 710 | 22.8 | 4 | 108 | 93 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 |
| Valiant | 18.1 | 6 | 225 | 105 |
format = "markdown" also works but can’t add captionUse this to convert model summaries to data.frames.
let’s use annova model as an example.
data(tli)
fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
Now convert the model into a data.frame using xtable and then use the kable function instead of the print function to print the xtable.
fm1.table <- xtable(fm1)
kable(fm1.table, caption = "Annova table")
| Df | Sum Sq | Mean Sq | F value | Pr(>F) | |
|---|---|---|---|---|---|
| sex | 1 | 75.37255 | 75.37255 | 0.3751912 | 0.5416830 |
| ethnicty | 3 | 2572.14918 | 857.38306 | 4.2679008 | 0.0071831 |
| grade | 1 | 36.30740 | 36.30740 | 0.1807318 | 0.6717271 |
| disadvg | 1 | 59.30338 | 59.30338 | 0.2952017 | 0.5882062 |
| Residuals | 93 | 18682.86749 | 200.89105 | NA | NA |
Stargazer is a good option too. Here we show how three different models are compared side by side.
## 2 OLS models
linear.1 <- lm(rating ~ complaints + privileges + learning + raises + critical, data=attitude)
linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
## create an indicator dependent variable, and run a probit model
attitude$high.rating <- (attitude$rating > 70)
probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude, family = binomial(link = "probit"))
build a dataframe using the stargazer function and then print using kable. However, in the code chunk below include the option results='hide' to hide the stargazer default output
star.1 <- stargazer(linear.1, linear.2, probit.model,
title="Title: Regression Results",
align=TRUE,
type = "html",
style = "ajs", # "ajs"
notes="this is a test note"
)
kable(star.1)
| RATING | HIGH.RATING | ||
| OLS | probit | ||
| 1 | 2 | 3 | |
| complaints | .692*** | .682*** | |
| (.149) | (.129) | ||
| privileges | .104 | .103 | |
| (.135) | (.129) | ||
| learning | .249 | .238 | .164** |
| (.160) | (.139) | (.053) | |
| raises | .033 | ||
| (.202) | |||
| critical | .015 | .001 | |
| (.147) | (.044) | ||
| advance | .062 | ||
| (.042) | |||
| Constant | 11.011 | 11.258 | -7.476* |
| (11.704) | (7.318) | (3.570) | |
| Observations | 30 | 30 | 30 |
| R2 | .715 | .715 | |
| Adjusted R2 | .656 | .682 | |
| Log Likelihood | -9.087 | ||
| Akaike Inf. Crit. | 26.175 | ||
| Residual Std. Error | 7.139 (df = 24) | 6.863 (df = 26) | |
| F Statistic | 12.063*** (df = 5; 24) | 21.743*** (df = 3; 26) | |
| Notes: | *P < .05 | ||
| **P < .01 | |||
| ***P < .001 | |||
| this is a test note | |||
compare the output with what xtable gives for just one model
kable(xtable(linear.1))
| Estimate | Std. Error | t value | Pr(>|t|) | |
|---|---|---|---|---|
| (Intercept) | 11.0111304 | 11.7039358 | 0.9408058 | 0.3561782 |
| complaints | 0.6920526 | 0.1488643 | 4.6488827 | 0.0001014 |
| privileges | -0.1035620 | 0.1347297 | -0.7686650 | 0.4495906 |
| learning | 0.2490613 | 0.1596189 | 1.5603496 | 0.1317680 |
| raises | -0.0334606 | 0.2022762 | -0.1654205 | 0.8699986 |
| critical | 0.0154883 | 0.1472500 | 0.1051840 | 0.9171040 |
The End