1
votes

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)

2
You need to present data objects created with R code. (That's also a redundant presentation.)IRTFM
@42- I hope the code and screenshots I added helped clarify my difficulties.Blundering Ecologist
Well, they do suggest a rather small number of events. And It's a bit unusual to have 2 intercepts. We are still no in a position to offer coding advice since there is still no data.IRTFM
I've added my data through figshare and clarified the data a bit more in text.Blundering Ecologist
stargazer accepts multiple models and includes these in separate columns. So make a second model (e.g. "ml.TatOR") and replace the coefficients with odds ratios. Then in stargazer, include both models and relevant arguments: stargazer(ml.Tat, ml.TatOR, type="text",ci = c(F,T),column.labels=c("coefficients","odds ratio"), single.row=TRUE)paqmo

2 Answers

3
votes

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
1
votes

Trying to extract those values from stargazer will be painful. The returned value from stargazer calls are just character lines. Instead you should look at the structure of the model. It resemble the structure of glm results:

> names(ml.Tat)
 [1] "coefficients"  "logLik"        "gradient"      "hessian"      
 [5] "est.stat"      "fitted.values" "probabilities" "residuals"    
 [9] "omega"         "rpar"          "nests"         "model"        
[13] "freq"          "formula"       "call"      

And the results of summary.mlogit resemble the results of summary.glm:

> names(summary(ml.Tat))
 [1] "coefficients"  "logLik"        "gradient"      "hessian"      
 [5] "est.stat"      "fitted.values" "probabilities" "residuals"    
 [9] "omega"         "rpar"          "nests"         "model"        
[13] "freq"          "formula"       "call"          "CoefTable"    
[17] "lratio"        "mfR2"         

So you should be using the [['CoefTable']] values which are most likely in the form of a matrix ... since they should be similar to the value of summary(mod)$coefficients.

> summary(ml.Tat)$CoefTable
                        Estimate   Std. Error     t-value     Pr(>|t|)
large:(intercept)  -444.39366673 2.209599e+01 -20.1119625 0.000000e+00
medium:(intercept) -187.91353927 1.195601e+01 -15.7170716 0.000000e+00
unk:(intercept)     117.92620950 2.597647e+02   0.4539731 6.498482e-01
large:age             0.02508481 4.088134e-03   6.1360059 8.462202e-10
medium:age            0.00804593 2.567671e-03   3.1335519 1.727044e-03
unk:age               0.01841371 4.888656e-02   0.3766620 7.064248e-01
large:sexM            1.38163894 6.068763e-02  22.7663996 0.000000e+00
medium:sexM           0.73646230 3.304341e-02  22.2877210 0.000000e+00
unk:sexM              1.27203654 7.208632e-01   1.7646018 7.763071e-02
large:yy              0.21941592 1.098606e-02  19.9722079 0.000000e+00
medium:yy             0.09308689 5.947246e-03  15.6521007 0.000000e+00
unk:yy               -0.06266765 1.292543e-01  -0.4848399 6.277899e-01

The way should now be clear to complete your homework assignment.