<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/f-test-compare-two-variances-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r"> Easy Guides</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<p>  </p>
<div>

<div>
<strong>F-test</strong> is used to assess whether the <strong>variances</strong> of two populations (A and B) are equal.
</div>
<p></p>
<p> <img alt="F-Test in R: Compare Two Sample Variances" src="https://i1.wp.com/www.sthda.com/sthda/RDoc/images/f-test.png?w=456" data-recalc-dims="1"> </p>
<p><strong>Contents</strong></p>
<div>
<ul>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#when-to-you-use-f-test" target="_blank">When to you use F-test?</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#research-questions-and-statistical-hypotheses" target="_blank">Research questions and statistical hypotheses</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#formula-of-f-test" target="_blank">Formula of F-test</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#compute-f-test-in-r" target="_blank">Compute F-test in R</a>
<ul>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#r-function" target="_blank">R function</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#import-and-check-your-data-into-r" target="_blank">Import and check your data into R</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#preleminary-test-to-check-f-test-assumptions" target="_blank">Preleminary test to check F-test assumptions</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#compute-f-test" target="_blank">Compute F-test</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#interpretation-of-the-result" target="_blank">Interpretation of the result</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#access-to-the-values-returned-by-var.test-function" target="_blank">Access to the values returned by var.test() function</a></li>
</ul>
</li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/f-test-compare-two-variances-in-r#infos" target="_blank">Infos</a></li>
</ul>
</div>
<div>
<h2>When to you use F-test?</h2>
<p>Comparing two variances is useful in several cases, including:</p>
<ul>
<li>
<p>When you want to perform a <a rel="nofollow" href="http://www.sthda.com/english/wiki/unpaired-two-samples-t-test-in-r" target="_blank">two samples t-test</a> to check the equality of the variances of the two samples</p>
</li>
<li>
<p>When you want to compare the variability of a new measurement method to an old one. Does the new method reduce the variability of the measure?</p>
</li>
</ul>
</div>
<div>
<h2>Research questions and statistical hypotheses</h2>
<p>Typical research questions are:</p>
<p></p>
<div>
<ol>
<li>whether the variance of group A (<span>\(\sigma^2_A\)</span>) <em>is equal</em> to the variance of group B (<span>\(\sigma^2_B\)</span>)?</li>
<li>whether the variance of group A (<span>\(\sigma^2_A\)</span>) <em>is less than</em> the variance of group B (<span>\(\sigma^2_B\)</span>)?</li>
<li>whether the variance of group A (<span>\(\sigma^2_A\)</span>) <em>is greather than</em> the variance of group B (<span>\(\sigma^2_B\)</span>)?</li>
</ol>
</div>
<p></p>
<p>In statistics, we can define the corresponding <em>null hypothesis</em> (<span>\(H_0\)</span>) as follow:</p>
<ol>
<li><span>\(H_0: \sigma^2_A = \sigma^2_B\)</span></li>
<li><span>\(H_0: \sigma^2_A \leq \sigma^2_B\)</span></li>
<li><span>\(H_0: \sigma^2_A \geq \sigma^2_B\)</span></li>
</ol>
<p>The corresponding <em>alternative hypotheses</em> (<span>\(H_a\)</span>) are as follow:</p>
<ol>
<li><span>\(H_a: \sigma^2_A \ne \sigma^2_B\)</span> (different)</li>
<li><span>\(H_a: \sigma^2_A > \sigma^2_B\)</span> (greater)</li>
<li><span>\(H_a: \sigma^2_A &lt; \sigma^2_B\)</span> (less)</li>
</ol>
<div>
<p>Note that:</p>
<ul>
<li>Hypotheses 1) are called <strong>two-tailed tests</strong></li>
<li>Hypotheses 2) and 3) are called <strong>one-tailed tests</strong></li>
</ul>
</div>
</div>
<div>
<h2>Formula of F-test</h2>
<p>The test statistic can be obtained by computing the ratio of the two variances <span>\(S_A^2\)</span> and <span>\(S_B^2\)</span>.</p>
<p><span>\[F = \frac{S_A^2}{S_B^2}\]</span></p>
<p>The degrees of freedom are <span>\(n_A – 1\)</span> (for the numerator) and <span>\(n_B – 1\)</span> (for the denominator).</p>
<p><span>Note that, the more this ratio deviates from 1, the stronger the evidence for unequal population variances.</span></p>
<p><span>Note that, the F-test requires the two samples to be <a rel="nofollow" href="http://www.sthda.com/english/wiki/normality-test-in-r" target="_blank">normally distributed</a>. </span></p>
</div>
<div>
<h2>Compute F-test in R</h2>
<div>
<h3>R function</h3>
<p>The R function <strong>var.test</strong>() can be used to compare two variances as follow:</p>
<pre><code># Method 1
var.test(values ~ groups, data, 
         alternative = &quot;two.sided&quot;)
