Odds ratios instead of logits in stargazer() LaTeX output
As per symbiotic comment in 2014, more recent versions of ''stargazer'' have the options ''apply.*'' for ''coef'' ''se'' ''t'' ''p'' and ''ci'' allowing the direct transformation of these statistics.
apply.coef a function that will be applied to the coefficients.
apply.se a function that will be applied to the standard errors.
apply.t a function that will be applied to the test statistics.
apply.p a function that will be applied to the p-values.
apply.ci a function that will be applied to the lower and upper bounds of the confidence intervals.
Meaning you can directly use...
stargazer(model,
apply.coef = exp,
apply.se = exp)
EDIT : I have noticed however that simply exponentiating the CIs does not give what you would expect.
EDIT : You can obtain the correct CIs using the method described here.
Stargazer for reporting odds ratios in logit report wrong significance stars
generally you should strive to provide a minimal working example so we can reproduce your results - in this case it would be great to have data and the code you use to produce your basic.logit.model
.
To your question, apply.coef
only transforms your coefficients but not the standard errors as your results show. stargazer calculates the significance level using these untransformed SEs which results in non-significant coefficients.
To avoid this, provide stargazer with the custom p-values (those from the original model) using the p
argument.
This should work for you
p.values <- list(summary(basic.logit.model)$coefficients[,4]
stargazer(basic.logit.model,
type="html",
apply.coef = OR,
p = p.values,
column.labels = c("Base"),
dep.var.labels.include = FALSE,
digits=2, out=("basic_model_only.htm"))
How do I add confidence intervals to odds ratios in stargazer table?
You can use the ci.custom
argument to feed stargazer
a list with custom confidence intervals (first column is the lower bound, second column is the upper bound). In your example, all you have to do is call:
stargazer(mylogit, coef = list(OR.vector), ci = T,
ci.custom = list(CI.vector), single.row = T, type = "text")
Alternatively, you can define your own function to exponentiate values and simply apply this function to your coefficients and/or confidence intervals (using arguments apply.coef
and apply.ci
):
exponentiate <- function(x) exp(x)
stargazer(mylogit, ci=T, apply.coef=exponentiate, apply.ci=exponentiate,
single.row = T, type="text")
How do I add coefficients, SE, confidence intervals, and odds ratios in stargazer table?
Stargazer accepts multiple models and appends each to a new row. So, you can make a second model and replace the standard coefficients with odds ratios and pass this to the stargazer
call.
tattoo <- read.table("https://ndownloader.figshare.com/files/6920972",
header=TRUE, na.strings=c("unk", "NA"))
library(mlogit)
Tat<-mlogit.data(tattoo, varying=NULL, shape="wide", choice="size", id.var="date")
ml.Tat<-mlogit(size~1|age+sex+yy, Tat, reflevel="small", id.var="date")
ml.TatOR<-mlogit(size~1|age+sex+yy, Tat, reflevel="small", id.var="date")
ml.TatOR$coefficients <- exp(ml.TatOR$coefficients) #replace coefficents with odds ratios
library(stargazer)
stargazer(ml.Tat, ml.TatOR, ci=c(F,T),column.labels=c("coefficients","odds ratio"),
type="text",single.row=TRUE, star.cutoffs=c(0.05,0.01,0.001),
out="table1.txt", digits=4)
The argument ci=c(F,T)
suppresses the confidence interval in the first column (so SEs are shown instead) and shows it in the second column. The column.labels
argument lets you name the columns.
====================================================================
Dependent variable:
-------------------------------------------------
size
coefficients odds ratio
(1) (2)
--------------------------------------------------------------------
large:(intercept) -444.6032*** (22.1015) 0.0000 (-43.3181, 43.3181)
medium:(intercept) -187.9871*** (11.9584) 0.0000 (-23.4381, 23.4381)
large:age 0.0251*** (0.0041) 1.0254*** (1.0174, 1.0334)
medium:age 0.0080** (0.0026) 1.0081*** (1.0030, 1.0131)
large:sexM 1.3818*** (0.0607) 3.9821*** (3.8632, 4.1011)
medium:sexM 0.7365*** (0.0330) 2.0886*** (2.0239, 2.1534)
large:yy 0.2195*** (0.0110) 1.2455*** (1.2239, 1.2670)
medium:yy 0.0931*** (0.0059) 1.0976*** (1.0859, 1.1093)
--------------------------------------------------------------------
Observations 18,162 18,162
R2 0.0410 0.0410
Log Likelihood -15,882.7000 -15,882.7000
LR Test (df = 8) 1,357.1140*** 1,357.1140***
====================================================================
Note: *p<0.05; **p<0.01; ***p<0.001
R package stargazer produces two table outputs instead of one
You can use the package called gtsummary
to make regression tables.
Replace your stargazer line with this:
library(gtsummary)
tbl_regression(glfit, exponentiate = TRUE,
intercept = TRUE) %>%
add_significance_stars() %>%
add_glance_source_note(include = c(AIC, BIC, df.residual,nobs)) %>%
modify_caption("Logistic regression on species")
The tbl_regression()
creates a basic table and the other functions called after pipes further customize the table.
see here for more details: https://www.danieldsjoberg.com/gtsummary/
How to keep just one variable in stargazer regression output? (oposite of omit)
Instead of using omit
, you can use keep
to keep only the variables that you need.
stargazer::stargazer(model1, type = "text", keep = 'Dummy1:Dummy2:Dummy3')
================================================
Dependent variable:
---------------------------
Y
------------------------------------------------
Dummy1:Dummy2:Dummy3 42.430
(35.315)
------------------------------------------------
Observations 100
R2 0.145
Adjusted R2 0.080
Residual Std. Error 43.587 (df = 92)
F Statistic 2.222** (df = 7; 92)
================================================
Note: *p<0.1; **p<0.05; ***p<0.01
Related Topics
How to Install Multiple Packages
How to Add Rmse, Slope, Intercept, R^2 to R Plot
Most Mature Sparse Matrix Package for R
For Each Group Summarise Means for All Variables in Dataframe (Ddply? Split)
Replace Two Dots in a String with Gsub
How to Plot 3D Scatter Diagram Using Ggplot
Dplyr Join Warning: Joining Factors with Different Levels
Rearrange Dataframe to a Table, the Opposite of "Melt"
Draw a Chronological Timeline with Ggplot2
Plotting the Average Values for Each Level in Ggplot2
Manipulating Multiple Files in R
Copying and Modifying a Default Theme