#en clase: #Ā Factorial completo en arreglo completamente al azar (interaccion) #Ā Factorial completo en bloques

#3)/Tactor completo en Diseno del segundo parcial #fatorial incompleto al azar (parcial

#TRABAJO #Factorial incompleto en bloques con covariable. #strip plot desing(diseno en franja) #SPLIT PLOT DESING (parcelas dividad)

#PRESENTACION #QUIZ: diseƱo en medias repetidas************* #diseƱo lattice #DiseƱo: modelos efectos aleatorios y mixtos #HERRAMIENTA DE ANALISIS DE DATOS: ANALISIS DE PERFILES (uso agronomico importante)***********

#Buscar: Computacion Estadistica #diseño en hacer dos experimentos a la vez (2 o mas factores), se realiza doble aleatorización

xy = expand.grid(y = seq(4), x = seq(6))
f2 = gl(3, 8, 24, paste0('R',1:3))
lf1 = paste0('V',1:4)
f1 = c(sample(lf1),sample(lf1),
       sample(lf1),sample(lf1),
       sample(lf1),sample(lf1))
rep = rep(rep(paste0('r',1:2), each=4), 3)

data = data.frame(xy, f1, f2, rep)
data$name = with(data, paste0(f1, rep))
library(ggplot2)
ggplot(data)+
  aes(x,y,label=name, fill=f1)+
  geom_tile(color='white')+
  geom_text()+
  facet_wrap(~f2, scales = 'free')+
  theme(axis.text = element_blank())

#COMPLETAMENTE AL AZAR
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
data = expand.grid(x=seq(6), y=seq(4))
data = sample_frac(xy)
data$f1= gl(4,6,24, paste0('y',1:4))
data$f2= gl(3,2,6, paste0('R',1:3))
data$rep= gl(2,1,24, paste0('r',1:2))
data$name =paste0(data$f1, data$f2, data$rep)

library(ggplot2)
ggplot(data)+
  aes(x,y,label=name, fill=f1)+
  geom_tile(color='white')+
  geom_text()+
  facet_wrap(~f2, scales = 'free')+
  theme(axis.text = element_blank())

library(ggplot2)
ggplot(data)+
  aes(x,y,label=name, fill= f2)+
  geom_tile(color="white")+
  geom_text(color="white")+
  labs(title= "Completamente al azar")

# ESTA MAL PLANTEADO: el anova debe tener 2 errores * los p- valor se calculan dependiendo de la cantidad de errores que hallan

mu=mean(data$biom)
## Warning in mean.default(data$biom): argument is not numeric or logical:
## returning NA
set.seed(123)
data$biom= rnorm(24,8,2)

ggplot(data)+
  aes(f2,biom)+
  geom_boxplot()

ggplot(data)+
  aes(f1,biom)+
  geom_boxplot()

ggplot(data)+
  aes(f2,biom, fill=f1)+
  geom_boxplot()

#analsis inferencial

mod1 = aov(biom ~ f2 * f1 + Error(f2:rep), data)
## Warning in aov(biom ~ f2 * f1 + Error(f2:rep), data): Error() model is singular
summary(mod1)
## 
## Error: f2:rep
##           Df Sum Sq Mean Sq F value Pr(>F)
## f2         2  0.477  0.2383   0.102  0.906
## Residuals  3  7.027  2.3425               
## 
## Error: Within
##           Df Sum Sq Mean Sq F value Pr(>F)
## f1         3  10.20   3.399   0.807  0.521
## f2:f1      6  29.00   4.833   1.148  0.409
## Residuals  9  37.89   4.210

#conclusion: #f1:f1(interaccion): p_valor mayor al 5%, NO se rechaza la H_0, hay evidencia estadistica suficiente para decir que no hay interaccion, se procede a mirar el factor (si hay interacion en este caso -p_valor>5%-)

#f1: 96%>5%, No se rechaza la H_0, el (riego), no tuvo efecto en el rendimiento de la biomasa #f2: 6.2%>5%, No se rechaza la h_0, la (variendad) no tuvo efecto en el rendimento de la biomasa