We show how to do this using proxy objects (see the "Proxies" section of this document ), first using proto package , and then without:
1) proto . Because confint.lm
calls vcov
, we need to make sure that (a) our new replacement for vcov
is in the revised confint.lm
environment, and (b) the revised confint.lm
can still access objects from the original. (For example, confint.lm
calls the hidden function format.perc
in statistics, so if we did not execute the second point to be true, then the hidden function cannot be accessed.)
To accomplish the above, we create a new confint.lm
, which is the same, except that it has a new environment (proxy environment) that contains our vcov
replacement and whose parent element, in turn, is the original confint.lm
environment. Below, the proxy environment is implemented as a proto-object in which the key elements that you should know here are: (a) proto-objects are environments and (b) the placement of the function in the proto-object in such a way, as shown, changes its environment as a proto object. In addition, to avoid problems when sending S3 confint
to confint.lm
, we call the confint.lm
method directly.
Although hccm
does not seem to have a different result, we can verify that it was running by noticing the output of trace
:
library(car) library(proto) trace(hccm) model <- lm(len ~ dose, data=ToothGrowth) proto(environment(stats:::confint.lm), # set parent vcov = function(x) hccm(x), #robust var-cov matrix confint.lm = stats:::confint.lm)[["confint.lm"]](model)
In the following example, see example 2 here .
2) environment . The code is a bit more onerous without proto (actually it roughly doubles the size of the code), but here it is:
library(car) trace(hccm) model <- lm(len ~ dose, data=ToothGrowth) local({ vcov <- function(x) hccm(x) #robust var-cov matrix confint.lm <- stats:::confint.lm environment(confint.lm) <- environment() confint.lm(model) # confint will call vcov, but not the above one. }, envir = new.env(parent = environment(stats:::confint.lm)))
EDIT: various improvements in clarity