Handling redundant factor variable levels for linear regressions in R Say I have two factor variables, X and Y, each with 3 levels. However, X==3 if and only if Y==3, while such a connection doesn't hold for X,Y==1,2. In this case, while X and Y are not redundant, my design matrix, when expanded, will contain the same column twice (0's everywhere, except 1's where X==Y==3). This is not good.
Theoretically, there is no problem: Just remove one of the redundant columns. However, I don't want to go ahead and re-implement the entire linear regression just for this case. Is there some technical or theoretical trick that will still allow me to use R's lm?
 A: I believe lm already handles perfectly collinear variables and you don't have to worry about this.
#example
DF <- expand.grid(x=factor(1:2, levels=1:3), y=factor(1:2, levels=1:3))
DF <- rbind(DF, data.frame(x=factor(3, levels=1:3),y=factor(3, levels=1:3)))
DF <- rbind(DF, DF, DF)
set.seed(42)
DF$r <- rnorm(15)

lm(r~x+y, data=DF)

# Coefficients:
#   (Intercept)           x2           x3           y2           y3  
#        0.6208       0.6928      -0.5514      -0.7585           NA  


#design matrix
dm <- model.matrix(r~x+y,DF)
#remove duplicated column
dm <- dm[,!duplicated(t(dm))]

#fit linear model
lm.fit(dm,DF$r)
    #$coefficients
#(Intercept)          x2          x3          y2 
#  0.6208076   0.6927758  -0.5513966  -0.7585111 

However, there will be warnings if you use the rank-deficient fit in, e.g., predict:
predict(fit, newdata=data.frame(x="1", y="2"))
#          1 
# -0.1377035 
# Warning message:
#   In predict.lm(fit, newdata = data.frame(x = "1", y = "2")) :
#   prediction from a rank-deficient fit may be misleading

unname(fit1$coef[1]+fit1$coef[4])
#[1] -0.1377035

