Search code examples
rconfidence-intervalp-valuestargazer

How do I add coefficients, SE, confidence intervals, and odds ratios in stargazer table?


A previous user asked How do I add confidence intervals to odds ratios in stargazer table? and outlined a clear solution to the problem.

Currently, I am typing out my tables by hand and that is very time consuming. example of my typed out table. Here is a link to the .txt file used.

My model has size as a dependent variable (categorical) and sex (categorical), age (continuous), and year (continuous) as independent variables. I am using mlogit to model the relationship between variables.

The code I used for the model is as follows:

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

library(stargazer)

OR.vector<-exp(ml.Tat$coef)
CI.vector<-exp(confint(ml.Tat))
p.values<-summary(ml.Tat)$CoefTable[,4]

#table with odds ratios and confidence intervals
stargazer(ml.Tat, coef=list(OR.vector), ci=TRUE, ci.custom=list(CI.vector), single.row=T, type="text", star.cutoffs=c(0.05,0.01,0.001), out="table1.txt", digits=4)

#table with coefficients and standard errors
stargazer(ml.Tat, type="text", single.row=TRUE, star.cutoffs=c(0.05,0.01,0.001), out="table1.txt", digits=4) 

The stargazer code I have tried is shown below for a small part of my data:

library(stargazer)
OR.vector<-exp(ml.Tat$coef)
CI.vector<-exp(confint(ml.Tat))
p.values<-summary(ml.Tat)$CoefTable[,4] #incorrect # of dimensions, unsure how to determine dimensions
stargazer(ml.Tat, coef=list(OR.vector), ci=TRUE, ci.custom=list(CI.vector), single.row=T, type="text", star.cutoffs=c(0.05,0.01,0.001), out="table1.txt", digits=4) #gives odds ratio (2.5%CI, 97.5%CI)

Odds ratio and confidence interval output: odds and CI

stargazer(ml.Tat, type="text", single.row=TRUE, star.cutoffs=c(0.05,0.01,0.001), out="table1.txt", digits=4) #gives coeff (SE)`

Coefficient and SE output: coeff and SE output

I can combine odds ratios with confidence intervals or standard errors or coefficients with confidence intervals and standard errors, but when I write all three together the ci=TRUE function seems to overwrite the SE default.

For my dissertation, I need tables to show the coefficients, standard errors, confidence intervals, and odds ratios (and p-values in some format). Is there a way for stargazer to include all four things? Perhaps in two different columns? I am able to export the tables to excel, however without all 4 things in the same stargazer table I am stuck manually putting the two above tables together. This is not a big deal for 1 table, but I am working with 36 models that all need tables (for my dissertation).

How can I use stargazer to show all four things? (odds ratio, confidence intervals, coefficients, and standard errors)


Solution

  • 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