# or Method 2
var.test(x, y, alternative = &quot;two.sided&quot;)</code></pre>
<p></p>
<div>
<ul>
<li><strong>x,y</strong>: numeric vectors</li>
<li><strong>alternative</strong>: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.</li>
</ul>
</div>
<p></p>
</div>
<div>
<h3>Import and check your data into R</h3>
<p>To import your data, use the following R code:</p>
<pre><code># If .txt tab file, use this
my_data &lt;- read.delim(file.choose())
# Or, if .csv file, use this
my_data &lt;- read.csv(file.choose())</code></pre>
<p>Here, we’ll use the built-in R data set named <a rel="nofollow" href="http://www.sthda.com/english/wiki/r-built-in-data-sets#toothgrowth" target="_blank">ToothGrowth</a>:</p>
<pre><code># Store the data in the variable my_data
my_data &lt;- ToothGrowth</code></pre>
<p>To have an idea of what the data look like, we start by displaying a random sample of 10 rows using the function <strong>sample_n</strong>()[in <strong>dplyr</strong> package]:</p>
<pre><code>library(&quot;dplyr&quot;)
sample_n(my_data, 10)</code></pre>
<pre><code>    len supp dose
43 23.6   OJ  1.0
28 21.5   VC  2.0
25 26.4   VC  2.0
56 30.9   OJ  2.0
46 25.2   OJ  1.0
7  11.2   VC  0.5
16 17.3   VC  1.0
4   5.8   VC  0.5
48 21.2   OJ  1.0
37  8.2   OJ  0.5</code></pre>
<p><span>We want to test the equality of variances between the two groups OJ and VC in the column “supp”.</span></p>
</div>
<div>
<h3>Preleminary test to check F-test assumptions</h3>
<p>F-test is very sensitive to departure from the normal assumption. You need to check whether the data is <a rel="nofollow" href="http://www.sthda.com/english/wiki/normality-test-in-r" target="_blank">normally distributed</a> before using the F-test.</p>
<p>Shapiro-Wilk test can be used to test whether the normal assumption holds. It’s also possible to use <a rel="nofollow" href="http://www.sthda.com/english/wiki/qq-plots-quantile-quantile-plots-r-base-graphs" target="_blank"><strong>Q-Q plot</strong> (quantile-quantile plot)</a> to graphically evaluate the normality of a variable. <a rel="nofollow" href="http://www.sthda.com/english/wiki/qq-plots-quantile-quantile-plots-r-base-graphs" target="_blank">Q-Q plot</a> draws the correlation between a given sample and the normal distribution.</p>
<p>If there is doubt about normality, the better choice is to use <strong>Levene’s test</strong> or <strong>Fligner-Killeen test</strong>, which are less sensitive to departure from normal assumption.</p>
</div>
<div>
<h3>Compute F-test</h3>
<pre><code># F-test
res.ftest &lt;- var.test(len ~ supp, data = my_data)
res.ftest</code></pre>
<pre><code>
    F test to compare two variances
data:  len by supp
F = 0.6386, num df = 29, denom df = 29, p-value = 0.2331
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.3039488 1.3416857
sample estimates:
ratio of variances 
         0.6385951 </code></pre>
</div>
<div>
<h3>Interpretation of the result</h3>
<p></div></div></div>