Econometrics and Free Software by Bruno Rodrigues.

RSS feed for blog post updates.

Follow me on twitter, or check out my Github.

Watch my youtube channel.

Have a question about one of the blog posts? Chat here with me.

Building formulae

R

This Stackoverflow question made me think about how to build formulae. For example, you might want to programmatically build linear model formulae and then map these models on data. For example, suppose the following (output suppressed):

data(mtcars)

lm(mpg ~ hp, data = mtcars)
lm(mpg ~I(hp^2), data = mtcars)
lm(mpg ~I(hp^3), data = mtcars)
lm(mpg ~I(hp^4), data = mtcars)
lm(mpg ~I(hp^5), data = mtcars)
lm(mpg ~I(hp^6), data = mtcars)

To avoid doing this, one can write a function that builds the formulae:

create_form = function(power){
  rhs = substitute(I(hp^pow), list(pow=power))
  rlang::new_formula(quote(mpg), rhs)
}

If you are not familiar with substitute(), try the following to understand what it does:

substitute(y ~ x, list(x = 1))
## y ~ 1

Then using rlang::new_formula() I build a formula by providing the left hand side, which is quote(mpg) here, and the right hand side, which I built using substitute(). Now I can create a list of formulae:

library(tidyverse)

list_formulae = map(seq(1, 6), create_form)

str(list_formulae)
## List of 6
##  $ :Class 'formula'  language mpg ~ I(hp^1L)
##   .. ..- attr(*, ".Environment")=<environment: 0x55605f897ca0> 
##  $ :Class 'formula'  language mpg ~ I(hp^2L)
##   .. ..- attr(*, ".Environment")=<environment: 0x55605f891418> 
##  $ :Class 'formula'  language mpg ~ I(hp^3L)
##   .. ..- attr(*, ".Environment")=<environment: 0x55605da76098> 
##  $ :Class 'formula'  language mpg ~ I(hp^4L)
##   .. ..- attr(*, ".Environment")=<environment: 0x55605da6a600> 
##  $ :Class 'formula'  language mpg ~ I(hp^5L)
##   .. ..- attr(*, ".Environment")=<environment: 0x55605da68980> 
##  $ :Class 'formula'  language mpg ~ I(hp^6L)
##   .. ..- attr(*, ".Environment")=<environment: 0x55605da66d38>

As you can see, power got replaced by 1, 2, 3,… and each element of the list is a nice formula. Exactly what lm() needs. So now it’s easy to map lm() to this list of formulae:

data(mtcars)

map(list_formulae, lm, data = mtcars)
## [[1]]
## 
## Call:
## .f(formula = .x[[i]], data = ..1)
## 
## Coefficients:
## (Intercept)      I(hp^1)  
##    30.09886     -0.06823  
## 
## 
## [[2]]
## 
## Call:
## .f(formula = .x[[i]], data = ..1)
## 
## Coefficients:
## (Intercept)      I(hp^2)  
##  24.3887252   -0.0001649  
## 
## 
## [[3]]
## 
## Call:
## .f(formula = .x[[i]], data = ..1)
## 
## Coefficients:
## (Intercept)      I(hp^3)  
##   2.242e+01   -4.312e-07  
## 
## 
## [[4]]
## 
## Call:
## .f(formula = .x[[i]], data = ..1)
## 
## Coefficients:
## (Intercept)      I(hp^4)  
##   2.147e+01   -1.106e-09  
## 
## 
## [[5]]
## 
## Call:
## .f(formula = .x[[i]], data = ..1)
## 
## Coefficients:
## (Intercept)      I(hp^5)  
##   2.098e+01   -2.801e-12  
## 
## 
## [[6]]
## 
## Call:
## .f(formula = .x[[i]], data = ..1)
## 
## Coefficients:
## (Intercept)      I(hp^6)  
##   2.070e+01   -7.139e-15

This is still a new topic for me there might be more elegant ways to do that, using tidyeval to remove the hardcoding of the columns in create_form(). I might continue exploring this.