# ==============================================================================
# SCRIPT DE ANÁLISIS COMPLETO - EXPERIMENTO MAÍZ
# ==============================================================================

# 1. CARGAR LIBRERÍA
# Si no la tienes instalada, descomenta la linea de abajo:
# install.packages("agricolae")
library(agricolae)

# 2. CARGAR DATOS
DATOS_MAIZ <- read.csv("DATOS_MAIZ.csv", header=TRUE, sep=",")

# 3. PREPARACIÓN DE DATOS (Vital)
# Convertimos Balde y Tratamiento a "Factores" (Categorías)
DATOS_MAIZ$balde <- as.factor(DATOS_MAIZ$balde)
DATOS_MAIZ$trt   <- as.factor(DATOS_MAIZ$trt)

# 4. FILTRADO 
# Eliminamos el Balde 4 para tener datos balanceados (solo 3 baldes)
DATOS_LIMPIOS <- subset(DATOS_MAIZ, balde != 4)

cat("Datos cargados y filtrados. Analizando solo Baldes 1, 2 y 3.\n")
## Datos cargados y filtrados. Analizando solo Baldes 1, 2 y 3.
cat("----------------------------------------------------------\n")
## ----------------------------------------------------------
# 5. BUCLE AUTOMÁTICO (LOOP)
# Esto recorrerá TODAS las columnas numéricas (alturas y hojas de todas las fechas) y hará el ANOVA + Tukey para cada una.

# Obtenemos los nombres de las columnas que son numéricas (las mediciones)
variables_a_analizar <- names(DATOS_LIMPIOS)[sapply(DATOS_LIMPIOS, is.numeric)]

for (variable in variables_a_analizar) {
  
  # Imprimir qué estamos analizando
  cat("\n========================================\n")
  cat(" ANALIZANDO: ", variable, "\n")
  cat("========================================\n")
  
  # a) Hacer el ANOVA
  # La fórmula se crea dinámicamente: variable ~ trt + balde
  formula_actual <- as.formula(paste(variable, "~ trt + balde"))
  modelo <- aov(formula_actual, data = DATOS_LIMPIOS)
  
  # b) Mostrar resumen estadístico (P-value)
  print(summary(modelo))
  
  # c) Prueba de Tukey (Para ver quién ganó)
  cat("\n--- Comparación de Medias (Tukey) ---\n")
  out <- HSD.test(modelo, "trt", group=TRUE, console=TRUE)
  
  # d) Guardar/Mostrar el gráfico
  # El gráfico aparecerá en la ventana de Plots. 
  # Le ponemos título automático con el nombre de la fecha.
  plot(out, main = paste("Resultados para:", variable))
  
  # Pausa pequeña para que alcances a ver el gráfico antes de que pase al siguiente
  Sys.sleep(2) 
}
## 
## ========================================
##  ANALIZANDO:  alt_24dic 
## ========================================
##             Df Sum Sq Mean Sq F value Pr(>F)
## trt          1  0.042  0.0417   0.030  0.864
## balde        2  2.041  1.0204   0.742  0.489
## Residuals   20 27.516  1.3758               
## 
## --- Comparación de Medias (Tukey) ---
## 
## Study: modelo ~ "trt"
## 
## HSD Test for alt_24dic 
## 
## Mean Square Error:  1.375792 
## 
## trt,  means
## 
##         alt_24dic       std  r       se Min Max   Q25  Q50    Q75
## Hormona  11.51667 1.3462631 12 0.338599  10  13 10.15 11.5 13.000
## Testigo  11.60000 0.9351714 12 0.338599  10  13 11.00 11.9 12.075
## 
## Alpha: 0.05 ; DF Error: 20 
## Critical Value of Studentized Range: 2.949998 
## 
## Minimun Significant Difference: 0.9988664 
## 
## Treatments with the same letter are not significantly different.
## 
##         alt_24dic groups
## Testigo  11.60000      a
## Hormona  11.51667      a

