[英]Passing Argument to lm in R within Function
I would like to able to call lm
within a function and specify the weights
variable as an argument passed to the outside function that is then passed to lm
. 我希望能够在函数内调用lm
并将weights
变量指定为传递给外部函数的参数,然后传递给lm
。 Below is a reproducible example where the call works if it is made to lm
outside of a function, but produces the error message Error in eval(expr, envir, enclos) : object 'weightvar' not found
when called from within a wrapper function. 以下是一个可重现的示例,其中,如果在函数外部对lm
进行调用,则该调用有效,但会生成错误消息Error in eval(expr, envir, enclos) : object 'weightvar' not found
从包装函数中调用时Error in eval(expr, envir, enclos) : object 'weightvar' not found
。
olswrapper <- function(form, weightvar, df){
ols <- lm(formula(form), weights = weightvar, data = df)
}
df <- mtcars
ols <- lm(mpg ~ cyl + qsec, weights = gear, data = df)
summary(ols)
ols2 <- olswrapper(mpg ~ cyl + qsec, weightvar = gear, df = df)
#Produces error: "Error in eval(expr, envir, enclos) : object 'weightvar' not found"
Building on the comments, gear
isn't defined globally. 在注释的基础上, gear
不是全局定义的。 It works inside the stand-alone lm
call as you specify the data you are using, so lm
knows to take gear
from df
. 它的工作原理单机内lm
呼叫您指定要使用的数据,所以lm
知道采取gear
从df
。
Howver, gear
itself doesn't exist outside that stand-alone lm
function. 但是, gear
本身并不存在于独立的lm
功能之外。 This is shown by the output of gear
这由gear
的输出表示
> gear
Error: object 'gear' not found
You can pass the gear
into the function using df$gear
您可以使用df$gear
将gear
传递给函数
weightvar <- df$gear
ols <- olswrapper(mpg ~ cyl + qsec, weightvar , df = df)
I know I'm late on this, but I believe the previous explanation is incomplete. 我知道我迟到了,但是我相信前面的解释是不完整的。 Declaring weightvar <- df$gear
and then passing it in to the function only works because you use weightvar
as the name for your weight argument. 声明weightvar <- df$gear
并将其传递给函数仅适用,因为您将weightvar
用作weight参数的名称。 This is just using weightvar
as a global variable. 这只是使用weightvar
作为全局变量。 That's why df$gear
doesn't work directly. 这就是df$gear
无法直接运行的原因。 It also doesn't work if you use any name except weightvar
. 如果您使用weightvar
以外的任何名称,它也将不起作用。
The reason why it doesn't work is that lm looks for data in two places: the dataframe argument (if specified), and the environment of your formula. 之所以不起作用,是因为lm在两个位置查找数据:dataframe参数(如果已指定)和公式的环境。 In this case, your formula's environment is R_GlobalEnv
. 在这种情况下,您公式的环境为R_GlobalEnv
。 (You can test this by running print(str(form))
from inside olswrapper
). (您可以通过从olswrapper
内部运行print(str(form))
进行olswrapper
)。 Thus, lm
will only look in the global environment and in df
, not the function environment. 因此, lm
将仅在全局环境和df
查看,而不会在函数环境中查看。
edit: In the lm
documentation the description of the data argument says: "an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken from environment(formula) , typically the environment from which lm is called." 编辑:在lm
文档中data参数的描述说:“包含模型中变量的可选数据框,列表或环境(或as.data.frame可强制转换为数据框的对象)。 如果在数据中找不到,这些变量取自environment(formula) ,通常是调用lm的环境。”
A quick workaround is to say environment(form) <- environment()
to change your formula's environment. 一种快速的解决方法是说environment(form) <- environment()
来更改公式的环境。 This won't cause any problems because the data in the formula is in the data frame you specify. 这不会造成任何问题,因为公式中的数据位于您指定的数据框中。
eval(substitute(...))
inside a body of a function allows us to employ non-standard evaluation eval(substitute(...))
体内的eval(substitute(...))
允许我们采用非标准评估
df <- mtcars
olswrapper <- function(form, weightvar, df)
eval(substitute(ols <- lm(formula(form), weights = weightvar, data = df)))
summary(ols)
olswrapper(mpg ~ cyl + qsec, weightvar = gear, df = df)
More here: http://adv-r.had.co.nz/Computing-on-the-language.html 此处更多信息: http : //adv-r.had.co.nz/Computing-on-the-language.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.