猿问

R模型矩阵中因子的所有层次

我有一个data.frame由数字和因子组成的变量,如下所示。


testFrame <- data.frame(First=sample(1:10, 20, replace=T),

           Second=sample(1:20, 20, replace=T), Third=sample(1:10, 20, replace=T),

           Fourth=rep(c("Alice","Bob","Charlie","David"), 5),

           Fifth=rep(c("Edward","Frank","Georgia","Hank","Isaac"),4))

我想建立一个matrix将虚拟变量分配给因子并仅保留数字变量的方法。


model.matrix(~ First + Second + Third + Fourth + Fifth, data=testFrame)

如预期的那样,在运行时,lm这会将每个因子的一个水平作为参考水平。但是,我想为matrix所有因素的每个层次建立一个带有虚拟/指标变量的。我正在为此建立矩阵,glmnet所以我不必担心多重共线性。


有没有一种方法可以model.matrix为每个因子水平创建虚拟对象?


牛魔王的故事
浏览 926回答 3
3回答

慕桂英546537

您需要重置contrasts因子变量:model.matrix(~ Fourth + Fifth, data=testFrame,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; contrasts.arg=list(Fourth=contrasts(testFrame$Fourth, contrasts=F),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fifth=contrasts(testFrame$Fifth, contrasts=F)))或者,键入少一点,但没有适当的名称:model.matrix(~ Fourth + Fifth, data=testFrame,&nbsp;&nbsp; &nbsp; contrasts.arg=list(Fourth=diag(nlevels(testFrame$Fourth)),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fifth=diag(nlevels(testFrame$Fifth))))

慕工程0101907

(尝试赎回自己...)为响应Jared关于@Fabians自动化的回答,请注意,您需要提供的只是一个对比矩阵的命名列表。contrasts()取一个向量/因数并从中产生对比度矩阵。对于这个那么我们可以用lapply()运行contrasts()在我们的数据集,例如用于各个因素testFrame提供的示例:> lapply(testFrame[,4:5], contrasts, contrasts = FALSE)$Fourth&nbsp; &nbsp; &nbsp; &nbsp; Alice Bob Charlie DavidAlice&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;0Bob&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;0Charlie&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;0David&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;1$Fifth&nbsp; &nbsp; &nbsp; &nbsp; Edward Frank Georgia Hank IsaacEdward&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;0Frank&nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;0Georgia&nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;0Hank&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp;0Isaac&nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;1@fabians哪个插槽很好地回答:model.matrix(~ ., data=testFrame,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contrasts.arg = lapply(testFrame[,4:5], contrasts, contrasts=FALSE))
随时随地看视频慕课网APP
我要回答