library(BMix)
#> Warning: replacing previous import 'cli::num_ansi_colors' by
#> 'crayon::num_ansi_colors' when loading 'BMix'
#> Warning: replacing previous import 'crayon::%+%' by 'ggplot2::%+%' when loading
#> 'BMix'
#> ✔ Loading BMix, 'Binomial and Beta-Binomial univariate mixtures'. Support : <https://caravagnalab.github.io/BMix/>Mixture models with BMix
Simulated data
We generate some simple data obtained from Binomial mixtures with Binomial probability , drawing , and samples each. The number of trials is fixed to .
data = data.frame(
successes = c(
rbinom(300, 100, .5), # First component - 300 points, peak at 0.5
rbinom(700, 100, .25), # Second component - 700 points, peak at 0.25
rbinom(700, 100, .1)), # Third component - 700 points, peak at 0.1
trials = 100)
print(head(data))
#> successes trials
#> 1 44 100
#> 2 48 100
#> 3 46 100
#> 4 43 100
#> 5 48 100
#> 6 56 100
require(ggplot2)
#> Loading required package: ggplot2
ggplot(data, aes(successes/trials)) + geom_histogram(binwidth = 0.01) + theme_linedraw()
Binomial mixtures
Fitting the data
Fitting is done with function bmixfit, the default
parameters test a number of configurations of Binomial components, but
sets to
the number of Beta-Binomial components.
# Default parameters
x = bmixfit(data, K.Binomials = 1:3, K.BetaBinomials = 0)
#>
#> ── BMix fit ────────────────────────────────────────────────────────────────────
#> ℹ Binomials k_B = 1, 2, and 3, Beta-Binomials k_BB = 0; 6 fits to run.
#> ℹ Bmix best fit completed in 0.01 mins
#> ── [ BMix ] My BMix model n = 1700 with k = 3 component(s) (3 + 0) ─────────────
#> • Clusters: π = 42% [Bin 3], 41% [Bin 1], and 17% [Bin 2], with π > 0.
#> • Binomial Bin 1 with mean = 0.25103641718154.
#> • Binomial Bin 2 with mean = 0.492736139245239.
#> • Binomial Bin 3 with mean = 0.0996534561700418.
#> ℹ Score (model selection): ICL = 12917.79.
# Maybe one could compare x to this
# y = bmixfit(data, K.Binomials = 0, K.BetaBinomials = 1:2)The fit is an object of class bmix which has S3 methods
available.
print(x)
#> ── [ BMix ] My BMix model n = 1700 with k = 3 component(s) (3 + 0) ─────────────
#> • Clusters: π = 42% [Bin 3], 41% [Bin 1], and 17% [Bin 2], with π > 0.
#> • Binomial Bin 1 with mean = 0.25103641718154.
#> • Binomial Bin 2 with mean = 0.492736139245239.
#> • Binomial Bin 3 with mean = 0.0996534561700418.
#> ℹ Score (model selection): ICL = 12917.79.Note: The input data is not stored inside the fit, you have keep it and pass it to plotting functions. Keep it consistent, the package is not doing any checks about the input.
You have getters to access the clusters (as tibble) and
the fit parameters.
# Augment data with cluster labels and latent variables
Clusters(x, data)
#> # A tibble: 1,700 × 6
#> successes trials cluster `Bin 1` `Bin 2` `Bin 3`
#> <int> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 44 100 Bin 2 7.10e- 4 0.999 1.37e-15
#> 2 48 100 Bin 2 7.28e- 6 1.000 2.60e-19
#> 3 46 100 Bin 2 7.19e- 5 1.000 1.89e-17
#> 4 43 100 Bin 2 2.23e- 3 0.998 1.16e-14
#> 5 48 100 Bin 2 7.28e- 6 1.000 2.60e-19
#> 6 56 100 Bin 2 7.63e-10 1.000 9.39e-27
#> 7 57 100 Bin 2 2.43e-10 1.000 1.10e-27
#> 8 49 100 Bin 2 2.32e- 6 1.000 3.05e-20
#> 9 50 100 Bin 2 7.37e- 7 1.000 3.59e-21
#> 10 53 100 Bin 2 2.37e- 8 1.000 5.80e-24
#> # ℹ 1,690 more rows
# Obtain for every fit component the mean and its overdispersion.
# Binomial components have 0 overdispersion by definition.
Parameters(x)
#> # A tibble: 3 × 3
#> cluster mean overdispersion
#> <chr> <dbl> <dbl>
#> 1 Bin 1 0.251 0
#> 2 Bin 2 0.493 0
#> 3 Bin 3 0.0997 0Plotting the fits
You can plot the clustering assignments (hard clustering).
plot_clusters(x, data)
You can plot the density, in frequency space. For this a number of
trials needs to be fixed; by default BMix takes the median
number of trials in the input data, but you can decide to use any other
interger number.
plot_density(x, data)
You can visualise the result of the model selection grid as a heatmap; the best model is the one that minimizes the chosen score, which is by default the Integrated Classification Likelihood, an extension of the Bayesian Information Criterion that accounts for the entropy of the latent variables.

