Formatting regression results in R for latex - r

I am trying to create a regression results table in R for latex. I would like this table to have two separate columns: one for the estimates and one for the standard error. The following code
library(fixest)
library(car)
library(pander)
##Using the built-in CO2 data frame, run regression
i<- feols(conc ~ uptake + Treatment | Type, CO2, vcov = "hetero")
summary(i)
##Create regression table for latex
etable(i, postprocess.df = pandoc.table.return, style = "rmarkdown")
my_style = style.df(depvar.title = "", fixef.title = "",
fixef.suffix = " fixed effect", yesNo = "yes", default = TRUE)
setFixest_etable(style.df = my_style, postprocess.df = pandoc.table.return)
etable(i, style = "rmarkdown", caption = "New default values", se.below = NULL )
etable(i, tex = TRUE)
print(etable(i, tex = TRUE), file = "filename2.tex")
When put into Latex document on overleaf.com the following image is produced.
How can I alter my above code to have estimates and standard error in different columns in my table?

You can try the following:
modelsummary(your_regression, fmt=2,
estimate = c("{estimate}{stars} (std.error)"),
statistic = c(),
output = "latex")
What I like with modelsummary too, is that it enable to put many different model as a list to compare them.

Thanks to #léo-henry for suggesting modelsummary. I just wanted to point out that in the latest version of the package you can use the shape argument to display the estimates and standard errors (or other statitics) side-by-side. You will find details here: https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#shape
Here is a minimal example:
library(modelsummary)
mod <- lm(mpg ~ hp + drat, data = mtcars)
modelsummary(mod, shape = term ~ model + statistic)

Related

Reporting mgcv::gam summary with modelsummary

