Site icon R-bloggers

glueformula: simply build regression formulas from vectors with variable names

[This article was first published on Economics and R - R posts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The small new package glueformula with a single function gf facilitates constructing regression formulas from vectors with variable names. The syntax is similar to glue strings. Here is an example:

# Example: build a formula
# for ivreg with gf
library(glueformula)

# Contol variables and instruments
contr = c("x1","x2","x3","log(x4)")
instr = c("z1","z2",contr)

# Create formula for ivreg
gf(q ~ p + {contr} | {instr})

## q ~ p + x1 + x2 + x3 + log(x4) | z1 + z2 + x1 + x2 + x3 + log(x4)

There is no big benefit if one wants to estimate a single regression, but in econometrics one often specifies a lot of similar regressions (for robustness checks not p-hacking!) that share a large set of common control variables. Here glueformula can be handy.

You can install it from Github as explained here: https://github.com/skranz/glueformula

There was also a discussion here whether a similar feature should be included into the glue package itself, but it looks as if that is not going to happen.

To leave a comment for the author, please follow the link and comment on their blog: Economics and R - R posts.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.