How can I perform an F-Test in R?

To perform an F-Test in R, you need to use the ‘var.test()’ function. This function takes two input vectors, and produces a test statistic, p-value, and the degrees of freedom. The test statistic is used to decide whether the variances of the two input vectors are equal or not. The p-value is used to decide if the test statistic is statistically significant, and the degrees of freedom can be used to interpret the test statistic.


An F-test is used to test whether two population variances are equal. The null and alternative hypotheses for the test are as follows:

H0: σ12 = σ22 (the population variances are equal)

H1: σ12 ≠ σ22 (the population variances are not equal)

To perform an F-test in R, we can use the function var.test() with one of the following syntaxes:

  • Method 1: var.test(x, y, alternative = “two.sided”)
  • Method 2: var.test(values ~ groups, data, alternative = “two.sided”)

Note that alternative indicates the alternative hypothesis to use. The default is “two.sided” but you can specify it to be “left” or “right” instead.

This tutorial explains how to perform an F-test in R using both methods.

Method 1: F-Test in R

The following code shows how to perform an F-test using the first method:

#define the two groups
x <- c(18, 19, 22, 25, 27, 28, 41, 45, 51, 55)
y <- c(14, 15, 15, 17, 18, 22, 25, 25, 27, 34)

#perform an F-test to determine in the variances are equal
var.test(x, y)

	F test to compare two variances

data:  x and y
F = 4.3871, num df = 9, denom df = 9, p-value = 0.03825
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
  1.089699 17.662528
sample estimates:
ratio of variances 
          4.387122 

The F test statistic is 4.3871 and the corresponding p-value is 0.03825. Since this p-value is less than .05, we would reject the null hypothesis. This means we have sufficient evidence to say that the two population variances are not equal.

Method 2: F-Test in R

The following code shows how to perform an F-test using the first method:

#define the two groups
data <- data.frame(values=c(18, 19, 22, 25, 27, 28, 41, 45, 51, 55,
                            14, 15, 15, 17, 18, 22, 25, 25, 27, 34),
                   group=rep(c('A', 'B'), each=10))

#perform an F-test to determine in the variances are equal
var.test(values~group, data=data)

	F test to compare two variances

data:  x and y
F = 4.3871, num df = 9, denom df = 9, p-value = 0.03825
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
  1.089699 17.662528
sample estimates:
ratio of variances 
          4.387122 

Once again the F test statistic is 4.3871 and the corresponding p-value is 0.03825. Since this is less than .05, we would reject the null hypothesis.

This means we have sufficient evidence to say that the two population variances are not equal.

Related: Perform an F-test using this free .

When to Use the F-Test

The F-test is typically used to answer one of the following questions:

1. Do two samples come from populations with equal variances?

2. Does a new treatment or process reduce the variability of some current treatment or process?

How to Perform an F-Test in Python
How to Interpret the F-Test of Overall Significance in Regression

x