Two way anova test for program instructor and supplement on sodium taking on students grade

Input = ("
Instructor        Supplement  Sodium
'Brendon Small'   A           1200
'Brendon Small'   A           1400
'Brendon Small'   A           1350
'Brendon Small'   A            950
'Brendon Small'   A           1400
'Brendon Small'   B           1150
'Brendon Small'   B           1300
'Brendon Small'   B           1325
'Brendon Small'   B           1425
'Brendon Small'   B           1500
'Brendon Small'   C           1250
'Brendon Small'   C           1150
'Brendon Small'   C            950
'Brendon Small'   C           1150
'Brendon Small'   C           1600
'Brendon Small'   D           1300
'Brendon Small'   D           1050
'Brendon Small'   D           1300
'Brendon Small'   D           1700
'Brendon Small'   D           1300
'Coach McGuirk'   A           1100
'Coach McGuirk'   A           1200
'Coach McGuirk'   A           1250
'Coach McGuirk'   A           1050
'Coach McGuirk'   A           1200
'Coach McGuirk'   B           1250
'Coach McGuirk'   B           1350
'Coach McGuirk'   B           1350
'Coach McGuirk'   B           1325
'Coach McGuirk'   B           1525
'Coach McGuirk'   C           1225
'Coach McGuirk'   C           1125
'Coach McGuirk'   C           1000
'Coach McGuirk'   C           1125
'Coach McGuirk'   C           1400
'Coach McGuirk'   D           1200
'Coach McGuirk'   D           1150
'Coach McGuirk'   D           1400
'Coach McGuirk'   D           1500
'Coach McGuirk'   D           1200
'Melissa Robins'  A            900
'Melissa Robins'  A           1100
'Melissa Robins'  A           1150
'Melissa Robins'  A            950
'Melissa Robins'  A           1100
'Melissa Robins'  B           1150
'Melissa Robins'  B           1250
'Melissa Robins'  B           1250
'Melissa Robins'  B           1225
'Melissa Robins'  B           1325
'Melissa Robins'  C           1125
'Melissa Robins'  C           1025
'Melissa Robins'  C            950
'Melissa Robins'  C            925
'Melissa Robins'  C           1200
'Melissa Robins'  D           1100
'Melissa Robins'  D            950
'Melissa Robins'  D           1300
'Melissa Robins'  D           1400
'Melissa Robins'  D           1100
")

data = read.table(textConnection(Input),header=TRUE)

data$Instructor = factor(data$Instructor,
                         levels=unique(data$Instructor))

data$Supplement       = factor(data$Supplement,
                         levels=unique(data$Supplement))
summary(data)
##           Instructor Supplement     Sodium    
##  Brendon Small :20   A:15       Min.   : 900  
##  Coach McGuirk :20   B:15       1st Qu.:1100  
##  Melissa Robins:20   C:15       Median :1200  
##                      D:15       Mean   :1219  
##                                 3rd Qu.:1325  
##                                 Max.   :1700

Check the cell sizes are balanced

library(FSA)
## ## FSA v0.8.13. See citation('FSA') if used in publication.
## ## Run fishR() for related website and fishR('IFAR') for related book.
Summarize(Sodium~ Supplement +Instructor, data=data, digits=3)
##    Supplement     Instructor n mean      sd  min   Q1 median   Q3  max
## 1           A  Brendon Small 5 1260 191.703  950 1200   1350 1400 1400
## 2           B  Brendon Small 5 1340 132.994 1150 1300   1325 1425 1500
## 3           C  Brendon Small 5 1220 238.747  950 1150   1150 1250 1600
## 4           D  Brendon Small 5 1330 233.452 1050 1300   1300 1300 1700
## 5           A  Coach McGuirk 5 1160  82.158 1050 1100   1200 1200 1250
## 6           B  Coach McGuirk 5 1360 100.933 1250 1325   1350 1350 1525
## 7           C  Coach McGuirk 5 1175 148.955 1000 1125   1125 1225 1400
## 8           D  Coach McGuirk 5 1290 151.658 1150 1200   1200 1400 1500
## 9           A Melissa Robins 5 1040 108.397  900  950   1100 1100 1150
## 10          B Melissa Robins 5 1240  62.750 1150 1225   1250 1250 1325
## 11          C Melissa Robins 5 1045 116.458  925  950   1025 1125 1200
## 12          D Melissa Robins 5 1170 178.885  950 1100   1100 1300 1400

Define the linear model

model <- lm(Sodium ~ Instructor+Supplement+ Instructor*Supplement, data=data)
summary(model)
## 
## Call:
## lm(formula = Sodium ~ Instructor + Supplement + Instructor * 
##     Supplement, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -310.00  -90.00  -17.50   86.25  380.00 
## 
## Coefficients:
##                                      Estimate Std. Error t value Pr(>|t|)
## (Intercept)                           1260.00      69.44  18.145   <2e-16
## InstructorCoach McGuirk               -100.00      98.20  -1.018   0.3136
## InstructorMelissa Robins              -220.00      98.20  -2.240   0.0297
## SupplementB                             80.00      98.20   0.815   0.4193
## SupplementC                            -40.00      98.20  -0.407   0.6856
## SupplementD                             70.00      98.20   0.713   0.4794
## InstructorCoach McGuirk:SupplementB    120.00     138.88   0.864   0.3919
## InstructorMelissa Robins:SupplementB   120.00     138.88   0.864   0.3919
## InstructorCoach McGuirk:SupplementC     55.00     138.88   0.396   0.6938
## InstructorMelissa Robins:SupplementC    45.00     138.88   0.324   0.7473
## InstructorCoach McGuirk:SupplementD     60.00     138.88   0.432   0.6677
## InstructorMelissa Robins:SupplementD    60.00     138.88   0.432   0.6677
##                                         
## (Intercept)                          ***
## InstructorCoach McGuirk                 
## InstructorMelissa Robins             *  
## SupplementB                             
## SupplementC                             
## SupplementD                             
## InstructorCoach McGuirk:SupplementB     
## InstructorMelissa Robins:SupplementB    
## InstructorCoach McGuirk:SupplementC     
## InstructorMelissa Robins:SupplementC    
## InstructorCoach McGuirk:SupplementD     
## InstructorMelissa Robins:SupplementD    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 155.3 on 48 degrees of freedom
## Multiple R-squared:  0.3491, Adjusted R-squared:    0.2 
## F-statistic: 2.341 on 11 and 48 DF,  p-value: 0.02121

Box plot for seperate factors

par(mfrow=c(1,2))
plot(Sodium ~Instructor +Supplement, data=data)

Judging by the boxplots, there appears to be differences in instructor as well as Supplement

Interaction plots

interaction.plot(data$Supplement,data$Instructor, data$Sodium)

interaction.plot(data$Instructor, data$Supplement,data$Sodium)

Although there are some interactions, but they are still roughly parallel, we will comfirm using the Anova model

Anova

anova(model)
## Analysis of Variance Table
## 
## Response: Sodium
##                       Df  Sum Sq Mean Sq F value   Pr(>F)   
## Instructor             2  290146  145073  6.0173 0.004658 **
## Supplement             3  306125  102042  4.2324 0.009841 **
## Instructor:Supplement  6   24438    4073  0.1689 0.983880   
## Residuals             48 1157250   24109                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Studying the output of ANOVA table, we see that there is no evidence of a significant interation since p-value=0.98. Furthermore, Supplement p-value=0.009841, it tells us there is not enough evidence to conclude there is significant supplement leve.

Check Anova model assupmtion, qq-plot and residual plots

qqnorm(model$residuals)

plot(model$fitted, model$residuals, xlab="fitted",ylab="residuals")

x<- residuals(model)
library(rcompanion)

plotNormalHistogram(x)

As a result, we can see it follows a normal distribution