I'm attempting to report the model summary from mgcv::gam() using the modelsummary package. The flextable package provides a summary that is consistent with the summary output in R and what is often presented in publications. It separates out reporting for the fixed/parametric effects and the smooth terms.
Although flextable works well, I'd like to use modelsummary (mainly for it's ability to output to gt, kable, etc.). My plan was to produce two separate tables and report the appropriate data for parametric and smooth terms separately (there might be a better way?). However, I get hung up trying to omit coefficients in modelsummary().
Flextable example:
library(mgcv)
library(flextable)
library(modelsummary)
dat <- gamSim(1, n = 4000, dist = "normal", scale = 2)
mod <- gam(y ~ s(x0) + s(x1) + s(x2), data = dat)
flextable::as_flextable(mod)
My first step at getting the summary for parametric terms using modelsummary():
modelsummary(mod,
estimate = "estimate",
statistic = c("Std.Error" = "std.error",
"t-value" = "statistic",
"p-value" = "p.value"),
shape = term ~ model + statistic,
gof_map = NA)
I want to drop the smooth terms and include those in a different table or group, so I tried the coef_omit argument:
modelsummary(mod,
estimate = "estimate",
statistic = c("Std.Error" = "std.error",
"t-value" = "statistic",
"p-value" = "p.value"),
coef_omit = "^(?!.*Intercept)", #this should retain the intercept term
omit = ".*",
shape = term ~ model + statistic,
gof_map = NA)
Error in if (dat$part[i] == "estimates" && dat[[column]][i - 1] == dat[[column]][i]) { :
missing value where TRUE/FALSE needed
Interestingly, if I remove the shape argument to report statistics in "long format" the error goes away. I might be approaching formatting this summary completely wrong and am open to suggestions.

Texreg: Format "groups"-headings with bold, italics etc

I am using the texreg-package to output nice regression tables in R.
However, when using the groups parameter to group variables, I would like to format the headings with bold/italics or similar to make them stand more out. One possible solution is to export the texreg using htmlreg and manually adding font-weight: bold; in the style parameters (and I guess that there are similar fixes for each output-type).
Do anyone know if there is an easier way that it can be done - before leaving R?
Here is an example from their own documentation with groups headings added:
library("nlme")
library("texreg")
model.1 <- lme(distance ~ age, data = Orthodont, random = ~ 1)
model.2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
texreg(list(model.1, model.2), booktabs = TRUE, dcolumn = TRUE, groups = c("A" = list(1:1), "B" = list(2:3)))

Removing table components in stargazer

I am putting together tables using stargazer and presenting them in a HTML file using RMarkdown. Reproducible code to create the image is pasted below.
I would like to remove the stars and standard errors associated with the constant ("Alpha" in the image below).
I understand that I can manually overwrite the table components but is there a way that I can automatically retain stars and SE for everything but the alpha row? To re-write everything into character vectors seems a little cumbersome.
Code:
library(tidyverse)
library(stargazer)
mdl1 <- lm(mpg~wt, mtcars)
mdl2 <- lm(mpg~disp, mtcars)
mdls <- list(mdl1,mdl2)
column.labels <- c('model 1',
'model 2')
covariate.labels <- c('Beta 1',
'Beta 2',
'Alpha')
keep.stat <- c('n')
stargazer(mdls, type = 'html',
column.labels = column.labels,
covariate.labels = covariate.labels,
column.sep.width = "10pt",
dep.var.labels.include = F,
keep.stat = keep.stat)
In a comment you asked if an alternative to stargazer could help you
with this. The answer is “yes”, the modelsummary
package can do this
relatively easily. (Disclaimer: I am the maintainer.)
I say “relatively” because what you are asking is very idiosyncratic, so
I don’t think you should expect it to work out of the box in any
package. But here’s an example.
First, we start with a basic table with nice labels:
library(modelsummary)
library(broom)
models <- list(
lm(mpg ~ wt + hp, mtcars),
lm(mpg ~ disp + hp, mtcars))
coef_map <- c(
"wt" = "Weight",
"disp" = "Displacement",
"hp" = "Horse Power")
modelsummary(models, stars = TRUE, coef_map = coef_map)
The Customizing Existing Models
section
of the documentation explains that modelsummary allows you to
overwrite any estimate (coef, standard error, p value, etc.) by defining
a new function called tidy_custom.CLASSNAME, where CLASSNAME refers
to the type of model object you are trying to summarize.
In our example above, we summarize lm models:
class(models[[1]])
## [1] "lm"
Therefore, our customizing function will be called tidy_custom.lm. Say
your goal is to remove the standard errors, stars, and p values from the
table, but only for the variable hp. What we can do is overwrite the
estimates with NA (please refer to the docs linked above for a
detailed explanation):
tidy_custom.lm <- function(model) {
out <- tidy(model)
out$p.value[out$term == "hp"] <- NA
out$std.error[out$term == "hp"] <- NA
return(out)
}
modelsummary(models, stars = TRUE, coef_map = coef_map)

Is there a way to write a regression table from R with coeffecients, se, p, odds ratio, and CIs?

I have rather robust data and would like to write my regression outputs to a "journal ready" format from R.
At the moment, I have gotten the outreg package to work in getting the coefficients, se, and p-values (code below). However, I cannot seem to find how I can also include the odds ratios or confidence intervals. Does anyone have suggestions on such a package that does this, or how I can achieve this using outreg?
Above-mentioned code for outreg table:
dm1.output <- list(dm1a, dm1b, dm1c, dm1d, dm1e, dm1f, dm1g, dm1h)
dm1.output2 <- as.data.frame(outreg(dm1.output, pv=T, starred = c("pv")))
If they are standard models you could look at modelsummary here:
library(modelsummary)
models <- list("Model 1" <- glm(am ~ mpg, data = mtcars, family = binomial),
"Model 2" <- glm(am ~ cyl, data = mtcars, family = binomial))
modelsummary(models, exponentiate = TRUE, stars = T, statistic = 'conf.int', conf_level = .95)
Or stargazer:
library(stargazer)
model_1 <- glm(am ~ mpg, data = mtcars, family = binomial)
stargazer(model_1, coef = list(exp(model_1$coefficients)), type = "text")
Or texreg
library(texreg)
model_trexreg <- texreg::extract(model_1)
screenreg(model_1, override.coef = exp(model_trexreg#coef), override.se = exp(model_trexreg#se))
The stargazer package is good for your tasks. It was designed with the goal of producing "journal ready" tables, since the developer is an academic.

sjt.lmer displaying incorrect p-values

I've just noticed that sjt.lmer tables are displaying incorrect p-values, e.g., p-values that do not reflect the model summary. This appears to be a new-ish issue, as this worked fine last month?
Using the provided data and code in the package vignette
library(sjPlot)
library(sjmisc)
library(sjlabelled)
library(lme4)
library(sjstats)
load sample data
data(efc)
prepare grouping variables
efc$grp = as.factor(efc$e15relat)
levels(x = efc$grp) <- get_labels(efc$e15relat)
efc$care.level <- rec(efc$n4pstu, rec = "0=0;1=1;2=2;3:4=4",
val.labels = c("none", "I", "II", "III"))
data frame for fitted model
mydf <- data.frame(
neg_c_7 = efc$neg_c_7,
sex = to_factor(efc$c161sex),
c12hour = efc$c12hour,
barthel = efc$barthtot,
education = to_factor(efc$c172code),
grp = efc$grp,
carelevel = to_factor(efc$care.level)
)
fit sample models
fit1 <- lmer(neg_c_7 ~ sex + c12hour + barthel + (1 | grp), data = mydf)
summary(fit1)
p_value(fit1, p.kr =TRUE)
model summary
p_value summary
sjt.lmer output does not show these p-values??
Note that the first summary comes from a model fitted with lmerTest, which computes p-values with df based on Satterthwaite approximation (see first line in output).
p_value(), however, with p.kr = TRUE, uses the Kenward-Roger approximation from package pbkrtest, which is a bit more conservative.
Your output from sjt.lmer() seems to be messed up somehow, and I can't reproduce it with your example. My output looks ok:

Resources