countreg_mix() estimates finite mixtures of count distributions and
finite-mixture (latent-class) count regressions by maximum likelihood.
countreg_lc() is the same model under the latent-class vocabulary
(classes, membership_formula): both functions normalize their
arguments into one specification and call one estimation engine, so
equivalent calls give the same fit.
For unit \(u\) (a row, or a panel when group_var is given) the
probability of the observed counts is
$$L_u = \sum_{k=1}^K \pi_{uk} \prod_{i \in u} f_k(y_i \mid x_i,
\theta_k),$$
where \(f_k\) is the probability mass function of component \(k\)
(any family of countreg(); see count_families) and \(\pi_{uk}\) are
the mixing (class-membership) probabilities. The log-likelihood is
\(\sum_u w_u \log L_u\), with frequency weights \(w_u\).
Usage
countreg_mix(
formula,
data,
family = "POISSON",
components = 2L,
mixing_formula = ~1,
fixed_mixing = NULL,
shared = NULL,
dis_param_formula_1 = NULL,
dis_param_formula_2 = NULL,
offset = NULL,
weights = NULL,
group_var = NULL,
method = "BFGS",
max.iters = 1000,
start.vals = NULL,
n_starts = 10L,
seed = NULL,
stderr = c("normal", "robust", "boot"),
bootstraps = NULL,
ndraws = 1500,
engine = "quadrature",
keep_data = TRUE,
verbose = FALSE,
control = mixture_control()
)
countreg_lc(
formula,
data,
family = "POISSON",
classes = 2L,
membership_formula = ~1,
fixed_mixing = NULL,
shared = NULL,
dis_param_formula_1 = NULL,
dis_param_formula_2 = NULL,
offset = NULL,
weights = NULL,
group_var = NULL,
method = "BFGS",
max.iters = 1000,
start.vals = NULL,
n_starts = 10L,
seed = NULL,
stderr = c("normal", "robust", "boot"),
bootstraps = NULL,
ndraws = 1500,
engine = "quadrature",
keep_data = TRUE,
verbose = FALSE,
control = lc_control()
)Arguments
- formula
a model formula for the counts, as in
countreg(). Usey ~ 1for a mixture of distributions without covariates. Offsets can be given withoffset()or theoffsetargument; the same offset applies to every component.- data
a data frame.
- family
a family code (see count_families) used by every component, or one code per component (e.g.
c(base = "POISSON", tail = "NB2")): a jointly estimated mixture of different families. Names become display labels of the components.- components, classes
the number of components (classes) \(K\). When
familyhas several entries, it can be omitted.- mixing_formula, membership_formula
a one-sided formula for the mixing (class-membership) probabilities, a multinomial logit with the last component as reference:
~ 1(default) gives constant proportions,~ zlets them depend onz(a mixture of experts). For panel models, its variables must be constant within each panel.- fixed_mixing
optional known mixing probabilities (one per component, nonnegative, summing to 1). They are not estimated and add no parameters. These are not frequency weights or starting values. Zero probabilities are not allowed (the component would be unidentified).
optional list saying which parameters are the same in all components:
mean, a character vector of formula terms (use"(Intercept)"for the intercept; a factor is shared as a whole term), anddis1,dis2(TRUE/FALSE) for the blocks of the first and second distribution parameter. Distribution parameters can only be shared between components of the same family. By default nothing is shared.- dis_param_formula_1, dis_param_formula_2
optional one-sided formulas for the first and second distribution parameter of the components, as in
countreg(). They apply to every component whose family has the parameter (coefficients are component-specific unless shared).- offset, weights
an offset (column names, log scale) and frequency weights, as in
countreg(). Withgroup_var, weights must be constant within each panel: a weight stands for that many independent copies of the panel.- group_var
optional column name(s) identifying panels. All rows of a panel belong to the same (unknown) component, so the panel, not the row, is the independent unit; rows are independent given the component.
- method
optimizer for the direct maximization of the observed log-likelihood (see
countreg()); default"BFGS".- max.iters
maximum number of iterations of the direct maximization.
- start.vals
optional starting values: a vector with one value per parameter (named as in
coef(), or in that order). It is used as the first start; the others are generated.- n_starts
number of starts. Starts are reproducible from
seedand are recorded in the fitted object (fit$model$starts).- seed
optional random seed for the generated starts (and the bootstrap). The caller's random-number stream is not changed. When
NULL, a seed is drawn from the current stream and stored.- stderr
"normal"(from the Hessian of the observed-data log-likelihood),"robust"(sandwich with unit-level scores) or"boot"(nonparametric bootstrap of units, refitting the full mixture with several starts and aligning component labels).- bootstraps
number of bootstrap replicates for
stderr = "boot"(default 200).- ndraws, engine
numerical settings of the mixed-Poisson families (PLN, PGE, PW, PLL) within each component, as in
countreg(). They are unrelated to the number of components.- keep_data
logical; store the training data in the fitted object.
- verbose
logical; report the progress of each start.
- control
a
mixture_control()object.
Value
An object of class c("countreg_mix", "flexCountReg")
(countreg_mix()) or c("countreg_lc", "countreg_mix", "flexCountReg") (countreg_lc()). The methods coef(), vcov(),
logLik(), AIC(), BIC(), summary(), predict()
(predict.countreg_mix()), fitted(), residuals(), simulate(),
tidy(), glance() and augment() work as for other models, and
component_probs() returns prior or posterior class probabilities.
rootogram(), cureplot() and margEffTable() (delta-method standard
errors) accept mixture models.
Details
Estimation
Each start is a response-based or random preliminary partition of the
units, turned into parameters by one M-step. Starts are improved by the
EM algorithm (posterior class probabilities in the E-step; weighted
updates of the component and mixing parameters in the M-step, with
shared parameters updated jointly), and the best distinct solutions are
polished by maximizing the observed-data log-likelihood directly with
maxLik. The selected solution is the best polished one that converged.
The start table (fit$model$starts) records every start's strategy,
log-likelihood, EM iterations and status, and whether the best solution
was reached by several starts; mixtures often have several local maxima,
so a solution found by one start only deserves caution.
Components of the same family are exchangeable: their numbering is
arbitrary. After estimation they are numbered in increasing order of
their average expected count over the training rows (a display
convention). Mixtures of different families keep the order given in
family.
Parameters
The coefficients are named Component<k>:Mean:<term>,
Component<k>:<parameter> (e.g. Component2:ln(alpha)),
Shared:... for shared parameters, and Mixing:Component<k>:<term> for
the membership logit of component \(k\) against the last component.
Component expected counts use each family's own mean (for PLN and PLL
it includes the lognormal factor), so shared link-scale coefficients do
not imply equal component means.
Inference and model choice
The covariance matrix is the inverse of the observed information of the
whole free parameter vector (not of the separate M-steps). Near a
boundary (an almost empty component, or two almost identical
components) it is unreliable; use the bootstrap and the start table.
The usual chi-square likelihood-ratio test does not apply to
comparing numbers of components (K versus K + 1); compare such models
with AIC/BIC on the same sample, start stability and held-out
log scores. regCompTest() refuses mixture models for this reason.
Interpretation
Classes are a modeling device. A good fit with two components does not show that two real subpopulations exist; the same mixture can describe a skewed or heavy-tailed distribution.
Zero-inflated, hurdle and underreporting components, component-specific formulas, and confidence/prediction/tolerance intervals for mixture predictions are not available yet.
References
McLachlan, G. J., & Peel, D. (2000). Finite Mixture Models. Wiley.
Grün, B., & Leisch, F. (2008). FlexMix version 2: Finite mixtures with concomitant variables and varying and constant parameters. Journal of Statistical Software, 28(4), 1-35.
Wedel, M., DeSarbo, W. S., Bult, J. R., & Ramaswamy, V. (1993). A latent class Poisson regression model for heterogeneous count data. Journal of Applied Econometrics, 8(4), 397-411.
Examples
# A two-component Poisson mixture of simulated counts
set.seed(1)
d <- data.frame(y = c(rpois(300, 1), rpois(200, 6)))
m <- countreg_mix(y ~ 1, data = d, family = "POISSON", components = 2,
n_starts = 5, seed = 42)
summary(m)
#> Call:
#> countreg_mix(formula = y ~ 1, data = d, family = "POISSON", components = 2,
#> n_starts = 5, seed = 42)
#>
#> Finite-mixture count model with 2 components
#> Component 1: POISSON
#> Component 2: POISSON
#> Mixing: constant proportions (reference: Component 2)
#> Optimizer: BFGS + Newton-Raphson refinement | gradient close to zero (gradtol)
#>
#> Coefficients (standard errors: observed information (Hessian)):
#>
#> Component 1 (POISSON):
#> Estimate Std. Error z value Pr(>|z|)
#> Component1:Mean:(Intercept) -0.0361 0.0828 -0.44 0.66
#>
#> Component 2 (POISSON):
#> Estimate Std. Error z value Pr(>|z|)
#> Component2:Mean:(Intercept) 1.8054 0.0374 48.2 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Mixing (multinomial logit):
#> Estimate Std. Error z value Pr(>|z|)
#> Mixing:Component1:(Intercept) 0.407 0.117 3.49 0.00048 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Component sizes:
#> label family prior posterior effective_n modal_n mean_count
#> Component1 POISSON 0.6003 0.6003 300.2 325 0.9646
#> Component2 POISSON 0.3997 0.3997 199.8 175 6.0830
#> (prior: mean membership probability; posterior: mean posterior probability;
#> effective_n: sum of posterior probabilities; modal_n: units whose most
#> probable component it is; mean_count: average expected count)
#>
#> Log-likelihood: -1107.547 | AIC: 2221.094 | BIC: 2233.738 | ICL: 2398.952
#> N: 500
#> Entropy: 82.607 | relative entropy: 0.762
#> Starts: 5 | 5 reached the selected log-likelihood
#> Observed information: positive definite (condition number 14.7 ) | max |gradient|: 4.82e-08
#> No likelihood-ratio test of the number of components is reported (non-standard distribution).
component_probs(m, conditioning = "prior")[1, ]
#> Component1 Component2
#> 0.600347 0.399653
# \donttest{
# The same model through the latent-class interface
lc <- countreg_lc(y ~ 1, data = d, family = "POISSON", classes = 2,
n_starts = 5, seed = 42)
all.equal(logLik(m), logLik(lc))
#> [1] TRUE
# Latent-class NB2 regression with a membership covariate
data("abalone_rings")
fit <- countreg_lc(rings ~ shell_weight, data = abalone_rings,
family = "NB2", classes = 2,
membership_formula = ~ sex, n_starts = 5, seed = 1)
#> Warning: The Hessian is not negative definite at the estimates, so the standard errors of Component1:ln(alpha) are NA. The estimate may be on the boundary of the parameter space or not at a maximum.
summary(fit)
#> Call:
#> countreg_lc(formula = rings ~ shell_weight, data = abalone_rings,
#> family = "NB2", classes = 2, membership_formula = ~sex, n_starts = 5,
#> seed = 1)
#>
#> Latent-class count model with 2 classes
#> Class 1: NB2
#> Class 2: NB2
#> Mixing: multinomial logit on covariates (reference: Class 2)
#> Optimizer: BFGS + Newton-Raphson refinement | successive function values within relative tolerance limit (reltol)
#>
#> Coefficients (standard errors: observed information (Hessian)):
#>
#> Class 1 (NB2):
#> Estimate Std. Error z value Pr(>|z|)
#> Component1:Mean:(Intercept) 2.0974 0.0145 145 <2e-16 ***
#> Component1:Mean:shell_weight 0.9675 0.0439 22 <2e-16 ***
#> Component1:ln(alpha) -24.6839 NA NA NA
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Class 2 (NB2):
#> Estimate Std. Error z value Pr(>|z|)
#> Component2:Mean:(Intercept) 1.7141 0.0189 90.6 <2e-16 ***
#> Component2:Mean:shell_weight 2.5624 0.1107 23.2 <2e-16 ***
#> Component2:ln(alpha) -16.0747 0.2460 -65.3 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Mixing (multinomial logit):
#> Estimate Std. Error z value Pr(>|z|)
#> Mixing:Component1:(Intercept) 4.32 1.08 4.01 6.0e-05 ***
#> Mixing:Component1:sexI -9.33 1.52 -6.14 8.2e-10 ***
#> Mixing:Component1:sexM 2.56 6.20 0.41 0.68
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Class sizes:
#> label family prior posterior effective_n modal_n mean_count
#> Class1 NB2 0.6764 0.6764 2825.3 2834 10.36
#> Class2 NB2 0.3236 0.3236 1351.7 1343 10.97
#> (prior: mean membership probability; posterior: mean posterior probability;
#> effective_n: sum of posterior probabilities; modal_n: units whose most
#> probable class it is; mean_count: average expected count)
#>
#> Log-likelihood: -9681.506 | AIC: 19381.01 | BIC: 19438.05 | ICL: 19724.96
#> N: 4177
#> Entropy: 143.454 | relative entropy: 0.95
#> Starts: 5 | 1 reached the selected log-likelihood
#> Note: only one start reached this solution; try more starts.
#> Observed information: indefinite | max |gradient|: 1.04
#> No likelihood-ratio test of the number of classes is reported (non-standard distribution).
# }
