0

I'm trying to use sapply to regress on a number of outcome variables. The sapply function works and returns the linear models properly, BUT the Call returns '''Call: FUN(formula = X[[i]], data = ..1)'''. I believe that this is the reason that I cannot then plug the sapply output into Stargazer without getting this error:

% Error: Unrecognized object type.

I have compare the output of the sapply variable with a manual plug in for the first outcome variable. The results are the same but Stargazer won't work with the sapply version.

This works:

normal_approach <- lm(y_1~x, data = df)
stargazer(normal_approach, type = 'text')

This does not:

test_col <- c("y_1") #I have more variables but troubleshooting so removed them
names(n) <- names(test_col)

forms <- paste(test_col, '~x')
sapp_approach <- sapply(forms, lm, data = df, simplify = FALSE, USE.NAMES = TRUE)

sapply_version <- sapp_approach$`y_1~x' #confirmed that this variable is correct
stargazer(sapply_version, type = 'text')

% Error: Unrecognized object type.

The Call function in the sapply approach is the problem, I think.

Call: FUN(formula = X[[i]], data = ..1)
Kay
  • 89
  • 1
  • 9
  • Without a reproducible example it is not that easy to tell for sure, but you could try an approach similar to this one: https://stackoverflow.com/questions/25007470/lapply-with-anonymous-function-call-to-svytable-results-in-object-x-not-found/25007913#25007913 – user12728748 Apr 01 '21 at 00:09
  • That is because you are passing a list that contains the model object to stargazer, instead of the model object directly.. In your particuar example, if you do stargazer(sapply_version[[1]], type = 'text') it should work. For more models you can do lapply(sapply_version, stargazer, type = "text") – Tomas Capretto Apr 01 '21 at 01:57
  • @TomasCapretto I've tried that before posting. It doesn't work. I'm fairly certain the issue is the Call function. – Kay Apr 01 '21 at 02:43

1 Answers1

1

stargazer has issues while displaying the output for list of models. A "hack" would be to get data in long format before creating the model.

Since there is no data shared here's a way using mtcars dataset keeping only the first 3 columns from it. In place of your x column I am using disp here.

library(stargazer)

df <- mtcars[1:3]
df1 <- tidyr::pivot_longer(df, cols = -disp)
list_df <- split(df1, df1$name)

lm_model_list <- lapply(list_df, function(x) lm(disp~value, x))

Output -

#For one model
stargazer(lm_model_list$cyl, type = 'text')


===============================================
                        Dependent variable:    
                    ---------------------------
                               disp            
-----------------------------------------------
value                        62.599***         
                              (5.469)          
                                               
Constant                    -156.609***        
                             (35.181)          
                                               
-----------------------------------------------
Observations                    32             
R2                             0.814           
Adjusted R2                    0.807           
Residual Std. Error      54.385 (df = 30)      
F Statistic           130.999*** (df = 1; 30)  
===============================================
Note:               *p<0.1; **p<0.05; ***p<0.01


#For list of models
stargazer(lm_model_list, type = 'text')


==========================================================
                                  Dependent variable:     
                              ----------------------------
                                          disp            
                                   (1)            (2)     
----------------------------------------------------------
value                           62.599***     -17.429***  
                                 (5.469)        (1.993)   
                                                          
Constant                       -156.609***    580.884***  
                                 (35.181)      (41.740)   
                                                          
----------------------------------------------------------
Observations                        32            32      
R2                                0.814          0.718    
Adjusted R2                       0.807          0.709    
Residual Std. Error (df = 30)     54.385        66.863    
F Statistic (df = 1; 30)        130.999***     76.513***  
==========================================================
Note:                          *p<0.1; **p<0.05; ***p<0.01
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213