Package {DEmixR}


Type: Package
Title: Fit Two-Component Normal and Lognormal Mixture Models
Version: 0.2.0
Description: Fits, bootstraps, and evaluates two-component normal and lognormal mixture models. Parameters are estimated by combining differential-evolution global optimization, as implemented in the 'DEoptim' package (Mullen, Ardia, Gil, Windover and Cline, 2011) <doi:10.18637/jss.v040.i06>, with a local 'L-BFGS-B' refinement step via optim(). Also provides preliminary diagnostic plots, automatic normal-versus-lognormal model selection by information criteria, and parametric or nonparametric bootstrap confidence intervals for the fitted parameters.
Imports: DEoptim (≥ 2.0.0), pbapply (≥ 1.0.0)
License: MIT + file LICENSE
Encoding: UTF-8
Suggests: knitr, rmarkdown, testthat (≥ 3.1.7)
VignetteBuilder: knitr
Config/testthat/edition: 3
NeedsCompilation: no
Config/roxygen2/version: 8.0.0
RoxygenNote: 7.3.3
Packaged: 2026-08-01 19:06:16 UTC; mac
Author: Farrokh Habibzadeh ORCID iD [aut, cre]
Maintainer: Farrokh Habibzadeh <farrokh.habibzadeh@gmail.com>
Repository: CRAN
Date/Publication: 2026-08-02 18:30:14 UTC

Core two-component mixture fitter (differential evolution + local refinement)

Description

Workhorse behind fit_norm2 and fit_lognorm2. Runs one or more differential-evolution searches via DEoptim and then refines the best candidate with a local "L-BFGS-B" optim step. Not exported; the tuning arguments documented here are the ones users pass through the ... of the exported fit functions.

Usage

.fit_mix2_core(
  x,
  family = c("lognormal", "normal"),
  lower = NULL,
  upper = NULL,
  NP = 100,
  itermax = 10000,
  reltol = 5e-06,
  steptol = 50,
  F = 0.8,
  CR = 0.9,
  strategy = 2,
  parallelType = 0,
  packages = c("stats"),
  parVar = NULL,
  n_runs = 20,
  quiet = 2,
  par_init = NULL,
  pgtol = 1e-08
)

Arguments

x

numeric data vector (strictly positive when family = "lognormal").

family

character; either "lognormal" or "normal".

lower, upper

optional numeric vectors of length 5 giving box constraints for the parameters (p, mu1, s1, mu2, s2). When NULL they are derived from the data.

NP

DEoptim population size.

itermax

maximum number of DEoptim iterations per run.

reltol, steptol

DEoptim convergence controls (relative tolerance and the number of stall iterations allowed before stopping).

F, CR, strategy

DEoptim differential-weighting, crossover-probability, and strategy controls (see DEoptim.control).

parallelType

DEoptim parallelization type (0 = serial).

packages, parVar

passed to DEoptim.control to set up parallel workers when parallelType != 0.

n_runs

number of independent DEoptim runs; the run with the best objective is refined locally.

quiet

verbosity: 0 = silent, 1 = per-run messages and DEoptim trace, 2 = a text progress bar.

par_init

optional numeric vector of length 5 giving user starting values for an additional local optimization, compared against the DE-based solution.

pgtol

projected-gradient tolerance for the "L-BFGS-B" refinement.

Value

An object of class demixr_fit (see fit_norm2).


Bootstrap mixture parameters

Description

Bootstrap mixture parameters

Usage

bootstrap_mix2(
  fit = NULL,
  x = NULL,
  par = NULL,
  family = NULL,
  B = 1000,
  parametric = TRUE,
  boot_size = NULL,
  parallelType = 0,
  quiet = 2,
  ci_level = 0.95
)

Arguments

fit

fitted object from fit_lognorm2 or fit_norm2

x

numeric vector (if fit not provided)

par

numeric vector of parameters (if fit not provided)

family

"lognormal" or "normal" (if fit not provided)

B

number of bootstrap replicates

parametric

logical, parametric bootstrap if TRUE

boot_size

size or fraction (if between 0 and 1) of bootstrap sample

parallelType

integer for DEoptim/pbapply parallelism

quiet

0/1/2 for verbosity

ci_level

confidence level

Value

an object of class demixr_boot: a list with cleaned bootstrap estimates (ests_clean), central tendency (central), and confidence intervals (ci). Has a print method.

Examples


