I have multiple regression models in R, which I want to summarize in a nice table format that could be included in the publication. I have all the results ready, but couldn\
For text table, try this:
x<-rnorm(1:20)
y<-(1:20)/10+x
result <- lm(y~x)
library(stargazer)
stargazer(result, type = "text")
results in...
===============================================
Dependent variable:
---------------------------
y
-----------------------------------------------
x 0.854***
(0.108)
Constant 1.041***
(0.130)
-----------------------------------------------
Observations 20
R2 0.777
Adjusted R2 0.765
Residual Std. Error 0.579 (df = 18)
F Statistic 62.680*** (df = 1; 18)
===============================================
Note: *p<0.1; **p<0.05; ***p<0.01
For multiple regression, just do
stargazer(result, result, type = "text")
And, just for the sake of making the asked outcome.
addStars <- function(coeffs) {
fb <- format(coeffs[, 1], digits = 4)
s <- cut(coeffs[, 4],
breaks = c(-1, 0.01, 0.05, 0.1, 1),
labels = c("***", "**", "*", ""))
sb <- paste0(fb, s)
}
addPar <- function(coeffs) {
se <- format(coeffs[, 2], digits = 3)
pse <- paste0("(", se, ")")
}
textTable <- function(result){
coeffs <- result$coefficients
lab <- rownames(coeffs)
sb <- addStars(coeffs)
pse <- addPar(coeffs)
out <- cbind(lab,sb, pse)
colnames(out) <- NULL
out
}
print(textTable(result), quote = FALSE)
You can use xtable::xtable
, Hmisc::latex
, Gmisc::htmltable
etc. once you have a text table. Someone posted a link in comments. :)
The Broom package is very good for making regression tables nice for export. Results can then be exported to csv for tarting up with Excel or one can use Rmarkdown and the kable function from knitr to make Word documents (or latex).
require(broom) # for tidy()
require(knitr) # for kable()
x<-rnorm(1:20)
y<-(1:20)/10+x
model <- lm(y~x)
out <- tidy(model)
out
term estimate std.error statistic p.value
1 (Intercept) 1.036583 0.1390777 7.453261 6.615701e-07
2 x 1.055189 0.1329951 7.934044 2.756835e-07
kable(out)
|term | estimate| std.error| statistic| p.value|
|:-----------|--------:|---------:|---------:|-------:|
|(Intercept) | 1.036583| 0.1390777| 7.453261| 7e-07|
|x | 1.055189| 0.1329951| 7.934044| 3e-07|
I should mention that I now use the excellent pixiedust for exporting regression results as it allows much finer control of the output, allowing the user to do more in R and less in any other package.
see the vignette on Cran
library(dplyr) # for pipe (%>%) command
library(pixiedust)
dust(model) %>%
sprinkle(cols = c("estimate", "std.error", "statistic"), round = 2) %>%
sprinkle(cols = "p.value", fn = quote(pvalString(value))) %>%
sprinkle_colnames("Term", "Coefficient", "SE", "T-statistic",
"P-value")
Term Coefficient SE T-statistic P-value
1 (Intercept) 1.08 0.14 7.44 < 0.001
2 x 0.93 0.14 6.65 < 0.001