maxjohnson
maxjohnson

Reputation: 29

How to automatically choose elements to render to stargazer table

I have saved three regressions (reg1, reg2, reg3) along with their standard errors (reg.se1, reg.se2, reg.se3) and cluster p values (reg.crp1, reg.crp2, reg.crp3). I want to create a chart with stargazer along the lines of:

stargazer(list(reg1, reg2, reg3), se=list(reg.se1, reg.se2, reg.se3), p=list(reg.crp1, reg.crp2,reg.crp3))

This works well but I need to add 30 more regressions, and I was wondering if there was a way to create a loop so that I can do it automatically instead of having to add the extra 30 regressions manually?

Thank you very much,

Max

Upvotes: 0

Views: 104

Answers (1)

Otto Kässi
Otto Kässi

Reputation: 3083

I do not think you need a loop. Some creative string matching for ls() should be enough. I have used stringr library to accomplish this, but ?grep in base R should work too.

library(stringr)

## this expression picks up everything that starts with "reg", but the fourth symbol is not '.'
coeflist <- ls()[!is.na(str_match(ls(), '^reg[^.;]+'))] 

## this expression picks up everything that starts with "reg.se"
selist <- ls()[!is.na(str_match(ls(), '^reg.se'))]

## this expression picks up everything that starts with "reg.crp"
pvallist <- ls()[!is.na(str_match(ls(), '^reg.crp'))]

Now you can create your stargazer table by

stargazer(coeflist, 
          selist,
          pvallist)

You need to make sure that your namespace does not have other elements that would have matching names.

Upvotes: 1

Related Questions