setwd("C:/Users/nazan/Desktop/ANLY510/Weekly Lab datafiles-20180511")
library(readxl)
WeeklyLab4Data <- read_excel("WeeklyLab4Data.xlsx")
View(WeeklyLab4Data)
data=WeeklyLab4Data
data
## # A tibble: 54 x 5
##    RunningTemperature StorageTemperature Formulation Machine LifeinHours
##    <chr>              <chr>              <chr>       <chr>         <dbl>
##  1 high               high               F1          M1               25
##  2 high               high               F1          M3               16
##  3 high               high               F1          M2               20
##  4 low                high               F1          M2               27
##  5 low                high               F1          M3               24
##  6 low                high               F1          M1               26
##  7 med                high               F1          M2               26
##  8 med                high               F1          M3               26
##  9 med                high               F1          M1               20
## 10 high               low                F1          M2               24
## # ... with 44 more rows
plot(density(data$LifeinHours)) ###Take a look at data and see how it is distributed

It looks data is distributed normally but lets check it by running agostino test

library(moments)
agostino.test(data$LifeinHours) ###P-value is larger than alpha==> fail to reject null hypothesis.(no sign of skew)
## 
##  D'Agostino skewness test
## 
## data:  data$LifeinHours
## skew = 0.49196, z = 1.57380, p-value = 0.1155
## alternative hypothesis: data have a skewness

Take a look at variables to see if they are defined correctly(To check equality of variances across factors we need them as factor)

library(dplyr)
## 
## 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
glimpse(data)
## Observations: 54
## Variables: 5
## $ RunningTemperature <chr> "high", "high", "high", "low", "low", "low"...
## $ StorageTemperature <chr> "high", "high", "high", "high", "high", "hi...
## $ Formulation        <chr> "F1", "F1", "F1", "F1", "F1", "F1", "F1", "...
## $ Machine            <chr> "M1", "M3", "M2", "M2", "M3", "M1", "M2", "...
## $ LifeinHours        <dbl> 25, 16, 20, 27, 24, 26, 26, 26, 20, 24, 24,...

Checking variances(Chaeck if they are equal across factors(The test has the null hypothesis that the variances are equal and the alterntive hypothesis that they are not equal,P-values are larger than alpha==>Fail to reject null hypothesis==>Variances are equal))

bartlett.test(data$LifeinHours~data$RunningTemperature)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  data$LifeinHours by data$RunningTemperature
## Bartlett's K-squared = 5.3959, df = 2, p-value = 0.06734
bartlett.test(data$LifeinHours~data$StorageTemperature)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  data$LifeinHours by data$StorageTemperature
## Bartlett's K-squared = 3.9424, df = 2, p-value = 0.1393
bartlett.test(data$LifeinHours~data$Formulation)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  data$LifeinHours by data$Formulation
## Bartlett's K-squared = 0.7178, df = 1, p-value = 0.3969
bartlett.test(data$LifeinHours~data$Machine)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  data$LifeinHours by data$Machine
## Bartlett's K-squared = 0.57663, df = 2, p-value = 0.7495

Looks everythings are what we want==>Run ANOVA(including machines as a blocking factor)

model=aov(LifeinHours~Machine+Formulation*StorageTemperature*RunningTemperature,data=data)
model
## Call:
##    aov(formula = LifeinHours ~ Machine + Formulation * StorageTemperature * 
##     RunningTemperature, data = data)
## 
## Terms:
##                  Machine Formulation StorageTemperature RunningTemperature
## Sum of Squares   64.0370    362.9630           585.4815           770.2593
## Deg. of Freedom        2           1                  2                  2
##                 Formulation:StorageTemperature
## Sum of Squares                          4.5926
## Deg. of Freedom                              2
##                 Formulation:RunningTemperature
## Sum of Squares                          7.1481
## Deg. of Freedom                              2
##                 StorageTemperature:RunningTemperature
## Sum of Squares                               201.6296
## Deg. of Freedom                                     4
##                 Formulation:StorageTemperature:RunningTemperature
## Sum of Squares                                             0.9630
## Deg. of Freedom                                                 4
##                 Residuals
## Sum of Squares   474.6296
## Deg. of Freedom        34
## 
## Residual standard error: 3.736268
## Estimated effects may be unbalanced
summary(model) ###It seems all main effects are significant and also interaction of StorageTemperature:RunningTemperature(For the formulas main effect we get the means)
##                                                   Df Sum Sq Mean Sq
## Machine                                            2   64.0    32.0
## Formulation                                        1  363.0   363.0
## StorageTemperature                                 2  585.5   292.7
## RunningTemperature                                 2  770.3   385.1
## Formulation:StorageTemperature                     2    4.6     2.3
## Formulation:RunningTemperature                     2    7.1     3.6
## StorageTemperature:RunningTemperature              4  201.6    50.4
## Formulation:StorageTemperature:RunningTemperature  4    1.0     0.2
## Residuals                                         34  474.6    14.0
##                                                   F value   Pr(>F)    
## Machine                                             2.294   0.1163    
## Formulation                                        26.001 1.28e-05 ***
## StorageTemperature                                 20.970 1.17e-06 ***
## RunningTemperature                                 27.589 7.60e-08 ***
## Formulation:StorageTemperature                      0.164   0.8490    
## Formulation:RunningTemperature                      0.256   0.7756    
## StorageTemperature:RunningTemperature               3.611   0.0148 *  
## Formulation:StorageTemperature:RunningTemperature   0.017   0.9994    
## Residuals                                                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
tapply(data$LifeinHours, data$Formulation, mean) ###F2 is largers that means lasts longer.
##       F1       F2 
## 27.48148 32.66667

