The default model in `rubias`

is a conditional model in which inference is done with the baseline (reference) allele counts fixed. The package now also includes a fully Bayesian GSI model, as is implemented in the software packages `BAYES`

and `cBayes`

, among others. This model differs from the conditional model in two main respects:

- Individuals within the mixture that are allocated (on any particular step of the MCMC) to one of the reference samples have their genotypes added to that reference sample’s allele counts.
- These updated allele counts are used as Dirichlet parameters to explicitly draw an estimate of baseline allele frequecies, from which genotype likelihoods are calculated (in the conditional model, genotype likelihoods are pulled straight from the Dirichlet parameters using a Compound Dirichlet-Multinomial distribution).

Ideally, updating the reference allele counts with mixture genotypes will refine the estimate of allele frequencies in that sample. This is especially useful in cases where the reference dataset is small relative to the number of mixture individuals, as well as those in which the mixture contains populations not present in the baseline. However, the fully Bayesian model is much more computationally intensive, and can exhibit pathological behavior when reference collections are not well-differentiated.

Load the required packages first:

```
library(rubias)
library(tidyverse)
```

```
##
## Attaching package: 'dplyr'
```

```
## The following objects are masked from 'package:stats':
##
## filter, lag
```

```
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
```

The basic way to invoke the fully Bayesian model is to use the `infer_mixture`

function with the `method`

option set to “BR” (for “baseline resampling”). **NOTE:** We use very few reps here to not take too much time when checking this package for CRAN. You should use more…

```
<- infer_mixture(
full_model reference = small_chinook_ref,
mixture = small_chinook_mix,
gen_start_col = 5,
method = "BR",
reps = 100,
burn_in = 35
)
```

```
## Collating data; compiling reference allele frequencies, etc. time: 0.23 seconds
## Computing reference locus specific means and variances for computing mixture z-scores time: 0.02 seconds
## Working on mixture collection: rec3 with 29 individuals
## calculating log-likelihoods of the mixture individuals. time: 0.00 seconds
## performing 100 sweeps of method "BR", 35 sweeps of which are burn-in. time: 0.05 seconds
## tidying output into a tibble.
```

```
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
## Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
```

```
## time: 0.41 seconds
## Working on mixture collection: rec1 with 36 individuals
## calculating log-likelihoods of the mixture individuals. time: 0.00 seconds
## performing 100 sweeps of method "BR", 35 sweeps of which are burn-in. time: 0.04 seconds
## tidying output into a tibble. time: 0.40 seconds
## Working on mixture collection: rec2 with 35 individuals
## calculating log-likelihoods of the mixture individuals. time: 0.00 seconds
## performing 100 sweeps of method "BR", 35 sweeps of which are burn-in. time: 0.04 seconds
## tidying output into a tibble. time: 0.37 seconds
```

Note that the output generated by “BR” is still a list of four tidy data frames, but the `bootstrapped_proportions`

data frame of methods “MCMC” and “PB” is replaced with an `allele_frequencies`

data frame. This data frame contains the posterior mean allele frequencies (theta) for each population, updated based on the allocations of mixture individuals throughout MCMC. The first three columns specify the mixture collection, locus and allele in question, and all subsequent columns report the frequency for a particular population.

`$allele_frequencies full_model`

```
## # A tibble: 546 × 9
## mixture_collecti… locus allele Deer_Cr_sp Feather_H_fa Sacramento_H Eel_R
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 rec3 AldB1.1… 2 0.833 0.918 0.900 0.936
## 2 rec3 AldB1.1… 4 0.167 0.0821 0.100 0.0636
## 3 rec3 AldoB4.… 1 0.0316 0.00128 0.000915 0.0447
## 4 rec3 AldoB4.… 4 0.968 0.999 0.999 0.955
## 5 rec3 OTNAML1… 1 0.227 0.202 0.449 0.243
## 6 rec3 OTNAML1… 3 0.773 0.798 0.551 0.757
## 7 rec3 OTSBMP.… 2 0.846 0.769 0.553 0.977
## 8 rec3 OTSBMP.… 4 0.154 0.231 0.447 0.0234
## 9 rec3 OTSTF1.… 3 0.892 0.886 0.683 0.862
## 10 rec3 OTSTF1.… 4 0.108 0.114 0.317 0.138
## # … with 536 more rows, and 2 more variables: Klamath_IGH_fa <dbl>,
## # Umpqua_sp <dbl>
```

Also note that the log-likelihoods and Z-scores included in the `indiv_posteriors`

output data frame are calculated using only the reference allele counts prior to MCMC, and so do **not** utilize the full model results.

Let’s compare the results of this to those from the conditional model:

```
set.seed(15)
<- infer_mixture(
cond_model reference = small_chinook_ref,
mixture = small_chinook_mix,
gen_start_col = 5,
method = "MCMC"
)
```

