# How can I calculate p-values in R?

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?

• 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). Instead, use ?leveneTest in the car package. Dec 4, 2014 at 18:28

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. • The OP is attempting to conduct Hartley's$F\$-max test, thus the df you give are incorrect (see here, & here). Dec 4, 2014 at 18:25
• @gung You are correct, I embarrassingly overlooked that. Updated now. Dec 4, 2014 at 18:34
• That's OK, it's an easy thing to overlook. Please correct the df in your answer, though. Dec 4, 2014 at 18:37