[R] Formula compared to call within model call
Martin Maechler
m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Wed Aug 11 11:51:25 CEST 2021
>>>>> Tim Taylor
>>>>> on Wed, 11 Aug 2021 08:45:48 +0000 writes:
> Manipulating formulas within different models I notice the following:
> m1 <- lm(formula = hp ~ cyl, data = mtcars)
> m2 <- update(m1, formula. = hp ~ cyl)
> all.equal(m1, m2)
> #> [1] TRUE
> identical(m1, m2)
> #> [1] FALSE
> waldo::compare(m1, m2)
> #> `old$call[[2]]` is a call
> #> `new$call[[2]]` is an S3 object of class <formula>, a call
> I'm aware formulas are a form of call but what I'm unsure
> of is whether there is meaningful difference between the
> two versions of the models?
A good question.
In principle, the promise of an update() method should be to
produce the *same* result as calling the original model-creation
(or more generally object-creation) function call.
So, already with identical(), you've shown that this is not
quite the case for simple lm(),
and indeed that is a bit undesirable.
To answer your question re "meaningful" difference,
given what I say above is:
No, there shouldn't be any relevant difference, and if there is,
that may considered a bug in the respective update() method,
here update.lm.
More about this in the following R code snippet :
## MM: indeed,
identical(m1$call, m2$call) #> [1] FALSE
noCall <- function(x) x[setdiff(names(x), "call")]
identical(noCall(m1), noCall(m2))# TRUE!
## look closer:
c1 <- m1$call
c2 <- m2$call
str(as.list(c1))
## List of 3
## $ : symbol lm
## $ formula: language hp ~ cyl
## $ data : symbol mtcars
str(as.list(c2))
## List of 3
## $ : symbol lm
## $ formula:Class 'formula' language hp ~ cyl
## .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## $ data : symbol mtcars
identical(c1[-2], c2[-2]) # TRUE ==> so, indeed the difference is *only* in the formula ( = [2]) component
f1 <- c1$formula
f2 <- c2$formula
all.equal(f1,f2) # TRUE
identical(f1,f2) # FALSE
## Note that this is typically *not* visible if the user uses the accessor functions:
identical(formula(m1), formula(m2)) # TRUE !
## and indeed, the formula() method for 'lm' does set the environment:
stats:::formula.lm
--
Martin Maechler
ETH Zurich and R Core
More information about the R-help
mailing list