[英]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.我正在使用moderndrive package 来计算线性回归,但使用的是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).我正在尝试创建一个 function ,我可以从我的数据框(Rona_2020)中传入两个选定的列(例如死亡和病例,列的标题)。 Below is the function...下面是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...当我运行 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.你想通过deaths
和cases
来做的事情叫做非标准评估。 You need to combine this with computing on the language if you want to run a model with the correct formula and scoping.如果要使用正确的公式和范围来运行 model,则需要将其与语言计算结合起来。 Computing on the language can be done with substitute
and bquote
.可以使用substitute
和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.如果您愿意,您可以让 function 返回Reg_Table
。
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最酷的方法之一是使用新配方 package 为我们生成公式,然后操作小标题来生成或结果
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)由代表 package (v0.3.0) 于 2020 年 6 月 9 日创建
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.