# Crear un vector de paquetes
pacotes <- c("agricolae", "ggplot2", "outliers", "tidyverse", "nortest", "stats", "performance")

# Script para instalar y cargar librerias y dependencias
if(sum(as.numeric(!pacotes %in% installed.packages())) != 0){
  instalador <- pacotes[!pacotes %in% installed.packages()]
  for(i in 1:length(instalador)) {
    install.packages(instalador, dependencies = T)
    break()}
  sapply(pacotes, require, character = T) 
} else {
  sapply(pacotes, require, character = T) 
}
## Loading required package: agricolae
## Warning: package 'agricolae' was built under R version 4.3.2
## Loading required package: ggplot2
## Loading required package: outliers
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: nortest
## 
## Loading required package: performance
## Warning: package 'performance' was built under R version 4.3.2
##   agricolae     ggplot2    outliers   tidyverse     nortest       stats 
##        TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 
## performance 
##        TRUE
library(readxl)
FC <- read_excel("C:/Users/HP/OneDrive/Escritorio/ING 5 SEMESTRE/dbca.xlsx")
View(FC)
str(FC)
## tibble [48 × 4] (S3: tbl_df/tbl/data.frame)
##  $ polinizacion: chr [1:48] "Sin polinización" "Sin polinización" "Sin polinización" "Sin polinización" ...
##  $ fertilizante: num [1:48] 50 50 50 50 50 50 50 50 50 50 ...
##  $ Bloques     : num [1:48] 1 1 1 1 1 1 1 1 1 1 ...
##  $ peso        : num [1:48] 13.5 12.5 12.5 14.5 13.5 13.5 13.5 10.5 16.5 16.5 ...
summary(FC) # No valores faltantes
##  polinizacion        fertilizante    Bloques       peso      
##  Length:48          Min.   : 50   Min.   :1   Min.   :10.50  
##  Class :character   1st Qu.: 50   1st Qu.:1   1st Qu.:14.50  
##  Mode  :character   Median :100   Median :2   Median :16.50  
##                     Mean   :100   Mean   :2   Mean   :16.15  
##                     3rd Qu.:150   3rd Qu.:3   3rd Qu.:17.50  
##                     Max.   :150   Max.   :3   Max.   :21.50
# Convertir la variable Bloques a factor
FC$Bloques <- as.factor(FC$Bloques)
FC$fertilizante<-as.factor(FC$fertilizante)


#GRAFICO DE BOXPLOT

ggplot(data = FC, aes(x = polinizacion, y = peso)) +
  geom_boxplot(fill = "lightblue") +
  labs(x = "polinizacion", y = "peso (kg)") +
  ggtitle("COMPORTAMIENTO DEL RENDIMIENTO DE 3 TIPOS DE FERTILIZANTE")+
  theme_bw()

# Gráfico de dispersión para comparar niveles de fertilizante y polinización

ggplot(FC,       aes(x = fertilizante, y = peso, color = polinizacion)) +
  geom_point() +
  labs(title = "Comparacion de niveles de fertilizante y polinizacion",
       x = "Fertilizante",
       y = "Peso")

#ANOVA DBCA------
model_DBCA<- aov(peso ~ polinizacion *  fertilizante  + Bloques + polinizacion:fertilizante, data = FC)
model_DBCA2<- aov(peso ~ polinizacion *  fertilizante  + Bloques + polinizacion:fertilizante, data = FC)

# Realizar el ANOVA
summary(model_DBCA)
##                           Df Sum Sq Mean Sq F value   Pr(>F)    
## polinizacion               3  30.73   10.24   4.624  0.00777 ** 
## fertilizante               2 144.54   72.27  32.624 8.25e-09 ***
## polinizacion:fertilizante  6  31.96    5.33   2.404  0.04675 *  
## Residuals                 36  79.75    2.22                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model_DBCA2)
##                           Df Sum Sq Mean Sq F value   Pr(>F)    
## polinizacion               3  30.73   10.24   4.624  0.00777 ** 
## fertilizante               2 144.54   72.27  32.624 8.25e-09 ***
## polinizacion:fertilizante  6  31.96    5.33   2.404  0.04675 *  
## Residuals                 36  79.75    2.22                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
 #Valores atípicos
FC%>%
  select_if(is.numeric)%>%
  outlier()
## peso 
## 10.5
View(FC)

#Gráfico de barras para comparar niveles de polinización por bloque
ggplot(FC, aes(x = polinizacion, fill = peso)) +
  geom_bar() +
  labs(
    title = "Comparación de niveles de polinización por bloque",
    y = "Peso del fruto",
    x = "polinizacion",
    caption = "Período: 2023"
  ) + 
  theme_light()+
  scale_fill_manual(values = c(
    "Sin polinización" = "red",
    "Polinización mecánica" = "blue",
    "Abejas" = "green",
    "Abejorros" = "purple"
  ))
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?