The argument in the predict.lm function

Head function predict.lm

predict.lm <- function (object, newdata, se.fit = FALSE, scale = NULL, df = Inf,
                        interval = c("none", "confidence", "prediction"), level = 0.95,
                        type = c("response", "terms"), terms = NULL, na.action = na.pass,
                        pred.var = res.var/weights, weights = 1, ...)

      

I am having trouble understanding how an argument is evaluated pred.var = res.var/weights

.

I know it means the variance of the residuals, but it is passed as a variable, as opposed to a character string, where it can subsequently be read and translated.

The reference for this feature is not explicit for this issue, which I think may be related to how R works, and not so much with this particular feature.

Somehow, the argument pred.var

will deflect from the remainders by default - sd(fit$residuals)^2

- but I can't see how.

+3


source to share


1 answer


What you see here is the R mechanism for lazy evaluation of function arguments. Take a look at a simplified example:

lazy_arg <- function(x, y = z) {
    z <- sum(x * x)
    x / y
}
lazy_arg(1:5, y = 10)
#[1] 0.1 0.2 0.3 0.4 0.5
lazy_arg(1:5)
#[1] 0.01818182 0.03636364 0.05454545 0.07272727 0.09090909

      

The second call will obviously not work in languages ​​that evaluate arguments immediately after the call. Instead, R simply stores the "recipe" y = z

and evaluates it only when it is actually used y

. Of course, when I write a function like this, I have to make sure it is z

correctly defined before using it y

, otherwise I would provide a great opportunity to shoot myself in the foot:

bad_arg <- function(x, y = z) {
    if (runif(1) > 0.5) z <- 1
    x / y
}

set.seed(112)
z <- 1e5
bad_arg(1:5)
#[1] 1e-05 2e-05 3e-05 4e-05 5e-05
bad_arg(1:5)
#[1] 1 2 3 4 5

      



If you are wondering why this happened, then how does R variable lookup work (in short, an unintentional top-level collision). So flexibility brings value here.

However, predict.lm

this is quite handy in case , as it provides a sensible default in place, and it is actually calculated much later and depends on other arguments.

For more information and options, see Hadley Wickham's "Advanced R" Lazy Assessment: http://adv-r.had.co.nz/Functions.html

+2


source







All Articles