An R package implementing an S3 class for probability distribution mixtures, with dedicated methods for random sampling, density visualization, and basic descriptive statistics. Includes a specialized subclass for mixtures of normal distributions.
MixLaw provides an S3 framework to represent and manipulate mixtures of probability distributions in R. A mixture distribution is a weighted combination of several component distributions, widely used in statistical modeling for capturing heterogeneous populations, latent subgroups, or multi-modal phenomena.
The package exposes:
- A general-purpose S3 class
melange_distthat accepts any list of one-argument random-number generators (e.g.rnorm,rexp,rgamma) along with a vector of mixture weights. - A specialized subclass
melange_normalfor mixtures of normal distributions, parameterized directly by component means, standard deviations, and weights. - Dedicated methods for the most common operations: random sampling, density plotting, descriptive statistics, and printing.
This package was developed as part of MAT8186 — Techniques avancées en programmation statistique : R, a graduate course at Université du Québec à Montréal (UQAM). The objective was to design and document a fully structured R package, demonstrating object-oriented programming through S3 dispatch, formal documentation with roxygen2, and unit testing with testthat.
- S3 class hierarchy with parent class
melange_distand specialized subclassmelange_normal. - Random sampling of mixture realizations via an S3-dispatched
rand()generic. - Density visualization through a custom
plot()method. - Descriptive statistics:
mean()andquantile()methods on mixture realizations. - Informative printing with a tabular summary of components and weights.
- Unit tests via
testthat(edition 3). - Full roxygen2 documentation with executable examples on every function.
The package is hosted on GitHub and can be installed directly using devtools:
# Install devtools if needed
install.packages("devtools")
# Install MixLaw from GitHub
devtools::install_github("komiayi/MixLaw")
# Load the package
library(MixLaw)
# Open the package help index
?MixLawlibrary(MixLaw)
# Build a mixture of three components: normal, exponential, and Gamma(shape=10)
M <- melange_dist(
list(rnorm, rexp, \(n) rgamma(n, 10)),
c(0.1, 0.2, 0.7)
)
# Inspect the object
print(M)
# Generate 100 realizations
sample_M <- rand(M, n = 100)
# Compute the empirical mean and quantiles
mean(M)
quantile(M, probs = c(0.25, 0.5, 0.75))
# Plot the empirical density
plot(M, color = "red", main = "Mixture density")# Mixture of two normals: N(1, 0.1²) and N(0.2, 0.8²), equal weights by default
N <- melange_normal(moy = c(1, 0.2), ect = c(0.1, 0.8))
# Inspect the components
print(N)
# Sample, summarize, and plot
mean(N)
quantile(N)
plot(N, main = "Normal mixture density")MixLaw/
├── R/ # Source code of the S3 class and its methods
│ ├── melange_dist.R # Constructor of the parent S3 class
│ ├── melange_normal.R # Specialized subclass for normal mixtures
│ ├── rand_mellange_dist.R # rand() S3 generic and method
│ ├── plot_melange_dist.R # plot() method
│ ├── print_melange_dist.R # print() method
│ ├── mean_melange_dist.R # mean() method
│ ├── quantile_melange_dist.R # quantile() method
│ └── MixLaw-package.R # Package-level documentation
├── man/ # Generated Rd documentation (roxygen2 output)
├── tests/ # Unit tests (testthat, edition 3)
├── DESCRIPTION # Package metadata
├── NAMESPACE # Exports and imports
├── .Rbuildignore
└── README.md
| Generic | Class | Purpose |
|---|---|---|
melange_dist() |
constructor | Build a mixture from a list of RNG functions and weights |
melange_normal() |
constructor | Build a normal mixture from means, standard deviations, weights |
rand() |
S3 generic | Sample n realizations from a mixture |
print() |
S3 method | Tabular summary of components and weights |
plot() |
S3 method | Empirical density plot of mixture realizations |
mean() |
S3 method | Empirical mean of realizations |
quantile() |
S3 method | Empirical quantiles of realizations |
The package was designed as a complete exercise in modern R package authoring. It illustrates:
- S3 object orientation, including class inheritance via vectorized class attributes (
class = c("melange_normal", "melange_dist")) and method dispatch throughUseMethod. - Custom S3 generics (
rand) alongside extensions of base R generics (print,plot,mean,quantile). - Defensive programming with explicit input validation and informative error messages.
- roxygen2 documentation with
@param,@return,@examples, and selective imports via@importFromand@import. - Unit testing under the
testthatframework (edition 3). - CRAN-style package layout, including
DESCRIPTION,NAMESPACE,R/,man/,tests/, and.Rbuildignore.
Distributed under the GNU General Public License v2.0 (GPL-2). See the LICENSE terms for full details.
Komi Roger Ayi Biostatistician — Data Scientist Université du Québec à Montréal · Montréal, Québec, Canada