set.seed(123)
x1 <- rnorm(100)
x2 <- rnorm(100)
x3 <- rnorm(100)
x4 <- rnorm(100)
y <- rnorm(100)
dat <- data.frame(y, x1,x2,x3,x4)
rm(x1,x2,x3,x4,y)
m1 <- lm(y~ x1*x2 + x4, data=dat)
lmRCm1 <- lmrc(m1)
## Here is final fitted regression: grabs the first object
(lmRCm1s <- summary(lmRCm1[["rcReg"]]))
## The stage 1 centering regressions can be viewed as well
## lapply(lmRCm1[[2]], summary)
##Verify that result manually
dat$x1rcx2 <- resid(lm(I(x1*x2) ~ x1 + x2, data=dat))
m1m <- lm(y ~ x1 + x2 + x4 + x1rcx2, data=dat)
summary(m1m)
cbind(coef(lmRCm1[["rcReg"]]), coef(m1m))
m2 <- lm(y~ x1*x2*x3 + x4, data=dat)
lmRCm2 <- lmrc(m2)
lmRCm2s <- summary(lmRCm2[["rcReg"]])
##Verify that result manually
dat$x2rcx3 <- resid(lm(I(x2*x3) ~ x2 + x3, data=dat))
dat$x1rcx3 <- resid(lm(I(x1*x3) ~ x1 + x3, data=dat))
dat$x1rcx2rcx3 <- resid(lm(I(x1*x2*x3) ~ x1 + x2 + x3 + x1rcx2 + x1rcx3 + x2rcx3 , data=dat))
(m2m <- lm(y ~ x1 + x2 + x3+ x4 + x1rcx2 + x1rcx3 + x2rcx3 + x1rcx2rcx3, data=dat))
cbind(coef(lmRCm2[["rcReg"]]), coef(m2m))
### lmrc as good as pequod's lmres
### not run because pequod generates R warnings
###
### if (require(pequod)){
### pequodm1 <- lmres(y ~ x1*x2*x3 + x4, data=dat)
### pequodm1s <- summary(pequodm1)
### coef(pequodm1s)
### }
### lmrc works with any number of interactions. See:
m3 <- lm(y~ x1*x2*x3*x4, data=dat)
lmRCm3 <- lmrc(m3)
summary(lmRCm3[["rcReg"]])
## Verify that one manually (Gosh, this is horrible to write out)
dat$x1rcx4 <- resid(lm(I(x1*x4) ~ x1 + x4, data=dat))
dat$x2rcx4 <- resid(lm(I(x2*x4) ~ x2 + x4, data=dat))
dat$x3rcx4 <- resid(lm(I(x3*x4) ~ x3 + x4, data=dat))
dat$x1rcx2rcx4 <- resid(lm(I(x1*x2*x4) ~ x1 + x2 + x4 + x1rcx2 + x1rcx4 + x2rcx4, data=dat))
dat$x1rcx3rcx4 <- resid(lm(I(x1*x3*x4) ~ x1 + x3 + x4 + x1rcx3 + x1rcx4 + x3rcx4, data=dat))
dat$x2rcx3rcx4 <- resid(lm(I(x2*x3*x4) ~ x2 + x3 + x4 + x2rcx3 + x2rcx4 + x3rcx4, data=dat))
dat$x1rcx2rcx3rcx4 <- resid(lm(I(x1*x2*x3*x4) ~ x1 + x2 + x3 + x4 + x1rcx2 + x1rcx3 + x2rcx3 + x1rcx4 + x2rcx4 + x3rcx4 + x1rcx2rcx3 + x1rcx2rcx4 + x1rcx3rcx4 + x2rcx3rcx4, data=dat))
(m3m <- lm(y ~ x1 + x2 + x3 + x4 + x1rcx2 + x1rcx3 + x2rcx3 + x1rcx4 + x2rcx4 + x3rcx4 + x1rcx2rcx3 + x1rcx2rcx4 + x1rcx3rcx4 + x2rcx3rcx4 + x1rcx2rcx3rcx4, data=dat))
cbind(coef(lmRCm3[["rcReg"]]), coef(m3m))
### If you want to fit a sequence of models, as in pequod, can do.
tm <-terms(m2)
tmvec <- attr(terms(m2), "term.labels")
f1 <- tmvec[grep(":", tmvec, invert = TRUE)]
f2 <- tmvec[grep(":.*:", tmvec, invert = TRUE)]
f3 <- tmvec[grep(":.*:.*:", tmvec, invert = TRUE)]
## > f1
## [1] "x1" "x2" "x3" "x4"
## > f2
## [1] "x1" "x2" "x3" "x4" "x1:x2" "x1:x3" "x2:x3"
## > f3
## [1] "x1" "x2" "x3" "x4" "x1:x2" "x1:x3" "x2:x3"
## [8] "x1:x2:x3"
lmf1 <- lm(paste("y","~", paste(f1, collapse="+ ")), data=dat)
lmRCf1 <- lmrc(lmf1)
summary(lmRCf1[["rcReg"]])
lmf2 <- lm(paste("y","~", paste(f2, collapse="+ ")), data=dat)
lmRCf2 <- lmrc(lmf2)
summary(lmRCf2[["rcReg"]])
lmf3 <- lm(paste("y","~", paste(f3, collapse="+ ")), data=dat)
lmRCf3 <- lmrc(lmf3)
summary(lmRCf3[["rcReg"]])
Run the code above in your browser using DataLab