Skip to content
String interpolation to build regression formulas
R
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R Bugfix Rd file Jan 10, 2020
man Bugfix Rd file Jan 10, 2020
.Rbuildignore First commit Jan 9, 2020
.gitignore First commit Jan 9, 2020
DESCRIPTION Bugfix Rd file Jan 10, 2020
NAMESPACE Bugfix Rd file Jan 10, 2020
README.md Add drat installation to Github Jan 11, 2020
glueformula.Rproj Add varnames_snippet Jan 10, 2020

README.md

glueformula: string interpolation to build regression formulas

The main function is gl. It is a variant of glue to simplify building regression formulas given vectors of variable names.

library(glueformula)

# Example: build a formula for ivreg
# with gf

contr = c("x1","x2","x3") # exogenous control variables
instr = c(contr,"z1","z2") # instruments

gf(q ~ p + {contr} | {instr})

Another helper function is varnames_snippet. It just uses the colnames of a data frame to create a code snippet with a vector of all variable names. Here is an example:

df = dplyr::tibble(x=1,y=5,z=4,`a b`=4)
varnames_snippet(df)

This prints (and on Windows copies to your clipboard) the following code snippet:

c("x", "y", "z", "`a b`")

You can then manually select the used variables in your regression by deleting the unused variables. With many variables, this can be much quicker than writing down all used variables.

Installation

The easiest way to install glueformula is from my Github powered package repository by running:

install.packages("glueformula",repos = c("https://skranz-repo.github.io/drat/",getOption("repos")))

Alteneratively, you can run the following code to install the source package from Github:

if (!require(glue)) install.packages("glue")
if (!require(remotes)) install.packages("remotes")
remotes::install_github("skranz/glueformula")

If the Github installation fails because unimportant warnings are converted to errors run before the installation:

Sys.setenv(R_REMOTES_NO_ERRORS_FROM_WARNINGS="true")

See https://remotes.r-lib.org/#environment-variables

You can’t perform that action at this time.