frutos <- c(rnorm(600,150,20))
genotipos <- rep(c(1:30),10)
ambientes <- rep(c(1:10),30)
df_1 <- data.frame(frutos,genotipos,ambientes)

#Modelo Efectos Fijos (30 genotipos, 10 ambientes)

mod_fij <- lm(frutos ~ genotipos + ambientes + (genotipos:ambientes), data = df_1)
summary(mod_fij)
## 
## Call:
## lm(formula = frutos ~ genotipos + ambientes + (genotipos:ambientes), 
##     data = df_1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -65.618 -13.852  -1.677  13.917  70.625 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         147.357173   3.320983  44.372   <2e-16 ***
## genotipos             0.037814   0.211953   0.178    0.858    
## ambientes            -0.196617   0.607095  -0.324    0.746    
## genotipos:ambientes   0.009516   0.033813   0.281    0.778    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 20.34 on 596 degrees of freedom
## Multiple R-squared:  0.001484,   Adjusted R-squared:  -0.003542 
## F-statistic: 0.2952 on 3 and 596 DF,  p-value: 0.8289
boxplot(frutos~genotipos,col=palette(rainbow(8)),xlab=("Categorias"),ylab=("Frutos"),main="Grafico de Medias y varianzas por genotipo",data = df_1)

boxplot(frutos~ambientes,col=palette(rainbow(8)),xlab=("Categorias"),ylab=("Frutos"),main="Grafico de Medias y varianzas por ambientes",data = df_1)

#Modelo Efectos Mixtos (5 genotipos, 10 ambientes)

frutos <- c(rnorm(100,150,20))
genotipos <- rep(c(1:5),20)
ambientes <- rep(c(1:10),10)
df_2 <- data.frame(frutos,genotipos,ambientes)
library(lme4)
## Warning: package 'Matrix' was built under R version 4.1.1
mod_mix <- lmer(frutos ~ ambientes + (1 | genotipos) +(1|ambientes:genotipos), data = df_2)
summary(mod_mix)
## Linear mixed model fit by REML ['lmerMod']
## Formula: frutos ~ ambientes + (1 | genotipos) + (1 | ambientes:genotipos)
##    Data: df_2
## 
## REML criterion at convergence: 861.9
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.49186 -0.67520  0.02512  0.75330  2.11892 
## 
## Random effects:
##  Groups              Name        Variance Std.Dev.
##  ambientes:genotipos (Intercept)  31.29    5.593  
##  genotipos           (Intercept)   0.00    0.000  
##  Residual                        325.88   18.052  
## Number of obs: 100, groups:  ambientes:genotipos, 10; genotipos, 5
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept) 154.1699     5.4596  28.238
## ambientes    -0.3489     0.8799  -0.397
## 
## Correlation of Fixed Effects:
##           (Intr)
## ambientes -0.886
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see ?isSingular
library(ggplot2)
ggplot(df_2, aes(x = genotipos, y = frutos, group = ambientes, col = ambientes)) + geom_point() + stat_summary(fun.y = mean, geom = "line")
## Warning: `fun.y` is deprecated. Use `fun` instead.

boxplot(frutos~genotipos,col=palette(rainbow(8)),xlab=("Genotipos"),ylab=("Frutos"),main="Grafico de Medias y varianzas por genotipo",data = df_2)

boxplot(frutos~ambientes,col=palette(rainbow(8)),xlab=("Ambientes"),ylab=("Frutos"),main="Grafico de Medias y varianzas por ambientes",data = df_2)

#Modelo Efectos Aleatorios (5 genotipos, 4 ambientes)

frutos <- c(rnorm(40,150,20))
genotipos <- rep(c(1:5),8)
ambientes <- rep(c(1:4),10)
df_3 <- data.frame(frutos,genotipos,ambientes)
library(lme4)
mod_ale <- lmer(frutos ~ (1 | ambientes) + (1 |genotipos)+(1|ambientes:genotipos) , data = df_3)
summary(mod_ale)
## Linear mixed model fit by REML ['lmerMod']
## Formula: frutos ~ (1 | ambientes) + (1 | genotipos) + (1 | ambientes:genotipos)
##    Data: df_3
## 
## REML criterion at convergence: 348.3
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.4966 -0.6429 -0.1288  0.5091  2.2786 
## 
## Random effects:
##  Groups              Name        Variance Std.Dev.
##  ambientes:genotipos (Intercept)  91.94    9.588  
##  genotipos           (Intercept)  57.40    7.576  
##  ambientes           (Intercept)   0.00    0.000  
##  Residual                        297.14   17.238  
## Number of obs: 40, groups:  ambientes:genotipos, 20; genotipos, 5; ambientes, 4
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  154.276      4.848   31.82
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see ?isSingular

#Varianza de los factores (genotipo, ambiente)

var_genotipo <- 2.598**2
var_ambiente <- 0.00**2 
var_residuales <- 15.802**2 
var_gen_amb <- 5.573**2 
var_total <- var_ambiente + var_residuales + var_genotipo + var_gen_amb

porce_geno <- 100*var_genotipo / var_total ; porce_geno
## [1] 2.347597
porce_ambi <- 100*var_ambiente / var_total ; porce_ambi
## [1] 0
porce_resi <- 100*var_residuales / var_total ; porce_resi
## [1] 86.84992
porce_gen_amb <- 100*var_gen_amb / var_total ; porce_gen_amb 
## [1] 10.80248
stripchart(frutos ~ genotipos, vertical = TRUE, pch = 16, xlab = "Genotipos", data = df_3)

boxplot(frutos~genotipos,col=palette(rainbow(8)),xlab=("Genotipos"),ylab=("Frutos"),main="Grafico de Medias y varianzas por genotipo",data = df_3)

boxplot(frutos ~ ambientes, col = palette(rainbow(8)), xlab = ("Ambientes"),ylab = ("Frutos"), main = "Grafico de Medias y varianzas por ambientes",data = df_3)