Search code examples
rstargazernnet

Get stargazer to print number of observations in multinomial logistic regression


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")

Solution

  • 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.