library(car) #leveneTest
## Loading required package: carData
x <- InsectSprays
#shows count of spray, here it seems that A, B, and F spray count are higher then C,D, E
stripchart(count~spray, vertical=T,pch=19, data = x, main='Count Vs Spray', xlab='Spray',
ylab='count')

#H0 = all variance are equal
#H1 = all variance are not equal
#p-value < significance level, we reject Null. We can assume homogeneity of variances is not
#in the different treatment groups.
leveneTest(count ~ spray, data = x)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 5 3.8214 0.004223 **
## 66
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#H0 = All means are equal
#H1 = All means are not equal
#we will run linear model for analysis of variance
analysis <- lm(count~spray, data = x)
#now look into the structure of the fitted model, we can see degree of freedom,sum squares
#mean square = varibility between sprays and varibility within the sprays, here we can see thats
#F value > 1 and P value < 0.05 thus we reject null hypothesis
anova(analysis)
## Analysis of Variance Table
##
## Response: count
## Df Sum Sq Mean Sq F value Pr(>F)
## spray 5 2668.8 533.77 34.702 < 2.2e-16 ***
## Residuals 66 1015.2 15.38
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#ploting the residuals
plot(analysis, which = 1)

plot(analysis, which = 2)

#positive right hand sqweness data
resids <- rstandard(analysis)
hist(resids)

#we have reject the null hypothesis thus accepting that there is a difference in effectiveness in sprays,
#but which are the effective sparys, diffence in means can be calculated by tukey test and aov for analysis
#of variance
#we can see that C-A, E-A, C-B, E-B, F-C, F-D, F-E are having 0.0000000 are differ from one another
TukeyHSD(aov(analysis))
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = analysis)
##
## $spray
## diff lwr upr p adj
## B-A 0.8333333 -3.866075 5.532742 0.9951810
## C-A -12.4166667 -17.116075 -7.717258 0.0000000
## D-A -9.5833333 -14.282742 -4.883925 0.0000014
## E-A -11.0000000 -15.699409 -6.300591 0.0000000
## F-A 2.1666667 -2.532742 6.866075 0.7542147
## C-B -13.2500000 -17.949409 -8.550591 0.0000000
## D-B -10.4166667 -15.116075 -5.717258 0.0000002
## E-B -11.8333333 -16.532742 -7.133925 0.0000000
## F-B 1.3333333 -3.366075 6.032742 0.9603075
## D-C 2.8333333 -1.866075 7.532742 0.4920707
## E-C 1.4166667 -3.282742 6.116075 0.9488669
## F-C 14.5833333 9.883925 19.282742 0.0000000
## E-D -1.4166667 -6.116075 3.282742 0.9488669
## F-D 11.7500000 7.050591 16.449409 0.0000000
## F-E 13.1666667 8.467258 17.866075 0.0000000