简体   繁体   English

如何在nnet包中的multinom()中设置特定的对比?

[英]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.

相关问题 如何在nnet包中为multinom方法指定weights参数 - How to specify the weights parameter in nnet package for multinom method 用R中的响应(nnet程序包)分离“多项式”估计 - separating `multinom` estimations by response (nnet package) in R 来自R package nnet的函数multinom如何计算多项式概率权重? - How does the function multinom from R package nnet compute the multinomial probability weights? 当使用nnet包中的multinom函数时,如何控制神经网络的架构? - When using the multinom function from the nnet package, how can I control the architecture of the neural networks? 来自 nnet::multinom 的预测 - predictions from nnet::multinom R中的多项式逻辑回归:nnet程序包中的多项式与mlogit程序包中的mlogit有何不同? - multinomial logistic regression in R: multinom in nnet package result different from mlogit in mlogit package? 来自 R 的 nnet package 的 function multinom() 是否适合多项逻辑回归或泊松回归? - Does the function multinom() from R's nnet package fit a multinomial logistic regression, or a Poisson regression? 多项回归(不同的结果 - 相同的数据集,R与SPSS)。 nnet包 - multinom功能 - Multinomial regression (different results — same dataset, R vs SPSS). nnet package — multinom function 如何在函数内的multinom / nnet对象上调用model.frame()? - How do you call model.frame() on a multinom/nnet object within a function? 如何使用基于 nnet::multinom() 模型的 {ggeffects} 获得预测概率图的置信区间? - How to get confidence intervals for predicted probability plot using {ggeffects} based on nnet::multinom() model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM