R實(shí)現(xiàn)多分類(lèi)logistic回歸

多分類(lèi)logistic回歸

在臨床研究中,接觸最多的是二分類(lèi)數(shù)據(jù),如淋巴癌是否轉(zhuǎn)移,是否死亡,這些因變量最后都可以轉(zhuǎn)換成二分類(lèi)0與1的問(wèn)題。然后建立二元logistic回歸方程,可以得到影響因素的OR值。

那么如果遇到多分類(lèi)變量,如何進(jìn)行l(wèi)ogistic回歸呢?譬如臨床療效分為好,中,差,三類(lèi),或者根據(jù)指標(biāo)進(jìn)行分類(lèi),分為高,中,低三類(lèi),我用1、2、3代表作為因變量,進(jìn)行l(wèi)ogistic回歸分析。


image.png

接下來(lái),該文,主要介紹,如果因變量為三分類(lèi)變量,如何進(jìn)行回歸分析及機(jī)器學(xué)習(xí)算法對(duì)三分類(lèi)資料的處理。關(guān)于原理理論部分可參見(jiàn);這里主要講如何在R實(shí)現(xiàn)三分類(lèi)回歸,計(jì)算系數(shù)及p值OR

1.數(shù)據(jù)案例

這里主要用到DALEX包里面包含的HR數(shù)據(jù),里面記錄了職工在工作崗位的狀態(tài)與年齡,性別,工作時(shí)長(zhǎng),評(píng)價(jià)及薪水有關(guān)。根據(jù)7847條記錄來(lái)評(píng)估,如果一個(gè)職工屬于男性,68歲,薪水及評(píng)價(jià)處于3等級(jí),那么該職工可能會(huì)處于什么狀態(tài)。

library(DALEX)
library(iBreakDown)
library(nnet)
library(questionr)
try(data(package="DALEX"))
data(HR)
HR= HR %>%  as.tbl() %>% 
  mutate(evaluation=factor(evaluation),
         salary=factor(salary))
HR

## GLM
fit =  multinom(status ~ . , data = HR, probabilities = TRUE, model = TRUE)
summary(fit)
coef(fit)
> summary(fit)
Call:
multinom(formula = status ~ ., data = HR, model = TRUE, probabilities = TRUE)

Coefficients:
         (Intercept) gendermale         age      hours evaluation3 evaluation4 evaluation5
ok          -5.47276 0.03437426 0.002594237 0.08305463 -0.07275332 -0.06763166   -0.156932
promoted   -13.10377 0.10391193 0.004277562 0.19697483 -0.11679839  3.49127986    3.290217
          salary1  salary2  salary3  salary4     salary5
ok       1.543631 2.469598 2.413207 1.758516 -0.09513189
promoted 1.650777 2.498608 2.435680 1.790657 -0.01215312

Std. Errors:
         (Intercept) gendermale         age       hours evaluation3 evaluation4
ok         0.2407953 0.06427342 0.002784445 0.003669856  0.07434565   0.1061762
promoted   0.3475852 0.08023646 0.003458588 0.004692886  0.11569077   0.1303817
         evaluation5   salary1   salary2   salary3   salary4   salary5
ok         0.1077599 0.1199301 0.1227032 0.1219577 0.1212186 0.1371131
promoted   0.1302725 0.1456579 0.1490790 0.1482045 0.1486752 0.1624046

Residual Deviance: 10744.64 
AIC: 10792.64 
> coef(fit)
         (Intercept) gendermale         age      hours evaluation3 evaluation4 evaluation5
ok          -5.47276 0.03437426 0.002594237 0.08305463 -0.07275332 -0.06763166   -0.156932
promoted   -13.10377 0.10391193 0.004277562 0.19697483 -0.11679839  3.49127986    3.290217
          salary1  salary2  salary3  salary4     salary5
ok       1.543631 2.469598 2.413207 1.758516 -0.09513189
promoted 1.650777 2.498608 2.435680 1.790657 -0.01215312

我們構(gòu)建了三元回歸模型,以statusfired為參照,計(jì)算okpromoted中各個(gè)因素的系數(shù)。
有了這些系數(shù),我們就可以寫(xiě)出回歸方程了,然后再計(jì)算各個(gè)因素對(duì)應(yīng)的p值
如,這里的例子介紹了其他因素的系數(shù),然后計(jì)算對(duì)因變量的方程here

image.png

P值

通過(guò)Anova函數(shù),可以輸出fit中影響因素的p值,其中hoursevaluationsalary有統(tǒng)計(jì)學(xué)意義。說(shuō)明他們對(duì)員工在職影響很大。然后進(jìn)一步計(jì)算or值。
需要借助questionr包中的odds.ratio函數(shù)。

> Anova(fit)
Analysis of Deviance Table (Type II tests)

Response: status
           LR Chisq Df Pr(>Chisq)    
gender          1.7  2     0.4299    
age             1.7  2     0.4329    
hours        3464.1  2     <2e-16 ***
evaluation   2390.2  6     <2e-16 ***
salary       1132.4 10     <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> round(odds.ratio(fit),2)
                        OR 2.5 % 97.5 %      p    
ok/(Intercept)        0.00  0.00   0.01 <2e-16 ***
ok/gendermale         1.03  0.91   1.17   0.59    
ok/age                1.00  1.00   1.01   0.35    
ok/hours              1.09  1.08   1.09 <2e-16 ***
ok/evaluation3        0.93  0.80   1.08   0.33    
ok/evaluation4        0.93  0.76   1.15   0.52    
ok/evaluation5        0.85  0.69   1.06   0.15    
ok/salary1            4.68  3.70   5.92 <2e-16 ***
ok/salary2           11.82  9.29  15.03 <2e-16 ***
ok/salary3           11.17  8.79  14.19 <2e-16 ***
ok/salary4            5.80  4.58   7.36 <2e-16 ***
ok/salary5            0.91  0.69   1.19   0.49    
promoted/(Intercept)  0.00  0.00   0.00 <2e-16 ***
promoted/gendermale   1.11  0.95   1.30   0.20    
promoted/age          1.00  1.00   1.01   0.22    
promoted/hours        1.22  1.21   1.23 <2e-16 ***
promoted/evaluation3  0.89  0.71   1.12   0.31    
promoted/evaluation4 32.83 25.43  42.39 <2e-16 ***
promoted/evaluation5 26.85 20.80  34.66 <2e-16 ***
promoted/salary1      5.21  3.92   6.93 <2e-16 ***
promoted/salary2     12.17  9.08  16.29 <2e-16 ***
promoted/salary3     11.42  8.54  15.27 <2e-16 ***
promoted/salary4      5.99  4.48   8.02 <2e-16 ***
promoted/salary5      0.99  0.72   1.36   0.94    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

參考

  1. MULTINOMIAL LOGISTIC REGRESSION
  2. 邏輯回歸(Logistic Regression)詳解
  3. iBreakDown plots for classification models
  4. MULTINOMIAL LOGISTIC REGRESSION USING R
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容