2

I've been trying for a while on R and I can't seem to extract the p value(value for "Pr(>f))" for a levene test. The usual approach for a statistical test on R is to end the test command with a $p.value at the end. However this does not seem to work for the Levene test as show:

> leveneTest(all.vec,factors)
Levene's Test for Homogeneity of Variance (center = median)
        Df F value    Pr(>F)    
group    3  8.9261 6.982e-06 ***
      2607                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> leveneTest(all.vec,factors)$p.value
NULL

Also, in a similar way for the jarque bera test I can't extract the p.value in the same way

> jarque.bera.test(lg.day.ret.vec)

    Jarque Bera Test

data:  lg.day.ret.vec
X-squared = 63087.83, df = 2, p-value < 2.2e-16

> jarque.bera.test(lg.day.ret.vec)$p.value
X-squared 
        0 

Thanks for the help

Vik
  • 469
  • 2
  • 6
  • 18

3 Answers3

3

There are several ways. Here's a reproducible example to work from:

library("car")
test <- with(Moore, leveneTest(conformity, fcategory))

First, look at the structure of the returned object as that will usually tell you what you are playing with:

str(test)

this gives:

> str(test)
Classes ‘anova’ and 'data.frame':   2 obs. of  3 variables:
 $ Df     : int  2 42
 $ F value: num  0.046 NA
 $ Pr(>F) : num  0.955 NA
 - attr(*, "heading")= chr "Levene's Test for Homogeneity of Variance (center = median)"

We see that the object is a data frame and that the p values are in the 3rd column. Hence any of the following will extract the data

test[,3]          # pull out the entire 3rd column
test[1,3]         # pull out only the none NA p-value
test$`Pr(>F)`     # pull out the P-value column by name
test$`Pr(>F)`[1]  # as above, but then take only the 1st element

These gives, for the above example:

> test[,3]
[1] 0.9550975        NA
> test[1,3]
[1] 0.9550975
> test$`Pr(>F)`
[1] 0.9550975        NA
> test$`Pr(>F)`[1]
[1] 0.9550975
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • This works with the Levene test but with the Jarque Bera Test, it seems to say the solution is 0 which is incorrect. – Vik Feb 21 '15 at 15:53
  • If you tell me which package that function is in I'll take a look, but you should be able to work this out yourself now. Assign the output from the test to an object, say `test`, then do `str(test)` and look for where the p value is stored. – Gavin Simpson Feb 21 '15 at 18:57
0

You could also use the levene.test function in the lawstat package:

> df<-data.frame(group=c(rep(LETTERS[1],20),rep(LETTERS[2],20)),
               value=c(sample(1:10,size=20,replace=T),sample(30:40,size=20,replace=T)))

> df
   group value
1      A     8
2      A     4
3      A     8
4      A     3
5      A     2
6      A     3
7      A     4
8      A     5
9      A     8
10     A     6
11     A     1
12     A     4
13     A     9
14     A     7
15     A     8
16     A     4
17     A     2
18     A    10
19     A     8
20     A     7
21     B    40
22     B    37
23     B    39
24     B    30
25     B    30
26     B    30
27     B    34
28     B    39
29     B    40
30     B    34
31     B    30
32     B    33
33     B    32
34     B    39
35     B    36
36     B    37
37     B    35
38     B    39
39     B    34
40     B    34

> library(lawstat)
> levene.test(df$value,df$group,location='median')

modified robust Brown-Forsythe Levene-type test based on the absolute deviations from the median

data:  df$value
Test Statistic = 1.7537, p-value = 0.1933

> levene.test(df$value,df$group,location='median')$p.value
[1] 0.1933243
ccapizzano
  • 1,556
  • 13
  • 20
0

You can also write leveneTest(all.vec,factors)$F value