Why is match.call useful?
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)'
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
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.