Tables and Output
This document is a work in progress.
mod <- lm(dist ~ speed, data = cars)
# Just the coefficients
coef(summary(mod))
Estimate Std. Error t value Pr(>|t|)
(Intercept) -17.579095 6.7584402 -2.601058 1.231882e-02
speed 3.932409 0.4155128 9.463990 1.489836e-12
# Or
summary(mod)$coef
Estimate Std. Error t value Pr(>|t|)
(Intercept) -17.579095 6.7584402 -2.601058 1.231882e-02
speed 3.932409 0.4155128 9.463990 1.489836e-12
There are several packages one might use to format the output from a linear model.
knitr
and the function kable()
xtable
and the function xtable()
htmlTable
and the function htmlTable()
knitr::kable(coef(summary(mod)), caption = "Write your descriptive caption between the quotes", booktabs = TRUE, digits = 4)
Table 1: Write your descriptive caption between the quotes
(Intercept) |
-17.5791 |
6.7584 |
-2.6011 |
0.0123 |
speed |
3.9324 |
0.4155 |
9.4640 |
0.0000 |
knitr::kable(coef(summary(mod)), caption = "Write your descriptive caption between the quotes 2", booktabs = TRUE, digits = 4, col.names = c("$\\alpha$", "$\\beta$", "$\\delta$", "$\\eta$"))
Table 2: Write your descriptive caption between the quotes 2
(Intercept) |
-17.5791 |
6.7584 |
-2.6011 |
0.0123 |
speed |
3.9324 |
0.4155 |
9.4640 |
0.0000 |
However, I can not figure out how to refer to the created Tables in a bookdown document with any package other than knitr
. If you can help, please do.
knitr::kable(mtcars[1:5, ], booktabs = TRUE, caption = "Something here")
Table 3: Something here
Mazda RX4 |
21.0 |
6 |
160 |
110 |
3.90 |
2.620 |
16.46 |
0 |
1 |
4 |
4 |
Mazda RX4 Wag |
21.0 |
6 |
160 |
110 |
3.90 |
2.875 |
17.02 |
0 |
1 |
4 |
4 |
Datsun 710 |
22.8 |
4 |
108 |
93 |
3.85 |
2.320 |
18.61 |
1 |
1 |
4 |
1 |
Hornet 4 Drive |
21.4 |
6 |
258 |
110 |
3.08 |
3.215 |
19.44 |
1 |
0 |
3 |
1 |
Hornet Sportabout |
18.7 |
8 |
360 |
175 |
3.15 |
3.440 |
17.02 |
0 |
0 |
3 |
2 |
Table 3!
XT <- xtable::xtable(coef(summary(mod)), caption = "Write your descriptive caption between the quotes", auto = TRUE)
print(XT, type = "html")
Write your descriptive caption between the quotes
|
Estimate
|
Std. Error
|
t value
|
Pr(>|t|)
|
(Intercept)
|
-17.5790949
|
6.7584402
|
-2.601058
|
0.012318816
|
speed
|
3.9324088
|
0.4155128
|
9.463990
|
0.000000000
|
options(table_counter = 3)
htmlTable::htmlTable(round(summary(mod)$coef, 4), css.cell = "padding-left: 1em; padding-right: 1em;", caption ="`dist` regressed on `speed`", align = "rrrr")
Table 4: `dist` regressed on `speed` |
|
Estimate |
Std. Error |
t value |
Pr(>|t|) |
(Intercept) |
-17.5791 |
6.7584 |
-2.6011 |
0.0123 |
speed |
3.9324 |
0.4155 |
9.464 |
0 |
DT::datatable(iris)
pander::pander(summary(mod)$coef, caption = "You add it here")
You add it here
(Intercept) |
-17.58 |
6.758 |
-2.601 |
0.01232 |
speed |
3.932 |
0.4155 |
9.464 |
1.49e-12 |
knitr::kable(head(iris), format = "html", table.attr = "id=\"mytable\"")
Sepal.Length
|
Sepal.Width
|
Petal.Length
|
Petal.Width
|
Species
|
5.1
|
3.5
|
1.4
|
0.2
|
setosa
|
4.9
|
3.0
|
1.4
|
0.2
|
setosa
|
4.7
|
3.2
|
1.3
|
0.2
|
setosa
|
4.6
|
3.1
|
1.5
|
0.2
|
setosa
|
5.0
|
3.6
|
1.4
|
0.2
|
setosa
|
5.4
|
3.9
|
1.7
|
0.4
|
setosa
|
Table 4: Your table caption.
5.1 |
3.5 |
1.4 |
4.9 |
3.0 |
1.4 |
4.7 |
3.2 |
1.3 |
4.6 |
3.1 |
1.5 |
Table 4
# options(table_counter = 3)
htmlTable::htmlTable(round(summary(mod)$coef, 4), css.cell = "padding-left: 1em; padding-right: 1em;", caption ="`dist` regressed on `speed`", align = "rrrr")
Table 5: `dist` regressed on `speed` |
|
Estimate |
Std. Error |
t value |
Pr(>|t|) |
(Intercept) |
-17.5791 |
6.7584 |
-2.6011 |
0.0123 |
speed |
3.9324 |
0.4155 |
9.464 |
0 |
Look at Table 1
For Indy
mx <- data.frame(j = 1:8, n = seq(50, 400, length = 8), ENA1 = rexp(8), ESA1 = rexp(8), ENA2 = rexp(8), ESA2 = rexp(8))
mx <- as.matrix(mx)
mx <- round(mx, 7)
options(table_counter = 5)
htmlTable::htmlTable(mx,
align = "lrllll",
css.cell = "padding-left: 1em; padding-right: 1em;",
ctable=c("single", "single"),
header = c("j", "n", "ESA", "ENA", "ESA", "ENA ∈ ℜ"),
cgroup =c("", "", "(n = 1000, k = 400, α = 0.3)", "(n = 1000, k = 300, β = 0.8)"),
n.cgroup = c(1, 1, 2, 2),
col.rgroup = c("none", "#F7F7F7"),
caption = "Table for Indy")
Table 6: Table for Indy |
| |
| |
(n = 1000, k = 400, α = 0.3) | |
(n = 1000, k = 300, β = 0.8) |
j |
|
n |
|
ESA |
ENA |
|
ESA |
ENA ∈ ℜ |
1 |
|
50 |
|
22.1807098 |
22.1807098 |
|
3.0851341 |
22.1807098 |
2 |
|
100 |
|
0.2547741 |
1.4743526 |
|
0.6461059 |
1.3636034 |
3 |
|
150 |
|
22.1807098 |
22.1807098 |
|
22.1807098 |
22.1807098 |
4 |
|
200 |
|
0.6804109 |
0.2241255 |
|
0.6802636 |
21.4875626 |
5 |
|
250 |
|
4.2917283 |
22.1807098 |
|
22.1807098 |
22.1807098 |
6 |
|
300 |
|
4.2917283 |
0.5814978 |
|
1.4743526 |
1.4743526 |
7 |
|
350 |
|
22.1807098 |
22.1807098 |
|
22.1807098 |
22.1807098 |
8 |
|
400 |
|
0.6772694 |
6.2383597 |
|
1.1222608 |
1.041811 |
The html for symbols and Greek letters can be found online at places such as this.