set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
fit <- fit_norm2(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
boot <- bootstrap_mix2(fit, B = 20, quiet = 0)
boot


Evaluate initial parameter values for mixture fitting

Description

Evaluate initial parameter values for mixture fitting

Usage

evaluate_init(
  par_init,
  x,
  family = c("lognormal", "normal"),
  lower = NULL,
  upper = NULL,
  pgtol = 1e-08
)

Arguments

par_init

numeric vector of initial parameters

x

numeric vector of data

family

"lognormal" or "normal"

lower

numeric vector of lower bounds

upper

numeric vector of upper bounds

pgtol

numeric, gradient tolerance for optim

Value

list with success flag, optimized parameters, log-likelihood, and convergence

Examples

set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
ev <- evaluate_init(par_init = c(0.7, 0, 1, 4, 1), x = x, family = "normal")
ev$success

Fit 2-component lognormal mixture

Description

Fit 2-component lognormal mixture

Usage

fit_lognorm2(x, ...)

Arguments

x

numeric vector of data to fit

...

additional arguments passed to .fit_mix2_core

Value

an object of class demixr_fit: a list with the fitted parameters (par), logLik, AIC, BIC, and convergence information. Has print, summary, and plot methods.

Examples

set.seed(1)
x <- c(rlnorm(150, 0, 0.5), rlnorm(50, 2, 0.3))
fit <- fit_lognorm2(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
fit

plot(fit)


Fit 2-component normal mixture

Description

Fit 2-component normal mixture

Usage

fit_norm2(x, ...)

Arguments

x

numeric vector of data to fit

...

additional arguments passed to .fit_mix2_core

Value

an object of class demixr_fit: a list with the fitted parameters (par), logLik, AIC, BIC, and convergence information. Has print, summary, and plot methods.

Examples

set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
fit <- fit_norm2(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
fit

plot(fit)


Plot a fitted DEmixR mixture model over a histogram of the data

Description

Plot a fitted DEmixR mixture model over a histogram of the data

Usage

## S3 method for class 'demixr_fit'
plot(
  x,
  hist_bins = 60,
  col_hist = "gray85",
  col_total = "black",
  col_comp1 = "steelblue",
  col_comp2 = "firebrick",
  ...
)

Arguments

x

an object of class demixr_fit

hist_bins

number of histogram bins

col_hist

histogram fill color

col_total

color of the fitted overall mixture density

col_comp1

color of the first fitted component density

col_comp2

color of the second fitted component density

...

further arguments passed to hist

Value

no return value, called for side effects (generating a plot)

Examples

set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
fit <- fit_norm2(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
plot(fit)

Preliminary diagnostic plots

Description

Preliminary diagnostic plots

Usage

prelim_plots(
  x,
  which = c("hist"),
  hist_bins = 60,
  col_hist = "gray85",
  col_density = "darkorange",
  col_qq = "gray60",
  col_line = "darkorange"
)

Arguments

x

numeric vector

which

character vector: "hist", "qq", "pp", "logqq"

hist_bins

number of bins for histogram

col_hist

color for histogram

col_density

color for the kernel-density curve drawn over the histogram. Set to NA to omit the density curve entirely; in that case the histogram title is "Histogram" instead of "Histogram with density".

col_qq

color for qq points

col_line

color for lines in "qq", "pp", "logqq" plots

Value

no return value, called for side effects (generating plots)

Examples

set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
prelim_plots(x, c("hist", "qq"), hist_bins = 15)

# Histogram without the kernel-density overlay (title becomes "Histogram")
prelim_plots(x, "hist", col_density = NA)

Print bootstrap results from a DEmixR mixture fit

Description

Print bootstrap results from a DEmixR mixture fit

Usage

## S3 method for class 'demixr_boot'
print(x, digits = 4, ...)

Arguments

x

an object of class demixr_boot, as returned by bootstrap_mix2

digits

number of significant digits to print

...

further arguments passed to or from other methods (currently unused)

Value

x, invisibly

Examples


set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
fit <- fit_norm2(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
boot <- bootstrap_mix2(fit, B = 20, quiet = 0)
print(boot)


Print a fitted DEmixR mixture model

Description

Print a fitted DEmixR mixture model

Usage

## S3 method for class 'demixr_fit'
print(x, digits = 4, ...)

Arguments

x

an object of class demixr_fit, as returned by fit_norm2 or fit_lognorm2

digits

number of significant digits to print

...

further arguments passed to or from other methods (currently unused)

Value

x, invisibly

Examples

set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
fit <- fit_norm2(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
print(fit)

Print model-selection results from DEmixR

Description

Print model-selection results from DEmixR

Usage

## S3 method for class 'demixr_select'
print(x, digits = 4, ...)

Arguments

x

an object of class demixr_select, as returned by select_best_mixture

digits

number of significant digits to print

...

further arguments passed to or from other methods (currently unused)

Value

x, invisibly

Examples


set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
mix <- select_best_mixture(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
print(mix)


Print a summary of a fitted DEmixR mixture model

Description

Print a summary of a fitted DEmixR mixture model

Usage

## S3 method for class 'summary.demixr_fit'
print(x, digits = 4, ...)

Arguments

x

an object of class summary.demixr_fit, as returned by summary.demixr_fit

digits

number of significant digits to print

...

further arguments passed to or from other methods (currently unused)

Value

x, invisibly

Examples

set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
fit <- fit_norm2(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
print(summary(fit))

Select best mixture model (lognormal or normal) based on BIC

Description

Select best mixture model (lognormal or normal) based on BIC

Usage

select_best_mixture(x, n_runs = 1, NP = 50, itermax = 10000, quiet = 2)

Arguments

x

numeric vector

n_runs

number of DEoptim runs

NP

population size for DEoptim

itermax

maximum iterations

quiet

verbosity

Value

an object of class demixr_select: a list with the best-fitting model (best), all fitted models (all), and the BIC of each (BICs). Has a print method.

Examples


set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
mix <- select_best_mixture(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
mix
mix$best$family


Summarize a fitted DEmixR mixture model

Description

Summarize a fitted DEmixR mixture model

Usage

## S3 method for class 'demixr_fit'
summary(object, ...)

Arguments

object

an object of class demixr_fit

...

further arguments passed to or from other methods (currently unused)

Value

an object of class summary.demixr_fit: a list holding the per-component weight/location/scale table together with the family, sample size, log-likelihood, AIC, BIC, and convergence code. Has a print method.

Examples

set.seed(1)
x <- c(rnorm(150, 0, 1), rnorm(50, 4, 1))
fit <- fit_norm2(x, n_runs = 1, NP = 20, itermax = 200, quiet = 0)
summary(fit)