0

When I run this code:

# Create example data
df <- tibble(age=rnorm(10),
         income=rnorm(10))

make_model <- function(response_var, df){
  
  # Create formula
  form <- as.formula(response_var ~ .)
  
  # Create model
  model <- lm(form , data=df)
  
  # Return coefficients
  return(coef(model))
}

make_model(income, df)

I obtain the following error

 Error in eval(predvars, data, env) : object 'income' not found 

How can I make this function work using quasiquotation? I assume the logic is the same as how we can call library(dplyr) instead of library("dplyr").

max
  • 4,141
  • 5
  • 26
  • 55
  • @max: No, it’s not the same logic, because `lm()` takes a formula as input. Easiest way is to use a string as argument and then `reformulate()`. – TimTeaFan Oct 29 '20 at 16:13
  • You can create your formula this way: `form <- reformulate(".", deparse(substitute(response_var)))` – Edo Oct 29 '20 at 16:19
  • There is no point in using quasiquotation since the function is not using tidyverse. – G. Grothendieck Oct 29 '20 at 17:31
  • @G.Grothendieck how would you recommend I make this function work? – max Oct 29 '20 at 17:35
  • See @Edo's comment. – G. Grothendieck Oct 29 '20 at 17:37
  • I think @Edo's comment is the best approach. Just in case you want other approach, you can use `formula(paste0(deparse(substitute(response_var)), " ~ ."))` – Tomas Capretto Oct 29 '20 at 17:43
  • @G.Grothendieck You can use quasiquotation for any programming-on-the-language task. That's why base R includes `bquote()`. – Lionel Henry Oct 29 '20 at 18:24
  • If you are going to add a depenency it should add sufficient value to offset it. If I were already using dplyr or other tidyverse package that supports tidyeval then I would consider it but not otherwise. – G. Grothendieck Oct 29 '20 at 19:34

2 Answers2

2

Use blast() (to be included in rlang 0.5.0)

blast <- function(expr, env = caller_env()) {
  eval_bare(enexpr(expr), env)
}

make_model <- function(data, column) {
  f <- blast(!!enexpr(column) ~ .)
  model <- lm(f, data = data)
  coef(model)
}

df <- data.frame(
  age = rnorm(10),
  income = rnorm(10)
)
make_model(df, income)
#> (Intercept)         age
#>  -0.3563103  -0.2200773

Works flexibly:

blast(list(!!!1:3))
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 2
#>
#> [[3]]
#> [1] 3
Lionel Henry
  • 6,652
  • 27
  • 33
0

The following should work:

library(tidyverse)

# Your original function, modified
make_model <- function(df, column) {
  column <- enexpr(column)
  form <- as.formula(paste0(quo_text(column), " ~ ."))

  model <- lm(form, data = df)

  return(coef(model))
}

# Your original data and call
tibble(
    age = rnorm(10),
    income = rnorm(10)
  ) %>% 
  make_model(income)
van Nijnatten
  • 404
  • 5
  • 15