#Question 3.7 (c,d,e,f)
MT_1<-c(3129,3000,2865,2890)
MT_2<-c(3200,3300,2975,3150)
MT_3<-c(2800,2900,2985,3050)
MT_4<-c(2600,2700,2600,2765)
dat<-data.frame(MT_1,MT_2,MT_3,MT_4)
#(C) Use the Fisher LSD method with alpha = 0.05 to make comparisons between pairs
#of means.
#Null Hypothesis (H0): u1=u2=u3=u4
#Alternative Hypothesis (Ha): At least one of the means is different from the others.
#Means of u1,u2,u3,u4
u1=mean(MT_1)
u2=mean(MT_2)
u3=mean(MT_3)
u4=mean(MT_4)
#anova test
Test_1<-aov(values ~ ind,data=(stack(dat)))
summary(Test_1)
## Df Sum Sq Mean Sq F value Pr(>F)
## ind 3 489740 163247 12.73 0.000489 ***
## Residuals 12 153908 12826
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#The **T-critical value for α = 0.05**, DFW =12 is 2.179 T alpha/2,Tn-a
#The T-value is taken from the table
T <- c(2.179)
#LSD-Least Significant Difference
#Lsd.test <- c(T*sqrt(2*MSE/4))
Lsd.test <- c(T*sqrt(2*12826/4))
#comparisons between pairs of means.
#|u1 - u2| = |2971−3156.25| = 185.25
#|u1 - u3| = |2971−2933.75| = 37.25
#|u1 - u4| = |2971−2933.75| = 304.75
#|u2 - u3| = |3156.25−2933.75| = 222.25
#|u2 - u4| = |3156.25−2666.25| = 490.00
#|u3 - u4| = |2933.75−2666.25| = 267.50
#As the p-value=0.00489 < 0.05, **we Reject Null Hypothesis(H0) and conclude that at least
#one of the group means is different from the others**.
#Question 3.7 (d)
#Construct a normal probability plot of the residuals. What conclusion would you draw about the validity of
#the normality assumption?
Test_1<-aov(values ~ ind,data=(stack(dat)))
plot(Test_1, 2)

#Answer: from the plot we can conclude that the probability of residuals is normally distributed.
#Question 3.7 (e)
#Plot the residuals versus the predicted tensile strength.Comment on the plot.
Test_1<-aov(values ~ ind,data=(stack(dat)))
plot(Test_1, 1)

#Answer: From the plot we can conclude that residuals tensile strength data has constant variance.
#Question 3.7 (f)
#Prepare a scatter plot of the results to aid the interpretation of the results of this experiment.
stripchart(values ~ ind,data=stack(dat), vertical = TRUE,col = 'Black', pch = 20)

#Question 3.10
#A product developer is investigating the tensile strength of a new synthetic fiber that will be used to make cloth
#for men's shirts. Strength is usually affected by the percentage of cotton used in the blend of materials for the
#fiber. The engineer conducts a completely randomized experiment with five levels of cotton content and replicates
#the experiment five times. The data are shown in the following table.
percent_15 <- c(7,7,15,11,9)
percent_20 <- c(12,17,12,18,18)
percent_25 <- c(14,19,19,18,18)
percent_30 <- c(19,25,22,19,23)
percent_35 <- c(7,10,11,15,11)
data<-data.frame(percent_15,percent_20,percent_25,percent_30,percent_35)
y1=mean(percent_15)
y2=mean(percent_20)
y3=mean(percent_25)
y4=mean(percent_30)
y5=mean(percent_35)
#Question 3.10 (b)
#) Use the Fisher LSD method to make comparisons between the pairs of means. What conclusions can you draw?
Test_2 <- aov(values ~ ind,data=(stack(data)))
summary(Test_2)
## Df Sum Sq Mean Sq F value Pr(>F)
## ind 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
#The t-critical value for α = 0.05, dfw =20 is 2.086 T alpha/2,Tn-a
#The T-value is taken from the table
T1<-2.086
# for the lsd test
Lsd.test1 <- c(T1*sqrt(2*8.06/5))
#Answer the LSD value for Test 1 is 3.75
#comparisons between pairs of means.
#|y1 - y2| = |9.8−15.4| = 5.6 >3.75
#|y1 - y3| = |9.8−17.6| = 7.8 >3.75
#|y1 - y4| = |9.8−21.6| = 11.8 >3.75
#|y1 - y4| = |9.8-10.8| = 1.0 <3.75
#|y2 - y3| = |15.4−17.6| = 2.2 <3.75
#|y2 - y4| = |15.4−21.6| = 6.2 >3.75
#|y2 - y5| = |15.4-10.8| = 4.6 >3.75
#|y3 - y4| = |17.6-21.6| = 4.0 >3.75
#|y3 - y5| = |17.6-10.8| = 6.8 >3.75
#|y4 - y5| = |21.6-10.8| = 10.8>3.75
#As the p-value=0.00489 < 0.05, **we Reject Null Hypothesis(H0) and conclude that at least
#one of the group means is different from the others**.
#Question 3.10 (C)
#Analyze the residuals from this experiment and comment on model adequacy.
Test_2<-aov(values ~ ind,data=(stack(data)))
plot(Test_2, 2)

Test_2<-aov(values ~ ind,data=(stack(data)))
plot(Test_2, 1)

# Answer: From the plot we can conclude that the probability of residuals is normally distributed.
#--------------------------------------------------------------------------------------------------------------------
#Question 3.44
#Suppose that four normal populations have means of u1=50,u2=60,u3=50, and u4=60. How many observations
#should be taken from each population so that the probability of rejecting the null hypothesis of equal
#population means is at least 0.90? Assume that alpha =0.05 and that a reasonable estimate of the error
#variance is sigma square = 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
# Answer : From the test we got n = 5, 5 samples need to be selected from each group.
#Question 3.45 (a)
# How would your answer change if a reasonable estimate of the experimental error variance were sigma square 36?
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 : From the test we got n = 7, 7 samples need to be selected from each group.
#Question 3.45 (b)
#How would your answer change if a reasonable estimate of the experimental error variance were sigma square 49?
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
# Answer : From the test we got n = 8, 8 samples need to be selected from each group.
#Question 3.45 (c)
#Can you draw any conclusions about the sensitivity of your answer in this particular situation about how
#your estimate of Sigma affects the decision about sample size?
# Answer : Here, with the variance we require more samples need to be collected from each group increases.
#Question 3.45 (d)
# Can you make any recommendations about how we should use this general approach to choosing n in practice?
# Answer : If we get the variance value, we can get to know how the select the no. of samples to be collected in each group.