如何在 R 中的 lm 中获取有序分类变量的 0-1 虚拟变量?
How to get 0-1 dummies for ordered categorical variables in lm in R?
当 运行 线性模型在 R
中具有分类因变量时,该变量在内部重新编码为虚拟变量:
unord <- data.frame(y = c(1, 2, 3, 12, 11, 13, 101, 103, 102, 1003, 1002, 1001),
cat = factor(rep(LETTERS[1:4], each = 3), ordered = FALSE))
model.matrix(lm(y~cat, data = unord))
(Intercept) catB catC catD
1 1 0 0 0
2 1 0 0 0
3 1 0 0 0
4 1 1 0 0
5 1 1 0 0
6 1 1 0 0
7 1 0 1 0
8 1 0 1 0
9 1 0 1 0
10 1 0 0 1
11 1 0 0 1
12 1 0 0 1
我喜欢什么。
但是,如果分类因变量是有序的,则虚拟变量出于某种原因不太直观:
ord <- data.frame(y = c(1, 2, 3, 12, 11, 13, 101, 103, 102, 1003, 1002, 1001),
cat = factor(rep(LETTERS[1:4], each = 3), ordered = TRUE))
model.matrix(lm(y~cat, data = ord))
(Intercept) cat.L cat.Q cat.C
1 1 -0.6708204 0.5 -0.2236068
2 1 -0.6708204 0.5 -0.2236068
3 1 -0.6708204 0.5 -0.2236068
4 1 -0.2236068 -0.5 0.6708204
5 1 -0.2236068 -0.5 0.6708204
6 1 -0.2236068 -0.5 0.6708204
7 1 0.2236068 -0.5 -0.6708204
8 1 0.2236068 -0.5 -0.6708204
9 1 0.2236068 -0.5 -0.6708204
10 1 0.6708204 0.5 0.2236068
11 1 0.6708204 0.5 0.2236068
12 1 0.6708204 0.5 0.2236068
问题是如何获得有序分类变量的"usual"虚拟变量?注意:问题不是关于如何正确使用来自排序的信息( https://stats.stackexchange.com/questions/33413/continuous-dependent-variable-with-ordinal-independent-variable).
您可以通过使用 contrasts
参数强制使用哪个对比度,在 lm
中或如下 model.matrix
中(因为我删除了额外的 lm
调用)
model.matrix(y ~ cat, data = ord, contrasts = list(cat=contr.treatment))
如果你有多个因子列,你可以这样做
nms <- names(ord)[sapply(ord, is.factor)] # get names of factor variables
model.matrix(y ~ cat, data = ord,
contrasts = sapply(nms, function(x) list(contr.treatment)))
当 运行 线性模型在 R
中具有分类因变量时,该变量在内部重新编码为虚拟变量:
unord <- data.frame(y = c(1, 2, 3, 12, 11, 13, 101, 103, 102, 1003, 1002, 1001),
cat = factor(rep(LETTERS[1:4], each = 3), ordered = FALSE))
model.matrix(lm(y~cat, data = unord))
(Intercept) catB catC catD
1 1 0 0 0
2 1 0 0 0
3 1 0 0 0
4 1 1 0 0
5 1 1 0 0
6 1 1 0 0
7 1 0 1 0
8 1 0 1 0
9 1 0 1 0
10 1 0 0 1
11 1 0 0 1
12 1 0 0 1
我喜欢什么。 但是,如果分类因变量是有序的,则虚拟变量出于某种原因不太直观:
ord <- data.frame(y = c(1, 2, 3, 12, 11, 13, 101, 103, 102, 1003, 1002, 1001),
cat = factor(rep(LETTERS[1:4], each = 3), ordered = TRUE))
model.matrix(lm(y~cat, data = ord))
(Intercept) cat.L cat.Q cat.C
1 1 -0.6708204 0.5 -0.2236068
2 1 -0.6708204 0.5 -0.2236068
3 1 -0.6708204 0.5 -0.2236068
4 1 -0.2236068 -0.5 0.6708204
5 1 -0.2236068 -0.5 0.6708204
6 1 -0.2236068 -0.5 0.6708204
7 1 0.2236068 -0.5 -0.6708204
8 1 0.2236068 -0.5 -0.6708204
9 1 0.2236068 -0.5 -0.6708204
10 1 0.6708204 0.5 0.2236068
11 1 0.6708204 0.5 0.2236068
12 1 0.6708204 0.5 0.2236068
问题是如何获得有序分类变量的"usual"虚拟变量?注意:问题不是关于如何正确使用来自排序的信息( https://stats.stackexchange.com/questions/33413/continuous-dependent-variable-with-ordinal-independent-variable).
您可以通过使用 contrasts
参数强制使用哪个对比度,在 lm
中或如下 model.matrix
中(因为我删除了额外的 lm
调用)
model.matrix(y ~ cat, data = ord, contrasts = list(cat=contr.treatment))
如果你有多个因子列,你可以这样做
nms <- names(ord)[sapply(ord, is.factor)] # get names of factor variables
model.matrix(y ~ cat, data = ord,
contrasts = sapply(nms, function(x) list(contr.treatment)))