## 
## ========================================
##  ANALIZANDO:  alt_27dic 
## ========================================
##             Df Sum Sq Mean Sq F value Pr(>F)
## trt          1   0.81   0.807   0.154  0.699
## balde        2   3.48   1.741   0.332  0.721
## Residuals   20 104.94   5.247               
## 
## --- Comparación de Medias (Tukey) ---
## 
## Study: modelo ~ "trt"
## 
## HSD Test for alt_27dic 
## 
## Mean Square Error:  5.246792 
## 
## trt,  means
## 
##         alt_27dic      std  r        se  Min  Max    Q25   Q50   Q75
## Hormona  14.79167 2.341118 12 0.6612357 12.0 18.0 12.875 14.20 16.50
## Testigo  15.15833 2.091741 12 0.6612357 11.2 18.5 14.350 15.15 15.95
## 
## Alpha: 0.05 ; DF Error: 20 
## Critical Value of Studentized Range: 2.949998 
## 
## Minimun Significant Difference: 1.950644 
## 
## Treatments with the same letter are not significantly different.
## 
##         alt_27dic groups
## Testigo  15.15833      a
## Hormona  14.79167      a

## 
## ========================================
##  ANALIZANDO:  alt_29dic 
## ========================================
##             Df Sum Sq Mean Sq F value Pr(>F)
## trt          1   0.67   0.667   0.055  0.817
## balde        2   1.14   0.572   0.047  0.954
## Residuals   20 241.28  12.064               
## 
## --- Comparación de Medias (Tukey) ---
## 
## Study: modelo ~ "trt"
## 
## HSD Test for alt_29dic 
## 
## Mean Square Error:  12.06417 
## 
## trt,  means
## 
##         alt_29dic      std  r      se  Min  Max   Q25   Q50    Q75
## Hormona  18.65000 3.598358 12 1.00267 14.1 23.6 15.45 18.10 22.400
## Testigo  18.31667 3.015063 12 1.00267 14.0 24.7 16.95 17.75 19.475
## 
## Alpha: 0.05 ; DF Error: 20 
## Critical Value of Studentized Range: 2.949998 
## 
## Minimun Significant Difference: 2.957874 
## 
## Treatments with the same letter are not significantly different.
## 
##         alt_29dic groups
## Hormona  18.65000      a
## Testigo  18.31667      a

## 
## ========================================
##  ANALIZANDO:  alt_02ene 
## ========================================
##             Df Sum Sq Mean Sq F value Pr(>F)
## trt          1    0.0   0.000   0.000  0.996
## balde        2    4.2   2.124   0.131  0.878
## Residuals   20  323.2  16.160               
## 
## --- Comparación de Medias (Tukey) ---
## 
## Study: modelo ~ "trt"
## 
## HSD Test for alt_02ene 
## 
## Mean Square Error:  16.15992 
## 
## trt,  means
## 
##         alt_02ene      std  r       se  Min  Max    Q25  Q50    Q75
## Hormona  21.38333 4.263766 12 1.160457 14.1 28.3 18.350 20.3 25.250
## Testigo  21.39167 3.404131 12 1.160457 17.1 30.1 19.825 20.5 21.925
## 
## Alpha: 0.05 ; DF Error: 20 
## Critical Value of Studentized Range: 2.949998 
## 
## Minimun Significant Difference: 3.423344 
## 
## Treatments with the same letter are not significantly different.
## 
##         alt_02ene groups
## Testigo  21.39167      a
## Hormona  21.38333      a

## 
## ========================================
##  ANALIZANDO:  hojas_24dic 
## ========================================
##             Df Sum Sq Mean Sq F value Pr(>F)  
## trt          1  4.167   4.167   7.353 0.0134 *
## balde        2  2.333   1.167   2.059 0.1538  
## Residuals   20 11.333   0.567                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## --- Comparación de Medias (Tukey) ---
## 
## Study: modelo ~ "trt"
## 
## HSD Test for hojas_24dic 
## 
## Mean Square Error:  0.5666667 
## 
## trt,  means
## 
##         hojas_24dic       std  r        se Min Max Q25 Q50 Q75
## Hormona    3.500000 0.7977240 12 0.2173067   2   5   3 3.5   4
## Testigo    4.333333 0.7784989 12 0.2173067   3   5   4 4.5   5
## 
## Alpha: 0.05 ; DF Error: 20 
## Critical Value of Studentized Range: 2.949998 
## 
## Minimun Significant Difference: 0.6410544 
## 
## Treatments with the same letter are not significantly different.
## 
##         hojas_24dic groups
## Testigo    4.333333      a
## Hormona    3.500000      b

