简体   繁体   中英

object '…' not found in R Functions with lm -->> (Error in eval(predvars, data, env) : object '…' not found)

I'm using the moderndrive package to calculate a linear regression but using a function. I am trying to create a function where i can just pass in two selected columns(eg deaths & cases, titles of the columns) from my data frame (Rona_2020). Below is the function...

score_model_Fxn <- function(y, x){
  score_mod <- lm(y ~ x, data = Rona_2020)
  Reg_Table <- get_regression_table(score_mod)
print(paste('The regression table is', Reg_Table))
}

when I run the function...

score_model_Fxn(deaths, cases)

I get...

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

What should i do? I have looked several similar issues but to no avail.

What you want to do by passing deaths and cases is called non-standard evaluation. You need to combine this with computing on the language if you want to run a model with the correct formula and scoping. Computing on the language can be done with substitute and bquote .

library(moderndive)
score_model_Fxn <- function(y, x, data){

  #get the symbols passed as arguments:
  data <- substitute(data)
  y <- substitute(y)
  x <- substitute(x)

  #substitute them into the lm call and evaluate the call:
  score_mod <- eval(bquote(lm(.(y) ~ .(x), data = .(data))))

  Reg_Table <- get_regression_table(score_mod)

  message('The regression table is') #better than your paste solution
  print(Reg_Table)

  invisible(score_mod) #a function should always return something useful
}

mod <- score_model_Fxn(Sepal.Length, Sepal.Width, iris)
#The regression table is
## A tibble: 2 x 7
#  term        estimate std_error statistic p_value lower_ci upper_ci
#  <chr>          <dbl>     <dbl>     <dbl>   <dbl>    <dbl>    <dbl>
#1 intercept      6.53      0.479     13.6    0         5.58    7.47 
#2 Sepal.Width   -0.223     0.155     -1.44   0.152    -0.53    0.083

print(mod)
#
#Call:
#lm(formula = Sepal.Length ~ Sepal.Width, data = iris)
#
#Coefficients:
#(Intercept)  Sepal.Width  
#     6.5262      -0.2234  

You could have the function return Reg_Table instead if you prefer.

One of the coolest ways of doing this is using the new recipes package to generate the formula for us and then manipulating a tibble to produce or result

library(tidyverse)
library(recipes)
#> 
#> Attaching package: 'recipes'
#> The following object is masked from 'package:stringr':
#> 
#>     fixed
#> The following object is masked from 'package:stats':
#> 
#>     step
library(moderndive)

score_model_Fxn <- function(df,x, y){
  formula_1 <- df %>% 
    recipe() %>%
    update_role({{x}},new_role = "outcome") %>% 
    update_role({{y}},new_role = "predictor") %>% 
    formula()

  Reg_Table <- mtcars %>%
    summarise(score_mod = list(lm(formula_1,data = .))) %>%
    rowwise() %>% 
    mutate(Reg_Table = list(get_regression_table(score_mod))) %>% 
    pull(Reg_Table)

  print(paste('The regression table is', Reg_Table))
  Reg_Table
}

k <- mtcars %>%
  score_model_Fxn(x = cyl,y = gear)
#> [1] "The regression table is list(term = c(\"intercept\", \"gear\"), estimate = c(10.585, -1.193), std_error = c(1.445, 0.385), statistic = c(7.324, -3.101), p_value = c(0, 0.004), lower_ci = c(7.633, -1.978), upper_ci = c(13.537, -0.407))"

k
#> [[1]]
#> # A tibble: 2 x 7
#>   term      estimate std_error statistic p_value lower_ci upper_ci
#>   <chr>        <dbl>     <dbl>     <dbl>   <dbl>    <dbl>    <dbl>
#> 1 intercept    10.6      1.44       7.32   0         7.63   13.5  
#> 2 gear         -1.19     0.385     -3.10   0.004    -1.98   -0.407

Created on 2020-06-09 by the reprex package (v0.3.0)

For those that might be interested...I modified Bruno's answer.

library(tidyverse); library(recipes); library(moderndive)

score_model_Fxn2 <- function(df,x, y){
  formula_1 <- df %>% 
    recipe() %>%
    update_role({{y}},new_role = "outcome") %>% 
    update_role({{x}},new_role = "predictor") %>% 
    formula()
  Reg_Table <- df %>%
    summarise(score_mod = list(lm(formula_1,data = .))) %>%
    rowwise() %>%  
     mutate(Reg_Table = list(get_regression_table(score_mod))) %>% 
    pull(Reg_Table)
  print(Reg_Table)
}
score_model_Fxn2()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM