Generalized Linear Models in R
Richard Erickson
Instructor
model.matrix() keyheight = c(72.3, 21.1, 3.7, 1.0)fish = c("red", "blue")y ~ xy ~ x -1y ~ x1 + x2y ~ x1+ x2 - 1model.matrix())color = c("red", "blue")y ~ colors:intercept = c(1, 1)blue = c(0, 1)y ~ colors - 1 :red = c(1, 0)blue = c(0, 1)model.matrix() does legwork for usmodel.matrix( ~ colors)
(Intercept) colorsred
1 1 1
2 1 0
attr(,"assign")
[1] 0 1
attr(,"contrasts")
attr(,"contrasts")$colors
"contr.treatment"
factor()month = c(1, 2, 3)month <- c( 1, 2, 3)
model.matrix( ~ month)
(Intercept) month
1 1 1
2 1 2
3 1 3
attr(,"assign")
0 1
month = factor(c( 1, 2, 3))model.matrix( ~ month)
(Intercept) month2 month3
1 1 0 0
2 1 1 0
3 1 0 1
attr(,"assign")
0 1 1
attr(,"contrasts")$month
"contr.treatment"
Generalized Linear Models in R