Skip to content
Prev 244811 / 398513 Next

Wrong contrast matrix for nested factors in lm(), rlm(), and lmRob()

On Dec 13, 2010, at 12:48 , Saul Sternberg wrote:

            
You are using "correct" and "incorrect" in a very absolute way. As far as I can tell, model.matrix is behaving according to its conventions. These conventions may not always be what you want, or even what is desirable for specific models, but that is not an error per se. 

In particular, one feature of the R way of generating contrast matrices from factors is that the rows of the result are the same, irrespective of which factor levels are present in data, i.e. that the model.matrix() for a subset of data is the corresponding subset of model.matrix() applied to all data. Breaking that convention looks like a path to madness. (Actually, it has been broken, historically, by the spline functions, and that did indeed lead to madness, at least initially; google for safe.predict() if you care)

It is not unlikely that what you are really looking for is

interaction(G, D, drop=TRUE)