Title: | String interpolation to build regression formulas |
---|---|
Description: | Has the main function gl that is a variant of glue. It is useful for building regression formulas given vectors of variable names. |
Authors: | Sebastian Kranz |
Maintainer: | Sebastian Kranz <[email protected]> |
License: | GPL >= 2.0 |
Version: | 0.1.1 |
Built: | 2024-10-11 14:57:11 UTC |
Source: | https://github.com/skranz/glueformula |
Insert vectors with variable names into { } placeholder in a formula and collapse vectors with +.
gf( form, values = list(), enclos = parent.frame(), as.formula = TRUE, form.env = parent.frame(), collapse = " + " )
gf( form, values = list(), enclos = parent.frame(), as.formula = TRUE, form.env = parent.frame(), collapse = " + " )
form |
A formula or string of a formula. E.g. |
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 |
form.env |
The environment assigned to the returned formula. By default the calling environment. |
collapse |
how shall variables in a vector be collapsed. Default |
An adaption of the glue
function specialized to build
regression formulas from variable names stored in vectors.
# 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}")
# 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}")
The if the data frame has 3 columns named "x", "y" and "name with space" the function cats and on windows also copies to your clipboard the following code:
varnames_snippet(x)
varnames_snippet(x)
x |
A data frame or other objects with |
c("x","y","`name with space`")
This can be helpful to specify vectors of control
variables when using gf
. If there are
many variables it is just easier to remove not-used variables
from the created code snippet than to write down
manually all used variables.
df = data.frame(x=1,y=5,z=4,`a b`=4) varnames_snippet(df)
df = data.frame(x=1,y=5,z=4,`a b`=4) varnames_snippet(df)