# Building formulae

**Econometrics and Free Software**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

**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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.