Search code examples
rlistlmstargazer

Using list of LM estimates as stargazer input


I'm trying to use stargazer over a several LM estimates at once, say "OLS1",...,"OLS5". I would usually insert them as separate arguments at the beginning of the stargazer input. What I'm looking for is a way to input them all with a list that contains them all, being one argument. Something like

stargazer(list,...)

stargazer arguments explanation states that

one or more model objects (for regression analysis tables) or data frames/vectors/matrices (for summary statistics, or direct output of content). They can also be included as lists (or even lists within lists).

I was wondering what is the correct way to gather LM estimates in a list so that this would work. When I just save the results in a list I get the following error

Error in list.of.objects[[i]] : subscript out of bounds

I will mention that I create the elements storing the estimate using assign. E.G:

assign(some_string,lm(...))

So what I have is a string, called some_string, and I want to put the LM result names some_string inside a list. Using get doesn't help with that.


Solution

  • EDIT: I think you want mget

    library(stargazer)
    
    Y <- rnorm(100)
    X <- rnorm(100)
    
    assign("string_1", lm(Y ~ X))
    assign("string_2", lm(Y ~ X))
    
    my_list <- mget(x = c("string_1", "string_2"))
    
    stargazer(my_list)
    

    works for me?

    library(stargazer)
    
    Y <- rnorm(100)
    X <- rnorm(100)
    fit_1 <- lm(Y ~ X)
    fit_2 <- lm(Y ~ X) 
    
    stargazer(list(fit_1, fit_2))
    

    did you name your list list? maybe it's grabbing the function?