判别分析及R使用Part1-Fisher判别法

2022-12-19  本文已影响0人  3between7

本章为MOOC课程《多元统计分析及R语言建模》课程第六章,前5章笔记均已发布过,想看的可以去我的主页巴拉巴拉。

6.1 判别分析的概念

6.2 线性判别分析

线性判别分析其实就是Y为分类变量(但不是0,1这种,是1,2这种)时的线性回归分析,如果我们能求解出如下函数,那么就可以用此函数判别Y的类型了:
Y = \alpha_1X_1 + \alpha_2X_2 + ... + \alpha_pX_p = \alpha'X

一、求Fisher线性判别函数

两总体.png
根据以往学过的知识我们知道,若想求两个总体G_1G_2(当然是满足正态分布的两个总体)是否有差异,可以使用t检验来做:
t = \frac{|\overline Y_1 - \overline Y_2 |}{S_p}
t检验是要判断两总体是否有差异,而线性判别分析是想让两总体有差异,因此也是可以用t检验的思想去处理。上述公式中的t其实就是Fisher检验中的\lambda,t检验是求t的值,而Fisher线性判别函数是使得t也就是\lambda最大,因为上述公式无法求导,所以需要平方一下:
\lambda = \frac{(\overline Y_1 - \overline Y_2 )^2}{S_p^2}
这里有个定理:线性组合Y=\alpha'X=(\overline X_1 - \overline X_2 )'S^{-1}_pX对所有的线性系数向量\alpha',使\lambda达到最大。

二、计算判别界值

简单的情况,求判别界值将求到的线性系数\alpha'与自变量代入线性判别函数求出Y_0即可:
\overline Y_1 = \alpha'\overline X_1 \\ \overline Y_2 = \alpha'\overline X_2

Y_0 =\frac{ \overline Y_1+\overline Y_2}{2}

三、建立判别标准

四、举例说明

表6.2.png
今天和昨天湿温差x_1及气温差x_2是预报明天下雨否的其中两个重要因子,试建立Fisher线性判别函数。如测得今天 x_1=8.1, x_2=2.0,试报明天是雨天还是晴天?
分析前先用t检验看下数据的情况:
>d6.1 <- read.xlsx("/home/my/桌面/MOOC/多元统计分析/mvstats5.xlsx",sheet="d6.1")
>boxplot(x1~G,d6.1)
>boxplot(x2~G,d6.1)
boxplot1.png
boxplot2.png

大致可以看出用x1无法很好的区分两组,而两组的x2分布的差异还是比较明显的,再用t检验看看:

>t.test(x1~G,d6.1)
    Welch Two Sample t-test

data:  x1 by G
t = 0.59897, df = 11.671, p-value = 0.5606
alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
95 percent confidence interval:
 -3.443696  6.043696
sample estimates:
mean in group 1 mean in group 2 
           0.92           -0.38 
>t.test(x2~G,d6.1)
    Welch Two Sample t-test

data:  x2 by G
t = -3.2506, df = 17.655, p-value = 0.004527
alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
95 percent confidence interval:
 -11.118792  -2.381208
sample estimates:
mean in group 1 mean in group 2 
           2.10            8.85 

可看出两组的x2有显著差异。因为因变量只有两类,我们其实可以用Logistic模型分析这个问题,只需要我们减个1即可:

>summary(glm(G-1~x1+x2,family=binomial,d6.1))
Call:
glm(formula = G - 1 ~ x1 + x2, family = binomial, data = d6.1)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-1.81637  -0.63629   0.04472   0.54520   2.13957  

Coefficients:
            Estimate Std. Error z value Pr(>|z|)  
(Intercept)  -2.0761     1.1082  -1.873   0.0610 .
x1           -0.1957     0.1457  -1.344   0.1791  
x2            0.3813     0.1681   2.269   0.0233 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 27.726  on 19  degrees of freedom
Residual deviance: 17.036  on 17  degrees of freedom
AIC: 23.036

Number of Fisher Scoring iterations: 5

结果和之前的t检验一样,x2的系数是显著的。若使用线性判别分析,就不用减1了:

>attach(d6.1)
>library(MASS)
>ld=lda(G~x1+x2)
>ld
Call:
lda(G ~ x1 + x2)

Prior probabilities of groups:
  1   2 
0.5 0.5 

Group means:
     x1   x2
1  0.92 2.10
2 -0.38 8.85

Coefficients of linear discriminants:
          LD1
x1 -0.1035305
x2  0.2247957

若想用求得的函数预测则使用predict函数:

>lp<- predict(ld)
>G1<-lp$class
>tab1<-table(G,G1)
   G1
G   1 2
  1 9 1
  2 1 9

可发现正确率为9+9/20=0.9

上一篇 下一篇

猜你喜欢

热点阅读