美文网首页
R语言统计4:正态性检验及t检验

R语言统计4:正态性检验及t检验

作者: 小程的学习笔记 | 来源:发表于2023-04-04 09:20 被阅读0次

    正态性检验:正态性检验主要用于判断连续性变量是否服从或近似服从正态分布,属于非参数检验。原假设为“样本来自的总体与正态分布无显著性差异”,只有P>0.05才能接受原假设,及数据符合正态分布。

    1.1 S-W检验

    夏皮洛-威尔克检验(Shapiro—Wilk test),简称S-W检验,用来检验是否数据符合正态分布,类似于线性回归的方法一样,是检验其于回归曲线的残差。该方法作者推荐在样本量很小的时候使用,比如N < 20 (现常用于N < 50)

    \color{green}{shapiro.test}

    x<-c(148 ,154, 158, 160, 161, 162, 166, 170, 182, 195, 236)
    shapiro.test(x)
    ##     Shapiro-Wilk normality test
    ## 
    ## data:  x
    ## W = 0.78881, p-value = 0.006704
    

    ★ W越小,越接近0,表示样本数据越接近正态分布
    ★ p小于显著性水平α(0.05),不能否定原假设,表示样本数据不符合正态分布

    fBasics包\color{green}{shapiroTest}

    # install.packages("fBasics")
    library(fBasics)
    shapiroTest(x)
    ## 
    ## Title:
    ##  Shapiro - Wilk Normality Test
    ## 
    ## Test Results:
    ##   STATISTIC:
    ##     W: 0.7888
    ##   P VALUE:
    ##     0.006704
    

    ★ 结果和上述一致

    1.2 K-S检验

    柯尔莫戈洛夫-斯米诺夫检验(Kolmogorov-Smirnov test),简称K-S检验,是一种基于累计分布函数的非参数检验,用以检验两个经验分布是否不同或一个经验分布与另一个理想分布是否不同。

    stats包(R自带)\color{green}{ks.test}

    ks.test(x,"pnorm")
    ## 
    ##  One-sample Kolmogorov-Smirnov test
    ## 
    ## data:  x
    ## D = 1, p-value < 2.2e-16
    ## alternative hypothesis: two-sided
    

    ★ D值越小,越接近0,表示样本数据越接近正态分布
    ★ p小于显著性水平α(0.05),表示样本数据不符合正态分布

    ks.test(scale(x),"pnorm") #scale标准化后再检验
    ## 
    ##  One-sample Kolmogorov-Smirnov test
    ## 
    ## data:  scale(x)
    ## D = 0.25922, p-value = 0.3838
    ## alternative hypothesis: two-sided
    

    fBasics包\color{green}{ksnormTest}

    library(fBasics)
    ksnormTest(x)
    ## 
    ## Title:
    ##  One-sample Kolmogorov-Smirnov test
    ## 
    ## Test Results:
    ##   STATISTIC:
    ##     D: 1
    ##   P VALUE:
    ##     Alternative Two-Sided: < 2.2e-16 
    ##     Alternative      Less: < 2.2e-16 
    ##     Alternative   Greater: 1 
    
    ksnormTest(scale(x))
    ## 
    ## Title:
    ##  One-sample Kolmogorov-Smirnov test
    ## 
    ## Test Results:
    ##   STATISTIC:
    ##     D: 0.2592
    ##   P VALUE:
    ##     Alternative Two-Sided: 0.3838 
    ##     Alternative      Less: 0.4839 
    ##     Alternative   Greater: 0.1929 
    

    ★ 结果和上述一致

    1.3 Lilliefor检验

    是Kolmogorov-Smirnov正态性检验修正,可用于正态性检验
    nortest包\color{green}{lillie.test}

    # install.packages("nortest")
    library(nortest)
    lillie.test(x)
    ## 
    ##  Lilliefors (Kolmogorov-Smirnov) normality test
    ## 
    ## data:  x
    ## D = 0.25922, p-value = 0.03741
    

    ★ 和数据进行scale标准化后结果一致
    fBasics包\color{green}{lillieTest}

    lillieTest(x)
    ## 
    ## Title:
    ##  Lilliefors (KS) Normality Test
    ## 
    ## Test Results:
    ##   STATISTIC:
    ##     D: 0.2592
    ##   P VALUE:
    ##     0.03741 
    

    ★ 结果同lillie.test

    1.4 Anderson-Darling正态性检验

    是一种基于 ECDF(经验累积分布函数)的检验

    nortest包\color{green}{ad.test}

    ad.test(x)
    ## 
    ##  Anderson-Darling normality test
    ## 
    ## data:  x
    ## A = 0.94677, p-value = 0.01045
    

    ★ A值越小,越接近0,表示样本数据越接近正态分布
    ★ p-value小于显著性水平α(0.05),则拒绝假设, 表示样本数据不符合正态分布

    t检验:t检验是假设检验的一种,又叫student t检验(Student’s t test),主要用于样本含量较小(例如n<30),总体标准差σ未知的正态分布资料。t检验用于检验两个总体的均值差异是否显著。原假设为“两组总体均值相等,无显著性差异”,只有P>0.05才能接受原假设,及两个总体的均值无显著性差异。

    ★ t检验只能比较两组之间的差异(多于两组,可使用方差分析)

    2.1 单样本t检验

    前提条件:

    1. 随机样本
    2. 样本符合正态分布
    3. 方差同质
    # 加载R里内置的鸢尾花数据集(iris)
    setosa <- iris[which(iris$Species=='setosa'),] # 提取setosa类的鸢尾花
    versicolor <- iris[which(iris$Species=='versicolor'),] # 提取versicolor类的鸢尾花
    mydata <- rbind(setosa, versicolor) # 按行合并数据集
    
    t.test(versicolor$Sepal.Length, mu = 5.5) # 原假设H0: mu=5.5(mu就是指总体的均值)
    ## 
    ##  One Sample t-test
    ## 
    ## data:  versicolor$Sepal.Length
    ## t = 6.3701, df = 49, p-value = 6.277e-08
    ## alternative hypothesis: true mean is not equal to 5.471
    ## 95 percent confidence interval:
    ##  5.789306 6.082694
    ## sample estimates:
    ## mean of x 
    ##     5.936
    

    ★ 以0.05水平为标准,结果为p<0.05,具体统计学意义,表明versicolor类鸢尾花的花萼长与总体间存在显著差异

    2.2 两个独立样本t检验

    根据样本数据对两个样本来自的两个独立总体的均值是否有显著差异进行判断

    前提条件:1) 两个样本应该是相互独立的
    \ \ \ \ \ \ \2) 样本来自的两个总体应该服从正态分布

    # t.test(setosa$Sepal.Length, versicolor$Sepal.Length) # 检验不同鸢尾花花萼长度差异
    t.test(mydata$Sepal.Length~mydata$Species) # 检验不同鸢尾花花萼长度差异
    ## 
    ##  Welch Two Sample t-test
    ## 
    ## data:  mydata$Sepal.Length by mydata$Species
    ## t = -10.521, df = 86.538, p-value < 2.2e-16
    ## alternative hypothesis: true difference in means between group setosa and group versicolor is not equal to 0
    ## 95 percent confidence interval:
    ##  -1.1057074 -0.7542926
    ## sample estimates:
    ##     mean in group setosa mean in group versicolor 
    ##                    5.006                    5.936
    
    t.test(mydata$Sepal.Length~mydata$Species,
           var.equal=TRUE # 指定样本之间是等方差
           ) #检验不同鸢尾花花萼长度差异
    ## 
    ##  Two Sample t-test
    ## 
    ## data:  mydata$Sepal.Length by mydata$Species
    ## t = -10.521, df = 98, p-value < 2.2e-16
    ## alternative hypothesis: true difference in means between group setosa and group versicolor is not equal to 0
    ## 95 percent confidence interval:
    ##  -1.1054165 -0.7545835
    ## sample estimates:
    ##     mean in group setosa mean in group versicolor 
    ##                    5.006                    5.936
    

    ★ 两种不同的鸢尾花(setosa和versicolor)在花萼长度上有显著的差异(p-value < 2.2e-16)
    ★ setosa的花萼比较短(mean=5.006)
    ★ 指定样本之间等方差厚的自由度df=98,和之前的自由度df=86.538不同,因为Welch主要通过调整自由度来矫正异方差性

    2.3 配对t检验

    配对设计主要有4种情况:1) 同一受试对象处理前后的数据
    \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 2) 同一受试对象两个部位的数据
    \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 3) 同一样品用两种方法检验的结果
    \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 4) 配对两个受试对象分别接受两种处理后的数据

    前提条件:1) 两样本必须是配对的.一是两样本的观察值数目相同,二是样本的观察值的顺序不随意更改
    \ \ \ \ \ \ \2) 样本来自的两个总体必须服从正态分布

    t.test(setosa$Sepal.Length, setosa$Sepal.Width, paired=TRUE)
    ## 
    ##  Paired t-test
    ## 
    ## data:  setosa$Sepal.Length and setosa$Sepal.Width
    ## t = 42.323, df = 49, p-value < 2.2e-16
    ## alternative hypothesis: true difference in means is not equal to 0
    ## 95 percent confidence interval:
    ##  1.503074 1.652926
    ## sample estimates:
    ## mean of the differences 
    ##                   1.578
    

    ★ p-value < 2.2e-16,拒绝原假设,setosa类鸢尾花花萼长度和宽度之间存在显著差异

    参考:

    1. https://zhuanlan.zhihu.com/p/592521502

    相关文章

      网友评论

          本文标题:R语言统计4:正态性检验及t检验

          本文链接:https://www.haomeiwen.com/subject/uierddtx.html