Question 7.12

library(GAD)
## Loading required package: matrixStats
## Loading required package: R.methodsS3
## R.methodsS3 v1.8.2 (2022-06-13 22:00:14 UTC) successfully loaded. See ?R.methodsS3 for help.
length<-c(-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1)
type<-c(-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1)
brk<-c(-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,1,1,1,1)
sp<-c(-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1,1)
I<-c(10,0,4,0,0,5,6.5,16.5,4.5,19.5,15,41.5,8,21.5,0,18)
II<-c(18,16.5,6,10,0,20.5,18.5,4.5,18,18,16,39,4.5,10.5,0,5)
III<-c(14,4.5,1,34,18.5,18,7.5,0,14.5,16,8.5,6.5,6.5,6.5,0,7)
IV<-c(12.5,17.5,14.5,11,19.5,20,6,23.5,10,5.5,0,3.5,10,0,4.5,10)
V<-c(19,20.5,12,25.5,16,29.5,0,8,0,10,0.5,7,13,15.5,1,32.5)
VI<-c(16,17.5,14,21.5,15,19,10,8,17.5,7,9,8.5,41,24,4,18.5)
VII<-c(18.5,33,5,0,11,10,0,8,6,36,3,36,14,16,6.5,8)
Obs<-c(I,II,III,IV,V,VI,VII)

block<-as.fixed(rep(seq(1,7),16))
length<-as.fixed(length)
type<-as.fixed(type)
brk<-as.fixed(brk)
sp<-as.fixed(sp)

data<-cbind.data.frame(length,type,brk,sp,Obs,block)
model<-lm(Obs~length*type*brk*sp+block,data=data)
model1<-aov(model)
summary(model1)
##                    Df Sum Sq Mean Sq F value  Pr(>F)   
## length              1    917   917.1  10.424 0.00174 **
## type                1    388   388.1   4.412 0.03849 * 
## brk                 1    145   145.1   1.650 0.20230   
## sp                  1      1     1.4   0.016 0.90007   
## block               6    397    66.2   0.752 0.60905   
## length:type         1    219   218.7   2.485 0.11841   
## length:brk          1     12    11.9   0.135 0.71397   
## type:brk            1    115   115.0   1.307 0.25592   
## length:sp           1     94    93.8   1.066 0.30458   
## type:sp             1     56    56.4   0.641 0.42532   
## brk:sp              1      2     1.6   0.018 0.89213   
## length:type:brk     1      7     7.3   0.082 0.77470   
## length:type:sp      1    113   113.0   1.284 0.26010   
## length:brk:sp       1     39    39.5   0.449 0.50464   
## type:brk:sp         1     34    33.8   0.384 0.53713   
## length:type:brk:sp  1     96    95.6   1.087 0.29991   
## Residuals          90   7919    88.0                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#we can see that the p-values are not significant but except the lp(0.00174) and tp(0.03849),because
#our alpha value is 0.05. Hence, run those two lp and tp effects.

model2<-lm(Obs~length+type+brk+sp+block,data = data)
model3<-aov(model2)
summary(model3)
##              Df Sum Sq Mean Sq F value  Pr(>F)   
## length        1    917   917.1  10.641 0.00151 **
## type          1    388   388.1   4.503 0.03627 * 
## brk           1    145   145.1   1.684 0.19735   
## sp            1      1     1.4   0.016 0.89902   
## block         6    397    66.2   0.768 0.59667   
## Residuals   101   8705    86.2                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#we can see that after running lp and tp significant factors, we get lp(0.00151)and tp(0.03627).
#which indicates that they are significant.

#normal probability plot
plot(model1)

#residual plot
plot(model3)

######Therefore we reject the null hypothesis, because all the P-Values of length and type are not significant.

Question 7.20

#To confound on ABCE and ABDF,CDEF creating,

Block1 <- c('a','b','cd','abcd','ace','bce','de','abde','cf','abcf','adf','bdf','ef','abef','acdef','bcdef')
Block2 <- c('c','abc','ad','bd','e','abe','acde','bcde','af','bf','cdf','abcdf','acef','bcef','def','abdef')
Block3 <- c('ac','bc','d','abd','ae','be','cde','abcde','f','abf','acdf','bcdf','cef','abcef','adef','bdef')
Block4 <- c('-1','ab','acd','bcd','ce','abce','ade','bde','acf','bcf','df','abdf','aef','bef','cdef','abcdef')

Question 7.21

#An eight block design confounded on ABCD,ACE, ABEF:
block1 <- c('b','acd','ce','abde','abcf','df','aef','bcdef')
block2 <- c('abc','d','ae','bcde','bf','acdf','cef','abdef')
block3 <- c('a','bcd','abce','de','cf','abdf','bef','acdef')
block4 <- c('c','abd','be','acde','af','bcdf','abcef','def')
block5 <- c('ac','bd','abe','cde','f','abcdf','bcef','adef')
block6 <- c('-1','abcd','bce','ade','acf','bdf','abef','cdef')
block7 <- c('bc','ad','e','abcde','abf','cdf','acef','bdef')
block8 <- c('ab','cd','ace','bde','bcf','adf','ef','abcdef')