## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE,
  fig.width = 6,
  fig.height = 4
)

## ----eval = FALSE-------------------------------------------------------------
# install.packages("DEmixR")

## -----------------------------------------------------------------------------
library(DEmixR)
set.seed(123)
x <- c(rnorm(150, mean = 0, sd = 1),
       rnorm(100, mean = 4, sd = 1))
mix2_bounds(x, "normal")

## -----------------------------------------------------------------------------
prelim_plots(x, which = c("hist", "qq"))

## -----------------------------------------------------------------------------
prelim_plots(x, which = "hist", col_density = NA)

## -----------------------------------------------------------------------------
sel <- select_best_mixture(x, n_runs = 1, NP = 25, itermax = 300, quiet = 0)
sel                 # uses print.demixr_select
sel$best$family

## -----------------------------------------------------------------------------
fit <- fit_norm2(x, n_runs = 1, NP = 25, itermax = 300, quiet = 0)

fit                 # print.demixr_fit: compact overview
summary(fit)        # summary.demixr_fit: per-component table
plot(fit)           # plot.demixr_fit: fitted mixture over a histogram

## -----------------------------------------------------------------------------
plot(fit, which = "pit")
plot(fit, which = "qq")

## -----------------------------------------------------------------------------
fit$par             # named vector: p, m1, s1, m2, s2
fit$logLik          # exact log-likelihood (log-sum-exp)
c(AIC = fit$AIC, BIC = fit$BIC)
fit$source          # "DEoptim" = converged L-BFGS-B refinement of the DE solution
fit$de_logLik       # log-likelihood reached by each DE run
fit$diagnostics$comp_n   # expected observations per component

## -----------------------------------------------------------------------------
set.seed(123)
y <- c(rlnorm(150, meanlog = 0, sdlog = 0.5),
       rlnorm(100, meanlog = 1.6, sdlog = 0.4))

fit_ln <- fit_lognorm2(y, n_runs = 1, NP = 25, itermax = 300, quiet = 0)
fit_ln
plot(fit_ln)

## -----------------------------------------------------------------------------
set.seed(123)
boot <- bootstrap_mix2(fit_ln, B = 40, parametric = TRUE, quiet = 0)
boot                # print.demixr_boot

## -----------------------------------------------------------------------------
ev <- evaluate_init(par_init = c(0.5, 0, 0.5, 1.6, 0.4), x = y,
                    family = "lognormal")
ev$success
ev$logLik