look at the effects of running and storage temperature.(Doing Tukey test to look at the temperature effects)

TukeyHSD(model, c("StorageTemperature", "RunningTemperature")) ###Low temperature leads to long life hour in both cases.
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = LifeinHours ~ Machine + Formulation * StorageTemperature * RunningTemperature, data = data)
## 
## $StorageTemperature
##               diff       lwr         upr     p adj
## low-high  8.000000  4.948172 11.05182766 0.0000007
## med-high  4.888889  1.837061  7.94071654 0.0011388
## med-low  -3.111111 -6.162939 -0.05928346 0.0449486
## 
## $RunningTemperature
##               diff       lwr       upr     p adj
## low-high  9.111111  6.059283 12.162939 0.0000001
## med-high  5.944444  2.892617  8.996272 0.0000981
## med-low  -3.166667 -6.218494 -0.114839 0.0406306

To see the interactions

library(lsmeans)
## The 'lsmeans' package is being deprecated.
## Users are encouraged to switch to 'emmeans'.
## See help('transition') for more information, including how
## to convert 'lsmeans' objects and scripts to work with 'emmeans'.
lsmip(model, StorageTemperature~RunningTemperature) ###NOTE: Results may be misleading due to involvement in interactions(Levels of running temerature and storage temperature are not in order)
## NOTE: Results may be misleading due to involvement in interactions

Defining order for levels of the factors

data$RunningTemperature=factor(data$RunningTemperature, levels =  c("high","med","low"))
data$RunningTemperature
##  [1] high high high low  low  low  med  med  med  high high high low  low 
## [15] low  med  med  med  high high high low  low  low  med  med  med  high
## [29] high high low  low  low  med  med  med  high high high low  low  low 
## [43] med  med  med  high high high low  low  low  med  med  med 
## Levels: high med low
data$StorageTemperature=factor(data$StorageTemperature, levels =  c("high","med","low"))
data$StorageTemperature
##  [1] high high high high high high high high high low  low  low  low  low 
## [15] low  low  low  low  med  med  med  med  med  med  med  med  med  high
## [29] high high high high high high high high low  low  low  low  low  low 
## [43] low  low  low  med  med  med  med  med  med  med  med  med 
## Levels: high med low
lsmip(model,StorageTemperature~RunningTemperature) ###There is two interactions.One from High to med and secondonr from med to low(running temperature for med and low storage temperatures)
## NOTE: Results may be misleading due to involvement in interactions

