Building formulae

December 26, 2017
By

(This article was first published on Econometrics and Free Software, and kindly contributed to R-bloggers)

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

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.

To leave a comment for the author, please follow the link and comment on their blog: Econometrics and Free Software.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)