---
title: "Broad-sense heritability in plant breeding"
description: 'How to calculate the heritability in plant breeding'
author: "Maria Belen Kistner & Flavio Lozano-Isla"
output: rmarkdown::html_vignette
bibliography: [files/pkgs.bib, files/book.bib]
link-citations: true
colorlinks: yes
csl: https://www.zotero.org/styles/crop-science
nocite: |  
  [@schmidt2019Estimating; @schmidt2019Heritability; @bernal-vasquez2016Outliera]
vignette: >
  %\VignetteIndexEntry{Broad-sense heritability in plant breeding}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
library(inti)
library(knitr)

knitr::opts_chunk$set(
    fig.align = "center" # Center images in the export file
  , out.width = "98%" # Figure width in html
  , echo = TRUE # Avoid print code in the export file
  , message = FALSE # Avoid print messages in the export file
  , warning = FALSE # Avoid print messages in the export file
  , collapse = TRUE
  , comment = "#>"
  )

options(
    OutDec= "." # Use "." instead of "," in the decimal values
  , scipen = 99 # Avoid use "6e-04"
  , knitr.kable.NA = "" # NA values will appear as empty cell
  , knitr.table.format = "pipe" # Format for export tables
  , citr.use_betterbiblatex = FALSE # For zotero addin 
  ) 
```

# 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.

### 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 pkgs}
library(inti)
library(agridat)
```

## H2cal function

```{r fig.width= 7, fig.height=5}
 dt <- john.alpha
 hr <- H2cal(data = dt
            , trait = "yield"
            , gen.name = "gen"
            , rep.n = 3
            , fix.model = "rep + (1|rep:block) + gen"
            , ran.model = "rep + (1|rep:block) + (1|gen)"
            , emmeans = TRUE
            , plot_diag = TRUE
            , plot_dots = "rep"
            , outliers.rm = TRUE
            )
```

## Model information

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

## Variance component

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

## Best Linear Unbiased Predictors (BLUPs)

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

## Best Linear Unbiased Estimators (BLUEs)

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

## Outliers

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

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

# References

::: {#refs}
:::

```{r references, echo=FALSE}
if(!file.exists("files/pkgs.bib")){write_bib(c(.packages()),'files/pkgs.bib')}
```

