#Question 3.23

#Null Hypothesis (H0):μ1=μ2=μ3=μ4
#Alternative Hypothesis (Ha):At least one of the μ1,μ2,μ3,μ4 differs.
Fluid_type1<-c(17.6,18.9,16.3,17.4,20.1,21.6)
Fluid_type2<-c(16.9,15.3,18.6,17.1,19.5,20.3)
Fluid_type3<-c(21.4,23.6,19.4,18.5,20.5,22.3)
Fluid_type4<-c(19.3,21.1,16.9,17.5,18.3,19.8)
Fluid_type<-c(Fluid_type1,Fluid_type2,Fluid_type3,Fluid_type4)
group1 <- as.factor(c(rep(1,6),rep(2,6),rep(3,6),rep(4,6)))
Fluid_life<- as.factor(cbind(Fluid_type,group1))
test1<-aov(Fluid_type~group1)
summary(test1)
##             Df Sum Sq Mean Sq F value Pr(>F)  
## group1       3  30.17   10.05   3.047 0.0525 .
## Residuals   20  65.99    3.30                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
μ1<-mean(Fluid_type1)
μ2<-mean(Fluid_type2)
μ3<-mean(Fluid_type3)
μ4<-mean(Fluid_type4)
plot(test1)

#(a) Is there any indication that the fluids differ? Use α = 0.05. #Answer: p-value(0.0525) > 0.05 , so we fail to reject null hypothesis. #and there is no differ in the fluids

#(b) Which fluid would you select, given that the objective is long life? #Answer:The mean life of fluid_type 3 is greater .so i would choose this.

#(c) Analyze the residuals from this experiment. Are the basic analysis of #variance assumptions satisfied? #Answer:By Checking both the assumption of normality and constant variance #therefore basic analysis of variance assumptions satisfied.

#Question 3.28

#(a)
Material1<- c(110,157,194,178)
Material2<- c(1,2,4,18)
Material3<- c(880,1256,5276,4355)
Material4<- c(495,7040,5307,10050)
Material5<- c(7,5,29,2)
Material<- c(Material1,Material2,Material3,Material4,Material5)
group2 <- as.factor(c(rep(1,4),rep(2,4),rep(3,4),rep(4,4),rep(5,4)))
FT <- as.data.frame(cbind(group2,Material))
test2 <- aov(Material~group2)
summary(FT)
##      group2     Material      
##  Min.   :1   Min.   :    1.0  
##  1st Qu.:2   1st Qu.:    6.5  
##  Median :3   Median :  167.5  
##  Mean   :3   Mean   : 1768.3  
##  3rd Qu.:4   3rd Qu.: 2030.8  
##  Max.   :5   Max.   :10050.0
#(b)
plot(test2)

library(MASS)
boxcox(Material~group2)           ## Lambda(λ)=0

Materialtest<- log(Material)      ## Here we are using  log transformation
test3 <- aov(Material~group2)
summary(test3)
##             Df    Sum Sq  Mean Sq F value  Pr(>F)   
## group2       4 103191489 25797872   6.191 0.00379 **
## Residuals   15  62505657  4167044                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(test3)

boxplot(Materialtest~group2)

#(a) Do all five materials have the same effect on mean failure time? #Answer: The p-value < 0.05, we reject the Null Hypothesis(H0).

#(b) Plot the residuals versus the predicted response. Construct a normal #probability plot of the residuals.What information is conveyed by these plots? #Answer: By observing plot of Test 2 data is neither normal or have constant variance

#(c) Based on your answer to part (b) conduct another analysis of the failure time data and draw appropriate conclusions. #Answer: p-value < .05, we reject the Null Hypothesis(H0). #By observing plot of Test 3 data is approximately normal and doe not have constant variance #From the box plot is equal to material 1 has least sd of all the materials.

#(a)
Method1<-c(31,10,21,4,1)
Method2<-c(62,40,24,30,35)
Method3<-c(53,27,120,97,68)
Method <-c(Method1,Method2,Method3)
group3 <- as.factor(c(rep(1,5),rep(2,5),rep(3,5)))
Method_count<- as.data.frame(cbind(Method,group3))
Method_count<- aov(Method~group3)
summary(Method_count)
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## group3       2   8964    4482   7.914 0.00643 **
## Residuals   12   6796     566                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#(b)

plot(Method_count)

#(c)

library(MASS)
boxcox(Method~group3)

# λ=0.5

trans_count <- Method^0.5 
qqnorm(trans_count)

plot(trans_count~group3)

kruskal.test(Method~group3)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  Method by group3
## Kruskal-Wallis chi-squared = 8.54, df = 2, p-value = 0.01398

#(a) Do all methods have the same effect on mean particle count? #Answer:The p-value <0.05, we reject the Null Hypothesis(H0).

#(b)Plot the residuals versus the predicted response. Construct a normal #probability plot of the residuals. Are there potential concerns about the #validity of the assumptions? #Answer:From observed plot we conclude that data is approximately normal and unequal variance

#(c)Based on your answer to part (b) conduct another analysis of the particle #count data and draw appropriate conclusions. #Answer:After transformation of the data, we have unequal variances. Therefore #it is better to use non parametric test.

#Question 3.51 & 3.52

kruskal.test(Fluid_type~group1)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  Fluid_type by group1
## Kruskal-Wallis chi-squared = 6.2177, df = 3, p-value = 0.1015

#Question 3.51 & 3.52 #Use the Kruskal–Wallis test for the experiment in Problem 3.23. Compare the #conclusions obtained with those from the usual analysis of variance.

The p-value = 0.1015 > 0.05 = α, so we cannot reject the null hypothesis