R语言与概率统计(三) 多元统计分析(下)广义线性回归

2020-12-13 06:03

阅读:359

标签:packages   step   解释   isp   src   ring   install   mil   nts   

广义线性回归

技术图片

技术图片

技术图片

技术图片

技术图片

> life glm.sol summary(glm.sol)

Call:
glm(formula = Y ~ X1 + X2 + X3, family = binomial, data = life)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.6960  -0.5842  -0.2828   0.7436   1.9292  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)    
(Intercept) -1.696538   0.658635  -2.576 0.010000 ** 
X1           0.002326   0.005683   0.409 0.682308    
X2          -0.792177   0.487262  -1.626 0.103998    
X3           2.830373   0.793406   3.567 0.000361 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 67.301  on 49  degrees of freedom
Residual deviance: 46.567  on 46  degrees of freedom
AIC: 54.567

Number of Fisher Scoring iterations: 5

技术图片技术图片

技术图片技术图片

可见拟合的效果不好

> pre p 
> pre p 
> step(glm.sol)
Start:  AIC=54.57
Y ~ X1 + X2 + X3

       Df Deviance    AIC
- X1    1   46.718 52.718
      46.567 54.567
- X2    1   49.502 55.502
- X3    1   63.475 69.475

Step:  AIC=52.72
Y ~ X2 + X3

       Df Deviance    AIC
      46.718 52.718
- X2    1   49.690 53.690
- X3    1   63.504 67.504

Call:  glm(formula = Y ~ X2 + X3, family = binomial, data = life)

Coefficients:
(Intercept)           X2           X3  
     -1.642       -0.707        2.784  

Degrees of Freedom: 49 Total (i.e. Null);  47 Residual
Null Deviance:	    67.3 
Residual Deviance: 46.72 	AIC: 52.72

 

> glm.new summary(glm.new)

Call:
glm(formula = Y ~ X2 + X3, family = binomial, data = life)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.6849  -0.5949  -0.3033   0.7442   1.9073  

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  -1.6419     0.6381  -2.573 0.010082 *  
X2           -0.7070     0.4282  -1.651 0.098750 .  
X3            2.7844     0.7797   3.571 0.000355 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 67.301  on 49  degrees of freedom
Residual deviance: 46.718  on 47  degrees of freedom
AIC: 52.718

Number of Fisher Scoring iterations: 5

> 
> pre p 
> pre p

 

#####再来看一个类似的问题
install.packages(AER)
data(Affairs,package=AER)#婚外情数据,包括9个变量,婚外斯通频率,性别,婚龄等。
summary(Affairs)
table(Affairs$affairs)
#我们感兴趣的是是否有过婚外情所以做如下处理
Affairs$ynaffair[Affairs$affairs>0]
Affairs$ynaffair[Affairs$affairs==0]0
Affairs$ynaffair‘NO,YES))
table(Affairs$ynaffair)
#接下来做逻辑回归
fit.full=glm(ynaffair~.-affairs,data=Affairs,family=binomial())
summary(fit.full)
#除掉较大p值所对应的变量,如性别,是否有孩子、学历和职业在做一次分析
fit.reduced=glm(ynaffair~age+yearsmarried+religiousness+rating,data=Affairs,family=binomial())
summary(fit.reduced)

AIC(fit.full,fit.reduced)#模型比较

#系数解释
exp(coef(fit.reduced))

 

R语言与概率统计(三) 多元统计分析(下)广义线性回归

标签:packages   step   解释   isp   src   ring   install   mil   nts   

原文地址:https://www.cnblogs.com/caiyishuai/p/11164543.html


评论


亲,登录后才可以留言!