fitverse_grouped: Fit Distributions to Grouped or Interval-Censored Data

View source: R/fit_grouped.R

fitverse_groupedR Documentation

Fit Distributions to Grouped or Interval-Censored Data

Description

fitverse_grouped() fits one or more parametric distributions to data that are available only in grouped (frequency-table) or interval-censored form. It accepts either class midpoints with counts, or explicit lower and upper interval boundaries with counts.

read_grouped_csv() reads a two- or three-column CSV file into the format expected by fitverse_grouped().

Usage

fitverse_grouped(midpoints = NULL,
                 counts,
                 lower   = NULL,
                 upper   = NULL,
                 dists   = "auto",
                 method  = "All",
                 xlab    = "x",
                 top_n   = 5L,
                 verbose = FALSE)

read_grouped_csv(file, sep = ",")

Arguments

midpoints

Numeric vector of class midpoints (one per class). Supply either midpoints or both lower and upper, but not both.

counts

Integer vector of observed frequencies for each class. Must be the same length as midpoints (or lower/upper), all non-negative, and sum to at least 10.

lower

Numeric vector of lower interval boundaries. Required when midpoints is not supplied.

upper

Numeric vector of upper interval boundaries. Required when midpoints is not supplied. Each element must exceed the corresponding element of lower.

dists

Character vector of distribution names to fit, or "auto" (default) to use all distributions in the FitVerse registry that support MLE.

method

Fitting method: "MLE", "MOM", "LMOM", or "All" (default).

xlab

Character label for the data axis used in output summaries. Default "x".

top_n

Integer: number of top-ranked distributions to include in the printed summary. Default 5L.

verbose

Logical: print per-distribution fitting progress? Default FALSE.

file

Path to a CSV file. The file may contain comment lines beginning with #.

sep

Column separator for read_grouped_csv(). Default ",".

Details

Input formats

fitverse_grouped() accepts two input formats.

Midpoint format: Supply midpoints and counts. Interval boundaries are reconstructed by assuming equal-width classes.

Boundary format: Supply lower, upper, and counts. Classes need not be equal-width. Open-ended classes (e.g., an upper bound of Inf) are handled by replacing the infinite bound with a large finite value for midpoint computation.

Estimation

For each candidate distribution, parameters are estimated by maximising the interval-censored log-likelihood

\ell(\theta) = \sum_{j} n_j \log\bigl[F(u_j;\theta) - F(l_j;\theta)\bigr]

where n_j is the count in class j, and l_j, u_j are its lower and upper boundaries.

CSV format for read_grouped_csv()

The CSV file should have either two columns (midpoint, count) or three columns (lower, upper, count). Column names are detected automatically from a set of common variants (mid, midpoint, lower, upper, etc.). Lines starting with # are treated as comments and ignored.

Value

fitverse_grouped() returns an object of class "fitverse_grouped" containing:

results

A data frame of fitted distributions ranked by AIC, with columns for distribution name, AIC, BIC, log-likelihood, estimated parameters, and goodness-of-fit statistics.

best

The name of the best-fitting distribution.

data

A list with elements midpoints, counts, lower, and upper.

call

The matched call.

read_grouped_csv() returns a named list with elements midpoints and counts (midpoint format) or lower, upper, and counts (boundary format), ready to be passed directly to fitverse_grouped().

Author(s)

Karuna G. Reddy karuna.reddy@auckland.ac.nz
M. G. M. Khan mgm.khan@usp.ac.fj

See Also

fitverse for fitting to individual (ungrouped) data.
fitverse_distributions for the full distribution catalogue.

Examples


## Midpoint format
mid   <- c(5, 15, 25, 35, 45, 55, 65, 75)
count <- c(3, 12, 25, 30, 18, 8, 3, 1)
fit <- fitverse_grouped(midpoints = mid, counts = count,
                        method = "MLE", verbose = FALSE)
print(fit)

## Boundary format
low  <- c(0, 10, 20, 30, 40, 50, 60, 70)
high <- c(10, 20, 30, 40, 50, 60, 70, 80)
fit2 <- fitverse_grouped(lower = low, upper = high, counts = count,
                         method = "MLE", verbose = FALSE)
print(fit2)

## Reading from CSV
## A file with columns: lower, upper, count
tmp <- tempfile(fileext = ".csv")
writeLines(c(
  "# Example grouped data",
  "lower,upper,count",
  "0,10,5",
  "10,20,18",
  "20,30,30",
  "30,40,25",
  "40,50,12",
  "50,60,4"
), tmp)
dat <- read_grouped_csv(tmp)
fit3 <- fitverse_grouped(lower = dat$lower, upper = dat$upper,
                         counts = dat$counts)
print(fit3)


FitVerse documentation built on Dec. 1, 2026, 1:07 a.m.