美文网首页散文想法简友广场
R语言单因素、多因素方差分析ANOVA analysis of

R语言单因素、多因素方差分析ANOVA analysis of

作者: Cache_wood | 来源:发表于2021-11-30 15:56 被阅读0次

    @[toc]

    假设检验的前提是要满足正态分布和方差齐性


    组内平方和SSE:同一组内的数据误差平方和
    组间平方和SSA:不同组之间的数据误差平方和

    F = MSA/MSE = \frac{SSA/k-1}{SSE/n-k}

    单因素方差分析

    独立变量的单因素方差分析

    一个分类型自变量
    例如四个班级学生的语文成绩,班级是分类型自变量,四个班级是自变量的四个水平

    data <- read.csv(file = file.choose(),header = TRUE)
    attach(data)
    
    results <- aov(scores~classes) ##analysis of variance
    
    summary(results)
    
    TukeyHSD(results) #post hoc multiple comparisons by Tukey Honest Significant Difference
    plot(TukeyHSD(results))
    

    测试班级对成绩的影响

                 Df Sum Sq Mean Sq F value
    classes       3   4412  1470.6   16.05
    Residuals   116  10631    91.6        
                  Pr(>F)    
    classes     8.62e-09 ***
    Residuals               
    ---
    Signif. codes:  
    0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    

    因为p<0.001,说明班级对成绩的影响非常显著

    Tukey multiple comparisons of means
        95% family-wise confidence level
    
    Fit: aov(formula = scores ~ classes)
    
    $classes
              diff        lwr        upr     p adj
    B-A   7.466667   1.023566  13.909767 0.0161736
    C-A  -1.033333  -7.476434   5.409767 0.9752930
    D-A  -9.633333 -16.076434  -3.190233 0.0009282
    C-B  -8.500000 -14.943101  -2.056899 0.0044571
    D-B -17.100000 -23.543101 -10.656899 0.0000000
    D-C  -8.600000 -15.043101  -2.156899 0.0039054
    

    图中跨越0分界线的班级对,有较大可能落在0上,也就是说两个班级之间没有明显差异。其他班级说明都有明显差异。


    重复次数的单因素方差分析

    同一班级在大学三年的三次测试

    $ANOVA
           Effect DFn DFd        SSn       SSd
    1 (Intercept)   1  29 563429.344 6672.9889
    2       tests   2  58   2149.956  992.7111
              F            p p<.05       ges
    1 2448.5956 1.447834e-29     * 0.9865772
    2   62.8065 3.063461e-15     * 0.2190333
    
    $`Mauchly's Test for Sphericity`
      Effect         W         p p<.05
    2  tests 0.8901137 0.1959912      
    
    $`Sphericity Corrections`
      Effect       GGe        p[GG] p[GG]<.05
    2  tests 0.9009932 5.878093e-14         *
            HFe        p[HF] p[HF]<.05
    2 0.9570476 1.103178e-14         *
    

    p<0.001,说明学生成绩在大学三年中有显著差异。球形检验的p-value大于0.05,所以可以认为方差相等。

    Mauchly's Test for Sphericity:适用于重复测量时检验不同测量之间的差值的方差是否相等,用于三次以及三次之上。

    Sphericity Corrections:球形矫正,当方差不相等时进行矫正,矫正方法有the Greenhouse-Geisser (1959), the Huynh-Feldt (1976), 简称GG和HF。

    双因素方差分析

    独立变量的双因素方差分析

    两个分类型自变量
    例如探究词汇量话题熟悉度对学生作文成绩的影响

    data <- read.csv(file = file.choose(),header = TRUE)
    attach(data)
    
    model <- lm(scores~vocabulary+familiarity+vocabulary:familiarity)
    
    # or model <- lm(scores~vocabulary*familiarity)
    
    summary(model)
    
    results <- aov(model)
    summary(results)
    
                           Df Sum Sq Mean Sq
    vocabulary              2   3758  1878.9
    familiarity             1   1203  1203.3
    vocabulary:familiarity  2     84    42.1
    Residuals              24    434    18.1
                           F value   Pr(>F)    
    vocabulary              103.90 1.52e-12 ***
    familiarity              66.54 2.23e-08 ***
    vocabulary:familiarity    2.33    0.119    
    Residuals                                  
    ---
    Signif. codes:  
    0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    

    词汇量和话题熟悉度两个变量对成绩的影响都很显著,交互项对成绩影响不显著。

    重复变量的双因素方差分析

    探究班级和测试次数对学生成绩的影响

    data <- read.csv(file = file.choose(),header = TRUE)
    attach(data)
    
    results <- ezANOVA(data, dv=.(scores), wid=.(subjects), within=.(tests), between=.(classes), detailed=TRUE)
    
    results
    
    $ANOVA
             Effect DFn DFd         SSn      SSd
    1   (Intercept)   1  42 849184.0667 9564.622
    2       classes   2  42  18092.9778 9564.622
    3         tests   2  84   1456.5778 6977.644
    4 classes:tests   4  84    323.1111 6977.644
                F            p p<.05        ges
    1 3728.922060 1.182737e-42     * 0.98089204
    2   39.724782 2.069760e-10     * 0.52238632
    3    8.767467 3.482406e-04     * 0.08092618
    4    0.972439 4.270782e-01       0.01915825
    
    $`Mauchly's Test for Sphericity`
             Effect         W           p p<.05
    3         tests 0.7210658 0.001225986     *
    4 classes:tests 0.7210658 0.001225986     *
    
    $`Sphericity Corrections`
             Effect       GGe       p[GG]
    3         tests 0.7819011 0.001109168
    4 classes:tests 0.7819011 0.413796347
      p[GG]<.05       HFe       p[HF] p[HF]<.05
    3         * 0.8067461 0.000971576         *
    4           0.8067461 0.415548681   
    

    班级和测试次数在原始检验中都很显著,然后交叉项不显著。

    但是在球形检验中,推翻了方差齐性的假设,所以tests需要使用球形矫正之后的p值,classes不用。

    矫正之前tests的p-value = 3.482406e-04,矫正之后的p-value = 0.001左右。

    多因素方差分析

    相关文章

      网友评论

        本文标题:R语言单因素、多因素方差分析ANOVA analysis of

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