How to extract the p-value and F-statistic from aov output in R?

R ProgrammingServer Side ProgrammingProgramming

The analysis of variance technique helps us to identify whether there exists a significant mean difference in more than two variables or not. To detect this difference, we either use F-statistic value or p-value. If the F-statistic value is greater than the critical value of F or if p-value is less than the level of significance then we say that at least one of the means is significantly different from the rest. To extract the p-value and F-statistic value, we can make use of summary function of the ANOVA model.

Example

 Live Demo

set.seed(123)
Group<-rep(c("G1","G2","G3","G4"),times=5)
Response<-runif(20,2,5)
df<-data.frame(Group,Response)
df

Output

Group Response
1 G1 2.862733
2 G2 4.364915
3 G3 3.226931
4 G4 4.649052
5 G1 4.821402
6 G2 2.136669
7 G3 3.584316
8 G4 4.677257
9 G1 3.654305
10 G2 3.369844
11 G3 4.870500
12 G4 3.360002
13 G1 4.032712
14 G2 3.717900
15 G3 2.308774
16 G4 4.699475
17 G1 2.738263
18 G2 2.126179
19 G3 2.983762
20 G4 4.863511
ANOVA<-aov(Response~Group,df)
summary(ANOVA)
Df Sum Sq Mean Sq F value Pr(F)
Group 3 4.813 1.6043 2.141 0.135
Residuals 16 11.990 0.7494
summary(ANOVA)[[1]][1,4:5]
F value Pr(F)
Group 2.1408 0.1351

If we want to extract only p-value then it can be done as shown below −

summary(ANOVA)[[1]][1,5]
[1] 0.1351315

If we want to extract only F-value then it can be done as shown below −

summary(ANOVA)[[1]][1,4]
[1] 2.140825

Example

Let’s have a look at one more example −

 Live Demo

Factor<-rep(c("F1","F2","F3","F4","F5"),each=4)
Dependent<-rnorm(20,2)
ANOVA_data<-data.frame(Factor,Dependent)
ANOVA_data

Output

Factor Dependent
1 F1 2.2236414
2 F1 4.0072015
3 F1 3.0119791
4 F1 1.6975408
5 F2 0.9747552
6 F2 1.7326152
7 F2 1.8008943
8 F2 2.1311226
9 F3 2.1457999
10 F3 2.3620647
11 F3 2.6739812
12 F3 4.0720358
13 F4 1.4589714
14 F4 0.9295078
15 F4 1.6275433
16 F4 1.5148586
17 F5 2.2747842
18 F5 1.5204874
19 F5 2.7981053
20 F5 0.9955488
ANOVA_Model<-aov(Dependent~Factor,ANOVA_data)
summary(ANOVA_Model)
Df Sum Sq Mean Sq F value Pr(F)
Factor 4 6.647 1.6617 3.04 0.0508 .
Residuals 15 8.200 0.5467
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
summary(ANOVA_Model)[[1]][1,4:5]
F value Pr(F)
Factor 3.0395 0.05078 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
summary(ANOVA_Model)[[1]][1,5]
[1] 0.05077798
summary(ANOVA_Model)[[1]][1,4]
[1] 3.039549
raja
Published on 21-Aug-2020 10:34:18
Advertisements