简体   繁体   English

将R中的model.matrix转换为组向量

[英]convert a model.matrix in R to a group vector

In R, we can use model.matrix() to construct design matrices, for example, 在R中,我们可以使用model.matrix()来构造设计矩阵,例如,

grp.ids = as.factor(c(rep(1,8), rep(2,4), rep(3,2)))
x = model.matrix(~grp.ids)

gives the design matrix x : 给出设计矩阵x

   (Intercept) grp.ids2 grp.ids3
1            1        0        0
2            1        0        0
3            1        0        0
4            1        0        0
5            1        0        0
6            1        0        0
7            1        0        0
8            1        0        0
9            1        1        0
10           1        1        0
11           1        1        0
12           1        1        0
13           1        0        1
14           1        0        1
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$grp.ids
[1] "contr.treatment"

However, if now I am given a design matrix x as above, and hope to get the "grouping vector" grp.ids by somehow manipulating on x . 但是,如果现在给我一个如上所述的设计矩阵x ,并希望以某种方式操纵x来获得“分组矢量” grp.ids How can I do that? 我怎样才能做到这一点? Thanks! 谢谢!

I don't believe you could recover grp.id exactly as it was originally created because it's not possible to tell what the original values of the ids were. 我不相信你可以完全像最初创建的那样恢复grp.id,因为它无法分辨出id的原始值是什么。 You can create a vector that results in the same model.maxtrix though. 您可以创建一个导致相同model.maxtrix的向量。

factor(apply(x, 1, paste, collapse = "."), labels = seq(ncol(x)))

However, this gets pretty close in this particular case. 但是,在这种特殊情况下,这种情况非常接近。


The labels for the previous one give the order of 1, 3, 2 (instead of the desired 1, 2, 3) and that is because we get "1.0.0", "1.1.0", "1.0.1" as our actual output and sorted alphanumerically these give the order 1, 3, 2. If we reversed the input string so we had "0.0.1", "0.1.1" and "1.0.1" then this would give the desired order so the following should work 前一个的标签给出了1,3,2(而不是所需的1,2,3)的顺序,这是因为我们得到“1.0.0”,“1.1.0”,“1.0.1”为我们的实际输出和字母数字排序这些给出顺序1,3,2。如果我们反转输入字符串所以我们有“0.0.1”,“0.1.1”和“1.0.1”然后这将给出所需的顺序所以以下应该有效

factor(apply(x, 1, function(x){paste(rev(x), collapse = ".")}), labels = seq(ncol(x)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM