Some additional information to help you to move forward with categorical variable...
When dealing with categorical variable (which can be enforced using factor()
to make sure that R does not treat it as another type), the reference is actually included in the intercept:
require(datasets); data(InsectSprays)
model1 <- lm(count ~ spray, data = InsectSprays)
summary(model1)$coefficients
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 14.5000000 1.132156 12.8074279 1.470512e-19
## sprayB 0.8333333 1.601110 0.5204724 6.044761e-01
## sprayC -12.4166667 1.601110 -7.7550382 7.266893e-11
## sprayD -9.5833333 1.601110 -5.9854322 9.816910e-08
## sprayE -11.0000000 1.601110 -6.8702352 2.753922e-09
## sprayF 2.1666667 1.601110 1.3532281 1.805998e-01
So, here, spray A is included in the intercept and its value is 14.5. Since it is the reference, all the other coefficients are given based on this value (they are compared with the reference sprayA). For instance, the coefficient for sprayB is 14.5 + 0.833 = 15.333. The coefficient for sprayC is 14.5 - 12.416 = 2.084.
If you want to get the coefficient value of each spray category, you can also do the following (subtracting by 1) which remove the intercept:
model2 <- lm(count ~ spray - 1, data = InsectSprays)
summary(model2)$coefficients
## Estimate Std. Error t value Pr(>|t|)
## sprayA 14.500000 1.132156 12.807428 1.470512e-19
## sprayB 15.333333 1.132156 13.543487 1.001994e-20
## sprayC 2.083333 1.132156 1.840148 7.024334e-02
## sprayD 4.916667 1.132156 4.342749 4.953047e-05
## sprayE 3.500000 1.132156 3.091448 2.916794e-03
## sprayF 16.666667 1.132156 14.721181 1.573471e-22
Which is equivalent to what we calculated manually earlier.
Finally, if you want to change your reference, you can use the relevel()
function:
sprayDRef <- relevel(InsectSprays$spray, "D")
model3 <- lm(count ~ sprayDRef, data = InsectSprays)
summary(model3)$coefficients
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.916667 1.132156 4.342749 4.953047e-05
## sprayDRefA 9.583333 1.601110 5.985432 9.816910e-08
## sprayDRefB 10.416667 1.601110 6.505905 1.212803e-08
## sprayDRefC -2.833333 1.601110 -1.769606 8.141205e-02
## sprayDRefE -1.416667 1.601110 -0.884803 3.794750e-01
## sprayDRefF 11.750000 1.601110 7.338660 4.035610e-10
Here, all the different spray category coefficients are compared with spray D.