<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/one-way-anova-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/one-way-anova-in-r/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Suppose as a business manager you have the responsibility for testing and comparing the lifetimes of four brands (Apollo, Bridgestone, CEAT and Falken) of automobile tyres. The lifetime of these sample observations are measured in mileage run in ’000 miles. For each brand of automobile tyre, sample of 15 observations have been collected. On the basis of these information, you have to take you decision regarding the four brands of automobile tyre. The data is provided in the csv file format (called, <a rel="nofollow" href="https://datascienceplus.com/wp-content/uploads/2017/08/tyre.csv" target="_blank">tyre.csv</a>).</p>
<p>In order to test and compare the lifetimes of four brands of tyre, you should apply one-way ANOVA method as there is only one factor or criterion (mileage run) to classify the sample observations. The null and alternative hypothesis to be used is given as:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/08/hypothesis.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/08/hypothesis-490x107.png?w=450&ssl=1" alt="" class="wp-image-8159" data-recalc-dims="1"></a></p>
<h2>Data Import and Outlier Checking</h2>
<p>Let us first import the data into R and save it as object ‘tyre’. The R codes to do this:</p>
<pre>
tyre&lt;- read.csv(file.choose(),header = TRUE, sep = &quot;,&quot;)
attach(tyre)
</pre>
<p>Before doing anything, you should check the variable type as in ANOVA, you need categorical independent variable (here the factor or treatment variable ‘brand’. In R, you can use the following code:</p>
<pre>
is.factor(Brands)
<em>[1] TRUE
</em>
</pre>
<p>As the result is ‘TRUE’, it signifies that the variable ‘Brands’ is a categorical variable.<br>
Now it is all set to run the ANOVA model in R. Like other linear model, in ANOVA also you should check the presence of outliers can be checked by boxplot. As there are four populations to study, you should use separate boxplot for each of the population. In R, it is done as:</p>
<pre>
boxplot(Mileage~Brands, main=&quot;Fig.-1: Boxplot of Mileage of Four Brands of Tyre&quot;, col= rainbow(4))
</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-1.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-1-490x328.png?w=450&ssl=1" alt="" class="wp-image-8161" data-recalc-dims="1"></a></p>
<p>If you are using advanced graphics, you can use the ‘ggplot2’ package with the following code to get the above boxplot.</p>
<pre>
library(ggplot2)
ggplot(tyre, aes(Brands,Mileage))+geom_boxplot(aes(col=Brands))+labs(title=&quot;Boxplot of Mileage of Four Brands of Tyre&quot;)
</pre>
<p>The above picture shows that there is one extreme observation in the CEAT brand. To find out the exact outliers or extreme observations, you can use the following command:</p>
<pre>
boxplot.stats(Mileage[Brands==&quot;CEAT&quot;])
<em>$stats
[1] 30.42748 33.11079 34.78336 36.12533 36.97277
$n
[1] 15
$conf
[1] 33.55356 36.01316
$out
[1] 41.05</em>
</pre>
<p>So, the outlier is the observation valued ‘41.05’. The confidence interval is (33.55 – 36.01) and the minimum and maximum values of the sample coming from the population ‘CEAT’ is 30.43 and 41.05 respectively. Considering all these points, we ignore the outlier value ‘41.05’ momentarily and carry out the analysis. If at later stage, we find that this outlier may create problems in the estimation, we will exclude it.</p>
<h2>Estimation of Model</h2>
<p>Let us now fit the model by using the <code>aov()</code> function in R.</p>
<pre>
model1&lt;- aov(Mileage~Brands)
</pre>
<p>To get the result of the one-way ANOVA:</p>
<pre>
summary(model1)
<em> Df Sum Sq Mean Sq F value Pr(>F) Brands 3 256.3 85.43 17.94 2.78e-08 ***
Residuals 56 266.6 4.76 ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
</em>
</pre>
<p>From the above results, it is observed that the F-statistic value is 17.94 and it is highly significant as the corresponding p-value is much less than the level of significance (1% or 0.01). Thus, it is wise to reject the null hypothesis of equal mean value of mileage run across all the tyre brands. In other words, the average mileage of the four tyre brands are not equal.<br>
Now you have to find out the pair of brands which differ. For this you may use the Tukey’s HSD test. In R, the following are the code for applying the Tukey’s HSD test:</p>
<pre>
TukeyHSD(model1, conf.level = 0.99)
<em>Tukey multiple comparisons of means
99% family-wise confidence level
Fit: aov(formula = Mileage ~ Brands)
$Brands
diff lwr upr p adj
Bridgestone-Apollo -3.01900000 -5.6155816 -0.4224184 0.0020527
CEAT-Apollo -0.03792661 -2.6345082 2.5586550 0.9999608
Falken-Apollo 2.82553333 0.2289517 5.4221149 0.0043198
CEAT-Bridgestone 2.98107339 0.3844918 5.5776550 0.0023806
Falken-Bridgestone 5.84453333 3.2479517 8.4411149 0.0000000
Falken-CEAT 2.86345994 0.2668783 5.4600415 0.0037424
</em>
</pre>
<p>The TukeyHSD command shows the pair-wise difference of mileage of four brands of tyres at 1% level of significance. Here, the “diﬀ” column provides mean diﬀerences. The “lwr” and “upr” columns provide lower and upper 99% confidence bounds, respectively. Finally, the “p adj” column provides the p-values adjusted for the number of comparisons made. As there are four brands, six possible pair-wise comparisons are obtained. The results show that all the pairs of mileage are statistically significantly different from the Tukey’s Honestly Significant Differences, except for the pair CEAT-Apollo. More specifically, the pair-wise difference between Bridgestone-Apollo is found to be -3.019 which means that Apollo has higher mileage than Bridgestone and this is statistically significant. Similarly, you can make other pair-wise comparison.<br>
You can also plot these the results of Tukey’s HSD comparison by using the plot function as follows:</p>
<pre>
plot(TukeyHSD(model1, conf.level = 0.99),las=1, col = &quot;red&quot;)
</pre>
<p>The diagram is show below:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-3.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-3-490x328.png?w=450&ssl=1" alt="" class="wp-image-8162" data-recalc-dims="1"></a></p>
<p>Another way of visualization by plotting means of mileage of four brands of tyre with the help of gplots package. By using the <code>plotmeans()</code> function in the gplots package, you can create the mean plots for single factors including confidence intervals.</p>
<pre>
library(gplots)
plotmeans(Mileage~Brands, main=&quot;Fig.-3: Mean Plot with 95% Confidence Interval&quot;, ylab = &quot;Mileage run ('000 miles)&quot;, xlab = &quot;Brands of Tyre&quot;)
</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-4.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-4-490x328.png?w=450&ssl=1" alt="" class="wp-image-8163" data-recalc-dims="1"></a></p>
<h2>Diagnostic Checking</h2>
<p>Diagnostic Checking: After estimating the ANOVA model and finding out the possible pairs of differences, it is now time to go for the different diagnostic checking with respect to model assumptions. The single call to plot function generates four diagnostic plots (Fig.-5). </p>
<pre>
par(mfrow=c(2,2))
plot(model1)
</pre>
<p>Give this plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-5.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-5-490x190.png?w=450&ssl=1" alt="" class="wp-image-8164" data-recalc-dims="1"></a></p>
<p>The Residuals vs. Fitted plot shown in the upper left of Fig.-4, shows the ﬁtted values plotted against the model residuals. If the residuals follow any particular pattern, such as a diagonal line, there may be other predictors not yet in the model that could improve it. The ﬂat lowess line looks very good as the single predictor variable or regressor sufficiently explaining the dependent variable.<br>
The Normal Q-Q Plot in the upper right of Fig.-4, shows the quantiles of the standardized residuals plotted against the quantiles you would expect if the data were normally distributed. Since these fall mostly on the straight line, the assumption of normally distributed residuals is met. Since there are only 15 observations in each individual brand of tyre, it is not wise to go for group-wise checking of normality assumption. Moreover, the normality of the overall residual can be checked by means of some statistical test such as Shapiro-Wilk test. Shortly I’ll show you this procedure too.<br>
The Scale-Location plot in the lower left of Fig.-4, shows the square root of the absolute standardized residuals plotted against the ﬁtted, or predicted, values. Since the lowess line that ﬁts this is fairly ﬂat, it indicates that the spread in the predictions is almost the same across the prediction line, indicating the very less chances of failure of meeting the assumption of homoscedasticity. This will be further verified by some statistical tests. In case of ANOVA, you can check the assumption of homogeneity of variances across the four brands of tyre.<br>
Finally, the Residuals vs. Leverage plot in the lower right corner, shows a measure of the influence of each point on the overall equation against the standardized residuals. Since no points stand out far from the pack, we can assume that there are no outliers having undue influence on the ﬁt of the model.<br>
Thus, the graphical diagnostic of the model fit apparently shows that the assumptions requirements of ANOVA model is fairly fulfilled. However, the normality assumption and homogeneity are supposed to be confirmed by the appropriate statistical tests.</p>
<p>Regarding the fulfillment of normality assumption, it has been already discussed that when the number of observations is less, it is wise to test normality for the overall residuals of the model, instead of checking it for separate group. In R the residuals of model is saved as follows:</p>
<pre>
uhat&lt;-resid(model1)
</pre>
<p>where resid function extracts the model residual and it is saved as object ‘uhat’.</p>
<p>Now you may apply the Shapiro-Wilk test for normality with the following hypotheses set-up:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-6.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/08/Fig-6-490x86.png?w=450&ssl=1" alt="" class="wp-image-8165" data-recalc-dims="1"></a></p>
<p>The test code and results are shown below:</p>
<pre>
shapiro.test(uhat)
<em>Shapiro-Wilk normality test
data: uhat
W = 0.9872, p-value = 0.7826
</em>
</pre>
<p>As the p-value is higher than the level of significance, you cannot reject the null hypothesis, which implies that the samples are taken from the normal populations.<br>
Another assumption requirement is the homogeneity of variances across the groups, which can be statistically tested by Bartlett test and Levene test. In both the test, the null hypothesis is set as the homogeneity of variance across the cross-sectional group. The tests are conducted as follows:</p>
<pre>
bartlett.test(Mileage~Brands)
<em>Bartlett test of homogeneity of variances
data: Mileage by Brands
Bartlett's K-squared = 2.1496, df = 3, p-value = 0.5419
</em>
</pre>
<pre>
library(car)
levene.test(Mileage~Brands)
<em>Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 3 0.6946 0.5592
56 </em>
</pre>
<p>Both the tests confirmed the presence of homogeneity of variance across the four brands of tyre as we cannot reject the null hypothesis of homogeneity of variances across the brands of tyre.</p>
<h2>The Conclusion</h2>
<p>The mileages of the four brands of tyre are different. The results show that all the pairs of mileage are statistically significantly different from the Tukey’s Honestly Significant Differences, except for the pair CEAT-Apollo. More specifically, the pair-wise difference between Bridgestone-Apollo is found to be -3.019 which means that Apollo has higher mileage than Bridgestone and this is statistically significant. </p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/cubic-and-smoothing-splines-in-r/" title="Permanent Link toCubic and Smoothing Splines in R" target="_blank">Cubic and Smoothing Splines in R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/chi-squared-test-in-r/" title="Permanent Link toChi-Squared Test – The Purpose, The Math, When and How to Implement?" target="_blank">Chi-Squared Test – The Purpose, The Math, When and How to Implement?</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/missing-value-treatment/" title="Permanent Link toMissing Value Treatment" target="_blank">Missing Value Treatment</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/r-for-publication-by-page-piccinini/" title="Permanent Link toR for Publication by Page Piccinini" target="_blank">R for Publication by Page Piccinini</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/assessing-significance-of-slopes-in-regression-models-with-interaction/" title="Permanent Link toAssessing significance of slopes in regression models with interaction" target="_blank">Assessing significance of slopes in regression models with interaction</a></li>
</ol></strong>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/one-way-anova-in-r/"> R Programming – DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Sd-38kQ1f2Y" height="1"></a></div>