#require is used inside functions, as it outputs a warning and continues if the package is not #found, whereas library will throw an error.
require(mosaic)
require(readr)
require(mosaic)
require(dplyr)
require(lattice)
require(graphics)

Read the Popcorn data set into your R session and attach it:

#note below command requires loading package readr 
Popcorn <- read_csv("P:/SUNY-Brockport/SUNY Brockport_1/teaching/Staitstical Methods/Spring2018/Ch4ANOVA/Ch4/Popcorn.csv")

View(Popcorn) #comment it while knitting, as you dont want it to pop up while creating html

Compute descriptive statistics by group and/or factor level, old school

attach(Popcorn)
mean(PopRate[Brand=="Fastco"])
## [1] 81.75507
#Calculate means by factor level, another way
meanF <- mean(Popcorn[Brand=="Fastco",]$PopRate)
mean(PopRate[Brand=="Pop Secret"])
## [1] 80.93226
mean(PopRate[Time==105])
## [1] 78.28701
mean(PopRate[Time==135])
## [1] 84.40032
mean(PopRate)
## [1] 81.34367

Quick and easy way using package mosaic

attach(Popcorn)
mean(PopRate~Brand)
##     Fastco Pop Secret 
##   81.75507   80.93226
mean(PopRate~Time)
##      105      135 
## 78.28701 84.40032
mean(PopRate~Microwave)
##   Lounge     Room 
## 80.42122 82.26611
mean(PopRate)
## [1] 81.34367
#The average popping rates for each level of Brand and Time
mean(PopRate~Brand+Time)
##     Fastco.105 Pop Secret.105     Fastco.135 Pop Secret.135 
##       81.13192       75.44209       82.37821       86.42244
#The average popping rates for each level of Brand,microwave and Time
mean(PopRate~Brand+Time+Microwave)
##     Fastco.105.Lounge Pop Secret.105.Lounge     Fastco.135.Lounge 
##              81.40000              72.19525              81.85560 
## Pop Secret.135.Lounge       Fastco.105.Room   Pop Secret.105.Room 
##              86.23405              80.86385              78.68892 
##       Fastco.135.Room   Pop Secret.135.Room 
##              82.90082              86.61083

Find the difference between average PopRate for the cooking times for each brand:

mean(PopRate[(Brand=="Fastco") & (Time==135)])-mean(PopRate[(Brand=="Fastco") & (Time==105)])
## [1] 1.246288
mean(PopRate[(Brand=="Pop Secret") & (Time==135)])-mean(PopRate[(Brand=="Pop Secret") & (Time==105)])
## [1] 10.98035
attach(Popcorn)
# In order to do anova, we have to make Time into a categorical variable
#Thus, using as.factor we can transform the data
Time1 <- as.factor(Time)
mutate(Popcorn,Time1) #if it doesnt work on your ocmputer, use cbind.data.frame
## # A tibble: 32 x 6
##     Brand Microwave  Time `Unpopped (percent)` PopRate  Time1
##     <chr>     <chr> <int>                <dbl>   <dbl> <fctr>
##  1 Fastco    Lounge   105                17.40 82.6000    105
##  2 Fastco    Lounge   105                12.50 87.5000    105
##  3 Fastco    Lounge   105                24.70 75.3000    105
##  4 Fastco    Lounge   105                19.80 80.2000    105
##  5 Fastco      Room   105                26.20 73.8000    105
##  6 Fastco      Room   105                22.40 77.6000    105
##  7 Fastco      Room   105                11.50 88.5000    105
##  8 Fastco      Room   105                16.44 83.5554    105
##  9 Fastco    Lounge   135                18.93 81.0667    135
## 10 Fastco    Lounge   135                 9.20 90.8000    135
## # ... with 22 more rows
plotPoints(PopRate~Time1|Brand)

xyplot(PopRate~Time1|Brand) # will do the same

Note on when to use what kind of graph

attach(Popcorn)
bwplot(PopRate~Time1|Brand)

#The two-way ANOVA is conducted using the aov() command.

attach(Popcorn)
## The following objects are masked from Popcorn (pos = 3):
## 
##     Brand, Microwave, PopRate, Time, Unpopped (percent)
## The following objects are masked from Popcorn (pos = 4):
## 
##     Brand, Microwave, PopRate, Time, Unpopped (percent)
## The following objects are masked from Popcorn (pos = 5):
## 
##     Brand, Microwave, PopRate, Time, Unpopped (percent)
## The following objects are masked from Popcorn (pos = 6):
## 
##     Brand, Microwave, PopRate, Time, Unpopped (percent)
pop.aov1 <- aov(PopRate~Brand+Time+Brand:Time) #store the ANOVA results in object pop.aov1
summary(pop.aov1) #Use the summary() command to obtain the ANOVA table:
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Brand        1    5.4    5.42   0.131 0.7204  
## Time         1  299.0  298.98   7.219 0.0120 *
## Brand:Time   1  189.5  189.50   4.575 0.0413 *
## Residuals   28 1159.7   41.42                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(Brand, Time1, PopRate, main= "Brand Time Interaction") # Brand Time interaction plots

interaction.plot(Brand, Microwave, PopRate,main= "Brand Microwave Interaction") # Brand Microwaveinteraction plots

interaction.plot(Time, Microwave, PopRate, main="Time Microwave Interaction") # Time Microwave interaction plots