为什么match.call有用? [英] Why is match.call useful?

查看:193
本文介绍了为什么match.call有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些R函数的主体中,例如 lm ,我看到了对 match.call 函数的调用。如其帮助页面所述,在函数 match.call 中使用时,将返回指定了参数名称的调用;这对于将大量参数传递给另一个函数很有用。

In the body of some R functions, for example lm I see calls to the match.call function. As its help page says, when used inside a function match.call returns a call where argument names are specified; and this is supposed to be useful for passing a large number of arguments to another functions.

例如,在 lm 函数中,我们看到了对函数模型的调用。框架 ...

For example, in the lm function we see a call to the function model.frame...

function (formula, data, subset, weights, na.action, method = "qr", 
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
contrasts = NULL, offset, ...) 
{
  cl <- match.call()
  mf <- match.call(expand.dots = FALSE)
  m <- match(c("formula", "data", "subset", "weights", "na.action", 
      "offset"), names(mf), 0L)
  mf <- mf[c(1L, m)]

  mf$drop.unused.levels <- TRUE
  mf[[1L]] <- quote(stats::model.frame)
  mf <- eval(mf, parent.frame())
  ...

... 为什么这样比直接调用 model.frame 指定参数名称更为有用吗?

...Why is this more useful than making a straight call to model.frame specifying the argument names as I do next?

function (formula, data, subset, weights, na.action, method = "qr", 
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
contrasts = NULL, offset, ...) 
{
  mf <- model.frame(formula = formula, data = data,
                    subset = subset, weights = weights, subset = subset)
  ...

(请注意, match.call 还有我不讨论的另一项用途,将调用存储在结果对象中。)

(Note that match.call has another use that I do not discuss, store the call in the resulting object.)

推荐答案

一个与此相关的原因是 match.call 捕获了呼叫的语言而没有评估它,在这种情况下,它允许 lm 将某些缺失变量视为可选。考虑:

One reason that is relevant here is that match.call captures the language of the call without evaluating it, and in this case it allows lm to treat some of the "missing" variables as "optional". Consider:

lm(x ~ y, data.frame(x=1:10, y=runif(10)))

Vs:

lm2 <- function (
  formula, data, subset, weights, na.action, method = "qr", 
  model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
  contrasts = NULL, offset, ...
) {
  mf <- model.frame(
    formula = formula, data = data, subset = subset, weights = weights
  ) 
}
lm2(x ~ y, data.frame(x=1:10, y=runif(10)))
## Error in model.frame.default(formula = formula, data = data, subset = subset,  :
##   invalid type (closure) for variable '(weights)'

lm2 中,因为权重为缺失,但您仍在 weights = weights 中使用它,R尝试使用 stats :: weights 函数显然不是预期的功能,您可以在调用 model.fr之前通过测试缺失来解决此问题ame ,但那时 match.call 开始看起来不错。看看如果我们 debug 调用会发生什么情况:

In lm2, since weights is "missing" but you still use it in weights=weights, R tries to use the stats::weights function which is clearly not what was intended. You could get around this by testing for missingness before you call model.frame, but at that point the match.call starts looking pretty good. Look at what happens if we debug the call:

debug(lm2)
lm2(x ~ y, data.frame(x=1:10, y=runif(10)))
## debugging in: lm2(x ~ y, data.frame(x = 1:10, y = runif(10)))
## debug at #5: {
##     mf <- model.frame(formula = formula, data = data, subset = subset,
##         weights = weights)
## }
Browse[2]> match.call()
## lm2(formula = x ~ y, data = data.frame(x = 1:10, y = runif(10)))

match.call 根本不涉及缺少的参数。

match.call doesn't involve the missing arguments at all.

您可能会争辩说,可选参数应该已经通过默认值显式设置为可选,但这不是这里发生的情况。

You could argue that the optional arguments should have been made explicitly optional via default values, but that's not what happened here.

这篇关于为什么match.call有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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