library(readxl)
library(ggplot2)

ruta_excel <- "C:\\Users\\jdom3\\Desktop\\Datos tesis.xlsx"

Analisisprev1.2 <- read_excel(ruta_excel, sheet = 'Preventivo - Exp 1 vertical')


Analisisprev1.2 <- na.omit(Analisisprev1.2)
Analisisprev1.2
## # A tibble: 168 × 4
##    Numero_petalo Tratamiento         Dia Area_herida
##            <dbl> <chr>             <dbl>       <dbl>
##  1            10 Control comercial     1       0    
##  2            17 Control comercial     1       0.194
##  3            43 Control comercial     1       0.965
##  4            54 Control comercial     1       0    
##  5            98 Control comercial     1       0.242
##  6            32 100 ppm               1       0    
##  7            63 100 ppm               1       0.269
##  8            80 100 ppm               1       0.088
##  9            81 100 ppm               1       0.464
## 10            85 100 ppm               1       0    
## # ℹ 158 more rows
library(agricolae)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
Analisisprev1.2 <- Analisisprev1.2 %>%
   group_by(Numero_petalo) %>% mutate(Max_dia = max(Dia)) 


 Tablaaudpcprev1 <- Analisisprev1.2 %>%
  group_by(Numero_petalo,Tratamiento, Max_dia) %>% 
  summarise(across(Area_herida, ~ audpc(.x, Dia)))
## `summarise()` has grouped output by 'Numero_petalo', 'Tratamiento'. You can
## override using the `.groups` argument.
 Tablaaudpcprev1 <-  Tablaaudpcprev1 %>%
 group_by(Numero_petalo)  %>% mutate(sAUDPC = Area_herida/Max_dia) 
  
 Tablaaudpcprev1  <-  Tablaaudpcprev1[with(Tablaaudpcprev1,order(Tablaaudpcprev1$Tratamiento)), ]
  Tablaaudpcprev1 
## # A tibble: 39 × 5
## # Groups:   Numero_petalo [39]
##    Numero_petalo Tratamiento Max_dia Area_herida sAUDPC
##            <dbl> <chr>         <dbl>       <dbl>  <dbl>
##  1             5 1 ppm             5       12.5    2.50
##  2            14 1 ppm             5       17.0    3.40
##  3            21 1 ppm             4        8.31   2.08
##  4            27 1 ppm             4        9.41   2.35
##  5            29 1 ppm             5       16.6    3.32
##  6            36 1 ppm             5       16.9    3.39
##  7            48 1 ppm             5       16.8    3.36
##  8            32 100 ppm           4        6.52   1.63
##  9            63 100 ppm           5       10.2    2.05
## 10            80 100 ppm           5       14.2    2.85
## # ℹ 29 more rows
ggplot(data = Tablaaudpcprev1, aes(x = Tratamiento, y = sAUDPC, color = Tratamiento)) +
    geom_boxplot() +
    theme_bw()

anovapr1 <- aov(Tablaaudpcprev1$sAUDPC ~ Tablaaudpcprev1$Tratamiento )
summary(anovapr1)
##                             Df Sum Sq Mean Sq F value   Pr(>F)    
## Tablaaudpcprev1$Tratamiento  4  16.47   4.118   9.779 2.24e-05 ***
## Residuals                   34  14.32   0.421                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
aovpr1_residuals <- residuals(object = anovapr1 )
hist(aovpr1_residuals)

shapiro.test(x = aovpr1_residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  aovpr1_residuals
## W = 0.96628, p-value = 0.2867
plot(anovapr1,2)

bartlett.test(aovpr1_residuals ~ Tablaaudpcprev1$Tratamiento)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  aovpr1_residuals by Tablaaudpcprev1$Tratamiento
## Bartlett's K-squared = 0.84327, df = 4, p-value = 0.9326
plot(aovpr1_residuals, pch = 20)

Tukeypr1 <- TukeyHSD(anovapr1)
library(multcompView)

tukeyletraspr1 <- multcompLetters4(anovapr1, Tukeypr1)
print(tukeyletraspr1)
## $`Tablaaudpcprev1$Tratamiento`
##                1 ppm    Control comercial              100 ppm 
##                  "a"                  "a"                  "a" 
##               50 ppm Control absoluto\r\n 
##                  "a"                  "b"
Tablafinalpr1 <- group_by(Tablaaudpcprev1,Tratamiento) %>%
  summarise(mean=mean(sAUDPC), sd=sd(sAUDPC)) %>%
  arrange(desc(mean))
Tablafinalpr1
## # A tibble: 5 × 3
##   Tratamiento             mean    sd
##   <chr>                  <dbl> <dbl>
## 1 "1 ppm"                 2.91 0.578
## 2 "Control comercial"     2.89 0.835
## 3 "100 ppm"               2.63 0.626
## 4 "50 ppm"                2.58 0.667
## 5 "Control absoluto\r\n"  1.27 0.598
letraspr1 <- as.data.frame.list(tukeyletraspr1$`Tablaaudpcprev1$Tratamiento`)
Tablafinalpr1f <- Tablafinalpr1 %>%
  group_by() %>%
  mutate( Tukey = letraspr1$Letters)

print(Tablafinalpr1f)
## # A tibble: 5 × 4
##   Tratamiento             mean    sd Tukey
##   <chr>                  <dbl> <dbl> <chr>
## 1 "1 ppm"                 2.91 0.578 a    
## 2 "Control comercial"     2.89 0.835 a    
## 3 "100 ppm"               2.63 0.626 a    
## 4 "50 ppm"                2.58 0.667 a    
## 5 "Control absoluto\r\n"  1.27 0.598 b
ggplot(Tablafinalpr1f, aes(x = Tratamiento, y = mean, fill =Tratamiento)) + 
  geom_bar(stat = "identity", position = "dodge", alpha = 0.5, colour = "gray25")  +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), position = position_dodge(0.9), width = 0.25,
                show.legend = FALSE, colour = "gray25") +
  labs(x="Tratamientos", y="sAUDPC") +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  geom_text(aes(label=Tukey), position = position_dodge(0.90), size = 3, 
            vjust=-0.8, hjust=-0.5, colour = "gray25") +
  ylim(0, 4) +
  scale_fill_grey()