TukeyHSD(model, "StorageTemperature:RunningTemperature") ###Confirmation of Low Temperature is better
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = LifeinHours ~ Machine + Formulation * StorageTemperature * RunningTemperature, data = data)
## 
## $`StorageTemperature:RunningTemperature`
##                           diff         lwr        upr     p adj
## low:high-high:high   5.0000000  -2.1378038 12.1378038 0.3600327
## med:high-high:high   2.6666667  -4.4711372  9.8044705 0.9422793
## high:low-high:high   5.8333333  -1.3044705 12.9711372 0.1841865
## low:low-high:high   18.8333333  11.6955295 25.9711372 0.0000000
## med:low-high:high   10.3333333   3.1955295 17.4711372 0.0009592
## high:med-high:high   4.0000000  -3.1378038 11.1378038 0.6475577
## low:med-high:high   10.0000000   2.8621962 17.1378038 0.0014913
## med:med-high:high   11.5000000   4.3621962 18.6378038 0.0001998
## med:high-low:high   -2.3333333  -9.4711372  4.8044705 0.9730950
## high:low-low:high    0.8333333  -6.3044705  7.9711372 0.9999817
## low:low-low:high    13.8333333   6.6955295 20.9711372 0.0000083
## med:low-low:high     5.3333333  -1.8044705 12.4711372 0.2803362
## high:med-low:high   -1.0000000  -8.1378038  6.1378038 0.9999261
## low:med-low:high     5.0000000  -2.1378038 12.1378038 0.3600327
## med:med-low:high     6.5000000  -0.6378038 13.6378038 0.0977980
## high:low-med:high    3.1666667  -3.9711372 10.3044705 0.8621541
## low:low-med:high    16.1666667   9.0288628 23.3044705 0.0000004
## med:low-med:high     7.6666667   0.5288628 14.8044705 0.0275519
## high:med-med:high    1.3333333  -5.8044705  8.4711372 0.9993722
## low:med-med:high     7.3333333   0.1955295 14.4711372 0.0402654
## med:med-med:high     8.8333333   1.6955295 15.9711372 0.0067147
## low:low-high:low    13.0000000   5.8621962 20.1378038 0.0000259
## med:low-high:low     4.5000000  -2.6378038 11.6378038 0.4986512
## high:med-high:low   -1.8333333  -8.9711372  5.3044705 0.9941701
## low:med-high:low     4.1666667  -2.9711372 11.3044705 0.5979620
## med:med-high:low     5.6666667  -1.4711372 12.8044705 0.2130636
## med:low-low:low     -8.5000000 -15.6378038 -1.3621962 0.0101667
## high:med-low:low   -14.8333333 -21.9711372 -7.6955295 0.0000021
## low:med-low:low     -8.8333333 -15.9711372 -1.6955295 0.0067147
## med:med-low:low     -7.3333333 -14.4711372 -0.1955295 0.0402654
## high:med-med:low    -6.3333333 -13.4711372  0.8044705 0.1153824
## low:med-med:low     -0.3333333  -7.4711372  6.8044705 1.0000000
## med:med-med:low      1.1666667  -5.9711372  8.3044705 0.9997649
## low:med-high:med     6.0000000  -1.1378038 13.1378038 0.1583827
## med:med-high:med     7.5000000   0.3621962 14.6378038 0.0333593
## med:med-low:med      1.5000000  -5.6378038  8.6378038 0.9985376
pairwise.t.test(data$LifeinHours,data$StorageTemperature:data$RunningTemperature, p.adj ="none" )
## 
##  Pairwise comparisons using t tests with pooled SD 
## 
## data:  data$LifeinHours and data$StorageTemperature:data$RunningTemperature 
## 
##          high:high high:med high:low med:high med:med med:low low:high
## high:med 0.13129   -        -        -        -       -       -       
## high:low 0.02997   0.48478  -        -        -       -       -       
## med:high 0.31100   0.61092  0.23003  -        -       -       -       
## med:med  6.2e-05   0.00604  0.03473  0.00145  -       -       -       
## med:low  0.00026   0.01898  0.09064  0.00508  0.65609 -       -       
## low:high 0.06105   0.70260  0.75029  0.37471  0.01623 0.04629 -       
## low:med  0.00038   0.02580  0.11636  0.00716  0.56723 0.89865 0.06105 
## low:low  4.5e-09   8.7e-07  9.3e-06  1.5e-07  0.00716 0.00209 3.2e-06 
##          low:med
## high:med -      
## high:low -      
## med:high -      
## med:med  -      
## med:low  -      
## low:high -      
## low:med  -      
## low:low  0.00145
## 
## P value adjustment method: none

We can see how much difference is between pvalues by comparing pvalues in Tukey test for model wich we had correction and Pairwise.t,test,without correction.

By looking at contrasts, we figured it out the lower temperature leads to longer life hour. To see if the effect is significant run the command below

aggregate(LifeinHours ~ RunningTemperature + StorageTemperature,data = data, FUN = "mean")
##   RunningTemperature StorageTemperature LifeinHours
## 1               high               high    22.50000
## 2                med               high    26.50000
## 3                low               high    28.33333
## 4               high                med    25.16667
## 5                med                med    34.00000
## 6                low                med    32.83333
## 7               high                low    27.50000
## 8                med                low    32.50000
## 9                low                low    41.33333

lower storage and running temperatures do lead to longer running life in general