# AGIGNATURA: Experimental Designs
# Faculty: Statistical and Information Engineering
# National University of the Altiplano

library(knitr)
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
DBCA <- read_excel("DBCA.xlsx")
DBCA
## # A tibble: 16 x 3
##        y Bloque Tratamiento
##    <dbl>  <dbl>       <dbl>
##  1  2.12      1           1
##  2  2.4       2           1
##  3  2.9       3           1
##  4  4         4           1
##  5  2.4       1           2
##  6  3.5       2           2
##  7  3.8       3           2
##  8  3.9       4           2
##  9  4.5       1           3
## 10  4.12      2           3
## 11  4.5       3           3
## 12  5         4           3
## 13  2.5       1           4
## 14  2.5       2           4
## 15  2.5       3           4
## 16  3.4       4           4
head(DBCA)
## # A tibble: 6 x 3
##       y Bloque Tratamiento
##   <dbl>  <dbl>       <dbl>
## 1  2.12      1           1
## 2  2.4       2           1
## 3  2.9       3           1
## 4  4         4           1
## 5  2.4       1           2
## 6  3.5       2           2
View(DBCA)

DBCA$Tratamiento = factor(DBCA$Tratamiento)
DBCA$Bloque = factor(DBCA$Bloque)

# We use the libraries

library(daewr)
## Warning: package 'daewr' was built under R version 4.0.5
## Registered S3 method overwritten by 'DoE.base':
##   method           from       
##   factorize.factor conf.design
library(AlgDesign)
## Warning: package 'AlgDesign' was built under R version 4.0.3
# It allows to know if the design can
BIBsize(t = 4 , k = 2)
## Posible BIB design with b= 6  and r= 3  lambda= 1
# We analyze block 1
mod1 <- aov(y ~ Tratamiento + Bloque, data = DBCA )
mod1
## Call:
##    aov(formula = y ~ Tratamiento + Bloque, data = DBCA)
## 
## Terms:
##                 Tratamiento Bloque Residuals
## Sum of Squares       8.1101 3.1901    1.2885
## Deg. of Freedom           3      3         9
## 
## Residual standard error: 0.3783737
## Estimated effects may be unbalanced
summary(mod1)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## Tratamiento  3  8.110  2.7034  18.883 0.000319 ***
## Bloque       3  3.190  1.0634   7.427 0.008309 ** 
## Residuals    9  1.288  0.1432                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# We analyze block 2
mod2 <- aov(y ~ Bloque + Tratamiento , data = DBCA )
mod2
## Call:
##    aov(formula = y ~ Bloque + Tratamiento, data = DBCA)
## 
## Terms:
##                 Bloque Tratamiento Residuals
## Sum of Squares  3.1901      8.1101    1.2885
## Deg. of Freedom      3           3         9
## 
## Residual standard error: 0.3783737
## Estimated effects may be unbalanced
summary(mod2)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## Bloque       3  3.190  1.0634   7.427 0.008309 ** 
## Tratamiento  3  8.110  2.7034  18.883 0.000319 ***
## Residuals    9  1.289  0.1432                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# We analyze for all treatments
mod3 <- lm(y ~ Tratamiento + Bloque , data = DBCA )
mod3
## 
## Call:
## lm(formula = y ~ Tratamiento + Bloque, data = DBCA)
## 
## Coefficients:
##  (Intercept)  Tratamiento2  Tratamiento3  Tratamiento4       Bloque2  
##        2.357         0.545         1.675        -0.130         0.250  
##      Bloque3       Bloque4  
##        0.545         1.195
library(car)
## Warning: package 'car' was built under R version 4.0.5
## Loading required package: carData
car::Anova(mod3, type="III")
## Anova Table (Type III tests)
## 
## Response: y
##              Sum Sq Df F value    Pr(>F)    
## (Intercept) 12.7036  1 88.7326 5.871e-06 ***
## Tratamiento  8.1101  3 18.8827 0.0003188 ***
## Bloque       3.1901  3  7.4275 0.0083094 ** 
## Residuals    1.2885  9                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Valuers of Just and real
fitted(mod3)
##      1      2      3      4      5      6      7      8      9     10     11 
## 2.3575 2.6075 2.9025 3.5525 2.9025 3.1525 3.4475 4.0975 4.0325 4.2825 4.5775 
##     12     13     14     15     16 
## 5.2275 2.2275 2.4775 2.7725 3.4225
#Plot of Normality
qqPlot(mod3)
## [1] 5 9
mtext("By:Amarillas", side = 3, adj = 1, family = "mono")

bartlett.test(y~Tratamiento, data=DBCA)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  y by Tratamiento
## Bartlett's K-squared = 2.1508, df = 3, p-value = 0.5417
#Plot-Box
boxplot(y~Tratamiento ,data=DBCA, col=c("orange","green","blue","red"), main="Diagrama de Cajas de los Tratamientos")
mtext("By:Amarillas", side = 3, adj = 1, family = "mono")

#Hypothesis de Independence
layout(matrix(c(1,2,3,4),1,1))
plot(mod3)