#COntexto : Se busca saber el rendimiento en un cultivo de manzanos con 2 fertilziantes 60N y 90N en 5 tiempo 10 dias, 20 dias, 30 dias ,40 dias y 45 dias

library(readxl)
Casambara <- read_excel("C:\\Users\\57321\\Documents\\R\\Excel rstudio\\Caramba.xlsx", 
    col_types = c("numeric", "text", "text", 
        "numeric"))

df_1 <- data.frame(Casambara)
library(collapsibleTree)
collapsibleTreeSummary(
  df_1,
  hierarchy = c('tratamiento', 'tiempo', 'rendimiento'))

#Estadisticos

library(rstatix)
## Warning: package 'rstatix' was built under R version 4.1.1
df_1 %>%
  group_by(tratamiento, tiempo) %>%
  get_summary_stats(rendimiento, type = "mean_sd")
## # A tibble: 15 x 6
##    tratamiento tiempo variable        n  mean    sd
##    <chr>       <chr>  <chr>       <dbl> <dbl> <dbl>
##  1 60N         10     rendimiento     5  2.29 1.42 
##  2 60N         20     rendimiento     5  2.67 0.867
##  3 60N         30     rendimiento     5  3.77 1.40 
##  4 60N         40     rendimiento     5  2.61 1.17 
##  5 60N         45     rendimiento     5  2.79 0.858
##  6 90N         10     rendimiento     5  3.48 1.04 
##  7 90N         20     rendimiento     5  3.27 1.83 
##  8 90N         30     rendimiento     5  2.47 1.48 
##  9 90N         40     rendimiento     5  2.74 1.54 
## 10 90N         45     rendimiento     5  2.90 1.20 
## 11 Control     10     rendimiento     5  2.43 0.751
## 12 Control     20     rendimiento     5  3.40 1.59 
## 13 Control     30     rendimiento     5  3.50 1.34 
## 14 Control     40     rendimiento     5  3.09 1.72 
## 15 Control     45     rendimiento     5  2.93 0.96

#Graficos

library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.1.1
ggqqplot(df_1, "rendimiento", facet.by = "tiempo")

#Datos atipicos(En la variable respuesta)

library(rstatix)
df_1 %>%
  group_by(tratamiento, tiempo) %>%
  identify_outliers(rendimiento)
## # A tibble: 7 x 6
##   tratamiento tiempo    id rendimiento is.outlier is.extreme
##   <chr>       <chr>  <dbl>       <dbl> <lgl>      <lgl>     
## 1 60N         10         1        4.66 TRUE       FALSE     
## 2 60N         20         4        1.18 TRUE       TRUE      
## 3 60N         20         5        3.42 TRUE       FALSE     
## 4 60N         45         1        3.66 TRUE       FALSE     
## 5 60N         45         2        1.36 TRUE       TRUE      
## 6 90N         30         1        4.97 TRUE       FALSE     
## 7 Control     10         2        1.24 TRUE       FALSE

#Prueba de normalidad(metodo casambara, a todo tratamiento ,control)

library(rstatix)
df_1 %>%
  group_by(tratamiento, tiempo) %>%
  shapiro_test(rendimiento)
## # A tibble: 15 x 5
##    tratamiento tiempo variable    statistic      p
##    <chr>       <chr>  <chr>           <dbl>  <dbl>
##  1 60N         10     rendimiento     0.849 0.192 
##  2 60N         20     rendimiento     0.787 0.0637
##  3 60N         30     rendimiento     0.828 0.135 
##  4 60N         40     rendimiento     0.955 0.772 
##  5 60N         45     rendimiento     0.859 0.223 
##  6 90N         10     rendimiento     0.878 0.301 
##  7 90N         20     rendimiento     0.764 0.0395
##  8 90N         30     rendimiento     0.823 0.122 
##  9 90N         40     rendimiento     0.930 0.593 
## 10 90N         45     rendimiento     0.868 0.258 
## 11 Control     10     rendimiento     0.929 0.591 
## 12 Control     20     rendimiento     0.905 0.437 
## 13 Control     30     rendimiento     0.949 0.730 
## 14 Control     40     rendimiento     0.827 0.133 
## 15 Control     45     rendimiento     0.979 0.929

