About Me Blog
Building formulae Easy peasy STATA-like marginal effects with R Functional peace of mind It's lists all the way down It's lists all the way down, part 2: We need to go deeper Mapping a list of functions to a list of datasets with a list of columns as arguments Teaching the tidyverse to beginners Why I find tidyeval useful tidyr::spread() and dplyr::rename_at() in action Introducing brotools Lesser known dplyr 0.7* tricks Lesser known dplyr tricks Lesser known purrr tricks Make ggplot2 purrr Functional programming and unit testing for data munging with R available on Leanpub How to use jailbreakr My free book has a cover! Work on lists of datasets instead of individual datasets by using functional programming Method of Simulated Moments with R New website! Nonlinear Gmm with R - Example with a logistic regression Simulated Maximum Likelihood with R Bootstrapping standard errors for difference-in-differences estimation with R Careful with tryCatch Data frame columns as arguments to dplyr functions Export R output to a file I've started writing a 'book': Functional programming and unit testing for data munging with R Introduction to programming econometrics with R Merge a list of datasets together Object Oriented Programming with R: An example with a Cournot duopoly R, R with Atlas, R with OpenBLAS and Revolution R Open: which is fastest? Read a lot of datasets at once with R Unit testing with R Update to Introduction to programming econometrics with R Using R as a Computer Algebra System with Ryacas

Building formulae

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: 0x562acd8cf670> 
##  $ :Class 'formula'  language mpg ~ I(hp^2L)
##   .. ..- attr(*, ".Environment")=<environment: 0x562acd833300> 
##  $ :Class 'formula'  language mpg ~ I(hp^3L)
##   .. ..- attr(*, ".Environment")=<environment: 0x562acfbb7e30> 
##  $ :Class 'formula'  language mpg ~ I(hp^4L)
##   .. ..- attr(*, ".Environment")=<environment: 0x562acfc10ac8> 
##  $ :Class 'formula'  language mpg ~ I(hp^5L)
##   .. ..- attr(*, ".Environment")=<environment: 0x562acfc4a1b0> 
##  $ :Class 'formula'  language mpg ~ I(hp^6L)
##   .. ..- attr(*, ".Environment")=<environment: 0x562acfc682a0>

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.