Blundering Ecologist - 19 days ago 22

R Question

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

The 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:

`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:

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`

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?

Thank you in advance!

UPDATE: Here is a link to the .txt file used.

UPDATE: 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.

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

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

UPDATE: With stargazer 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).

Answer

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.