2

I am trying to construct a hypothesis test for the variance of two populations. My test statistic is $F=$${\sigma_b^2}$/${\sigma_A^2}$. I want to implement this as a function in R so I computed: a=function(B,A){ var(B)/var(A) }

Now, I want to compute the p-value using this test statistic in R. How can I do this?

user189013
  • 35
  • 4
  • 3
    If you really have two *populations*, there is no point in testing; they are either equal or not. OTOH, this is not a good way to test the variance of two *samples*, use the Levene / Brown-Forsythe test instead (see: [Why Levene test of equality of variances rather than F-ratio](http://stats.stackexchange.com/a/24024/7290)). Instead, use [?leveneTest](http://www.inside-r.org/packages/cran/car/docs/leveneTest) in the `car` package. – gung - Reinstate Monica Dec 04 '14 at 18:28

1 Answers1

6

Since you are calculating the F-statistic could use the pf function.

As an example, let's take the following data as an example.

set.seed(123)
x <- rnorm(50, mean = 0, sd = 2)
y <- rnorm(30, mean = 1, sd = 1)
f <- var(x)/var(y)

# make sure to double for two-sided
pf(f, df1=49, df2=29, lower.tail=F)*2
[1] 0.0001897506

As gung noted, I failed to realize you are trying to do Hartley's Fmax test. In which case, you don't even need to calculate the F statistic or even the variances, you can just use var.test.

var.test(x, y)$p.value
[1] 0.0001897506

Also as noted by gung, there is Levene's test (leveneTest) in the car package.

cdeterman
  • 4,543
  • 1
  • 20
  • 34