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

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)))

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.

Formatting regression results in R for latex

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)

R survey package: svyby + svymean: one vs many variables

Let's assume the a data set mydata with the variables foo1..foo20 which are factors with the labels "Easy" and "Difficult". Now let's consider this code:
library(survey)
svd <- svydesign(ids = ~ 1, weights = ~ weight, data = mydata)
svyby(~ foo1, by = ~ group, svd, svymean)$foo1Difficult
svyby(~ foo1 + foo2 + foo3 + ... + foo20, by = ~ group, svd, svymean)$foo1Difficult
Are the results supposed to be identical? Is there a reason why the results could differ? Why does it make a difference whether I iterate over each variable or use all variables at once?
As #AnthonyDamico pointed out, the difference was caused by NAs.

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:

stargazer, xtable, etc.: outputting custom standard errors with stars, excluding control variables

The image below demonstrates what I am trying to output from R into LaTeX. I'm willing to use any approach/package that works.
As you can see in the image, the desired output has multiple models (the columns) and multiple regressions (the rows).
I have three hurdles:
First, how can I output standard errors from two different models below one point estimate? Neither of the SEs I seek to present are the conventional SEs; both are modified cluster-robust SEs calculated after I run the regression by using custom coeftest() functions, and outputting a coeftest object.
Second, how can I present the stars? I have devised a workaround in R to output the point estimate with the two SE calculations below it, but not with the stars automatically transferred as something like xtable or stargazer does.
Third, I would like to output only the point estimate and standard errors for the treatment variable. As you can see at the bottom of the table, in models (2) & (4) there are control variables, but I do not want to display any further information about them.
Also, it is worth noting that my output is not an lm object, but instead a coeftest object, which is stargazer-compatible but not xtable-compatible.
Take a look at texreg, you will need to modify the latext code, inserting the code you want.
library("texreg");library(lmtest);library("sandwich")
library(nlme)
m1<-lm(distance ~ age, data = Orthodont)
coeftest(m1, vcov=sandwich)
m2<-lm(distance ~ age + Sex, data = Orthodont)
coeftest(m2, vcov=sandwich)
test1=texreg(list(m1,m2),caption="Models",
label="Compmod",stars=c(0.05, 0.01))
#cat(test1)
model.1 <- lme(distance ~ age, data = Orthodont, random = ~ 1)
model.2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
texreg(list(m1,m2,model.1, model.2),caption="Models",
label="Compmod", booktabs = TRUE, dcolumn = TRUE)

Resources