gf: String interpolation to build regression formulas from...

View source: R/gf.R

gfR Documentation

String interpolation to build regression formulas from vectors with variable names.

Description

Insert vectors with variable names into { } placeholder in a formula and collapse vectors with +.

Usage

gf(
  form,
  values = list(),
  enclos = parent.frame(),
  as.formula = TRUE,
  form.env = parent.frame(),
  collapse = " + "
)

Arguments

form

A formula or string of a formula. E.g. y~x+{controls} where controls is a character vector with variables that will be inserted.

values

A named list containing the vectors of variable names. If empty the enclosing environment will be searched.

enclos

the enclosing environment in which the vectors with variable names will be taken if not provided by values. By default the calling enviornment.

as.formula

if TRUE (default) return a formula object, otherwise a string.

form.env

The environment assigned to the returned formula. By default the calling environment.

collapse

how shall variables in a vector be collapsed. Default " # ".

Details

An adaption of the glue function specialized to build regression formulas from variable names stored in vectors.

Examples

# Assume we want to estimate a demand function
# using ivreg

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

# Replace {contr} and {instr} in formula
gf(q ~ p + {contr} | {instr})

# You could also provide the formula as string
gf("q ~ p + {contr} | {instr}")


skranz/glueformula documentation built on Oct. 12, 2024, 5:43 a.m.