1

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)

Blundering Ecologist
  • 1,199
  • 2
  • 14
  • 38
  • You need to present data objects created with R code. (That's also a redundant presentation.) – IRTFM Nov 20 '16 at 09:03
  • @42- I hope the code and screenshots I added helped clarify my difficulties. – Blundering Ecologist Nov 20 '16 at 14:39
  • 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 Nov 20 '16 at 16:51
  • I've added my data through figshare and clarified the data a bit more in text. – Blundering Ecologist Nov 20 '16 at 16:56
  • Looks like stargazer might not be the right method to to extract all those values at once. (Perhaps it's author agrees with me that they would be redundant.) It appears you may be using a different regression program than `glm`. I don't think it can produce that sort of model. I do have the data downloaded and the dataframe shows18,171 rows with the size variable having 4 levels, so I'm guessing you excluded the 9 "unk" cases. If you want an answer specific to this problem you need to include the data processing and modeling code. – IRTFM Nov 20 '16 at 17:47
  • Warning: The download facility used in the question used browser facilities to display a bogus popup message about needing virus checking. – IRTFM Nov 20 '16 at 17:52
  • @42- I did exclude "unk" (unknown) cases from the analysis. I updated my question with the code I used. If you could let me know what is meant by data processing (still learning the ropes!) I can add that too. – Blundering Ecologist Nov 20 '16 at 19:33
  • 1
    `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 Nov 20 '16 at 20:09
  • @paqmo If I understand correctly, I could code my model `ml.Tat` twice to have the `stargazer` output show the four things I need in my table? My current model looks like `ml.Tat<-mlogit(size~1|age+sex+yy, Tat, reflevel="small", id.var="date")` and I do not specify anything about model output. How can I go from an output that shows coefficients and standard errors to odds ratios and confidence intervals? – Blundering Ecologist Nov 20 '16 at 20:42
  • Include a second model: `ml.TatOR <-mlogit(size~1|age+sex+yy, Tat, reflevel="small", id.var="date")`. Replace coefficients with odds ratios: `ml.TatOR$coef <- exp(ml.TatOR$coef)`. Then, put both models into your `stargazer` call as above. The argument `ci = c(F,T)` suppresses the confidence interval in the first column, but not the second. – paqmo Nov 20 '16 at 20:45

2 Answers2

3

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
paqmo
  • 3,649
  • 1
  • 11
  • 21
  • Thank you for going through the trouble of writing the code out. I appreciate your patience and your help. Your solution has fully answered my question. – Blundering Ecologist Nov 20 '16 at 21:02
  • I used the code above and for some reason my output for both columns is the same (that is, the coefficient is repeated in the second column instead of showing the odds ratio). For example for `large:age` it is both `0.0251***` under coefficients and odds ratio. Might there be a mistake in the above code or could this error be specific to my R console? – Blundering Ecologist Nov 20 '16 at 21:18
  • Thank you so much! You have saved me so much time since I was doing everything manually before. Again, I appreciate you taking the time to help me with this. All the best! – Blundering Ecologist Nov 20 '16 at 21:36
1

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks @42-. Your help is much appreciated. If only it was homework! I am working on my PhD, actually. – Blundering Ecologist Nov 20 '16 at 20:23
  • I should clarify: this solution allows me to see the output (that makes sense), however, I suppose the answer may be that it is not possible to have a stargazer table that has all 4 things (odds ratio, confidence intervals, coefficients, and standard errors). I have been using stargazer as a tool to efficiently and aesthetically export my data to excel (I have 36 tables of data), so it seems like I will have to copy and paste the two together perhaps? – Blundering Ecologist Nov 20 '16 at 20:28
  • It should be fairly easy to construct a single function that delivers a "print"-method for cbind of that result with the exp(coef), exp(coef+1.96*se) and coef(coef-1.96*se). Then use that function on each of the 36 models with a for-loop. I'm actually surprised that stargazer can't handle that. I thought it had a matrix method. The reason I thought it was homework is that there was another questioner with a very similar mlogit model specification posting yesterday. I thought the coincidence was highly improbable (but quantum mechanically possible, I suppose.) – IRTFM Nov 20 '16 at 23:04
  • I see what you mean. Although, the solution posed by another user did do a quick fix. It seems I was going about the model set-up in stargazer incorrectly. I did ask a question earlier in Cross Validated about the proper interpretation of odds ratios from mlogit output, perhaps that is what you read? Either way, thank you again for all your help. It's much appreciated! – Blundering Ecologist Nov 20 '16 at 23:48