问题
I'm having a bit of trouble using texreg
to create output TeX
files for multiple multinom
models.
Let's use a version of this multionmial logit setup for concreteness:
library(foreign)
library(nnet)
library(data.table)
ml <- data.table(read.dta("http://www.ats.ucla.edu/stat/data/hsbdemo.dta"))
mnl<-lapply(c(model1="male",model2="female"),
function(x){multinom(prog ~ ses + write,
data = ml[female==x,])})
I want to create two tables: one for coefficients corresponding to "academic" and one corresponding to "vocation" (the third outcome of prog
, "general", is omitted).
Basically, I don't know how separately access the levels from an nnet
/multinom
object, because I don't know where the coefficients are stored. If we just run texreg(mnl)
, we'll get a table with 4 columns:
model1 & model2 & NA & NA \\
What's worse, these columns are mislabeled--the actual order is
model1-academic, model1-vocation, model2-academic, model2-vocation
If I simply wanted a table of male and a table of female coefficients, I would simply run texreg(mnl["model1"])
and texreg(mnl["model2"])
, but it's not clear how to split the coefficients by levels of prog
.
How can I use texreg
to get the two tables I want, which (skeletally) look like:
#TABLE 1: ACADEMIC
> cbind(male=coef(mnl[["model1"]])[1,],
+ female=coef(mnl[["model2"]])[1,])
male female
(Intercept) -3.23410968 -3.01401061
sesmiddle 1.15893835 0.17086744
seshigh 2.00007946 0.57690699
write 0.05464579 0.06598217
#TABLE 2: VOCATION
> cbind(male=coef(mnl[["model1"]])[2,],
+ female=coef(mnl[["model2"]])[2,])
male female
(Intercept) 3.69215046 1.57234796
sesmiddle 1.15573930 0.69043245
seshigh 0.67476976 -0.16955825
write -0.09640053 -0.03412729
回答1:
Got in contact with package developer Prof. Philip Leifeld, and he so graciously added functionality for handling exactly this problem to texreg
, specifically adding two options: levels
and beside
.
The answer to my original question is accomplished through levels
:
texreg(mnl,levels="vocation")
texreg(mnl,levels="academic")
\begin{table}
\begin{center}
\begin{tabular}{l c c }
\hline
& vocation & vocation \\
\hline
(Intercept) & $3.69^{*}$ & $1.57$ \\
& $(1.77)$ & $(1.84)$ \\
sesmiddle & $1.16$ & $0.69$ \\
& $(0.79)$ & $(0.65)$ \\
seshigh & $0.67$ & $-0.17$ \\
& $(0.99)$ & $(0.91)$ \\
write & $-0.10^{*}$ & $-0.03$ \\
& $(0.04)$ & $(0.04)$ \\
\hline
AIC & 167.56 & 218.73 \\
BIC & 187.65 & 240.26 \\
Log\ Likelihood & -75.78 & -101.36 \\
Deviance & 151.56 & 202.73 \\
Num.\ obs. & 91 & 109 \\
\hline
\multicolumn{3}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
(I'll suppress output from "academic"
to save space, but you get the point).
The beside
functionality works slightly differently, instead stacking the coefficients, if that's what suits your fancy:
\begin{table}
\begin{center}
\begin{tabular}{l c c }
\hline
& model1 & model2 \\
\hline
academic: (Intercept) & $-3.23$ & $-3.01$ \\
& $(1.66)$ & $(1.82)$ \\
academic: sesmiddle & $1.16$ & $0.17$ \\
& $(0.75)$ & $(0.58)$ \\
academic: seshigh & $2.00^{*}$ & $0.58$ \\
& $(0.85)$ & $(0.69)$ \\
academic: write & $0.05$ & $0.07^{*}$ \\
& $(0.03)$ & $(0.03)$ \\
vocation: (Intercept) & $3.69^{*}$ & $1.57$ \\
& $(1.77)$ & $(1.84)$ \\
vocation: sesmiddle & $1.16$ & $0.69$ \\
& $(0.79)$ & $(0.65)$ \\
vocation: seshigh & $0.67$ & $-0.17$ \\
& $(0.99)$ & $(0.91)$ \\
vocation: write & $-0.10^{*}$ & $-0.03$ \\
& $(0.04)$ & $(0.04)$ \\
\hline
AIC & 167.56 & 218.73 \\
BIC & 187.65 & 240.26 \\
Log\ Likelihood & -75.78 & -101.36 \\
Deviance & 151.56 & 202.73 \\
Num.\ obs. & 91 & 109 \\
\hline
\multicolumn{3}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
来源:https://stackoverflow.com/questions/30017129/texreg-ing-multiple-multinom-models