Search code examples
rstatisticscluster-analysislogistic-regressionstargazer

Resolve error in creating table of logit regression results with stargazer in R


I am hoping to present a nice table of a logistic regression measuring hypertension using stargazer which includes the coefficients, standard error, and significance (indicated by stars). When I try and plug in the specifications for stargazer, I see the following error message: "% Error: Unrecognized object type." I've included some sample data/the code I've run below. How might this be resolved? Thank you!

library(stargazer)
library(mfx)

structure(list(AGE = c(40L, 23L, 24L, 18L, 30L, 33L, 32L, 63L, 
22L, 24L), IMMIGRANT = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 1), FAMSIZE = c(2L, 
2L, 2L, 3L, 2L, 6L, 2L, 1L, 2L, 1L), HLTH_INS = c(1, 1, 1, 1, 
1, 0, 1, 1, 1, 0), HYPERTEN = c(0, 0, 0, 0, 0, 0, 0, 1, 0, 0), 
    SMOKE = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1), PSU = c(2L, 1L, 
    2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L)), row.names = c(NA, -10L), class = "data.frame")

#The regression works without adjusting for clustered SE 
logit<-logitmfx(HYPERTEN~AGE+IMMIGRANT+FAMSIZE+HLTH_INS+
                 SMOKE,data=sample,
                atmean=TRUE,robust=T)

logit_mfx_coef <- logit$mfxest[,1]  
logit_mfx_se <- logit$mfxest[,2] 

stargazer(logit, type="text",title = "Predicting Probability of Hypertension",intercept.bottom=FALSE,
          coef = logit_mfx_coef,
          se   = logit_mfx_coef, column.labels="Logit mfx",
          digits=4,align=TRUE) 

Solution

  • Pass logit$fit as the first argument to stargazer().

    The logitmfx() operation returns a bunch of stuff, but stargazer() expects a fitted model object (or a data frame) as its first argument.

    stargazer(logit$fit, type="text",title = "Predicting Probability of Hypertension",intercept.bottom=FALSE,
              coef = logit_mfx_coef,
              se   = logit_mfx_coef, column.labels="Logit mfx",
              digits=4,align=TRUE) 
    

    Output:

    Predicting Probability of Hypertension
    =============================================
                          Dependent variable:    
                      ---------------------------
                               HYPERTEN          
                               Logit mfx         
    ---------------------------------------------
    Constant                    0.0000           
                               (0.0000)          
                                                 
    AGE                                          
                                                 
                                                 
    IMMIGRANT                                    
                                                 
                                                 
    FAMSIZE                                      
                                                 
                                                 
    HLTH_INS                                     
                                                 
                                                 
    SMOKE                                        
                                                 
                                                 
    ---------------------------------------------
    Observations                  10             
    Log Likelihood              -0.0000          
    Akaike Inf. Crit.           10.0000          
    =============================================
    Note:             *p<0.1; **p<0.05; ***p<0.01