library(readxl)
ejercicio1 <- read_excel("C:\\Users\\57321\\Documents\\R\\Excel rstudio\\ejercicio1.xlsx", 
    col_types = c("text", "numeric"))
df_1<-data.frame(ejercicio1)
library(psych)
describeBy(df_1$datos)
## Warning in describeBy(df_1$datos): no grouping variable requested
##    vars  n  mean    sd median trimmed   mad min  max range  skew kurtosis   se
## X1    1 16 36.96 20.26  37.35   36.99 26.39 9.5 63.9  54.4 -0.02    -1.62 5.07
boxplot(ejercicio1$datos~ejercicio1$cat,
        col= palette(rainbow(8)),xlab=("Categorias"),ylab=("Frecuencias"),main="Grafico de Medias y varianzas por tratamiento")

library(lattice)
bwplot(df_1$datos~df_1$cat)

\[H_0:T_{.25Tsp}=T_{.5Tsp}=T_{.75Tsp}=T_{1Tsp}=0\]

ANV_1=aov(formula = datos ~ cat, data = ejercicio1)
summary(ANV_1)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## cat          3   6146  2048.6    1823 3.23e-16 ***
## Residuals   12     13     1.1                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#La prueba indica que se rechaza la hipotesis de que los tratamientos tienen efecto (segun los datos).

prueba_1=shapiro.test(ANV_1$residuals)
ifelse(prueba_1$p.value<0.05,"No son normales","Son normales")
## [1] "Son normales"
datos_1=split(ejercicio1$datos,ejercicio1$cat);datos_1
## $`100tsp`
## [1] 61.6 62.4 63.0 63.9
## 
## $`25tsp`
## [1] 11.4 11.0 11.3  9.5
## 
## $`5tsp`
## [1] 27.8 29.2 26.8 26.0
## 
## $`75tsp`
## [1] 47.6 47.0 47.3 45.5
TukeyHSD(ANV_1,'cat')
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = datos ~ cat, data = ejercicio1)
## 
## $cat
##                 diff       lwr       upr p adj
## 25tsp-100tsp -51.925 -54.15064 -49.69936     0
## 5tsp-100tsp  -35.275 -37.50064 -33.04936     0
## 75tsp-100tsp -15.875 -18.10064 -13.64936     0
## 5tsp-25tsp    16.650  14.42436  18.87564     0
## 75tsp-25tsp   36.050  33.82436  38.27564     0
## 75tsp-5tsp    19.400  17.17436  21.62564     0

#Ningun tratamiento es igual, segun la prueba anterior todos difieren

prueba_md1=t.test(datos_1$`100tsp`,datos_1$`75tsp`,datos_1$`5tsp`,datos_1$`25tsp`,alternative = "t",mu = 0,paired = T,conf.level = 0.95);prueba_md1
## 
##  Paired t-test
## 
## data:  datos_1$`100tsp` and datos_1$`75tsp`
## t = 17.263, df = 3, p-value = 0.0004235
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  12.94849 18.80151
## sample estimates:
## mean of the differences 
##                  15.875
ifelse(prueba_md1$p.value<0.05,"Rechazo Ho","No rechazo")
## [1] "Rechazo Ho"
#Supuestos
Res_1<-ANV_1$residuals
#Normalidad de los residuales
shapiro.test(Res_1)
## 
##  Shapiro-Wilk normality test
## 
## data:  Res_1
## W = 0.93838, p-value = 0.3297
plot(Res_1)

#Igualdad de varianzas
bartlett.test(Res_1~ejercicio1$cat)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  Res_1 by ejercicio1$cat
## Bartlett's K-squared = 0.71323, df = 3, p-value = 0.8701

#Las varianzas son homogenias entres los tratamientos ,y los residuales son normales

outliers::grubbs.test(Res_1)
## 
##  Grubbs test for one outlier
## 
## data:  Res_1
## G.6 = 1.8455, U = 0.7578, p-value = 0.4225
## alternative hypothesis: highest value 1.75 is an outlier

#Segun la prubea no hay valores atipicos


#Ejercicio 2

library(readxl)
ejercicio_3 <- read_excel("C:\\Users\\57321\\Documents\\R\\Excel rstudio\\ejercicio 2.xlsx", 
    col_types = c("text", "text", "numeric"))
df_3<-data.frame(ejercicio_3)
library(collapsibleTree)
collapsibleTreeSummary(
  df_3,
  hierarchy = c('Bloques', 'Cat', 'Datos'))
library(lattice)
bwplot(Datos~Cat,df_3)

library(lattice)
bwplot(Datos~Bloques,df_3)

tapply(df_3$Datos, list(df_3$Bloques,df_3$Cat), mean)
##          Control Tratamiento1 Tratamiento2
## Bloque 1    1147         1169         1009
## Bloque 2    1273         1323         1260
## Bloque 3    1216         1276         1143
## Bloque 4    1046         1240         1099
## Bloque 5    1108         1432         1385
## Bloque 6    1265         1562         1164
ANV_2 = aov(Datos ~ Bloques + Cat, df_3)
summary(ANV_2)
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Bloques      5 134190   26838   2.854 0.0743 .
## Cat          2  99122   49561   5.271 0.0273 *
## Residuals   10  94024    9402                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#Eficiencia de bloqueo (Si es mayor a 1 bloquear valio la pena)

H=26838/9402;H
## [1] 2.854499

\[H_0: \mu_{Control} = \mu_{Tratamiento_1} = \mu_{Tratamiento_2}\] #Variablilidad entre los tratamientos(Comparacion)

TukeyHSD(ANV_2,'Cat')
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Datos ~ Bloques + Cat, data = df_3)
## 
## $Cat
##                                   diff         lwr        upr     p adj
## Tratamiento1-Control       157.8333333    4.366592 311.300075 0.0440035
## Tratamiento2-Control         0.8333333 -152.633408 154.300075 0.9998778
## Tratamiento2-Tratamiento1 -157.0000000 -310.466741  -3.533259 0.0450905