Collecting Polynomial Terms in Coefficient Recentering Matrices
When using Kronecker products to form polynomial transformations, we want to collect like terms. That is, if I have a variable \(x\) with mean \(\mu\) in an equation \[y = \beta_0 + \beta_1 x + \beta_2 x^2\] that I would like to recenter, with \(x_\delta = x – \mu\) so that \[y = \beta_{\delta 0} + \beta_{\delta 1}x_\delta + \beta_{\delta 2} x_\delta^2\] The basic recentering matrix can be formed in the same manner as for interaction terms.
mu <- 3
names(mu) <- "x"
A <- mean.to.matrix(mu) # recentering x - 3
C <- kron(A,A)
C
## (Intercept) x x x:x
## (Intercept) 1 3 3 9
## x 0 1 0 3
## x 0 0 1 3
## x:x 0 0 0 1
However, notice that we have two \(x\) terms in the rows and columns. This implies that our original equation is of the form \[y = b_0 + b_1 x + b_2 x + b_3 x^2\] We can rewrite our original equation in this form, letting \(b_2=0\) – the \(x\) terms were already collected in our original equation.
Now notice that this coefficient choice zeros out one column of our recentering matrix. We could write this more succinctly by dropping the zeroed column and leaving our coefficient vector in it’s original form.
cnames <- colnames(C)
C <- C %*% diag(1,4,4)[,-3]
colnames(C) <- cnames[-3]
C
## (Intercept) x x:x
## (Intercept) 1 3 9
## x 0 1 3
## x 0 0 3
## x:x 0 0 1
We still have two rows for the \(x_\delta\) terms. These do not zero out, so we want to collect them in a single term.
rnames <- rownames(C)
C <- cbind(c(1,0,0),c(0,1,0),c(0,1,0),c(0,0,1)) %*% C
rownames(C) <- rnames[-3]
C
## (Intercept) x x:x
## (Intercept) 1 3 9
## x 0 1 6
## x:x 0 0 1