3

is there a way to print the number of observations for the multinomial logistic regression model in a stargazer table? This sample code illustrates the problem. Thank you.

var1<-sample(c('A', 'B', 'C'), size=1000, replace=T)
var2<-rnorm(n=1000)
var3<-rnorm(n=1000)
df<-data.frame(var1, var2, var3)
library(nnet)
mod1<-multinom(var1~var2+var3, data=df)
library(stargazer)
stargazer(mod1, nobs=T, type="text")
spindoctor
  • 1,719
  • 1
  • 18
  • 42

2 Answers2

1

Stargazer is really nice package however sometimes you have to do some tweaks "by-hand". If you want to have number of observation in you output this is how to go about it:

stargazer(mod1, 
          type="text", 
          add.lines = list(c("n", nrow(df), nrow(df))))

if you want to create a table in latex you can use:

stargazer(mod1, 
          type="latex", 
          add.lines = list(c("\\textit{$n$}", nrow(df), nrow(df))))

This approach is so that once you render it in latex "n" will be in mathematical font.

==============================================
                      Dependent variable:     
                  ----------------------------
                        B              C      
                       (1)            (2)     
----------------------------------------------
var2                  0.0002        -0.055    
                     (0.080)        (0.079)   
                                              
var3                  -0.088         0.012    
                     (0.078)        (0.077)   
                                              
Constant              -0.029         0.030    
                     (0.078)        (0.077)   
                                              
----------------------------------------------
n                      1000          1000     
Akaike Inf. Crit.   2,206.078      2,206.078  
==============================================
Note:              *p<0.1; **p<0.05; ***p<0.01

Hopefully, this is what you have been asking for.

Petr
  • 1,606
  • 2
  • 14
  • 39
  • Nice! However, this assumes that all rows of the data are used in the multinomial regression (no missing data). If the data has missing values the number of observations used in the regression will be lower than the number of rows. To avoid that you can set n to `nrow(mod1$residuals)`. This will give you the full rank of the regression model. – MECoskun Mar 31 '22 at 15:09
0

Would texreg work for you instead?

library(texreg)
screenreg(list(mod1))
# ====================================
#                 B          C        
# ------------------------------------
# (Intercept)         0.01      -0.03 
#                    (0.08)     (0.08)
# var2               -0.06       0.01 
#                    (0.08)     (0.08)
# var3                0.04       0.10 
#                    (0.08)     (0.08)
# ------------------------------------
# AIC              2206.34    2206.34 
# BIC              2235.79    2235.79 
# Log Likelihood  -1097.17   -1097.17 
# Deviance         2194.34    2194.34 
# Num. obs.        1000       1000    
# ====================================
# *** p < 0.001, ** p < 0.01, * p < 0.05

I may be wrong but I'm not sure this can be easily done with nnet and stargazer. You could make your model mimic that of different output. See here for that type of approach.

user63230
  • 4,095
  • 21
  • 43
  • That will work but somehow B and C are currently listed under one column. So the covariates are B: var2, B:var3, C:var2, C:var3. I much prefer this output; how do I get that? – spindoctor Jul 14 '20 at 17:03
  • I'm not sure I understand you, the output looks very similar to `stargazer`? – user63230 Jul 14 '20 at 17:06
  • In your output, you have two columns B and C; one column for the coefficients of var2 and var3 for P (B) ~ P(A) and the second column hsas the saame coefficients but for P (C) . ~ P(A) I like that output, but I'm not getting that output using your code. Instead I get one column that lists all the coefficients in one column. B:var2, B:var3, C:var2, C:var3. Stargazer's output actually looks a lot like what you produced here, and I prefer it, but cannot get that using your texreg code. – spindoctor Jul 14 '20 at 17:17
  • so you're saying when you run `screenreg(list(mod1))`, you get different looking output than I did above? – user63230 Jul 14 '20 at 17:22
  • I dont know, what package version are you using? `packageVersion("texreg") [1] ‘1.36.23’`, maybe refresh R? – user63230 Jul 14 '20 at 17:36
  • 1.37.5!! Maybe that's the problem. – spindoctor Jul 14 '20 at 17:41
  • I installed your version but that doesn't work either! Thanks for helping! – spindoctor Jul 14 '20 at 19:38