Cuadrado de Youden

library(readxl)
## Warning: package 'readxl' was built under R version 4.0.5
Datos_Youden = Datos_Taller_Julio_16 <- read_excel("D:/Users/Usuario/Desktop/Trabajos Diseno/Datos Taller Julio 16.xlsx");Datos_Youden
## # A tibble: 56 x 4
##    Longitud Latitud Tratamiento Rendimiento
##    <chr>    <chr>   <chr>             <dbl>
##  1 1 N      74 O    D                  16.6
##  2 2 N      75 O    F                  15.9
##  3 3 N      76 O    B                  17.1
##  4 4 N      77 O    A                  17.7
##  5 5 N      78 O    C                  17.4
##  6 6 N      79 O    E                  16.5
##  7 7 N      80 O    H                  18.6
##  8 1 N      81 O    H                  16.9
##  9 2 N      74 O    E                  16.4
## 10 3 N      75 O    C                  16.8
## # ... with 46 more rows
Tratamiento=Datos_Youden$Tratamiento
Longitud=Datos_Youden$Longitud
Latitud=Datos_Youden$Latitud
Rendimiento=Datos_Youden$Rendimiento
b<-8#columnas
k<-7#filas
library(collapsibleTree)
collapsibleTreeSummary(Datos_Youden, hierarchy = c('Longitud', 'Latitud', 'Tratamiento','Rendimiento' ))
N<-xtabs(~Tratamiento + Longitud, data <- Datos_Youden);N
##            Longitud
## Tratamiento 1 N 2 N 3 N 4 N 5 N 6 N 7 N
##           A   1   1   1   1   1   1   1
##           B   1   1   1   1   1   1   1
##           C   1   1   1   1   1   1   1
##           D   1   1   1   1   1   1   1
##           E   1   1   1   1   1   1   1
##           F   1   1   1   1   1   1   1
##           G   1   1   1   1   1   1   1
##           H   1   1   1   1   1   1   1
v<-nlevels(Tratamiento);
n<-b*k;
T1<-aggregate(Datos_Youden$Rendimiento, by<-list(Trat<-Datos_Youden$Tratamiento),FUN<-sum);
Tt<-matrix(T1$x,v,1);# totales de Trat
## Warning in matrix(T1$x, v, 1): la longitud de los datos excede el tamaño de la
## matriz
T2<-aggregate(Datos_Youden$Rendimiento, by<-list(Latitud<-Datos_Youden$Latitud),FUN<-sum);
Tr<-matrix(T2$x,k,1);# totales filas
## Warning in matrix(T2$x, k, 1): la longitud de los datos [8] no es un submúltiplo
## o múltiplo del número de filas [7] en la matriz
T3<-aggregate(Datos_Youden$Rendimiento, by<-list(Longitud<-Datos_Youden$Longitud),FUN<-sum);
Tc<-matrix(T3$x,b,1)#totales Columna
## Warning in matrix(T3$x, b, 1): la longitud de los datos [7] no es un submúltiplo
## o múltiplo del número de filas [8] en la matriz
i<-1:k
j<-1:b
if((b==k)&&(N[i,j]==1)){
  print("dado el diseño cuadrado latino");
  #si es un DCL uno corre el código antes considerado.
  }else{
    x<-0;z<-0;
    if(v>=2 && k<v)
      {
      A<-N%*%t(N);
              for(i in 2:v){
                 x1<-A[1,1]-A[i,i];
                x<-x+abs(x1);
                }
              sum1<-sum(x);sum1
              for(i in 1:v){
                for(j in 1:v){
                  if(i!=j){
                    x2<-A[1,2]-A[i,j];
                    z<-z+abs(x2)}
                  }}
              sum2<-sum(z);
              if(sum1==0 && sum2==0){
                r<-A[1,1];
                lambda<-A[1,2];
  cat("dado el diseño cuadrado de Youden \n con r y lambda respectivamente")
                print(r);
                print(lambda);
                #se define el vector columna de k entradas todas en 1
                ek<-c(matrix(rep(1,k),k,1));
                Dk<-diag(k)-(ek%*%t(ek))/v;
                Q<-Tt-(N%*%Tc)/k;
                alpha_hat<-(k/(lambda*v))*Q;
                beta_hat<-(Tc/k)-(t(N)%*%Q)/(lambda*v);
                gamma_hat<-Tr/v;
                print("Las estimaciones son");
                print("mu_hat");
                print("0");
                print("alpha_hat");
                print(alpha_hat);
                print("beta_hat")
                print(beta_hat)
                print("gamma_hat")
                print(gamma_hat);
                #probando hipótesis H_alpha, H_beta y H_gamma
YOUDEN<- lm(Rendimiento~Tratamiento+Latitud+Long, df);YOUDEN
A(Youden_Erick) }}}
med_Tratamiento = tapply(Datos_Youden$Rendimiento, Datos_Youden$Tratamiento, mean);Tratamiento
##  [1] "D" "F" "B" "A" "C" "E" "H" "H" "E" "C" "G" "B" "F" "D" "C" "G" "H" "E" "D"
## [20] "A" "B" "B" "A" "D" "F" "H" "G" "C" "E" "H" "G" "C" "A" "D" "F" "A" "B" "F"
## [39] "D" "E" "C" "G" "G" "C" "E" "H" "F" "B" "A" "F" "D" "A" "B" "G" "H" "E"
barplot(med_Tratamiento, ylim = c(0,50),
        main='Respuesta por tratamiento')
abline(h=mean(Datos_Youden$Rendimiento))

med_Tratamiento2 = tapply(Datos_Youden$Rendimiento, Datos_Youden$Latitud, mean)
barplot(med_Tratamiento2, ylim = c(0,70),
        main='Disposición de Bloque por fila')
abline(h=mean(Datos_Youden$Rendimiento))