## 
## ========================================
##  ANALIZANDO:  hojas_27dic 
## ========================================
##             Df Sum Sq Mean Sq F value Pr(>F)
## trt          1  1.042  1.0417   1.276  0.272
## balde        2  3.250  1.6250   1.990  0.163
## Residuals   20 16.333  0.8167               
## 
## --- Comparación de Medias (Tukey) ---
## 
## Study: modelo ~ "trt"
## 
## HSD Test for hojas_27dic 
## 
## Mean Square Error:  0.8166667 
## 
## trt,  means
## 
##         hojas_27dic       std  r        se Min Max  Q25 Q50  Q75
## Hormona    4.666667 1.0730867 12 0.2608746   3   6 4.00   5 5.25
## Testigo    5.083333 0.7929615 12 0.2608746   4   6 4.75   5 6.00
## 
## Alpha: 0.05 ; DF Error: 20 
## Critical Value of Studentized Range: 2.949998 
## 
## Minimun Significant Difference: 0.7695794 
## 
## Treatments with the same letter are not significantly different.
## 
##         hojas_27dic groups
## Testigo    5.083333      a
## Hormona    4.666667      a

## 
## ========================================
##  ANALIZANDO:  hojas_29dic 
## ========================================
##             Df Sum Sq Mean Sq F value Pr(>F)
## trt          1  1.042  1.0417   1.121  0.302
## balde        2  2.333  1.1667   1.256  0.306
## Residuals   20 18.583  0.9292               
## 
## --- Comparación de Medias (Tukey) ---
## 
## Study: modelo ~ "trt"
## 
## HSD Test for hojas_29dic 
## 
## Mean Square Error:  0.9291667 
## 
## trt,  means
## 
##         hojas_29dic       std  r        se Min Max  Q25 Q50  Q75
## Hormona    5.583333 1.1645002 12 0.2782635   4   7 4.75   6 6.25
## Testigo    6.000000 0.7385489 12 0.2782635   5   7 5.75   6 6.25
## 
## Alpha: 0.05 ; DF Error: 20 
## Critical Value of Studentized Range: 2.949998 
## 
## Minimun Significant Difference: 0.8208766 
## 
## Treatments with the same letter are not significantly different.
## 
##         hojas_29dic groups
## Testigo    6.000000      a
## Hormona    5.583333      a

## 
## ========================================
##  ANALIZANDO:  hojas_02ene 
## ========================================
##             Df Sum Sq Mean Sq F value Pr(>F)
## trt          1  0.042  0.0417   0.053  0.821
## balde        2  1.083  0.5417   0.684  0.516
## Residuals   20 15.833  0.7917               
## 
## --- Comparación de Medias (Tukey) ---
## 
## Study: modelo ~ "trt"
## 
## HSD Test for hojas_02ene 
## 
## Mean Square Error:  0.7916667 
## 
## trt,  means
## 
##         hojas_02ene       std  r        se Min Max  Q25 Q50 Q75
## Hormona    6.250000 1.0552897 12 0.2568506   5   8 5.75   6   7
## Testigo    6.333333 0.6513389 12 0.2568506   5   7 6.00   6   7
## 
## Alpha: 0.05 ; DF Error: 20 
## Critical Value of Studentized Range: 2.949998 
## 
## Minimun Significant Difference: 0.7577086 
## 
## Treatments with the same letter are not significantly different.
## 
##         hojas_02ene groups
## Testigo    6.333333      a
## Hormona    6.250000      a

cat("\n¡ANÁLISIS COMPLETADO! Revisa los resultados arriba en la consola.")
## 
## ¡ANÁLISIS COMPLETADO! Revisa los resultados arriba en la consola.