→ clansoda: 所以是probability 僅有0 1兩值才會出現這個warning事吧 02/13 00:08
fitted probability出現0 1才會有warning
※ 編輯: celestialgod (36.233.49.208), 02/13/2017 00:12:30
→ clansoda: 就我的理解就是在regression的時候 fit的值都大於正負二 02/13 10:34
→ clansoda: 才會在sigmoid後 所有的值都趨近於1或0對嗎 02/13 10:35
→ markbaseball: 那該如何解決完美切割的問題呢? 02/13 19:10
推 maoc: 第一個例子會造成理論上的MLE是無窮大,所以程式跑出來的答 02/14 00:34
→ maoc: 案並不收歛,做出來的模型並不可用,warning 就在提醒這件 02/14 00:34
→ maoc: 事的發生。 02/14 00:34
→ celestialgod: 感謝樓上指正,小弟沒有指出要點 02/14 02:31
> -------------------------------------------------------------------------- <
作者: celestialgod (天) 看板: R_Language
標題: Re: [問題] Logistic Regression
時間: Sun Feb 12 23:45:34 2017
※ 引述《markbaseball (Mark)》之銘言:
:
:
: [問題類型]:
:
: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
:
: [軟體熟悉度]:
: 請把以下不需要的部份刪除
: 入門(寫過其他程式,只是對語法不熟悉)
: [問題敘述]:
: 各位版上的大大們,我參照網路的寫法嘗試跑羅吉斯迴歸,
: model.3<-glm(Overpayment~rs+ni+asset+fcf+ocf,family=binomial(logit), data
: =mydata)
: 但是卻出現錯誤訊息
: Warning message:
: glm.fit: fitted probabilities numerically 0 or 1 occurred
: 依照我的理解,程式是跟我說我的Overpayment一定要是0或1才可以。
: 可是,我很確定我的overpayment變數只有0和1兩種結果而已啊!!
: 請問各位大大是否可以跟我說我錯在哪裡嗎? 感激
:
: 選擇性,也許未來有用
:
舉個例子
set.seed(53)
N <- 100
p <- 3
x <- matrix(rnorm(N * p), N)
beta <- rnorm(p + 1)
logit <- function(x) 1 / (1 + exp(-x))
y <- logit(cbind(1, x) %*% beta) > 0.5
# y這種生成方法保證用現在的x可完美切開0, 1
# 意思是y=1算出來的機率一定是1,y=0算出來的機率一定是0
# 不懂再往下看
glmFit <- glm(y ~ x, family = binomial(logit),
control = glm.control(maxit = 100))
# Warning message:
# glm.fit: fitted probabilities numerically 0 or 1 occurred
table(round(fitted(glmFit), 6)) # fitted這個函數會得到fitted機率
# 0 1
# 94 6
可以看到機率不是0就是1
正常情況下應該是下面這樣:
y2 <- rbinom(N, 1, logit(cbind(1, x) %*% beta))
glmFit2 <- glm(y2 ~ x, family = binomial(logit),
control = glm.control(maxit = 100))
table(cut(fitted(glmFit2), seq(0, 1, by = 0.1)))
# (0,0.1] (0.1,0.2] (0.2,0.3] (0.3,0.4] (0.4,0.5] (0.5,0.6] (0.6,0.7]
# 21 30 19 15 6 4 4
# (0.7,0.8] (0.8,0.9] (0.9,1]
# 1 0 0
也就是不會直接切開0,1
不過這個只是警告,告知user說
你的資料可能有問題,才會這樣直接切開
(通常是output(response)跟input(covariates)高度相關)
Note: 要增加iteration是因為 default maxit = 25,會導致不收斂
--
R資料整理套件系列文:
magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9
data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue
dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b
tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz
pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.233.49.208
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1486914338.A.015.html
→ clansoda: 所以是probability 僅有0 1兩值才會出現這個warning事吧 02/13 00:08
fitted probability出現0 1才會有warning
※ 編輯: celestialgod (36.233.49.208), 02/13/2017 00:12:30
→ clansoda: 就我的理解就是在regression的時候 fit的值都大於正負二 02/13 10:34
→ clansoda: 才會在sigmoid後 所有的值都趨近於1或0對嗎 02/13 10:35
→ markbaseball: 那該如何解決完美切割的問題呢? 02/13 19:10
推 maoc: 第一個例子會造成理論上的MLE是無窮大,所以程式跑出來的答 02/14 00:34
→ maoc: 案並不收歛,做出來的模型並不可用,warning 就在提醒這件 02/14 00:34
→ maoc: 事的發生。 02/14 00:34
→ celestialgod: 感謝樓上指正,小弟沒有指出要點 02/14 02:31