Load data set
dietR<-read.csv("D:/Analytics/BACP-Dec2017/06_ANOVA/diet.csv",header=T,sep=",")
Attach dataset
attach(dietR)
Calculate the weight lost by person (difference in weight before and after the diet) and add to the dataset.
dietR$weightlost<-pre.weight-weight6weeks
#attach again your data and remove the missing values.
dietR<-na.omit(dietR)
attach(dietR)
## The following objects are masked from dietR (pos = 3):
##
## Age, Diet, gender, Height, Person, pre.weight, weight6weeks
Carrying out the two-way ANOVA, telling R that Diet and gender are categorical using as.factor().
anova2<-aov(weightlost~as.factor(gender)*as.factor(Diet),data=dietR)
The Levene’s test for equality of variances is in the car package. Load the additional library car.
#install.packages("car",repos = "http://cran.us.r-project.org")
library(car)
## Warning: package 'car' was built under R version 3.4.3
Carry out Levene’s test.
leveneTest(weightlost~as.factor(gender)*as.factor(Diet),data=dietR)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 5 0.3867 0.8563
## 70
To see the ANOVA output use summary().
summary(anova2)
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(gender) 1 0.3 0.278 0.052 0.82062
## as.factor(Diet) 2 60.4 30.209 5.619 0.00546 **
## as.factor(gender):as.factor(Diet) 2 33.9 16.952 3.153 0.04884 *
## Residuals 70 376.3 5.376
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
There is a statistically significant interaction between the effects of Diet and Gender on weight loss [F(2, 70)=3.153, p = 0.049]. Since the interaction effect is significant (p = 0.049), the ’Diet’effect cannot be generalised for both males and females together.
If there are significant results in the ANOVA, post hoc test should be carried out.
To carry out Tukey’s post hoc adjustments for the pairwise comparisons.
If the interaction is NOT significant, interpret the main effects.
if it is significant interpret the interaction post hoc tests only.
TukeyHSD(anova2)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = weightlost ~ as.factor(gender) * as.factor(Diet), data = dietR)
##
## $`as.factor(gender)`
## diff lwr upr p adj
## 1-0 0.1221283 -0.9480861 1.192343 0.8206233
##
## $`as.factor(Diet)`
## diff lwr upr p adj
## 2-1 -0.03484966 -1.6215073 1.551808 0.9984761
## 3-1 1.84475570 0.2871469 3.402365 0.0162482
## 3-2 1.87960536 0.3385771 3.420634 0.0128844
##
## $`as.factor(gender):as.factor(Diet)`
## diff lwr upr p adj
## 1:1-0:1 0.6000000 -2.2129628 3.4129628 0.9887997
## 0:2-0:1 -0.4428571 -3.0107291 2.1250148 0.9958151
## 1:2-0:1 1.0590909 -1.6782698 3.7964516 0.8656520
## 0:3-0:1 2.8300000 0.3052886 5.3547114 0.0191170
## 1:3-0:1 1.1833333 -1.4893925 3.8560592 0.7855223
## 0:2-1:1 -1.0428571 -3.8558199 1.7701056 0.8852416
## 1:2-1:1 0.4590909 -2.5093998 3.4275816 0.9975014
## 0:3-1:1 2.2300000 -0.5436187 5.0036187 0.1863470
## 1:3-1:1 0.5833333 -2.3256625 3.4923292 0.9915569
## 1:2-0:2 1.5019481 -1.2354126 4.2393087 0.5963201
## 0:3-0:2 3.2728571 0.7481458 5.7975685 0.0040103
## 1:3-0:2 1.6261905 -1.0465354 4.2989163 0.4833188
## 0:3-1:2 1.7709091 -0.9260048 4.4678230 0.3965102
## 1:3-1:2 0.1242424 -2.7117126 2.9601974 0.9999949
## 1:3-0:3 -1.6466667 -4.2779524 0.9846191 0.4513580
The interaction was significant so the main effects are not interpreted here but if your data does not have a significant interaction, interpret these in the same way as post hoc tests on the one-way ANOVA resource.
The interactions post hoc tests compare each pair of combinations.
This shows that the only significant differences are for females and are between diets 1 and 3 (p=0.0191) and diets 2 and 3 (p=0.004).
Women on diet 3 lose on average 2.83kg more than those on diet 1 and 3.27kg more than those on diet 2.