# Fisher’s F-Test in R Programming

Fisher’s F test calculates the ratio between the larger variance and the smaller variance. We use the F test when we want to check whether the means of three or more groups are different or not. F-test is used to assess whether the variances of two populations (A and B) are equal. The method is simple; it consists of taking the ratio between the larger variance and the smaller variance. var.test() function in R Programming performs an F-test between 2 normal populations with the hypothesis that variances of the 2 populations are equal.

#### Formula for Fisher’s F-Test

F = Larger Sample Variance / Smaller Sample Variance

#### Implementation in R

• To test the equality of variances between the two samples use var.test(x, y)
• To compare two variance use var.test(x, y, alternative = “two.sided”)

Syntax: var.test(x, y, alternative = “two.sided”) Parameters: x, y: numeric vectors alternative: a character string specifying the alternative hypothesis.

Example 1:

Let we have two samples x, y. The R function var.test() can be used to compare two variances as follow:

## r

 `# Taking two samples` `x <- ``rnorm``(249, mean = 20)` `y <- ``rnorm``(79, mean = 30)` `# var test in R` `var.test``(x, y, alternative = ``"two.sided"``)`

Output:

```       F test to compare two variances
data:  x and y
F = 0.88707, num df = 248, denom df = 78, p-value = 0.4901
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.6071405 1.2521004
sample estimates:
ratio of variances
0.8870677 ```

It returns the following:

1. the value of the F test statistic.
2. the degrees of freedom of the F distribution of the test statistic.
3. the p-value of the test is 0.4901
4. a confidence interval for the ratio of the population variances.
5. the ratio of the sample variances is 0.8870677

The p-value of the F-test is p = 0.4901 which is greater than the alpha level of 0.05. In conclusion, there is no difference between the two samples.

Example 2:

Let us have two random samples from two random populations. Test whether two populations have the same variance.

## r

 `# Taking two random samples` `A = ``c``(16, 17, 25, 26, 32,` `        ``34, 38, 40, 42)` `B = ``c``(600, 590, 590, 630, 610, 630)` `# var test in R` `var.test``(A, B, alternative = ``"two.sided"``)`

Output:

```       F test to compare two variances
data:  A and B
F = 0.27252, num df = 8, denom df = 5, p-value = 0.1012
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.04033118 1.31282683
sample estimates:
ratio of variances
0.2725248```

It returns the following:

1. the value of the F test statistic.
2. the degrees of freedom of the F distribution of the test statistic.
3. the p-value of the test is 0.1012
4. 95% confidence interval for the ratio of the population variances.
5. the ratio of the sample variances is 0.2725248

The p-value of the F-test is p = 0.1012 which is greater than the alpha level of 0.05. In conclusion, there is no difference between the two samples.

Example 3:

Let us have two random samples.

## r

 `# Taking two random samples` `x = ``c``(25, 29, 35, 46, 58, 66, 68)` `y = ``c``(14, 16, 24, 28, 32, 35, ` `          ``37, 42, 43, 45, 47)` `# var test in R` `var.test``(x, y)`

Output:

```       F test to compare two variances
data:  x and y
F = 2.4081, num df = 6, denom df = 10, p-value = 0.2105
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.5913612 13.1514157
sample estimates:
ratio of variances
2.4081```

It returns the following:

1. the value of the F test statistic.
2. the degrees of freedom of the F distribution of the test statistic.
3. the p-value of the test is 0.2105
4. 95% confidence interval for the ratio of the population variances.
5. the ratio of the sample variances is 2.4081

The p-value of the F-test is p = 0.2105 which is greater than the alpha level of 0.05. In conclusion, there is no difference between the two samples.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next