The following are the same:
y ~ (x1 + x2)^2y ~ (1 + x1 + x2)^2formula(terms(y ~ (x1 + x2)^2, simplify = TRUE))
## y ~ x1 + x2 + x1:x2
formula(terms(y ~ (1 + x1 + x2)^2, simplify = TRUE))
## y ~ x1 + x2 + x1:x2
What is happening for the latter?
Is it evaluated as follows?
y ~ 1 + x1 + x2 + 1:x1 + 1:x2 + x1:x2y ~ 1 + x1 + x2 + 1 + 1 + x1:x2 (since 1:x1 = 1)y ~ 1 + x1 + x2 + x1:x2 (since x + x = x)y ~ x1 + x2 + x1:x2 (since 1 is included by default?)But looking at the AST
lobstr::ast(y ~ (1 + x1 + x2)^2)
## █─`~`
## ├─y
## └─█─`^`
## ├─█─`(`
## │ └─█─`+`
## │ ├─█─`+`
## │ │ ├─1
## │ │ └─x1
## │ └─x2
## └─2
and seeing that y ~ 1 + x are simplifeid as y ~ x in R, then it is actually like
y ~ ((1 + x1) + x2)^2y ~ (x1 + x2)^2y ~ x1 + x2 + x1:x2y ~ (0 + x1 + x2)^2
then it is evaluated hypothetically as
y ~ ((0 + x1) + x2)^2y ~ (-1 + x1 + x2)^2y ~ (-1 + x1 + x2)^2 (doesn’t change)y ~ -1 + x1 + x2 + -1:x1 + -1:x2 + x1:x2y ~ -1 + x1 + x2 + 1 - 1 + 1 - 1 + x1:x2 (maybe??)y ~ -1 + x1 + x2 + x1:x2formula(terms(y ~ -1:x, simplify = TRUE))
## y ~ 1 - 1