Search code examples
rstargazer

Stargazer one line per data set


I am running regressions using various subsets of a data set and a number of dependent variables.

An example using attitude data:

library(stargazer)

#REGRESSIONS USING DATASET 1
linear1.1 <- lm(rating ~ complaints, data = attitude) #dependent 1
linear1.2 <- lm(privileges ~ complaints, data = attitude) #dependent 2

#REGRESSIONS USING DATASET 2
linear2.1 <- lm(rating ~ complaints, data = attitude[1:15,]) #dependent 1
linear2.2 <- lm(privileges ~ complaints, data = attitude[1:15,]) #dependent 2

As you can see, both depdendent variables rating and privileges are used in regressions for both subsets of the data. Using a standard stargazer approach produces the following table:

stargazer::stargazer(linear1.1,linear1.2,linear2.1,linear2.2,
                     omit.stat = "all",
                     keep = "complaints")

Table 1

Each column represents one of the regression models. However, I'd like to have each column represent one dependent variable. Each subset of the data should represent one row:

Table 2

I have produced this table by hand. Does anyone know whether it's possible to achieve this using stargazer? I have a lot of regression subsets and dependent variables, so a highly automatic solution is appreciated. Thanks!


Solution

  • I just wonder if this little modification from this (Exporting output of custom multiple regressions from R to Latex) will suit you

    library(stargazer)
    library(broom)
    
    ## generate dummy data
    set.seed(123)
    x <- runif(1000)
    z <- x^0.5
    y <-  x + z + rnorm(1000, sd=.05)
    model1 <- lm(y ~ x)
    model2 <- lm(y ~ z)
    
    ## transform model summaries into dataframes
    tidy(model1) -> model1_tidy
    tidy(model2) -> model2_tidy
    
    output <- rbind(model1_tidy,model2_tidy)
    stargazer(output, type='text', summary=FALSE)