---
title: Broad-sense heritability in plant breeding
description: > 
  How to calculate the heritability in plant breeding.
author: "Maria Belen Kistner & Flavio Lozano-Isla"
vignette: >
  %\VignetteIndexEntry{heritability}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{quarto::html}
knitr:
  opts_chunk: 
    collapse: true
    comment: '#>'
    echo: false
    warning: false
    message: false
editor_options: 
  chunk_output_type: console
bibliography: [files/pkgs.bib, files/book.bib]
link-citations: true
colorlinks: true
csl: https://www.zotero.org/styles/crop-science
nocite: |  
  [@schmidt2019Estimating; @schmidt2019Heritability; @bernal-vasquez2016Outliera;  @buntaran2020Cross; @bolker2021Mean]
---

```{r setup}
#| include: false

source("https://raw.githubusercontent.com/Flavjack/inti/master/pkgdown/favicon/docs.r")
knitr::opts_chunk$set(echo = TRUE)
```

# Broad-sense heritability ($H^2$)

Broad-sense heritability ($H^2$) is defined as the proportion of phenotypic variance that is attributable to an overall genetic variance for the genotype [@schmidt2019Estimating]. There are usually additional interpretations associated with $H^2$: *(i)* It is equivalent to the coefficient of determination of a linear regression of the unobservable genotypic value on the observed phenotype; *(ii)* It is also the squared correlation between predicted phenotypic value and genotypic value; and *(iii)* It represents the proportion of the selection differential ($S$) that can be realized as the response to selection ($R$) [@falconer2005Introduction].

There are two main reasons why heritability on an entry-mean basis is of interest in plant breeding [@schmidt2019Heritability]:

1.  It is plugged into the breeder’s Equation to predict the response to selection.
2.  It is a descriptive measure used to assess the usefulness and precision of results from cultivar evaluation trials.

## Breeder´s equation

$$\Delta G=H^2S$$ Where:

-   $\Delta G$ is the genetic gain
-   $S$ is the mean phenotypic value of the selected genotypes, expressed as a deviation from the population mean.

# Usual Problems

In practice, most trials are conducted in a multienvironment trial (MET) presente unbalanced data as not all cultivars are tested at each environment or simply when plot data is lost or when the number of replicates at each location varies between genotypes [@schmidt2019Estimating]. However, the standard method for estimating heritability implicitly assumes balanced data, independent genotype effects, and homogeneous variances.

# How calculate the Heritability?

According @schmidt2019Heritability, the variance components could be calculated in two ways:

## 1) Two stages approach

For the two stage approach, in the first stage each experiment is analyzed individually according their experiment design (Lattice, CRBD, etc) [@zystro2018Alternative]. And for the second stage environments are denotes a year-by-location interaction. This approach assumes a single variance for genotype-by-environment interactions (GxE), even when multiple locations were tested across multiple years [@buntaran2020Cross].

### Model

$$y_{ikt}=\mu\ +\ G_i+E_t+GxE_{it}+\varepsilon_{ikt}$$

### Phenotypic variance

$$\sigma_p^2=\sigma_g^2+\frac{\sigma_{g\cdot e}^2}{n_e}+\frac{\sigma_{\varepsilon}^2}{n_e\cdot n_r}$$

## 2) One stage approach

For the one stage approach only one model is used for the MET analysis. The environmental effects are included via separate year, and location main interaction effects.

$$y_{ikt}=\mu+G_i+Y_m+E_q+YxE_{mq}+GxY_{im}+GxE_{iq}+GxYxE_{imq}+\varepsilon_{ikmq}$$

### Phenotypic variance

$$\sigma_p^2=\sigma_g^2+\frac{\sigma_{g\cdot e}^2}{n_e}+\frac{\sigma_{g\cdot y}^2}{n_y}+\frac{\sigma_{g\cdot y\cdot e}^2}{n_y\cdot n_e}+\ \frac{\sigma_{\epsilon}^2}{n_e\cdot n_y\cdot n_r}$$

# Differentes heritability calculations

```{r}
#| echo: false

tibble(
  Standart = "$H^2=\\frac{\\sigma_g^2}{\\sigma_p^2}=\\frac{\\Delta G}{S}$",
  Cullis = "$H_{Cullis}^2=1-\\frac{\\overline{V}_{\\Delta..}^{^{BLUP}}}{2\\cdot\\sigma_g^2}$",
  Piepho = "$H_{Piepho}^2=\\frac{\\sigma_g^2}{\\sigma_g^2+\\frac{\\overline{V}_{\\Delta..}^{BLUE}}{2}}$"
  ) %>% 
  kable(caption = "Differentes heritability calculation")
```

# Heritability function in the package

For calculate the standard heritability in MET experiments the number of location and replication should be include manually in the function `H2cal()`. In the case of difference number of replication in each experiments, take the maximum value (often done in practice) [@schmidt2019Estimating].

For remove the outliers the function implemented is the Method 4 used for @bernal-vasquez2016Outliera: Bonferroni-Holm using re-scaled MAD for standardizing residuals (BH-MADR).

## Load packages

```{r}
library(inti)
```

## H2cal function

```{r}
dt <- inti::potato
hr <- H2cal(data = dt
          , trait = "stemdw"
          , gen.name = "geno"
          , rep.n = 5
          , fixed.model = ~ 0 + (1|bloque) + geno
          , random.model = ~ 1 + (1|bloque) + (1|geno)
          , emmeans = TRUE
          , plot_diag = FALSE
          , outliers.rm = TRUE
          )
```

## Model information

```{r}
hr$model %>% summary()
```

## Variance components

```{r}
hr$tabsmr %>% kable(caption = "Variance component table")
```

## Best Linear Unbiased Estimators (BLUEs)

```{r}
hr$blues %>% kable(caption = "BLUEs")
```

## Best Linear Unbiased Predictors (BLUPs)

```{r}
hr$blups %>% kable(caption = "BLUPs")
```

## Outliers

```{r}
hr$outliers$fixed %>% kable(caption = "Outliers fixed model")
```

```{r}
hr$outliers$random %>% kable(caption = "Outliers random model")
```

<!-- # Comparison: H2cal and asreml -->

<!-- > <https://inkaverse.com/articles/extra/stagewise.html> -->

::: {#refs}
:::

```{r}
#| echo: false
if(!file.exists("files/pkgs.bib")){write_bib(c(.packages()),'files/pkgs.bib')}
```
