在 R 中使用线性回归创建新函数:
Creating new Functions with Linear Regression in R :
我在创建调用 lm()
函数的函数时遇到了问题:
regresionLineal <- function (vardep, varindep1, varindep2, DATA) {
lm(vardep ~ varindep1 + varindep2, data = DATA)
}
然后我使用我之前创建的数据框中的数据调用它 (DATOS
)...
regresionLineal(Estatura, Largo, Ancho, DATOS)
Error in eval(expr, envir, enclos) : object 'Estatura' not found
Called from: eval(expr, envir, enclos)
欢迎任何帮助...
你应该做的:
regresionLineal <- function (vardep, varindep1, varindep2, DATA) {
lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA)
}
将 vardep
、varindep1
、varindep2
作为字符串传入的位置。例如,我使用 R 的内置 trees
数据集:
regresionLineal("Height", "Girth", "Volumn", trees)
# Call:
# lm(formula = paste(vardep, "~", varindep1, "+", varindep2), data = DATA)
# Coefficients:
# (Intercept) Girth Volume
# 83.2958 -1.8615 0.5756
但是,我不明白我们为什么要这样做。如果我们必须指定公式中的每个变量,为什么不简单地传递一个完整的公式呢?在这种情况下,您可以直接使用 lm()
而无需定义自己的函数。
此外,您可能已经知道这一点,但记住这里创建的回归对象不会存在于函数之外可能会有所帮助,除非分配给全局环境或您正在工作的任何环境.如果你需要打电话给reg。出于某种原因,稍后在此函数之外的对象您应该将其分配为:model1 <<- lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA)
以便能够从全局环境中调用。
如果你想创建一个包含任意数量自变量的模型,你可以使用下面的方法:
create_lm <- function(data, dep, covs) {
# Create the first part of the formula with the dependent variable
form_base <- paste(dep, "~")
# Create a string that concatenates your covs vector with a "+" between each variable
form_vars <- paste(covs, collapse = " + ")
# Paste the two parts together
formula <- paste(form_base, form_vars)
# Call the lm function on your formula
lm(formula, data = data)
}
例如,使用 built-in mtcars 数据集:
create_lm(mtcars, "mpg", c("wt", "cyl"))
Call:
lm(formula = formula, data = data)
Coefficients:
(Intercept) wt cyl
39.686 -3.191 -1.508
缺点是模型的打印输出没有反映您对 lm 的特定调用,不确定是否有任何解决方法。
只是想我会为以后的任何事情添加到这里 reader。
我想出的解决方案(并不完美)是以下函数:
f <- function(y, x1, x2, df) {
cmd = paste0("lm(", y, " ~ ", x1, " + ", x2, ", data = ", deparse1(substitute(df)), ")")
eval(parse(text = cmd))
}
通过这样做,您可以调用,例如,
R> f("mpg", "hp", "wt", mtcars)
Call:
lm(formula = mpg ~ hp + wt, data = mtcars)
Coefficients:
(Intercept) hp wt
37.2273 -0.0318 -3.8778
与其他方法相比的主要优点是 lm
的输出不会混淆变量或数据帧的名称。
也许未来的 reader 会意识到 运行 此命令需要 R 基本函数的知识:parse
、deparse1
、substitute
和 eval
谢谢!
我在创建调用 lm()
函数的函数时遇到了问题:
regresionLineal <- function (vardep, varindep1, varindep2, DATA) {
lm(vardep ~ varindep1 + varindep2, data = DATA)
}
然后我使用我之前创建的数据框中的数据调用它 (DATOS
)...
regresionLineal(Estatura, Largo, Ancho, DATOS)
Error in eval(expr, envir, enclos) : object 'Estatura' not found Called from: eval(expr, envir, enclos)
欢迎任何帮助...
你应该做的:
regresionLineal <- function (vardep, varindep1, varindep2, DATA) {
lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA)
}
将 vardep
、varindep1
、varindep2
作为字符串传入的位置。例如,我使用 R 的内置 trees
数据集:
regresionLineal("Height", "Girth", "Volumn", trees)
# Call:
# lm(formula = paste(vardep, "~", varindep1, "+", varindep2), data = DATA)
# Coefficients:
# (Intercept) Girth Volume
# 83.2958 -1.8615 0.5756
但是,我不明白我们为什么要这样做。如果我们必须指定公式中的每个变量,为什么不简单地传递一个完整的公式呢?在这种情况下,您可以直接使用 lm()
而无需定义自己的函数。
此外,您可能已经知道这一点,但记住这里创建的回归对象不会存在于函数之外可能会有所帮助,除非分配给全局环境或您正在工作的任何环境.如果你需要打电话给reg。出于某种原因,稍后在此函数之外的对象您应该将其分配为:model1 <<- lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA)
以便能够从全局环境中调用。
如果你想创建一个包含任意数量自变量的模型,你可以使用下面的方法:
create_lm <- function(data, dep, covs) {
# Create the first part of the formula with the dependent variable
form_base <- paste(dep, "~")
# Create a string that concatenates your covs vector with a "+" between each variable
form_vars <- paste(covs, collapse = " + ")
# Paste the two parts together
formula <- paste(form_base, form_vars)
# Call the lm function on your formula
lm(formula, data = data)
}
例如,使用 built-in mtcars 数据集:
create_lm(mtcars, "mpg", c("wt", "cyl"))
Call:
lm(formula = formula, data = data)
Coefficients:
(Intercept) wt cyl
39.686 -3.191 -1.508
缺点是模型的打印输出没有反映您对 lm 的特定调用,不确定是否有任何解决方法。
只是想我会为以后的任何事情添加到这里 reader。
我想出的解决方案(并不完美)是以下函数:
f <- function(y, x1, x2, df) {
cmd = paste0("lm(", y, " ~ ", x1, " + ", x2, ", data = ", deparse1(substitute(df)), ")")
eval(parse(text = cmd))
}
通过这样做,您可以调用,例如,
R> f("mpg", "hp", "wt", mtcars)
Call:
lm(formula = mpg ~ hp + wt, data = mtcars)
Coefficients:
(Intercept) hp wt
37.2273 -0.0318 -3.8778
与其他方法相比的主要优点是 lm
的输出不会混淆变量或数据帧的名称。
也许未来的 reader 会意识到 运行 此命令需要 R 基本函数的知识:parse
、deparse1
、substitute
和 eval
谢谢!