天天看點

logistic回歸分析 r_Logistic回歸模型與R

logistic回歸分析 r_Logistic回歸模型與R
1.分組資料的Logistic回歸

例1: 在一次住房展銷會上,與房地産商簽訂初步購房意向書的共有名

logistic回歸分析 r_Logistic回歸模型與R

顧客,在随後的3個月的時間内,隻有一部分顧客确實購買了房屋.購買了房屋的顧客記為1,沒有購買房屋的顧客記為0,以顧客的年家庭收入為自變量x,對下面表所示的資料,

x n m p y

1 1.5 25 8 0.32 -0.753771802

2 2.5 32 13 0.40625 -0.379489622

3 3.5 58 26 0.448275862 -0.207639365

4 4.5 52 22 0.423076923 -0.310154928

5 5.5 43 20 0.465116279 -0.139761942

6 6.5 39 22 0.564102564 0.257829109

7 7.5 28 16 0.571428571 0.287682072

8 8.5 21 12 0.571428571 0.287682072

9 9.5 15 10 0.666666667 0.693147181

其中,x為年家庭收入(萬元),n為簽訂意向書人數,m為實際購房人數,

logistic回歸分析 r_Logistic回歸模型與R

logistic回歸分析 r_Logistic回歸模型與R

. 試建立Logistic回歸模型.

(1) Logistic回歸模型
logistic回歸分析 r_Logistic回歸模型與R

作Logistic 變換可得

logistic回歸分析 r_Logistic回歸模型與R

.令

logistic回歸分析 r_Logistic回歸模型與R

,

logistic回歸分析 r_Logistic回歸模型與R

logistic回歸分析 r_Logistic回歸模型與R

條件下

logistic回歸分析 r_Logistic回歸模型與R

的頻率, 那麼

logistic回歸分析 r_Logistic回歸模型與R

logistic回歸分析 r_Logistic回歸模型與R

的估計。令

logistic回歸分析 r_Logistic回歸模型與R

,則

logistic回歸分析 r_Logistic回歸模型與R

logistic回歸分析 r_Logistic回歸模型與R

估計。令

logistic回歸分析 r_Logistic回歸模型與R

為估計誤差,這樣便有

logistic回歸分析 r_Logistic回歸模型與R
(2)
logistic回歸分析 r_Logistic回歸模型與R
的估計

R實作

x<-c(1.5, 2.5, 3.5, 4.5, 5.5, 6.5,  7.5, 8.5, 9.5)
n<-c(25, 32, 58, 52, 43,  39, 28, 21, 15)
m<-c(8 , 13,  26, 22, 20, 22,  16,   12,  10 )        
p<-m/n
logit<-log(p/(1-p))
summary(lm(logit~x))
           

--------------------------

R 實作結果

Call:

lm(formula = logit ~ x)

Residuals:

Min 1Q Median 3Q Max

-0.150322 -0.110376 0.005474 0.117288 0.133341

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) -0.88627 0.10242 -8.653 5.50e-05 ***

x 0.15580 0.01686 9.242 3.59e-05 ***

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1306 on 7 degrees of freedom

Multiple R-squared: 0.9243, Adjusted R-squared: 0.9134

F-statistic: 85.42 on 1 and 7 DF, p-value: 3.588e-05

----------------------

結果解釋:
logistic回歸分析 r_Logistic回歸模型與R

.

Logistic回歸模型為
logistic回歸分析 r_Logistic回歸模型與R

利用該模型可以對購房機率進行預測。例如當

logistic回歸分析 r_Logistic回歸模型與R
logistic回歸分析 r_Logistic回歸模型與R

=0.59

進而可知年收入8萬元的家庭預計實際購房比例為59%。

2.未

組資料的Logistic回歸

在一次關于在一次關于公共交通的社會調查中,一個調查項目是“是乘坐公共汽車上下班,還是騎自行車上下班”。因變量y=1表示要乘坐公共汽車上下班,y=0表示要乘自行車上下班。自變量x1是年齡,作為連續型變量;x2是月收入;x3是性别,x3=1表示男性,x3=0表示女性。調查對象為工薪族群體,資料如下表。試建立y與自變量間的Logistic回歸。

