set.seed(2914)
data("Machines", package = "nlme")
## technical details for shorter output:
class(Machines) <- "data.frame"
Machines[, "Worker"] <- factor(Machines[, "Worker"], levels = 1:6, 
                               ordered = FALSE)
str(Machines, give.attr = FALSE) 
## 'data.frame':    54 obs. of  3 variables:
##  $ Worker : Factor w/ 6 levels "1","2","3","4",..: 1 1 1 2 2 2 3 3 3 4 ...
##  $ Machine: Factor w/ 3 levels "A","B","C": 1 1 1 1 1 1 1 1 1 1 ...
##  $ score  : num  52 52.8 53.1 51.8 52.8 53.1 60 60.2 58.4 51.1 ...
names (Machines) = c("Ambiente","Genotipo","Rendimiento")

head(Machines)
##   Ambiente Genotipo Rendimiento
## 1        1        A        52.0
## 2        1        A        52.8
## 3        1        A        53.1
## 4        2        A        51.8
## 5        2        A        52.8
## 6        2        A        53.1
boxplot(Machines$Rendimiento~Machines$Genotipo)

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.2
ggplot(Machines, aes(x = Genotipo, 
               y = Rendimiento, 
               group = Ambiente, 
               col = Ambiente)) + 
  geom_point() + stat_summary(fun = mean, geom = "line") + theme_bw()

La libreria se usa para verificar la interacion y variación según las variables, y también se define un modelo para esta comparativa, se espera una interacción de genotipo por ambiente pues este generalmente sucede

library(lme4)
## Warning: package 'lme4' was built under R version 4.2.2
## Loading required package: Matrix
## Warning: package 'Matrix' was built under R version 4.2.2
#mod 1 Modelo que calcula todo
mod1<- lmer(Rendimiento ~ (1 | Genotipo) + (1 | Ambiente) +
              (1 | Ambiente:Genotipo), data = Machines)

Se realiza el ANOVA donde: vag= varianza interacciòn ambiente genotipo vg = varianza Genotipo va = varianza ambiente vr = varianza del eror vt = varianza total (suma de todas las anteriores)

summary(mod1)
## Linear mixed model fit by REML ['lmerMod']
## Formula: 
## Rendimiento ~ (1 | Genotipo) + (1 | Ambiente) + (1 | Ambiente:Genotipo)
##    Data: Machines
## 
## REML criterion at convergence: 230.2
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.26887 -0.55428 -0.00345  0.44662  2.53208 
## 
## Random effects:
##  Groups            Name        Variance Std.Dev.
##  Ambiente:Genotipo (Intercept) 13.9099  3.7296  
##  Ambiente          (Intercept) 22.8585  4.7811  
##  Genotipo          (Intercept) 46.3893  6.8110  
##  Residual                       0.9246  0.9616  
## Number of obs: 54, groups:  Ambiente:Genotipo, 18; Ambiente, 6; Genotipo, 3
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)   59.650      4.479   13.32
vag=13.9095 
vg=46.3893
va=22.8584
vr=0.9246 
vt=vag+va+vr+vg

Se espula el tipo de heredabilidad

vsa=vag/vt*100#variacion segun el genotipo y ambiente
vsg=vg/vt*100#variacion segun el genotipo
va/vt*100#variacion segun lel ambiente
## [1] 27.18591
vr/vt*100#variacion segun los residuales
## [1] 1.099643
hr=vsa/vsg

Valor de la heredabilidad estadistica

hr
## [1] 0.2998429