model.matrix(): why do I lose control of contrast in this case

谁说胖子不能爱 提交于 2019-11-29 12:46:31
李哲源

Whenever we lose control of something at R level, there must be some default, unchangable behaviour at C level. C code for model.matrix.default() can be found in R source package at:

R-<release_number>/src/library/stats/src/model.c

We can find the explanation here:

/* If there is no intercept we look through the factor pattern */
/* matrix and adjust the code for the first factor found so that */
/* it will be coded by dummy variables rather than contrasts. */

Let's make a small test on this, with a data frame

x <- data.frame(x1 = gl(2, 2, labels = letters[1:2]), x2 = sin(1:4))
  1. if we only have x2 on the RHS, we can drop intercept successfully:

    model.matrix(~ x2 - 1, data = x)
    #          x2
    #1  0.8414710
    #2  0.9092974
    #3  0.1411200
    #4 -0.7568025
    
  2. if we have only x1 on the RHS, contrast is not applied:

    model.matrix(~ x1 - 1, data = x)
    #  x1a x1b
    #1   1   0
    #2   1   0
    #3   0   1
    #4   0   1
    
  3. when we have both x1 and x2, contrast is not applied:

    model.matrix(~ x1 + x2 - 1, data = x)
    #  x1a x1b         x2
    #1   1   0  0.8414710
    #2   1   0  0.9092974
    #3   0   1  0.1411200
    #4   0   1 -0.7568025
    

This implies that while there is difference between:

lm(y ~ x2, data = x)
lm(y ~ x2 - 1, data = x)

there is no difference between

lm(y ~ x1, data = x)
lm(y ~ x1 - 1, data = x)

or

lm(y ~ x1 + x2, data = x)
lm(y ~ x1 + x2 - 1, data = x)

The reason for such behaviour is not to ensure numerical stability, but to ensure the sensibility of estimation / prediction. If we really drop the intercept while applying contrast to x1, we end up with a model matrix:

    #  x1b
    #1   0
    #2   0
    #3   1
    #4   1

The effect is that we constrain estimation for level a to 0.

In this post: How can I force dropping intercept or equivalent in this linear model?, we have a dataset:

#           Y    X1    X2
#1  1.8376852  TRUE  TRUE
#2 -2.1173739  TRUE FALSE
#3  1.3054450 FALSE  TRUE
#4 -0.3476706  TRUE FALSE
#5  1.3219099 FALSE  TRUE
#6  0.6781750 FALSE  TRUE

There isn't joint existence (X1 = FALSE, X2 = FALSE) in this dataset. But in broad sense, model.matrix() has to do something safe and sensible. It is biased to assume that no joint existence of two factor levels in the training dataset implies that they need not be predicted. If we really drop intercept while applying contrast, such joint existence is constrained at 0. However, the OP of that post deliberately wants such non-standard behaviour (for some reason), in which case, a possible workaround was given in my answer there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!