num x3 x1 x2 y num x 3 x1 x3 y

1 0 18 850 0 15 1 20 1000 0

2 0 21 1200 0 16 1 25 1200 0

3 0 23 850 1 17 1 27 1300 0

4 0 23 950 1 18 1 28 1500 0

5 0 28 1200 1 19 1 30 950 1

6 0 31 850 0 20 1 32 1000 0

7 0 36 1500 1 21 1 33 1800 0

8 0 42 1000 1 22 1 33 1000 0

9 0 46 950 1 23 1 38 1200 0

10 0 48 1200 0 24 1 41 1500 0

11 0 55 1800 1 25 1 45 1800 1

12 0 56 2100 1 26 1 48 1000 0

13 0 58 1800 1 27 1 52 1500 1

14 1 18 850 0 28 1 56 1800 1

(1) Logistic 回歸模型

因變量

logistic回歸分析 r_Logistic回歸模型與R

為0-1型變量,在

logistic回歸分析 r_Logistic回歸模型與R

已知的條件下,y服從貝努力分布。令

logistic回歸分析 r_Logistic回歸模型與R

,建立Logistic 回歸模型

logistic回歸分析 r_Logistic回歸模型與R

顯然

logistic回歸分析 r_Logistic回歸模型與R

的機率分布為

logistic回歸分析 r_Logistic回歸模型與R

.

(2)參數

logistic回歸分析 r_Logistic回歸模型與R

的極大似然估計

logistic回歸分析 r_Logistic回歸模型與R

的似然函數為

logistic回歸分析 r_Logistic回歸模型與R
logistic回歸分析 r_Logistic回歸模型與R

其中

logistic回歸分析 r_Logistic回歸模型與R
logistic回歸分析 r_Logistic回歸模型與R
logistic回歸分析 r_Logistic回歸模型與R
logistic回歸分析 r_Logistic回歸模型與R

.

(3)R實作

>datax<-read.table("clipboard",header=T)

#資料讀入到data3.1中

>glm.logit<-glm(y~x1+x2+x3,family=binomial,data=datax)

#建立y關于x的logistic回歸模型,資料為data3.1

>summary(glm.logit)

#模型彙總,給出模型回歸系數的估計和顯著性檢驗等

結果顯示:

------------------------

Call:

glm(formula

= y ~ x1 + x2 + x3, family = binomial, data = data3.1)

Deviance

Residuals:

Min 1Q Median 3Q Max

-2.1090 -0.7486 -0.2850 0.7011 2.1683

Coefficients:

Estimate Std. Error z value Pr(>|z|)

(Intercept) -3.655016 2.091218 -1.748 0.0805 .

x1 0.082168 0.052119 1.577 0.1149

x2 0.001517 0.001865 0.813 0.4160

x3 -2.501844 1.157815 -2.161 0.0307 *

Signif.

codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 38.673 on 27 degrees of freedom

Residual

deviance: 25.971 on 24 degrees of freedom

AIC:

33.971

Number

of Fisher Scoring iterations: 5

-------------

結果解釋與分析:

輸出結果中x2最不顯著,決定将其剔除。用y對性别和年齡兩個自變量作回歸,R程式和輸出的結果分别為:

>glm.logit<-glm(y~x1+x3,family=binomial,data=datax)

>summary(glm.logit)

------------

Coefficients:

Estimate Std. Error z value

Pr(>|z|)

(Intercept) -2.6285 1.5537 -1.692 0.0907 .

x1 0.1023 0.0458 2.233 0.0256 *

x3 -2.2239 1.0476 -2.123 0.0338 *

---

Signif.

codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05‘.’ 0.1 ‘ ’ 1

------------

結果分析與解釋:

可以看出,x3,x1都是顯著的。Logistic回歸方程為

logistic回歸分析 r_Logistic回歸模型與R

繼續閱讀