在不同变量上运行lm的函数 [英] Function which runs lm over different variables
问题描述
我想创建一个函数,该函数可以对给定数据集中的不同变量运行回归模型(例如,使用lm).在此函数中,我将使用的数据集,自变量y和自变量x指定为参数.我希望这是一个函数而不是循环,因为我想在脚本的各个位置调用代码.我的天真函数看起来像这样:
I would like to create a function which can run a regression model (e.g. using lm) over different variables in a given dataset. In this function, I would specify as arguments the dataset I'm using, the dependent variable y and the independent variable x. I want this to be a function and not a loop as I would like to call the code in various places of my script. My naive function would look something like this:
lmfun <- function(data, y, x) {
lm(y ~ x, data = data)
}
该函数显然不起作用,因为lm函数无法将y和x识别为数据集的变量.
This function obviously does not work because the lm function does not recognize y and x as variables of the dataset.
我做了一些研究,偶然发现了以下有用的插图:使用dplyr编程 .小插图为与我面临的问题类似的问题提供了以下解决方案:
I have done some research and stumbled upon the following helpful vignette: programming with dplyr. The vignette gives the following solution to a similar problem as the one I am facing:
df <- tibble(
g1 = c(1, 1, 2, 2, 2),
g2 = c(1, 2, 1, 2, 1),
a = sample(5),
b = sample(5)
)
my_sum <- function(df, group_var) {
group_var <- enquo(group_var)
df %>%
group_by(!! group_var) %>%
summarise(a = mean(a))
}
我知道lm不是dplyr软件包的一部分,但想提出类似的解决方案.我尝试了以下方法:
I am aware that lm is not a function that is part of the dplyr package but would like to come up with a solution similar as this. I've tried the following:
lmfun <- function(data, y, x) {
y <- enquo(y)
x <- enquo(x)
lm(!! y ~ !! x, data = data)
}
lmfun(mtcars, mpg, disp)
运行此代码将显示以下错误消息:
Running this code gives the following error message:
is_quosure(e2)中的错误:缺少参数"e2",没有默认值
Error in is_quosure(e2) : argument "e2" is missing, with no default
任何人都有关于如何修改代码以使其正常工作的想法?
Anyone has an idea on how to amend the code to make this work?
谢谢
Joost.
推荐答案
您可以使用quo_name
和formula
来解决此问题:
You can fix this problem by using the quo_name
's and formula
:
lmfun <- function(data, y, x) {
y <- enquo(y)
x <- enquo(x)
model_formula <- formula(paste0(quo_name(y), "~", quo_name(x)))
lm(model_formula, data = data)
}
lmfun(mtcars, mpg, disp)
# Call:
# lm(formula = model_formula, data = data)
#
# Coefficients:
# (Intercept) disp
# 29.59985 -0.04122
这篇关于在不同变量上运行lm的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!