How to set contrasts for my variable in regression analysis with R?
Asked Answered
D

2

3

During coding, I need to change the dummy value assigned to a factor. However, the following code does not work. Any suggestion?

test_mx= data.frame(a= c(T,T,T,F,F,F), b= c(1,1,1,0,0,0))
test_mx
      a b
1  TRUE 1
2  TRUE 1
3  TRUE 1
4 FALSE 0
5 FALSE 0
6 FALSE 0

model= glm(b ~ a, data= test_mx, family= "binomial")
summary(model)

model= glm(a ~ b, data= test_mx, family= "binomial")
summary(model)

Here I will get the coef for b is 47. Now if I swap the dummy value, it should be -47 then. However, this is not the case.

test_mx2= test_mx
contrasts(test_mx2$a)
      TRUE
FALSE    0
TRUE     1
contrasts(test_mx2$a) = c(1,0)
contrasts(test_mx2$a)
      [,1]
FALSE    1
TRUE     0
model= glm(a ~ b, data= test_mx2, family= "binomial")
summary(model)

coef for b is still the same. What is going on? Thanks.

Duester answered 1/10, 2016 at 2:28 Comment(7)
dont you mean b ~ a? since you are modeling a (a factor) as the outcome, the second level is "success" which still is "TRUE" (as in all the examples with a as the outcome), and the coefficient is for b=1 as in the other modelsDoran
Hi Zheyuan, I am not reversing a and b: I am reversing the dummy variable assignment within a.Duester
Hi rawr, I am doing logistic regression (not least square regression) and the coef for b is not 1.Duester
"the coefficient is for b=1" is what I said, you misunderstoodDoran
Hi Zheyuan, please see the updated question: b ~ a does not have a problem even using direct dummy value assignment. The problem is in a ~ b.Duester
coef(glm(a ~ b, data = test_mx, family = binomial(), contrasts = list(a = contr.treatment(n = 2, base = 1)))) and coef(glm(a ~ b, data = test_mx, family = binomial(), contrasts = list(a = contr.treatment(n = 2, base = 2)))) produce the same answer. That is not correct. Note: It is a~b not b~a.Duester
In modeling a~b, in which a is a categorical response with 2 levels: L1, L2. When predicted probability y.prob > 0.5 (y.prob= predict(glm.model, test_mx, type="response"), should the item be assigned to L1 or L2? Since contrasts of a do not control assignment, what does?Duester
S
7

There are several confusing things regarding your question. You have used both a ~ b and b ~ a, so what are you looking at exactly?

  • Contrasts only applies to covariates / independent variables, as it is related to construction of model matrix; So for a ~ b, contrasts should be applied to b, while for b ~ a, contrasts should be applied to a;
  • Contrasts only works for factor / logical variables, rather than numerical variables. So unless you have b as a factor, you can't play contrasts with it.

Without changing data type, it is clear that only a model b ~ a is legitimate for further discussion. In the following, I will show how to set contrasts for a.


Method 1: using contrasts argument of glm and lm

We can control contrasts treatment by the contrasts argument of glm (the same for lm):

## dropping the first factor level (default)
coef(glm(b ~ a, data = test_mx, family = binomial(),
     contrasts = list(a = contr.treatment(n = 2, base = 1))))
#(Intercept)          a2 
#  -24.56607    49.13214 

## dropping the second factor level
coef(glm(b ~ a, data = test_mx, family = binomial(),
     contrasts = list(a = contr.treatment(n = 2, base = 2))))
#(Intercept)          a1 
#   24.56607   -49.13214 

Here, contr.treatment is generating a contrasts matrix:

contr.treatment(n = 2, base = 1)
#  2
#1 0
#2 1

contr.treatment(n = 2, base = 2)
#  1
#1 1
#2 0

and they are passed to glm to effectively change the behaviour of model.matrix.default. Let's compare the model matrix for two cases:

model.matrix.default( ~ a, test_mx, contrasts.arg =
                     list(a = contr.treatment(n = 2, base = 1)))

#  (Intercept) a2
#1           1  1
#2           1  1
#3           1  1
#4           1  0
#5           1  0
#6           1  0

model.matrix.default( ~ a, test_mx, contrasts.arg =
                     list(a = contr.treatment(n = 2, base = 2)))

#  (Intercept) a1
#1           1  0
#2           1  0
#3           1  0
#4           1  1
#5           1  1
#6           1  1

The second column for a is just a flip between 0 and 1, which is what you have expected for a dummy variable.


Method 2: setting "contrasts" attribute to data frame directly

We can use C or contrasts to set "contrasts" attributes (C is only for setting, but contrasts can be used for viewing as well):

test_mx2 <- test_mx
contrasts(test_mx2$a) <- contr.treatment(n = 2, base = 1)
str(test_mx2)
#'data.frame':  6 obs. of  2 variables:
# $ a: Factor w/ 2 levels "FALSE","TRUE": 2 2 2 1 1 1
#  ..- attr(*, "contrasts")= num [1:2, 1] 0 1
#  .. ..- attr(*, "dimnames")=List of 2
#  .. .. ..$ : chr  "FALSE" "TRUE"
#  .. .. ..$ : chr "2"
# $ b: num  1 1 1 0 0 0

test_mx3 <- test_mx
contrasts(test_mx3$a) <- contr.treatment(n = 2, base = 2)
str(test_mx3)
#'data.frame':  6 obs. of  2 variables:
# $ a: Factor w/ 2 levels "FALSE","TRUE": 2 2 2 1 1 1
#  ..- attr(*, "contrasts")= num [1:2, 1] 1 0
#  .. ..- attr(*, "dimnames")=List of 2
#  .. .. ..$ : chr  "FALSE" "TRUE"
#  .. .. ..$ : chr "1"
# $ b: num  1 1 1 0 0 0

Now we can fit glm without using contrasts argument:

coef(glm(b ~ a, data = test_mx2, family = "binomial"))
#(Intercept)          a2 
#  -24.56607    49.13214 

coef(glm(b ~ a, data = test_mx3, family = "binomial"))
#(Intercept)          a1 
#   24.56607   -49.13214 

Method 3: setting options("contrasts") for a global change

Hahaha, @BenBolker yet mentions another option, which is by setting the global options of R. For your specific example with a factor involving only two levels, we can makes use of ?contr.SAS.

## using R default contrasts options
#$contrasts
#        unordered           ordered 
#"contr.treatment"      "contr.poly" 

coef(glm(b ~ a, data = test_mx, family = "binomial"))
#(Intercept)       aTRUE 
#  -24.56607    49.13214 

options(contrasts = c("contr.SAS", "contr.poly"))
coef(glm(b ~ a, data = test_mx, family = "binomial"))
#(Intercept)      aFALSE 
#   24.56607   -49.13214 

But I believe Ben is just mention this to complete the picture; He will not take this way in reality, as changing global options is not good for getting reproducible R code.

Another issue is that contr.SAS will just treat the last factor level as reference. In your particular case with only 2 levels, this effectively does the "flipping".


Method 4: Manually recoding your factor levels

I had no intention to mention this as it is so trivial, but since I have added "Method 3", I'd better add this one, too.

test_mx4 <- test_mx
test_mx4$a <- factor(test_mx4$a, levels = c("TRUE", "FALSE"))
coef(glm(b ~ a, data = test_mx4, family = "binomial"))
#(Intercept)       aTRUE 
#  -24.56607    49.13214 

test_mx5 <- test_mx
test_mx5$a <- factor(test_mx5$a, levels = c("FALSE", "TRUE"))
coef(glm(b ~ a, data = test_mx5, family = "binomial"))
#(Intercept)      aFALSE 
#   24.56607   -49.13214 
Shortbread answered 1/10, 2016 at 4:27 Comment(1)
option 3: use global options, options(contrast=c("contr.SAS","contr.poly")). (?contr.SAS is useful here ...)Affliction
D
0

As pointed out by Zheyuan, Contrasts only control dummy value assignment for categorical predictors (x values) but not for categorical response (y value) in glm modeling. I have reported this issue to R core team.

To assign dummy value manually for predictors, another way can be direct assignment by a vector/matrix such as

contrasts(test_mx$a) = c(1,0)

However, there is a risk to do so: if later in the code you try to use test_mx$a as a response value in modeling, dummy value assignment can be confusing, as the assignment there will NOT match with contrasts(test_mx$a).

Duester answered 2/10, 2016 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.