---
title: "Distribution Building and Comparison"
description: Learn to use mixIndependR to build and compare observed and expected distributions of number of heterozygous loci (K) and number of shared alleles (X). 
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Distribution Building and Comparison}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
This vignette will introduce you to how the package `mixIndependR` will build and compare the observed and expected distributions for number of heterozygous loci (K) and number of shared alleles (X).


```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```
```{r load, include = FALSE}
library(mixIndependR)
library(ggplot2)
```
With required parameters obtained from "basic genetics" part, observed and expected distributions can be built:

```{r,include=FALSE}
x <- mixexample
p <- AlleleFreq(x)
h <-Heterozygous(x)
H <- RxpHetero(h,p,HWE=FALSE)
AS<-AlleleShare(x,replacement =FALSE)
e <-RealProAlleleShare(AS)
```
`FreqHetero` and `DistHetero` build the observed and expected distribution for number of heterozygous loci (K).
```{r}
ObsDist_K<-FreqHetero(h)
ExpDist_K<- DistHetero(H)
```
`FreqAlleleShare` and `DistAlleleShare` build the observed and expected distribution for number of shared alleles (X).
```{r}
ObsDist_X<-FreqAlleleShare(AS)
ExpDist_X<-DistAlleleShare(e)
```
`ComposPare`s convert the above distributions into a format suitable for visualization. With the `trans=TRUE`, the observed frequencies and the expected density would be in separate columns. If `trans=FALSE`, two variables would be “OvE” (denoting the status of “observed or expected”) and “frequencies”.

```{r}
df_K <- ComposPare_K(h,ExpDist_K,trans = F)
df_X <- ComposPare_X(AS,ExpDist_X,trans = F)
```
```{r echo=FALSE, fig.height=4, fig.show='hold', fig.width=7}

ggplot(df_K,aes(x=freq))+
  geom_histogram(aes(y=..density..,color=OvE,fill=OvE),alpha=0.5,binwidth = 1,position = "identity")+
  ggtitle("No. of Heterozyous loci")+
  xlab("No. of Heterozygous Loci(K)")+ylab("Density/Probability")+
  stat_function(data=ExpDist_K,mapping = aes(x=K,y=Density),fun = splinefun(ExpDist_K$K,ExpDist_K$Density),color="Red")

ggplot(df_X,aes(x=freq))+
  geom_histogram(aes(y=..density..,color=OvE,fill=OvE),alpha=0.5,binwidth = 1,position = "identity")+
  ggtitle("No. of Shared Alleles")+
  xlab("No. of Shared Alleles(X)")+ylab("Density/Probability")+
  stat_function(data=ExpDist_X,mapping = aes(x=X,y=Density),fun = splinefun(ExpDist_X$X,ExpDist_X$Density),color="Red")

```
