为什么这个调用`lm(...,subset)`的简单函数失败? [英] Why does this simple function calling `lm(..., subset)` fail?

查看:218
本文介绍了为什么这个调用`lm(...,subset)`的简单函数失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含对 lm()的调用的自定义函数,但由于某种原因,函数失败。我无法理解它为什么会失败。



考虑将这个例子简化为简单:

  myfun <  - 函数(形式,数据,子集,...){
lm(表单,数据,子集,...)
}

这会以错误结束:

  ## eval(expr,envir,enclos)中的错误:object'subs。'not found 

然而直接使用 lm()会很好:

  lm(mpg〜cyl + hp,mtcars,TRUE)
##
##调用:
## lm (formula = mpg〜cyl + hp,data = mtcars,subset = TRUE)
##
##系数:
##截距cyl hp
## 36.90833 - 2.26469 -0.01912

我试过调试,但仍然无法找到问题的根本。为什么自定义函数失败?显然 subs。 has 已被提供给函数...






编辑:



尽管下面提出的大多数解决方案都有助于解决这个简单情况,但如果我添加一个简单的转折,该函数仍然会失败。例如 expand.model.frame()依赖于公式的环境,但如果我使用正常的评估解决方案,则失败:

  myfun<  -  function(form。,data。,subs。,...){
fit < - lm(form。,data。[subs。 ,],...)
expand.model.frame(fit,〜drat)
}

myfun(mpg〜cyl + hp,mtcars,TRUE)
## ## eval(expr,envir,enclos)中的错误:object'data。'not found

这显然与原始问题有关,但我不知道如何。模型公式的环境是否已损坏?

解决方案

正如评论中所建议的,另一个解决方案是避免<$

 子集参数完全用于非交互式使用, myfun<  -  function(form。,data。,subs。,...){
lm(form。,data。[subs。,],...)
}

现在这个按预期工作:

  myfun(公式(mpg〜cyl + hp),mtcars,TRUE)






然而,如果您的自定义函数随后包含诸如 expand.model.frame()或类似的调用,这仍然不够似乎对子集参数的非标准评估敏感。要使函数健壮并避免意外,您需要(1)在自定义函数中定义公式(另请参阅 reconform 方法)和(2)数据子集 em>到 lm()调用,同时显着避免子集参数。



像这样:

  myfun<  -  function(form。 ,data。,subs。,...){
stopifnot(is.character(form。))
data。 < - data。[subs。]]
fit< -lm(as.formula(form。),data。,...)
expand.model.frame(fit,〜drat)


myfun(mpg〜cyl + hp,mtcars,TRUE)

我尝试使用(1)或(2),但仍然设法遇到来自某些函数的奇怪错误,并且仅与(1)和(2)一起,错误似乎已消失...

I am working on a custom function that includes a call to lm(), but for some reason the function fails. I can't make any sense of why it fails.

Consider this example simplified to the bare-bones:

myfun <- function(form., data., subs., ...){
    lm(form., data., subs., ...)
}

This will end up in an error:

myfun(mpg ~ cyl + hp, mtcars, TRUE)
## Error in eval(expr, envir, enclos) : object 'subs.' not found

However using lm() directly will work just fine:

lm(mpg ~ cyl + hp, mtcars, TRUE)
## 
## Call:
## lm(formula = mpg ~ cyl + hp, data = mtcars, subset = TRUE)
## 
## Coefficients:
## (Intercept)          cyl           hp  
##    36.90833     -2.26469     -0.01912  

I tried debugging, but still can't get to the bottom of the problem. Why does the custom function fail? Clearly subs. has been supplied to the function...


Edit:

While most of the solutions suggested below help in this simple case, the function will still fail if I add a simple twist. For instance expand.model.frame() relies on the formula's environment, but fails if I use the normal evaluation solution:

myfun <- function(form., data., subs., ...){
    fit <- lm(form., data.[ subs., ], ...)
    expand.model.frame(fit, ~ drat)
}

myfun(mpg ~ cyl + hp, mtcars, TRUE)
## Error in eval(expr, envir, enclos) : object 'data.' not found

This is obviously related to the original issue, but I can't figure how. Is the environment of the model formula somehow corrupted?

解决方案

As suggested in the comments, another solution would be to avoid the subset argument altogether in non-interactive use, and use standard evaluation instead:

myfun <- function(form., data., subs., ...){
    lm(form., data.[ subs., ], ...)
}

Now this works as expected:

myfun(formula(mpg ~ cyl + hp), mtcars, TRUE)


However this won't still be enough if your custom function subsequently contains calls like expand.model.frame() or similar, which seem to be themselves sensitive to the non-standard evaluation of the subset argument. To make the function robust and avoid surprises, you need to both (1) define the formula within the custom function (see also the reformulate approach) and (2) subset the data prior to the lm() call while conspicuously avoiding the subset argument.

Like this:

myfun <- function(form., data., subs., ...){
    stopifnot(is.character(form.))
    data. <- data.[ subs., ]
    fit <- lm(as.formula(form.), data., ...)
    expand.model.frame(fit, ~ drat)
}

myfun("mpg ~ cyl + hp", mtcars, TRUE)

I tried using either (1) or (2), but still managed to run into strange errors from some functions, and it's only with both (1) and (2) that the errors seem to have gone away...

这篇关于为什么这个调用`lm(...,subset)`的简单函数失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