# logistic regression(nomial data, predictive power) ###### tags: `Categorical data analysis` ## Logistic regression <font size = 5.5> $logit(\pi(x)) = \alpha + \beta x$ $\pi(x) = \frac{e^{\alpha + \beta x}}{1 + e^{\alpha + \beta x}}$ $1 -\pi(x) = \frac{1}{1 + e^{\alpha + \beta x}}$ $\Omega(x) = e^{\alpha + \beta x}$ </font> 由下可以導出 $x$ 跟 odds ratio 的 relation  由此知, $x$ 每增加1,勝算變為 $e^\beta$ 倍 要注意的是 $\beta$ 的值會隨著reference的不同而不同,所以稱這個 $\beta$ 為不可估的。唯一會相同的是對勝算比的 intepretation 由下可知 $\pi(x)$ 在不同 x 下的變化率  可以發現當 $x = 0.5$ 時, $\pi(x)$ 的變化率最大 ### SAS code ``` sas= proc logistic data = <data>; weight <count>; model <response> = <explanetory>; run; ``` ### SAS intepretation 以下考慮horseshoe crab data,可以發現隨著width的大小越大,有satellite的horseshoe crab的比例越高      1. $\beta_0$跟$\beta_1$的估計值 2. 卡方值的p-value 3. $x$ 每增加一,有satellite 的勝算會變為1.603倍,即 $e^{\beta_1}$。也就是: $x + 1$ 的勝算是 $x$ 的1.603倍 4. 檢定 $H_0: \beta_1 = \beta_2 = ... = \beta_n = 0$,當模型配適合適時,這三個的結果應該要一致 ## Logistic with $z$ 考慮以下資料  ### SAS code ```sas= proc logistic data = data desc; model symptoms = AZT_use race; run; ``` ### SAS intepretation SAS結果的intepretation方式都跟前面一樣 但這邊由於是2x2x2 data,特別再解釋一次odds ratio  由此可知odds ratio for AZT_use = 0.192, odds ratio for race = 3.635 有用AZT的人,其有symptoms的勝算為沒用AZT的人的0.192倍 race是black的人,其有symptoms的勝算是white的人的3.635倍 如果$z$不是一個confounder,那這麼做是ok的,但如果$z$是confounder,則必須考慮interaction。而如何確認 $z$ 是不是confounder,可從 CMH test或logistic中interaction項的假設檢定得知 ### SAS code(confounder) ```sas= proc logistic data = data desc; model symptoms = AZT_use race AZT_use * race; run; ``` ### SAS intepretation(confounder)  可以看到interaction項的p-value = 0.0061 < 0.05,拒絕$H_0$ 因此有足夠的證據認為Race會影響到AZT use跟symptoms的關係 ## Logistic with nominal data 在 nominal data 裡需要設置參考組別,有兩種設定方式,一種叫 reference,即把參考組別的係數設為0。另一種叫 effect,即把參考組別的係數設為-1。 以下考慮horseshoe crab data 中有 satellite 的 probability跟color的關係,color(2, 3, 4, 5)數字越大代表顏色越深。 <i>effect for color = 2 and 5</i> <font size = 5.5> color = 2 :::success $logit(\pi(x)) = \beta_0 + \beta_1$ ::: color = 5 :::success $logit(\pi(x)) = \beta_0 -\beta_1 - \beta_2 - \beta_3$ ::: </font> <i>reference (以5當作reference) for color = 2 and 5</i> <font size = 5.5> color = 2 :::success $logit(\pi(x)) = \beta_0 + \beta_1$ ::: color = 5 :::success $logit(\pi(x)) = \beta_0$ ::: </font> ### SAS code <i>effect</i> ```sas= proc logistic data = r desc; class color; model y = color; run; ``` <i>reference(color = 5)</i> ```sas= proc logistic data = r desc; class color / param = ref; model y = color; run; ``` <i>reference first</i> ```sas= proc logistic data = r desc; class color (ref = first) / param = ref; model y = color; run; ``` ### SAS intepretation <i>effect</i>    <i>reference(color = 5)</i>    1. 參數的係數 2. 各 $\beta$ 的估計結果 3. 各個level相對於reference的odds ratio 可以發現 $\beta$ 會隨參考的方式設置而改變,就連檢定結果也會不同,驗證了前面說的 $\beta$ 是不可估的。所以拿odds ratio來做推論是最適當的,因為他不隨 reference 的設置而改變。而odds ratio的計算拿 effect 的 3, 5來說,便為 :::success <font size = 5.5> $\theta = \frac{\Omega(3)}{\Omega(5)} = \frac{e^{\beta_0 + \beta_2}}{e^{\beta_0 - \beta_1 - \beta_2 - \beta_3}}$ $\hat{\theta} = e^{\hat{\beta_1} + 2\hat{\beta_2} + \hat{\beta_3} } = e^{0.6786 + 1.112 - 0.0523} = 5.687$ </font> ::: ## Predictive power of logistic 由於 logistic 的結果為 $0 < \pi(x) < 1$,但實際想要的 $y$ 值是 0,1 ,所以需要設定一個 threshold $\pi_0$。當 $\pi(x) > \pi_0$ 則 $y = 1$, 否則 $y = 0$ | Real/Predict | 0 | 1 | | ------------ | --- | --- | | 0 | A | B | | 1 | C | D | <b>Sensitivity:</b> $P[Predict = 1| Real = 1]$ <b>Specificity:</b> $P[Predict = 0| Real = 0]$ 藉由 Receiver Operating Characteristics (ROC) curve,可以了解這個logistic的predictive power如何 其$x$軸為 1 - Specificity, $y$軸為 Sensitivity。其原理為窮舉 $\pi_0$,算出每組的sensitivity跟1 - specificity之後選最適當的。  而 1 - Specificity其實為 $P[Predict = 1 | Real = 0]$ 所以斜線的部分為 $P[Predict = 1 | Real = 0] = P[Predict = 1 | Real = 1]$,也就是說不管真實值是多少,預測他是0或1的機率都是一樣的,代表這個模型有跟沒有一樣。而最左上方的點代表Sensitivity = Specificity = 1,代表說全部答對。 簡的來說,curve越接近斜線,代表predictive power越差,所以會用曲線下的面積來代表這個logistic的predictive power。 ### SAS code 以前面crab data示範 ```sas= proc logistic data = r desc plots = (ROC); class color / param = ref; model y = color / ctable; run; ``` ### SAS intepretation  其中的 $c$ 代表的即是 ROC curve 下的面積  1. 分類正確且是發生event的,即是上面表格的D 2. 分類正確且是non event的,即A 3. 錯誤的分類到event裡,即C 4. 錯誤的分類到non event裡,即B 5. 正確率 6. False positive $P[Real = 0 | Predict = 1]$ 7. False negative $P[Real = 1 | Predict = 0]$ 當 $\pi_0 \approx p$ 時,sensitivity跟specificity會接近
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up