Question 3.23

#Null Hypothesis H0:mu1=mu2=mu3=mu4=mu
#Alternate Hypothesis HA:atleast one mui differs

#Reading the data

library(agricolae)
library(tidyr)
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
type1 <- c(17.6, 18.9, 16.3, 17.4, 20.1, 21.6)
type2 <- c(16.9, 15.3, 18.6, 17.1, 19.5, 20.3)
type3 <- c(21.4, 23.6, 19.4, 18.5, 20.5, 22.3)
type4 <- c(19.3, 21.1, 16.9, 17.5, 18.3, 19.8)

fluid <- data.frame(type1,type2,type3,type4)

fluid <- pivot_longer(data = fluid, c(type1,type2,type3,type4))

#Question A
#Checking if we can reject null hypothesis or not,by Performing ANOVA test

aov.fluid <- aov(value~name, data=fluid)
summary(aov.fluid)
##             Df Sum Sq Mean Sq F value Pr(>F)  
## name         3  30.16   10.05   3.047 0.0525 .
## Residuals   20  65.99    3.30                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# By the ANOVA test,P value(0.052) is not smaller than alpha, we cannot reject the null hypothesis H0

#Question B

#As the P value is too close to alpha we can say that there might be a level that actually differs
print(LSD.test(aov.fluid,"name",alpha=0.05))
## $statistics
##    MSerror Df     Mean       CV  t.value      LSD
##   3.299667 20 19.09167 9.514614 2.085963 2.187666
## 
## $parameters
##         test p.ajusted name.t ntr alpha
##   Fisher-LSD      none   name   4  0.05
## 
## $means
##          value      std r      LCL      UCL  Min  Max    Q25   Q50    Q75
## type1 18.65000 1.952178 6 17.10309 20.19691 16.3 21.6 17.450 18.25 19.800
## type2 17.95000 1.854454 6 16.40309 19.49691 15.3 20.3 16.950 17.85 19.275
## type3 20.95000 1.879096 6 19.40309 22.49691 18.5 23.6 19.675 20.95 22.075
## type4 18.81667 1.554885 6 17.26975 20.36358 16.9 21.1 17.700 18.80 19.675
## 
## $comparison
## NULL
## 
## $groups
##          value groups
## type3 20.95000      a
## type4 18.81667     ab
## type1 18.65000      b
## type2 17.95000      b
## 
## attr(,"class")
## [1] "group"
#From the test we can say that type 3 fluid is differed

#Question C

plot(aov.fluid,1)

plot(aov.fluid,2)

#from the plot we can see that first column of data has a larger variance, as the size of the column is bigger when compared to others

Question 3.28

#Null Hypothesis H0:mu1=mu2=mu3=mu4=mu
#Alternate Hypothesis HA:atleast one mui differs

#Reading the data

library(tidyr)
library(dplyr)

M1<-c(110,157,194,178)
M2<-c(1,2,4,18)
M3<-c(880,1256,5276,4355)
M4<-c(495,7040,5307,10050)
M5<-c(7,5,29,2)
M<-data.frame(M1,M2,M3,M4,M5)
mat<-pivot_longer(data = M, c(M1,M2,M3,M4,M5))

#Question A
aov.mat<-aov(value~name, data=mat)
summary(aov.mat)
##             Df    Sum Sq  Mean Sq F value  Pr(>F)   
## name         4 103191489 25797872   6.191 0.00379 **
## Residuals   15  62505657  4167044                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#As the P value is (0.00228) which is less than alpha value, we can reject the null hypothesis

#Question B
plot(aov.mat,1)

plot(aov.mat,2)

#From the plot we can see that the data is not  normal

#Question C
kruskal.test(value~name, data=mat)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  value by name
## Kruskal-Wallis chi-squared = 16.873, df = 4, p-value = 0.002046
#As the Pvalue is 0.00424,which is less than alpha, we can successfully reject the null hypothesis

Question 3.29

#Null Hypothesis H0:mu1=mu2=mu3=mu4=mu
#Alternate Hypothesis HA:atleast one mui differs

#Reading the data

library(tidyr)
library(dplyr)
method1<-c(31, 10, 21, 4, 1)
method2<-c(62, 40, 24, 30, 35)
method3<-c(53, 27, 120, 97, 68)

method<-data.frame(method1,method2,method3)

method<-pivot_longer(data = method, c(method1,method2,method3))
#Question A
aov.method <- aov(value~name, dat=method)
summary(aov.method)
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## name         2   8964    4482   7.914 0.00643 **
## Residuals   12   6796     566                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#As the P value 0.00643 is less than the alpha we can reject the null hypothesis

#Question B
plot(aov.method,1)

plot(aov.method,2)

#As per the plot,the variance is not constant,Therefore ANOVA test is not the correct model

#Question C
#As the data is normal and variances are differed,trying to transform the data using boxcox transformation
library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
boxcox(value~name, data=method)

lambda<-0.5

method_transf<-method
method_transf$value<-method_transf$value^lambda

aov.method.transformed<-aov(value~name,data=method_transf)
plot(aov.method.transformed,1)

kruskal.test(value~name, data=method)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  value by name
## Kruskal-Wallis chi-squared = 8.54, df = 2, p-value = 0.01398
summary(aov.method.transformed)
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## name         2  63.90   31.95    9.84 0.00295 **
## Residuals   12  38.96    3.25                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#As the PValue 0.01398 is less than alpha, we can reject the null hypothesis 

Question 3.51 and 3.52

#Null Hypothesis H0:mu1=mu2=mu3=mu4=mu
#Alternate Hypothesis HA:atleast one mui differs

#Reading the data

library(agricolae)
library(tidyr)
library(dplyr)

type1 <- c(17.6, 18.9, 16.3, 17.4, 20.1, 21.6)
type2 <- c(16.9, 15.3, 18.6, 17.1, 19.5, 20.3)
type3 <- c(21.4, 23.6, 19.4, 18.5, 20.5, 22.3)
type4 <- c(19.3, 21.1, 16.9, 17.5, 18.3, 19.8)

fluid <- data.frame(type1,type2,type3,type4)

fluid <- pivot_longer(data = fluid, c(type1,type2,type3,type4))

#kruskal wallis test
kruskal.test(value~name, data=fluid)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  value by name
## Kruskal-Wallis chi-squared = 6.2177, df = 3, p-value = 0.1015
#From the kruskals test, pvalue is 0.1015 which is greater than alpha value.So, we cannot reject the null hypothesis and this indicates that fluid does not differ

#Question 3.52
#Yes,the results of the Kruskal-Wallis test do not assume normality, whereas ANOVA does. If the data did not demonstrate normality, the Kruskal-Wallis test would be more valid, but this is not the case here.