#Question 3.7

#NULL HYPOTHESES: All means are same
#ALTERNATE HYPOTHESES: Atleast one mean is different

#install.packages("agricolae")
library(agricolae)
#importing the data

#answer c

t1<-c(3129, 3000, 2865, 2890)
t2<-c(3200, 3300, 2975, 3150)
t3<-c(2800, 2900, 2985, 3050)
t4<-c(2600, 2700, 2600, 2765)
s<-c(t1,t2,t3,t4)
tec<-c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4)
d<-cbind(tec,s)
df<-as.data.frame(d)
df$tec<-as.factor(df$tec)
aov.model<-aov(s~tec,data = df)
summary(aov.model)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## tec          3 489740  163247   12.73 0.000489 ***
## Residuals   12 153908   12826                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#As P value is less than alpha we reject Null Hypotheses
#question 3.44
#LSD

lsd<-LSD.test(s,tec,12,12836,console = TRUE)
## 
## Study: s ~ tec
## 
## LSD t Test for s 
## 
## Mean Square Error:  12836 
## 
## tec,  means and individual ( 95 %) CI
## 
##         s       std r      LCL      UCL  Min  Max
## 1 2971.00 120.55704 4 2847.575 3094.425 2865 3129
## 2 3156.25 135.97641 4 3032.825 3279.675 2975 3300
## 3 2933.75 108.27242 4 2810.325 3057.175 2800 3050
## 4 2666.25  80.97067 4 2542.825 2789.675 2600 2765
## 
## Alpha: 0.05 ; DF Error: 12
## Critical Value of t: 2.178813 
## 
## least Significant Difference: 174.55 
## 
## Treatments with the same letter are not significantly different.
## 
##         s groups
## 2 3156.25      a
## 1 2971.00      b
## 3 2933.75      b
## 4 2666.25      c
plot(lsd)

#answer d

meanstr<-c(rep(mean(t1),4),rep(mean(t2),4),rep(mean(t3),4),rep(mean(t4),4))
residual<-s-meanstr
qqnorm(residual)

#from normal probability we can say that given data is normal
plot(meanstr,residual, xlab="tensile strength",ylab="residual",main="variance")

#answer e
#from the plot we can conclude that there is not much variation in data. so we can say that variance for different technique is equal

#answer f
#install.packages("car",dependencies = TRUE)
library(car)
## Loading required package: carData
scatterplot(s~tec)

#Question 3.10

#NULL HYPOTHESIS:H0:MU1=MU2=MU3=MU4
#ALTERNATE HYPOTHESIS:H1:atleast one of the mean is different

#Question 3.10 b,c

P1 <- c(7, 7, 15, 11, 9)
P2 <- c(12, 17, 12, 18, 18)
P3 <- c(14, 19, 19, 18, 18)
P4 <- c(19, 25, 22, 19, 23)
P5 <- c(7, 10, 11, 15, 11)
s<-c(P1,P2,P3,P4,P5)
g<-c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
d<-cbind(g,s)
df<-as.data.frame(d)
df$g<-as.factor(df$g)
aov.model<-aov(s~g,data = df)
summary(aov.model)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## g            4  475.8  118.94   14.76 9.13e-06 ***
## Residuals   20  161.2    8.06                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Answer b
#the fisher LSDmethod states that pair mean of 2 and 3 are slightly equal as wellas pair mean of 1 and 5

library(agricolae)
lsd<-LSD.test(s,g,20,8.06,console = TRUE)
## 
## Study: s ~ g
## 
## LSD t Test for s 
## 
## Mean Square Error:  8.06 
## 
## g,  means and individual ( 95 %) CI
## 
##      s      std r       LCL      UCL Min Max
## 1  9.8 3.346640 5  7.151566 12.44843   7  15
## 2 15.4 3.130495 5 12.751566 18.04843  12  18
## 3 17.6 2.073644 5 14.951566 20.24843  14  19
## 4 21.6 2.607681 5 18.951566 24.24843  19  25
## 5 10.8 2.863564 5  8.151566 13.44843   7  15
## 
## Alpha: 0.05 ; DF Error: 20
## Critical Value of t: 2.085963 
## 
## least Significant Difference: 3.745452 
## 
## Treatments with the same letter are not significantly different.
## 
##      s groups
## 4 21.6      a
## 3 17.6      b
## 2 15.4      b
## 5 10.8      c
## 1  9.8      c
plot(lsd)

#answer c
#Normal probability plot of residuals is almost very close to normality as the data lies in single line


plot(aov.model)

#Question 3.44

#how many observations must be taken

#given data
p1=50
p2=60
p3=50
p4=60
v=25

library(pwr)

pwr.anova.test(k=4,n=NULL, f=sqrt((5)^2/25),sig.level = 0.05,power=0.90)
## 
##      Balanced one-way analysis of variance power calculation 
## 
##               k = 4
##               n = 4.658119
##               f = 1
##       sig.level = 0.05
##           power = 0.9
## 
## NOTE: n is number in each group
#Therefore by the power test number of observations to be selected from each populations is approximately equal to 5

#Question 3.45

#given data
p1=50
p2=60
p3=50
p4=60
v=36
library(pwr)
pwr.anova.test(k=4, n=NULL,f=sqrt((5)^2/36),sig.level = 0.05,power=0.90)
## 
##      Balanced one-way analysis of variance power calculation 
## 
##               k = 4
##               n = 6.180857
##               f = 0.8333333
##       sig.level = 0.05
##           power = 0.9
## 
## NOTE: n is number in each group
#Answer a

#Therefore by the power test number of observations to be selected from each populations is approximately equal to 7

#Answer b

#given data

p1-50
## [1] 0
p2=60
p3=50
p4=60
v=49
library(pwr)
pwr.anova.test(k=4,n=NULL,f=sqrt((5)^2/49),sig.level=0.05,power=0.90)
## 
##      Balanced one-way analysis of variance power calculation 
## 
##               k = 4
##               n = 7.998751
##               f = 0.7142857
##       sig.level = 0.05
##           power = 0.9
## 
## NOTE: n is number in each group
#Therefore by the power test number of observations to be selected from each populations is approximately equal to 8

#Answer c

#As the variance increases number of samples to be collected from each population also increases inorder to obtain the results with power level

#Answer d

# In General, when an experiment has to be set up and samples have to be collected, it would be a better approach to determine the upper and lower limits of variance and then make the best possible estimate of number of samples to be collected.