Skip to contents

Predictions for finite-mixture and latent-class models

Usage

# S3 method for class 'countreg_mix'
predict(
  object,
  newdata = NULL,
  ...,
  type = c("response", "component", "variance", "prob", "link"),
  conditioning = c("prior", "history", "posterior"),
  history = NULL,
  new_data = NULL,
  interval = c("none", "confidence", "prediction"),
  level = 0.95
)

Arguments

object

a model from countreg_mix() or countreg_lc().

newdata, new_data

optional data frame (give one or the other). When omitted, the data used to fit the model (rows removed for missing values give NA).

...

max_count and overflow for type = "prob".

type

the prediction:

"response"

the expected count of the mixture, \(\sum_k \pi_k m_k\), where \(m_k\) is the actual expected count of component \(k\) (for PLN and PLL it includes the lognormal factor).

"component"

a matrix of the component expected counts \(m_k\), one column per component.

"variance"

the variance of the mixture, \(\sum_k \pi_k (V_k + m_k^2) - m^2\), including the variation between components (Inf when a component with positive probability has infinite variance).

"prob"

a matrix of count probabilities \(P(Y = y) = \sum_k \pi_k f_k(y)\) for \(y = 0, \dots,\) max_count (passed in ...; default the largest training count); overflow = TRUE adds a column with \(P(Y >\) max_count\()\).

"link"

a matrix of the component linear predictors \(x'\beta_k\) plus offset. There is no single linear predictor of the mixture.

conditioning

which class probabilities \(\pi_k\) are used:

"prior" (default)

membership probabilities from the covariates only. The outcomes in newdata are never used, so these are predictions for new units and the basis of fitted values, residuals and diagnostics.

"history"

for panel models: the prior updated once with the likelihood of each panel's earlier outcomes in history, \(\pi^H_{gk} \propto \pi_{gk} \prod_{i \in H_g} f_k(y_i)\). Rows of panels without history use the prior. The outcomes of newdata are not used.

"posterior"

probabilities conditional on the outcomes in newdata itself (whole panels for panel models). This is a retrospective summary, not an out-of-sample prediction: the outcomes being "predicted" were used.

history

for conditioning = "history": a data frame of earlier rows of the panels in newdata, with outcomes and the panel identifier.

interval

only "none": confidence, prediction and tolerance intervals are not yet available for mixture models.

level

unused (for compatibility).

Value

A numeric vector ("response", "variance") with one value per row of the data, or a matrix ("component", "prob", "link").

See also

component_probs() for the class probabilities themselves.

Examples

set.seed(1)
d <- data.frame(y = c(rpois(300, 1), rpois(200, 6)))
m <- countreg_mix(y ~ 1, data = d, components = 2, n_starts = 3,
                  seed = 1)
predict(m, newdata = d[1:3, , drop = FALSE])
#> [1] 3.01 3.01 3.01
predict(m, newdata = d[1:3, , drop = FALSE], type = "component")
#>      Component1 Component2
#> [1,]  0.9645638   6.082595
#> [2,]  0.9645638   6.082595
#> [3,]  0.9645638   6.082595
predict(m, newdata = d[1:3, , drop = FALSE], type = "prob",
        max_count = 5, overflow = TRUE)
#>             0         1         2         3          4          5        >5
#> [1,] 0.229734 0.2262613 0.1233191 0.0684353 0.06027532 0.06487828 0.2270966
#> [2,] 0.229734 0.2262613 0.1233191 0.0684353 0.06027532 0.06487828 0.2270966
#> [3,] 0.229734 0.2262613 0.1233191 0.0684353 0.06027532 0.06487828 0.2270966