Then plot assembles all these plots using the
cowplot package.
BMix::plot.bmix(x, data) 
Beta-Binomial mixtures
If you want to test the same model with only Beta-Binomial components
you can run function bmixfit as follows.
# Custom parameters
x = bmixfit(data,
K.Binomials = 0,
K.BetaBinomials = 1:3)
#>
#> ── BMix fit ────────────────────────────────────────────────────────────────────
#> ℹ Binomials k_B = 0, Beta-Binomials k_BB = 1, 2, and 3; 6 fits to run.
#> ℹ Bmix best fit completed in 0.07 mins
#> ── [ BMix ] My BMix model n = 1700 with k = 3 component(s) (0 + 3) ─────────────
#> • Clusters: π = 42% [BBin 2], 41% [BBin 1], and 18% [BBin 3], with π > 0.
#> ℹ Score (model selection): ICL = 12895.54.
# Show outputs
print(x)
#> ── [ BMix ] My BMix model n = 1700 with k = 3 component(s) (0 + 3) ─────────────
#> • Clusters: π = 42% [BBin 2], 41% [BBin 1], and 18% [BBin 3], with π > 0.
#> ℹ Score (model selection): ICL = 12895.54.
BMix::plot.bmix(x, data) 
Comparing mixtures
You can compare models with both type of components.
# Custom parameters
x = bmixfit(data,
K.Binomials = 0:3,
K.BetaBinomials = 0:3)
#>
#> ── BMix fit ────────────────────────────────────────────────────────────────────
#> ℹ Binomials k_B = 0, 1, 2, and 3, Beta-Binomials k_BB = 0, 1, 2, and 3; 12 fits to run.
#> ℹ Bmix best fit completed in 0.07 mins
#> ── [ BMix ] My BMix model n = 1700 with k = 3 component(s) (3 + 0) ─────────────
#> • Clusters: π = 42% [Bin 2], 41% [Bin 1], and 17% [Bin 3], with π > 0.
#> • Binomial Bin 1 with mean = 0.251598139285135.
#> • Binomial Bin 2 with mean = 0.100347088720446.
#> • Binomial Bin 3 with mean = 0.492194004503043.
#> ℹ Score (model selection): ICL = 12921.86.
# Show outputs
print(x)
#> ── [ BMix ] My BMix model n = 1700 with k = 3 component(s) (3 + 0) ─────────────
#> • Clusters: π = 42% [Bin 2], 41% [Bin 1], and 17% [Bin 3], with π > 0.
#> • Binomial Bin 1 with mean = 0.251598139285135.
#> • Binomial Bin 2 with mean = 0.100347088720446.
#> • Binomial Bin 3 with mean = 0.492194004503043.
#> ℹ Score (model selection): ICL = 12921.86.
BMix::plot.bmix(x, data) 