```
## Collating data; compiling reference allele frequencies, etc. time: 0.20 seconds
## Computing reference locus specific means and variances for computing mixture z-scores time: 0.02 seconds
## Working on mixture collection: rec3 with 29 individuals
## calculating log-likelihoods of the mixture individuals. time: 0.00 seconds
## performing 2000 total sweeps, 100 of which are burn-in and will not be used in computing averages in method "MCMC" time: 0.03 seconds
## tidying output into a tibble. time: 0.03 seconds
## Working on mixture collection: rec1 with 36 individuals
## calculating log-likelihoods of the mixture individuals. time: 0.00 seconds
## performing 2000 total sweeps, 100 of which are burn-in and will not be used in computing averages in method "MCMC" time: 0.03 seconds
## tidying output into a tibble. time: 0.03 seconds
## Working on mixture collection: rec2 with 35 individuals
## calculating log-likelihoods of the mixture individuals. time: 0.00 seconds
## performing 2000 total sweeps, 100 of which are burn-in and will not be used in computing averages in method "MCMC" time: 0.03 seconds
## tidying output into a tibble. time: 0.03 seconds
```

```
<- cond_model$mixing_proportions %>%
comppi mutate(cond_pi = pi, full_pi = full_model$mixing_proportions$pi)
ggplot(comppi, aes(x = cond_pi, y = full_pi, colour = collection)) +
geom_point() +
geom_abline(slope = 1, intercept = 0) +
facet_wrap(~mixture_collection) +
theme(legend.position = "bottom")
```

The two methods are largely in agreement for this small test set.

When collections are poorly resolved, the fully Bayesian model may show pathological behaviors such as allocating all individuals to only one of the closely related populations. One way to reduce this behavior is to initialize the fully Bayesian model with the output from the conditional model. `rubias`

explicitly supports this through the options `prelim_reps`

and `prelim_burn_in`

: if changed from the `NULL`

default, these parameters specify conditional MCMC cycles to perform, the posterior mean mixing proportions of which are used as the initial mixing proportion estimates in the fully Bayesian model. For example:

```
set.seed(15)
<- infer_mixture(
prelim_full_model reference = small_chinook_ref,
mixture = small_chinook_mix,
gen_start_col = 5,
method = "BR",
reps = 100,
burn_in = 35,
prelim_reps = 100,
prelim_burn_in = 50,
)
```

```
## Collating data; compiling reference allele frequencies, etc. time: 0.21 seconds
## Computing reference locus specific means and variances for computing mixture z-scores time: 0.02 seconds
## Working on mixture collection: rec3 with 29 individuals
## calculating log-likelihoods of the mixture individuals. time: 0.01 seconds
## performing 100 initial sweeps, 50 of which are burn-in and will not be used in computing averages to initialize starting point for method "BR". time: 0.00 seconds
## performing 100 sweeps of method "BR", 35 sweeps of which are burn-in. time: 0.04 seconds
## tidying output into a tibble. time: 0.38 seconds
## Working on mixture collection: rec1 with 36 individuals
## calculating log-likelihoods of the mixture individuals. time: 0.00 seconds
## performing 100 initial sweeps, 50 of which are burn-in and will not be used in computing averages to initialize starting point for method "BR". time: 0.00 seconds
## performing 100 sweeps of method "BR", 35 sweeps of which are burn-in. time: 0.04 seconds
## tidying output into a tibble. time: 0.38 seconds
## Working on mixture collection: rec2 with 35 individuals
## calculating log-likelihoods of the mixture individuals. time: 0.00 seconds
## performing 100 initial sweeps, 50 of which are burn-in and will not be used in computing averages to initialize starting point for method "BR". time: 0.00 seconds
## performing 100 sweeps of method "BR", 35 sweeps of which are burn-in. time: 0.04 seconds
## tidying output into a tibble. time: 0.38 seconds
```

```
<- prelim_full_model$mix_prop_traces %>%
prelimpi filter(sweep == 0) %>%
select(-sweep) %>%
mutate(prelim_pi = pi, full_pi = prelim_full_model$mixing_proportions$pi)
ggplot(prelimpi, aes(x = prelim_pi, y = full_pi, colour = collection)) +
geom_point() +
geom_abline(slope = 1, intercept = 0) +
facet_wrap(~mixture_collection) +
theme(legend.position = "bottom")
```

In this case, the results are largely identical to those from uniform initial proportions, potentially because the collections are well-resolved to begin with.

To incorporate baseline updates while keeping runtimes reasonable, genotype likelihood calculations in the fully Bayesian model are parallelized using `RcppParallel`

. By default, `RcppParallel`

runs one thread per core on your machine; to check the number of cores available, use the `detectCores()`

function from package `parallel`

. However, this default behavior is dangerous on HPC systems, where a mismatch between the number of cores requested by the user and the number of threads demanded by `RcppParallel`

might cause the job to abort. In these cases, the number of threads should be manually set with `RcppParallel::setThreadOptions`

. For example, to set the number of threads used to 1:

`::setThreadOptions(numThreads = 1) RcppParallel`

Then you would run it just like before:

```
<- infer_mixture(
full_model reference = small_chinook_ref,
mixture = small_chinook_mix,
gen_start_col = 5,
method = "BR"
)
```

This will slow things down relatively to using multiple cores.

To reset the threading options to utilize all available cores, just use:

`::setThreadOptions(numThreads = RcppParallel::defaultNumThreads()) RcppParallel`