[英]How to set specific contrasts in multinom() in nnet package?
I have a 3-class problem that needs classification. 我有一个需要分类的3级问题。 I want to use the multinomial logistic regression in nnet
package. 我想在nnet
包中使用多项逻辑回归。 The Class outcome has 3 factors, P, Q, R. I want to treat Q as the base factor. 类结果有3个因子,P,Q,R。我想把Q作为基本因子。
So I tried to write it the contrasts like this: 所以我试着写下这样的对比:
P <- c(1,0,0)
R <- c(0,0,1)
contrasts(trainingLR$Class) <- cbind(P,R)
checked it: 检查了它:
> contrasts(trainingLR$Class)
P R
P 1 0
Q 0 0
R 0 1
Now multinom()
: 现在multinom()
:
library(nnet)
multinom(Class ~., data=trainingLR)
Output: 输出:
> multinom(Class ~., data=trainingLR)
# weights: 39 (24 variable)
initial value 180.172415
iter 10 value 34.990665
iter 20 value 11.765136
iter 30 value 0.162491
iter 40 value 0.000192
iter 40 value 0.000096
iter 40 value 0.000096
final value 0.000096
converged
Call:
multinom(formula = Class ~ ., data = trainingLR)
Coefficients:
(Intercept) IL8 IL17A IL23A IL23R
Q -116.2881 -16.562423 -34.80174 3.370051 6.422109
R 203.2414 6.918666 -34.40271 -10.233787 31.446915
EBI3 IL6ST IL12A IL12RB2 IL12B
Q -8.316808 12.75168 -7.880954 5.686425 -9.665776
R 5.135609 -20.48971 -2.093231 37.423452 14.669226
IL12RB1 IL27RA
Q -6.921755 -1.307048
R 15.552842 -7.063026
Residual Deviance: 0.0001922658
AIC: 48.00019
Question: 题:
So as you see, since P class didn't appear in the output, it means that it was treated as base being the first one in alphabetical order as expected when dealing with factor variables in R, and Q class was not treated as base level in this case, how to make it base to the other two levels? 如你所见,由于P类没有出现在输出中,这意味着它在处理R中的因子变量时按字母顺序被视为基数是第一个,并且Q类不被视为基础级别在这种情况下,如何使其基于其他两个级别?
I tried to avoid using contrasts and I discovered the relevel
function for choosing a desired level as baseline. 我试图避免使用对比,我发现了relevel
功能选择所需的水平基线。 The following code 以下代码
trainingLR$Class <- relevel(trainingLR$Class, ref = "P")
should set "P" level as your baseline. 应将“P”级别设为您的基线。 Therefore, try the same thing with "Q" or "R" levels. 因此,尝试“Q”或“R”级别相同的事情。
The R Documentation ( ?relevel
) mentions "This is useful for contr.treatment
contrasts which take the first level as the reference." R文档( ?relevel
)提到“这对于以第一级作为参考的contr.treatment
对比有用”。
Though might be too late to answer now, but since others might be interested, I thought is worthwhile sharing the above option. 虽然现在回答可能为时已晚,但由于其他人可能会感兴趣,我认为值得分享上述选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.