#library(memisc, warn.conflicts = FALSE, quietly=TRUE)
#install.packages('mosaic')
require(mosaic)
## Loading required package: mosaic
## Warning: package 'mosaic' was built under R version 3.2.5
## Loading required package: dplyr
## Warning: package 'dplyr' was built under R version 3.2.5
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## Loading required package: lattice
## Warning: package 'lattice' was built under R version 3.2.5
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.2.5
## Loading required package: mosaicData
## Warning: package 'mosaicData' was built under R version 3.2.5
## Loading required package: Matrix
## 
## The 'mosaic' package masks several functions from core packages in order to add additional features.  
## The original behavior of these functions should not be affected by this.
## 
## Attaching package: 'mosaic'
## The following object is masked from 'package:Matrix':
## 
##     mean
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cov, D, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
#Activities from textbook
#Read the Popcorn data set into your R session and attach it:

library(readr)
## Warning: package 'readr' was built under R version 3.2.5
popcorn<- read_csv("P:/SUNY-Brockport/SUNY Brockport_1/teaching/Staitstical Methods/Spring 2017/Ch4_ANOVA_Lab/C4 Popcorn.csv")
## Parsed with column specification:
## cols(
##   Brand = col_character(),
##   Microwave = col_character(),
##   Time = col_integer(),
##   `Unpopped (percent)` = col_double(),
##   PopRate = col_double()
## )
#View(C4_Popcorn) #commented as you dont want it to pop up while creating html
attach(popcorn)

#Compute descriptive statistics by group:

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
#If you are using the mosaic package: 
mean(PopRate~Brand)
##     Fastco Pop Secret 
##   81.75507   80.93226
mean(PopRate~Time)
##      105      135 
## 78.28701 84.40032
mean(PopRate)
## [1] 81.34367
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 and Time, as well as overall average PopRate:
#If you are using the mosaic package: 
favstats(PopRate~Brand+Time)
##       Brand.Time     min       Q1   median       Q3     max     mean
## 1     Fastco.105 73.8000 77.02500 81.40000 84.54155 88.5000 81.13192
## 2 Pop Secret.105 65.9810 72.01545 73.53510 78.48333 88.9696 75.44209
## 3     Fastco.135 71.5000 79.37502 82.35165 85.74178 92.6000 82.37821
## 4 Pop Secret.135 78.7096 82.10000 86.91290 90.76937 94.2780 86.42244
##         sd n missing
## 1 5.398159 8       0
## 2 7.063566 8       0
## 3 7.226651 8       0
## 4 5.866157 8       0
mean(PopRate~Brand+Time)
##     Fastco.105 Pop Secret.105     Fastco.135 Pop Secret.135 
##       81.13192       75.44209       82.37821       86.42244
#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
# 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)
require(dplyr)
plotPoints(PopRate~Time1|Brand)

require(lattice)

###########################################
#Only Categorical Variables: Bargraph
#One Numeric Variable (can also have one categorical grouping variable): Histogram or Boxplot
#Two Numeric Variables: Scatterplot
############################################



popcorn<-mutate(popcorn,Time1) #package dplyr needed

bwplot(PopRate~Time1|Brand)

xyplot(PopRate~Time1|Brand)

#The two-way ANOVA is conducted using the aov() command. We will store the ANOVA results in the
#object pop.aov1:

pop.aov1 <- aov(PopRate~Brand+Time+Brand:Time)

#Use the summary() command to obtain the ANOVA table:
summary(pop.aov1)
##             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
require(graphics)

# interaction plots
attach(popcorn)
## The following object is masked _by_ .GlobalEnv:
## 
##     Time1
## The following objects are masked from popcorn (pos = 3):
## 
##     Brand, Microwave, PopRate, Time, Unpopped (percent)
interaction.plot(Brand, Time1, PopRate)