1
$\begingroup$

Using this code and sample data:

hsb2 <- read.csv("https://stats.idre.ucla.edu/stat/data/hsb2.csv")
# creating the factor variable
hsb2$race.f <- factor(hsb2$race)

#creating the contrast matrix manually by modifying the dummy coding scheme
c<-contr.treatment(4)
my.coding<-matrix(rep(1/4, 12), ncol=3)
my.simple<-c-my.coding
my.simple

contrasts(hsb2$race.f)<-my.simple
m1=lm(write~race.f, hsb2)    
summary(m1)

We get:

my.simple
      2     3     4
1 -0.25 -0.25 -0.25
2  0.75 -0.25 -0.25
3 -0.25  0.75 -0.25
4 -0.25 -0.25  0.75


summary(m1))
 Call:
lm(formula = write ~ race.f, data = hsb2)

Residuals:
     Min       1Q   Median       3Q      Max 
-23.0552  -5.4583   0.9724   7.0000  18.8000 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  51.6784     0.9821  52.619  < 2e-16 ***
race.f2      11.5417     3.2861   3.512 0.000552 ***
race.f3       1.7417     2.7325   0.637 0.524613    
race.f4       7.5968     1.9889   3.820 0.000179 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 9.025 on 196 degrees of freedom
Multiple R-squared:  0.1071,    Adjusted R-squared:  0.0934 
F-statistic: 7.833 on 3 and 196 DF,  p-value: 5.785e-05

But if you were only interested in the contrast coded in col 4 of my.simple (-.25,-.25, -.25,.75), and thus wrote

hsb2$race.fx=as.numeric(ifelse(hsb2$race.f=="1","-.25",ifelse(hsb2$race.f=="2","-.25",ifelse(hsb2$race.f=="3","-.25",".75"))))
m2=lm(write~race.fx, hsb2)    
    summary(m2)

we get

summary(m2)

Call:
lm(formula = write ~ race.f2, data = hsb2)

Residuals:
     Min       1Q   Median       3Q      Max 
-23.0552  -5.4000   0.7724   7.9448  17.6000 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   50.564      0.957  52.834  < 2e-16 ***
race.f2        4.655      1.468   3.171  0.00176 ** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 9.27 on 198 degrees of freedom
Multiple R-squared:  0.04833,   Adjusted R-squared:  0.04353 
F-statistic: 10.06 on 1 and 198 DF,  p-value: 0.00176

This result seems counter-intuitive to me, shouldn't the effect of race.fx be comparable to race.f4? Can anyone explain what I am not understanding in this example?

$\endgroup$
1
  • 1
    $\begingroup$ This isn't a variable-coding issue: you have fit two distinct models. $\endgroup$
    – whuber
    Commented Apr 1 at 18:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.