#Prueba normalidad a residuales (Metodo profesor)

anc <- aov(rendimiento ~ tiempo + tratamiento,data = df_1)
summary(anc)
##             Df Sum Sq Mean Sq F value Pr(>F)
## tiempo       4   2.85  0.7120   0.423  0.791
## tratamiento  2   0.76  0.3795   0.226  0.799
## Residuals   68 114.36  1.6818
norm_resi = shapiro.test(anc$residuals)
ifelse(norm_resi$p.value<0.05,"No son normales","Son normales")
## [1] "No son normales"

#Atipicos de los residuales

Res_1<-anc$residuals

#Normalidad de los residuales
shapiro.test(Res_1)
## 
##  Shapiro-Wilk normality test
## 
## data:  Res_1
## W = 0.94394, p-value = 0.002352
#Igualdad de varianzas
bartlett.test(Res_1~df_1$tratamiento)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  Res_1 by df_1$tratamiento
## Bartlett's K-squared = 1.2085, df = 2, p-value = 0.5465

#Analisis de varianza

library(rstatix)
res.aov <- anova_test(data = df_1, dv = rendimiento, wid = id,within = c(tratamiento, tiempo))
get_anova_table(res.aov)
## ANOVA Table (type III tests)
## 
##               Effect DFn DFd     F     p p<.05   ges
## 1        tratamiento   2   8 0.223 0.805       0.007
## 2             tiempo   4  16 0.357 0.836       0.027
## 3 tratamiento:tiempo   8  32 1.479 0.204       0.090

#Bonferroni

one.way <- df_1 %>%
  group_by(tiempo) %>%
  anova_test(dv = rendimiento, wid = id, within = tratamiento) %>%
  get_anova_table() %>%
  adjust_pvalue(method = "bonferroni")
one.way
## # A tibble: 5 x 9
##   tiempo Effect        DFn   DFd     F     p `p<.05`   ges p.adj
##   <chr>  <chr>       <dbl> <dbl> <dbl> <dbl> <chr>   <dbl> <dbl>
## 1 10     tratamiento     2     8 2.59  0.136 ""      0.222 0.68 
## 2 20     tratamiento     2     8 0.468 0.642 ""      0.054 1    
## 3 30     tratamiento     2     8 2.36  0.157 ""      0.165 0.785
## 4 40     tratamiento     2     8 0.52  0.613 ""      0.022 1    
## 5 45     tratamiento     2     8 0.025 0.975 ""      0.005 1

#Variabilidad de varianzas casambara

##Prueba de Levene

library(car)
## Warning: package 'car' was built under R version 4.1.1
leveneTest(rendimiento ~ tiempo, data = df_1)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  4  1.5931 0.1858
##       70

##Prueba de Fligner-Killeen

fligner.test(rendimiento ~ tiempo, data = df_1)
## 
##  Fligner-Killeen test of homogeneity of variances
## 
## data:  rendimiento by tiempo
## Fligner-Killeen:med chi-squared = 6.8897, df = 4, p-value = 0.1418

##Prueba de Bartlett

bartlett.test(rendimiento ~ interaction(tratamiento,tiempo), data=df_1)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  rendimiento by interaction(tratamiento, tiempo)
## Bartlett's K-squared = 7.0703, df = 14, p-value = 0.932

#Cual es el mejor tratamiento

medr=tapply(df_1$rendimiento,df_1$tratamiento,mean);medr
##      60N      90N  Control 
## 2.826350 2.972829 3.071206
tapply(df_1$rendimiento, list(df_1$tratamiento, df_1$tiempo), mean)
##               10       20       30       40       45
## 60N     2.288028 2.673147 3.772619 2.608863 2.789093
## 90N     3.474953 3.272994 2.474044 2.736772 2.905380
## Control 2.430293 3.401733 3.502762 3.087857 2.933384

#El mejor tratamiento es el de 60N a los 30 dias ya que presento la mayor media entre todos los tratamiento y los demas tiempo