Type: Package
Title: Algebraic Maximum Likelihood Estimators
Version: 2.0.2
Description: The maximum likelihood estimator (MLE) is a technology: under regularity conditions, any MLE is asymptotically normal with variance given by the inverse Fisher information. This package exploits that structure by defining an algebra over MLEs. Compose independent estimators into joint MLEs via block-diagonal covariance ('joint'), optimally combine repeated estimates via inverse-variance weighting ('combine'), propagate transformations via the delta method ('rmap'), and bridge to distribution algebra via conversion to normal or multivariate normal objects ('as_dist'). Supports asymptotic ('mle', 'mle_numerical') and bootstrap ('mle_boot') estimators with a unified interface for inference: confidence intervals, standard errors, AIC, Fisher information, and predictive intervals. For background on maximum likelihood estimation, see Casella and Berger (2002, ISBN:978-0534243128). For the delta method and variance estimation, see Lehmann and Casella (1998, ISBN:978-0387985022).
License: GPL (≥ 3)
Encoding: UTF-8
ByteCompile: true
Imports: algebraic.dist (≥ 0.9.1), stats, boot, mvtnorm, MASS, numDeriv
RoxygenNote: 7.3.3
URL: https://github.com/queelius/algebraic.mle, https://queelius.github.io/algebraic.mle/
BugReports: https://github.com/queelius/algebraic.mle/issues
Suggests: testthat (≥ 3.0.0), rmarkdown, knitr, ggplot2, tibble, CDFt
VignetteBuilder: knitr
Config/testthat/edition: 3
NeedsCompilation: no
Packaged: 2026-03-17 17:49:30 UTC; spinoza
Author: Alexander Towell ORCID iD [aut, cre]
Maintainer: Alexander Towell <lex@metafunctor.com>
Repository: CRAN
Date/Publication: 2026-03-19 05:00:23 UTC

algebraic.mle: Algebraic Maximum Likelihood Estimators

Description

The maximum likelihood estimator (MLE) is a technology: under regularity conditions, any MLE is asymptotically normal with variance given by the inverse Fisher information. This package exploits that structure by defining an algebra over MLEs. Compose independent estimators into joint MLEs via block-diagonal covariance ('joint'), optimally combine repeated estimates via inverse-variance weighting ('combine'), propagate transformations via the delta method ('rmap'), and bridge to distribution algebra via conversion to normal or multivariate normal objects ('as_dist'). Supports asymptotic ('mle', 'mle_numerical') and bootstrap ('mle_boot') estimators with a unified interface for inference: confidence intervals, standard errors, AIC, Fisher information, and predictive intervals. For background on maximum likelihood estimation, see Casella and Berger (2002, ISBN:978-0534243128). For the delta method and variance estimation, see Lehmann and Casella (1998, ISBN:978-0387985022).

Author(s)

Maintainer: Alexander Towell lex@metafunctor.com (ORCID)

See Also

Useful links:


Convert an MLE to a distribution object.

Description

Converts an mle_fit object to its asymptotic normal or mvn distribution. The MLE must have a variance-covariance matrix available.

Usage

## S3 method for class 'mle_fit'
as_dist(x, ...)

Arguments

x

An mle_fit object.

...

Additional arguments (not used).

Value

A normal (univariate) or mvn (multivariate) distribution.

Examples

fit <- mle(theta.hat = c(mu = 5), sigma = matrix(0.25))
d <- as_dist(fit)
mean(d)   # 5
vcov(d)   # 0.25

fit2 <- mle(theta.hat = c(a = 1, b = 2), sigma = diag(c(0.1, 0.2)))
d2 <- as_dist(fit2)
mean(d2)  # c(1, 2)

Convert a bootstrap MLE to an empirical distribution.

Description

Converts an mle_fit_boot object to an empirical_dist built from the bootstrap replicates.

Usage

## S3 method for class 'mle_fit_boot'
as_dist(x, ...)

Arguments

x

An mle_fit_boot object.

...

Additional arguments (not used).

Value

An empirical_dist object.

Examples

set.seed(123)
x <- rexp(50, rate = 2)
rate_mle <- function(data, indices) 1 / mean(data[indices])
boot_result <- boot::boot(data = x, statistic = rate_mle, R = 200)
fit <- mle_boot(boot_result)
d <- as_dist(fit)
mean(d)

Generic method for computing the bias of an estimator object.

Description

Generic method for computing the bias of an estimator object.

Usage

bias(x, theta = NULL, ...)

Arguments

x

the object to compute the bias of.

theta

true parameter value. usually, this is unknown (NULL), in which case we estimate the bias

...

pass additional arguments

Value

The bias of the estimator. The return type depends on the specific method.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
bias(fit, theta = c(mu = 5, sigma2 = 4))

Computes the bias of an 'mle_fit' object assuming the large sample approximation is valid and the MLE regularity conditions are satisfied. In this case, the bias is zero (or zero vector).

Description

This is not a good estimate of the bias in general, but it's arguably better than returning 'NULL'.

Usage

## S3 method for class 'mle_fit'
bias(x, theta = NULL, ...)

Arguments

x

the 'mle_fit' object to compute the bias of.

theta

true parameter value. normally, unknown (NULL), in which case we estimate the bias (say, using bootstrap)

...

additional arguments to pass

Value

Numeric vector of zeros (asymptotic bias is zero under regularity conditions).

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
bias(fit)

Computes the estimate of the bias of a 'mle_fit_boot' object.

Description

Computes the estimate of the bias of a 'mle_fit_boot' object.

Usage

## S3 method for class 'mle_fit_boot'
bias(x, theta = NULL, ...)

Arguments

x

the 'mle_fit_boot' object to compute the bias of.

theta

true parameter value (not used for 'mle_fit_boot').

...

pass additional arguments (not used)

Value

Numeric vector of estimated bias (mean of bootstrap replicates minus original estimate).

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
bias(fit)

Build a block-diagonal matrix from a list of square matrices.

Description

Build a block-diagonal matrix from a list of square matrices.

Usage

block_diag(blocks, dims)

Arguments

blocks

List of matrices (scalars are promoted to 1x1 matrices).

dims

Integer vector of block dimensions.

Value

A square matrix with blocks placed along the diagonal.


CDF of the asymptotic distribution of an MLE.

Description

CDF of the asymptotic distribution of an MLE.

Usage

## S3 method for class 'mle_fit'
cdf(x, ...)

Arguments

x

An mle_fit object.

...

Additional arguments (not used).

Value

A function computing the CDF at given points.

Examples

fit <- mle(theta.hat = c(mu = 5), sigma = matrix(0.1))
F <- cdf(fit)
F(5)

Extract coefficients from an mle_fit object.

Description

Delegates to params().

Usage

## S3 method for class 'mle_fit'
coef(object, ...)

Arguments

object

the mle_fit object

...

additional arguments (not used)

Value

Named numeric vector of parameter estimates.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
coef(fit)

Collect a field from a list of MLEs, returning NULL if any are NULL.

Description

Collect a field from a list of MLEs, returning NULL if any are NULL.

Usage

collect_or_null(mles, extractor)

Arguments

mles

List of mle_fit objects.

extractor

Function that extracts the field from a single MLE.

Value

A list of field values, or NULL if any value is NULL.


Combine independent MLEs for the same parameter.

Description

Given multiple independent MLEs that estimate the same parameter \theta, produces an optimally weighted combination using inverse-variance (Fisher information) weighting.

Usage

combine(x, ...)

## S3 method for class 'list'
combine(x, ...)

## S3 method for class 'mle_fit'
combine(x, ...)

Arguments

x

An mle_fit object, or a list of mle_fit objects.

...

Additional mle_fit objects to combine.

Details

The combined estimator has:

When the Fisher information matrix is not directly available but the variance-covariance matrix is, the FIM is computed as ginv(vcov).

Value

An mle_fit object representing the optimally weighted combination.

See Also

joint

Examples

# Three independent estimates of the same rate
fit1 <- mle(theta.hat = c(lambda = 2.1), sigma = matrix(0.04), nobs = 50L)
fit2 <- mle(theta.hat = c(lambda = 1.9), sigma = matrix(0.02), nobs = 100L)
fit3 <- mle(theta.hat = c(lambda = 2.0), sigma = matrix(0.03), nobs = 70L)

comb <- combine(fit1, fit2, fit3)
params(comb)
se(comb)

Internal workhorse for combine.

Description

Internal workhorse for combine.

Usage

combine_mles(mles)

Arguments

mles

A list of mle_fit objects.

Value

An mle_fit object.


Conditional distribution from an MLE.

Description

Computes the conditional distribution of a subset of parameters given observed values for other parameters. Uses the closed-form Schur complement for the multivariate normal. Returns a dist object (not an mle_fit), since conditioning loses the MLE context.

Usage

## S3 method for class 'mle_fit'
conditional(x, P = NULL, ..., given_indices = NULL, given_values = NULL)

Arguments

x

An mle_fit object with at least 2 parameters.

P

Optional predicate function for Monte Carlo fallback.

...

Additional arguments forwarded to P.

given_indices

Integer vector of conditioned parameter indices.

given_values

Numeric vector of observed values.

Value

A normal, mvn, or empirical_dist object.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
conditional(fit, given_indices = 2, given_values = 4)

Function to compute the confidence intervals of 'mle_fit' objects.

Description

Function to compute the confidence intervals of 'mle_fit' objects.

Usage

## S3 method for class 'mle_fit'
confint(object, parm = NULL, level = 0.95, use_t_dist = FALSE, ...)

Arguments

object

the 'mle_fit' object to compute the confidence intervals for

parm

character or integer vector of parameters to return intervals for. If NULL (default), all parameters are returned.

level

confidence level, defaults to 0.95 (alpha=.05)

use_t_dist

logical, whether to use the t-distribution to compute the confidence intervals.

...

additional arguments to pass

Value

Matrix of confidence intervals with columns for lower and upper bounds.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
confint(fit)

Method for obtained the confidence interval of an 'mle_fit_boot' object. Note: This impelements the 'vcov' method defined in 'stats'.

Description

Method for obtained the confidence interval of an 'mle_fit_boot' object. Note: This impelements the 'vcov' method defined in 'stats'.

Usage

## S3 method for class 'mle_fit_boot'
confint(
  object,
  parm = NULL,
  level = 0.95,
  type = c("norm", "basic", "perc", "bca"),
  ...
)

Arguments

object

the 'mle_fit_boot' object to obtain the confidence interval of

parm

character or integer vector of parameters to return intervals for. If NULL (default), all parameters are returned.

level

the confidence level

type

the type of confidence interval to compute

...

additional arguments to pass into 'boot.ci'

Value

Matrix of bootstrap confidence intervals with columns for lower and upper bounds.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
confint(fit)

Function to compute the confidence intervals from a variance-covariance matrix

Description

Function to compute the confidence intervals from a variance-covariance matrix

Usage

confint_from_sigma(sigma, theta, level = 0.95)

Arguments

sigma

either the variance-covariance matrix or the vector of variances of the parameter estimator

theta

the point estimate

level

confidence level, defaults to 0.95 (alpha=.05)

Value

Matrix of confidence intervals with rows for each parameter and columns for lower and upper bounds.

Examples

# Compute CI for a bivariate parameter
theta <- c(mu = 5.2, sigma2 = 4.1)
vcov_matrix <- diag(c(0.1, 0.5))  # Variance of estimators

confint_from_sigma(vcov_matrix, theta)
confint_from_sigma(vcov_matrix, theta, level = 0.99)

PDF of the asymptotic distribution of an MLE.

Description

Returns a closure computing the probability density function of the asymptotic normal (univariate) or multivariate normal (multivariate) distribution of the MLE.

Usage

## S3 method for class 'mle_fit'
density(x, ...)

Arguments

x

An mle_fit object.

...

Additional arguments (not used).

Value

A function computing the PDF at given points.

Examples

fit <- mle(theta.hat = c(mu = 5), sigma = matrix(0.1))
f <- density(fit)
f(5)

PDF of the empirical distribution of bootstrap replicates.

Description

PDF of the empirical distribution of bootstrap replicates.

Usage

## S3 method for class 'mle_fit_boot'
density(x, ...)

Arguments

x

An mle_fit_boot object.

...

Additional arguments (not used).

Value

A function computing the empirical PMF at given points.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
density(fit)

Dimension (number of parameters) of an MLE.

Description

Dimension (number of parameters) of an MLE.

Usage

## S3 method for class 'mle_fit'
dim(x)

Arguments

x

An mle_fit object.

Value

Integer; the number of parameters.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
dim(fit)

Dimension (number of parameters) of a bootstrap MLE.

Description

Dimension (number of parameters) of a bootstrap MLE.

Usage

## S3 method for class 'mle_fit_boot'
dim(x)

Arguments

x

An mle_fit_boot object.

Value

Integer; the number of parameters.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
dim(fit)

Expectation operator applied to 'x' of type 'mle_fit' with respect to a function 'g'. That is, 'E(g(x))'.

Description

Optionally, we use the CLT to construct a CI('alpha') for the estimate of the expectation. That is, we estimate 'E(g(x))' with the sample mean and Var(g(x)) with the sigma^2/n, where sigma^2 is the sample variance of g(x) and n is the number of samples. From these, we construct the CI.

Usage

## S3 method for class 'mle_fit'
expectation(x, g = function(t) t, ..., control = list())

Arguments

x

'mle_fit' object

g

characteristic function of interest, defaults to identity

...

additional arguments to pass to 'g'

control

a list of control parameters: compute_stats - Whether to compute CIs for the expectations, defaults to FALSE n - The number of samples to use for the MC estimate, defaults to 10000 alpha - The significance level for the confidence interval, defaults to 0.05

Value

If 'compute_stats' is FALSE, then the estimate of the expectation, otherwise a list with the following components: value - The estimate of the expectation ci - The confidence intervals for each component of the expectation n - The number of samples

Examples

fit <- mle(theta.hat = c(mu = 5), sigma = matrix(0.1), nobs = 100L)

expectation(fit)


Extract FIM from an MLE, falling back to ginv(vcov).

Description

Extract FIM from an MLE, falling back to ginv(vcov).

Usage

fim_or_ginv_vcov(m)

Arguments

m

An mle_fit object.

Value

A matrix (FIM).


Quantile function of the asymptotic distribution of an MLE.

Description

Quantile function of the asymptotic distribution of an MLE.

Usage

## S3 method for class 'mle_fit'
inv_cdf(x, ...)

Arguments

x

An mle_fit object.

...

Additional arguments (not used).

Value

A function computing quantiles for given probabilities.

Examples

fit <- mle(theta.hat = c(mu = 5), sigma = matrix(0.1))
q <- inv_cdf(fit)
q(0.975)

Determine if an object 'x' is an 'mle_fit' object.

Description

Determine if an object 'x' is an 'mle_fit' object.

Usage

is_mle(x)

Arguments

x

the object to test

Value

Logical TRUE if x is an mle_fit object, FALSE otherwise.

Examples

fit <- mle(theta.hat = c(mu = 5), sigma = matrix(0.1))
is_mle(fit)       # TRUE
is_mle(list(a=1)) # FALSE

Determine if an object is an 'mle_fit_boot' object.

Description

Determine if an object is an 'mle_fit_boot' object.

Usage

is_mle_boot(x)

Arguments

x

the object to test

Value

Logical TRUE if x is an mle_fit_boot object, FALSE otherwise.

Examples

# Create a simple mle_fit object (not bootstrap)
fit_mle <- mle(theta.hat = 5, sigma = matrix(0.1))
is_mle_boot(fit_mle)  # FALSE

# Bootstrap example would return TRUE

Compose independent MLEs into a joint MLE.

Description

Given two or more independent MLEs with disjoint parameter sets, produces a joint MLE with block-diagonal variance-covariance structure.

Usage

joint(x, ...)

## S3 method for class 'mle_fit'
joint(x, ...)

Arguments

x

An mle_fit object.

...

Additional mle_fit objects to join.

Details

The joint MLE has:

Value

An mle_fit object representing the joint MLE.

Examples

# Two independent experiments
fit_rate <- mle(theta.hat = c(lambda = 2.1), sigma = matrix(0.04), nobs = 50L)
fit_shape <- mle(theta.hat = c(k = 1.5, s = 3.2),
                 sigma = matrix(c(0.1, 0.02, 0.02, 0.3), 2, 2), nobs = 100L)

# Joint MLE: 3 params, block-diagonal covariance
j <- joint(fit_rate, fit_shape)
params(j)   # c(lambda = 2.1, k = 1.5, s = 3.2)
vcov(j)     # 3x3 block-diagonal

# Existing algebra works on the joint:
marginal(j, 2:3)   # recover shape params
as_dist(j)          # MVN for distribution algebra

Label a confidence interval matrix with column and row names.

Description

Label a confidence interval matrix with column and row names.

Usage

label_ci(ci, theta, alpha)

Arguments

ci

A two-column matrix of lower and upper bounds.

theta

Named numeric vector of parameter estimates.

alpha

The lower-tail probability (e.g., 0.025 for a 95 percent CI).

Value

The ci matrix with column names set to percentile labels and row names set to parameter names (or "param1", "param2", ...).


Log-likelihood of an mle_fit object.

Description

Returns a "logLik" object with df (number of parameters) and nobs attributes, enabling AIC() and BIC() via stats::AIC.default.

Usage

## S3 method for class 'mle_fit'
logLik(object, ...)

Arguments

object

the mle_fit object

...

additional arguments (not used)

Value

A "logLik" object.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
logLik(fit)

Method for obtaining the marginal distribution of an MLE that is based on asymptotic assumptions:

Description

'x ~ MVN(params(x), inv(H)(x))'

Usage

## S3 method for class 'mle_fit'
marginal(x, indices)

Arguments

x

The distribution object.

indices

The indices of the marginal distribution to obtain.

Details

where H is the (observed or expecation) Fisher information matrix.

Value

An mle_fit object representing the marginal distribution for the selected parameter indices.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
marginal(fit, 1)

Mean of the asymptotic distribution of an MLE.

Description

Returns the parameter estimates, which are the mean of the asymptotic distribution.

Usage

## S3 method for class 'mle_fit'
mean(x, ...)

Arguments

x

An mle_fit object.

...

Additional arguments (not used).

Value

Numeric vector of parameter estimates.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
mean(fit)

Mean of bootstrap replicates.

Description

Returns the empirical mean of the bootstrap replicates (which includes bootstrap bias). For the bias-corrected point estimate, use params().

Usage

## S3 method for class 'mle_fit_boot'
mean(x, ...)

Arguments

x

An mle_fit_boot object.

...

Additional arguments (not used).

Value

Numeric vector of column means of bootstrap replicates.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
mean(fit)

Constructor for making 'mle_fit' objects, which provides a common interface for maximum likelihood estimators.

Description

This MLE makes the asymptotic assumption by default. Other MLEs, like 'mle_fit_boot', may not make this assumption.

Usage

mle(
  theta.hat,
  loglike = NULL,
  score = NULL,
  sigma = NULL,
  info = NULL,
  obs = NULL,
  nobs = NULL,
  superclasses = NULL
)

Arguments

theta.hat

the MLE

loglike

the log-likelihood of 'theta.hat' given the data

score

the score function evaluated at 'theta.hat'

sigma

the variance-covariance matrix of 'theta.hat' given that data

info

the information matrix of 'theta.hat' given the data

obs

observation (sample) data

nobs

number of observations in 'obs'

superclasses

class (or classes) with 'mle_fit' as base

Value

An object of class mle_fit.

Examples

# MLE for normal distribution (mean and variance)
set.seed(123)
x <- rnorm(100, mean = 5, sd = 2)
n <- length(x)
mu_hat <- mean(x)
var_hat <- mean((x - mu_hat)^2)  # MLE of variance

# Asymptotic variance-covariance of MLE
# For normal: Var(mu_hat) = sigma^2/n, Var(var_hat) = 2*sigma^4/n
sigma_matrix <- diag(c(var_hat/n, 2*var_hat^2/n))

fit <- mle(
  theta.hat = c(mu = mu_hat, var = var_hat),
  sigma = sigma_matrix,
  loglike = sum(dnorm(x, mu_hat, sqrt(var_hat), log = TRUE)),
  nobs = n
)

params(fit)
vcov(fit)
confint(fit)

Bootstrapped MLE

Description

Sometimes, the large sample asymptotic theory of MLEs is not applicable. In such cases, we can use the bootstrap to estimate the sampling distribution of the MLE.

Usage

mle_boot(x)

Arguments

x

the 'boot' return value

Details

This takes an approach similiar to the 'mle_fit_numerical' object, which is a wrapper for a 'stats::optim' return value, or something that is compatible with the 'optim' return value. Here, we take a 'boot' object, which is the sampling distribution of an MLE, and wrap it in an 'mle_fit_boot' object and then provide a number of methods for the 'mle_fit_boot' object that satisfies the concept of an 'mle_fit' object.

Look up the 'boot' package for more information on the bootstrap.

Value

An mle_fit_boot object (wrapper for boot object).

Examples

# Bootstrap MLE for mean of exponential distribution
set.seed(123)
x <- rexp(50, rate = 2)

# Statistic function: MLE of rate parameter
rate_mle <- function(data, indices) {
  d <- data[indices]
  1 / mean(d)  # MLE of rate is 1/mean
}

# Run bootstrap
boot_result <- boot::boot(data = x, statistic = rate_mle, R = 200)

# Wrap in mle_boot
fit <- mle_boot(boot_result)
params(fit)
bias(fit)
confint(fit)

This function takes the output of 'optim', 'newton_raphson', or 'sim_anneal' and turns it into an 'mle_fit_numerical' (subclass of 'mle_fit') object.

Description

This function takes the output of 'optim', 'newton_raphson', or 'sim_anneal' and turns it into an 'mle_fit_numerical' (subclass of 'mle_fit') object.

Usage

mle_numerical(sol, options = list(), superclasses = NULL)

Arguments

sol

the output of 'optim' or 'newton_raphson'

options

list, options for things like sigma and FIM

superclasses

list, superclasses to add to the 'mle_fit_numerical' object

Value

An object of class mle_fit_numerical (subclass of mle_fit).

Examples

# Fit exponential distribution using optim
set.seed(123)
x <- rexp(100, rate = 2)

# Log-likelihood for exponential distribution
loglik <- function(rate) {
  if (rate <= 0) return(-Inf)
  sum(dexp(x, rate = rate, log = TRUE))
}

# Optimize (maximize by setting fnscale = -1)
result <- optim(
  par = 1,
  fn = loglik,
  method = "Brent",
  lower = 0.01, upper = 10,
  hessian = TRUE,
  control = list(fnscale = -1)
)

# Wrap in mle_numerical
fit <- mle_numerical(result, options = list(nobs = length(x)))
params(fit)
se(fit)

Convert an MLE to its asymptotic distribution.

Description

Internal helper that creates a normal (univariate) or mvn (multivariate) distribution from an mle_fit object using the asymptotic normality of the MLE.

Usage

mle_to_dist(x)

Arguments

x

An mle_fit object with a non-NULL variance-covariance matrix.

Value

A normal or mvn distribution object.


Generic method for computing the mean squared error (MSE) of an estimator, 'mse(x) = E[(x-mu)^2]' where 'mu' is the true parameter value.

Description

Generic method for computing the mean squared error (MSE) of an estimator, 'mse(x) = E[(x-mu)^2]' where 'mu' is the true parameter value.

Usage

mse(x, theta, ...)

Arguments

x

the object to compute the MSE of

theta

the true parameter value

...

additional arguments passed to methods

Value

The mean squared error (matrix or scalar).

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
mse(fit, theta = c(mu = 5, sigma2 = 4))

Computes the MSE of an 'mle_fit' object.

Description

The MSE of an estimator is just the expected sum of squared differences, e.g., if the true parameter value is 'x' and we have an estimator 'x.hat', then the MSE is “' mse(x.hat) = E[(x.hat-x) vcov(x.hat) + bias(x.hat, x) “'

Usage

## S3 method for class 'mle_fit'
mse(x, theta = NULL, ...)

Arguments

x

the 'mle_fit' object to compute the MSE of.

theta

true parameter value, defaults to 'NULL' for unknown. If 'NULL', then we let the bias method deal with it. Maybe it has a nice way of estimating the bias.

...

additional arguments (not used)

Details

Since 'x' is not typically known, we normally must estimate the bias. Asymptotically, assuming the regularity conditions, the bias of an MLE is zero, so we can estimate the MSE as 'mse(x.hat) = vcov(x.hat)', but for small samples, this is not generally the case. If we can estimate the bias, then we can replace the bias with an estimate of the bias.

Sometimes, we can estimate the bias analytically, but if not, we can use something like the bootstrap. For example, if we have a sample of size 'n', we can bootstrap the bias by sampling 'n' observations with replacement, computing the MLE, and then computing the difference between the bootstrapped MLE and the MLE. We can repeat this process 'B' times, and then average the differences to get an estimate of the bias.

Value

The MSE as a scalar (univariate) or matrix (multivariate).

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
mse(fit)

Computes the estimate of the MSE of a 'boot' object.

Description

Computes the estimate of the MSE of a 'boot' object.

Usage

## S3 method for class 'mle_fit_boot'
mse(x, theta = NULL, ...)

Arguments

x

the 'boot' object to compute the MSE of.

theta

true parameter value (not used for 'mle_boot')

...

pass additional arguments into 'vcov'

Value

The MSE matrix estimated from bootstrap variance and bias.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
mse(fit)

Method for obtaining the number of observations in the sample used by an 'mle_fit'.

Description

Method for obtaining the number of observations in the sample used by an 'mle_fit'.

Usage

## S3 method for class 'mle_fit'
nobs(object, ...)

Arguments

object

the 'mle_fit' object to obtain the number of observations for

...

additional arguments to pass (not used)

Value

Integer number of observations, or NULL if not available.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
nobs(fit)

Method for obtaining the number of observations in the sample used by an 'mle_fit_boot'.

Description

Method for obtaining the number of observations in the sample used by an 'mle_fit_boot'.

Usage

## S3 method for class 'mle_fit_boot'
nobs(object, ...)

Arguments

object

the 'mle_fit_boot' object to obtain the number of observations for

...

additional arguments to pass (not used)

Value

Integer number of observations in the original sample.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
nobs(fit)

Method for obtaining the number of parameters of an 'mle_fit' object.

Description

Method for obtaining the number of parameters of an 'mle_fit' object.

Usage

## S3 method for class 'mle_fit'
nparams(x)

Arguments

x

the 'mle_fit' object to obtain the number of parameters of

Value

Integer number of parameters.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
nparams(fit)

Method for obtaining the number of parameters of an 'boot' object.

Description

Method for obtaining the number of parameters of an 'boot' object.

Usage

## S3 method for class 'mle_fit_boot'
nparams(x)

Arguments

x

the 'boot' object to obtain the number of parameters of

Value

Integer number of parameters.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
nparams(fit)

Method for obtaining the observations used by the 'mle_fit' object 'x'.

Description

Method for obtaining the observations used by the 'mle_fit' object 'x'.

Usage

## S3 method for class 'mle_fit'
obs(x)

Arguments

x

the 'mle_fit' object to obtain the number of observations for

Value

The observation data used to fit the MLE, or NULL if not stored.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), obs = rnorm(100), nobs = 100L)
head(obs(fit))

Method for obtaining the observations used by the 'mle_fit_boot'.

Description

Method for obtaining the observations used by the 'mle_fit_boot'.

Usage

## S3 method for class 'mle_fit_boot'
obs(x)

Arguments

x

the 'mle_fit_boot' object to obtain the number of observations for

Value

The original data used for bootstrapping.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
head(obs(fit))

Generic method for computing the observed FIM of an 'mle_fit' object.

Description

Fisher information is a way of measuring the amount of information that an observable random variable 'X' carries about an unknown parameter 'theta' upon which the probability of 'X' depends.

Usage

observed_fim(x, ...)

Arguments

x

the object to obtain the fisher information of

...

additional arguments to pass

Details

The inverse of the Fisher information matrix is the variance-covariance of the MLE for 'theta'.

Some MLE objects do not have an observed FIM, e.g., if the MLE's sampling distribution was bootstrapped.

Value

The observed Fisher Information Matrix.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), info = solve(diag(c(0.04, 0.32))),
  loglike = -120, nobs = 100L)
observed_fim(fit)

Function for obtaining the observed FIM of an 'mle_fit' object.

Description

Function for obtaining the observed FIM of an 'mle_fit' object.

Usage

## S3 method for class 'mle_fit'
observed_fim(x, ...)

Arguments

x

the 'mle_fit' object to obtain the FIM of.

...

pass additional arguments

Value

The observed Fisher Information Matrix, or NULL if not available.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), info = solve(diag(c(0.04, 0.32))),
  nobs = 100L)
observed_fim(fit)

Generic method for determining the orthogonal parameters of an estimator.

Description

Generic method for determining the orthogonal parameters of an estimator.

Usage

orthogonal(x, tol, ...)

Arguments

x

the estimator

tol

the tolerance for determining if a number is close enough to zero

...

additional arguments to pass

Value

Logical vector or matrix indicating which parameters are orthogonal.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), info = solve(diag(c(0.04, 0.32))),
  loglike = -120, nobs = 100L)
orthogonal(fit)

Method for determining the orthogonal components of an 'mle_fit' object 'x'.

Description

Method for determining the orthogonal components of an 'mle_fit' object 'x'.

Usage

## S3 method for class 'mle_fit'
orthogonal(x, tol = sqrt(.Machine$double.eps), ...)

Arguments

x

the 'mle_fit' object

tol

the tolerance for determining if a number is close enough to zero

...

pass additional arguments

Value

Logical matrix indicating which off-diagonal FIM elements are approximately zero (orthogonal parameters), or NULL if FIM unavailable.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), info = solve(diag(c(0.04, 0.32))),
  nobs = 100L)
orthogonal(fit)

Method for obtaining the parameters of an 'mle_fit' object.

Description

Method for obtaining the parameters of an 'mle_fit' object.

Usage

## S3 method for class 'mle_fit'
params(x)

Arguments

x

the 'mle_fit' object to obtain the parameters of

Value

Numeric vector of parameter estimates.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
params(fit)

Method for obtaining the parameters of an 'boot' object.

Description

Method for obtaining the parameters of an 'boot' object.

Usage

## S3 method for class 'mle_fit_boot'
params(x)

Arguments

x

the 'boot' object to obtain the parameters of.

Value

Numeric vector of parameter estimates (the original MLE).

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
params(fit)

Generic method for computing the predictive confidence interval given an estimator object 'x'.

Description

Generic method for computing the predictive confidence interval given an estimator object 'x'.

Usage

pred(x, samp = NULL, alpha = 0.05, ...)

Arguments

x

the estimator object

samp

a sampler for random variable that is parameterized by mle 'x'

alpha

(1-alpha)/2 confidence interval

...

additional arguments to pass

Value

Matrix of predictive confidence intervals.

Examples

fit <- mle(theta.hat = c(mu = 5), sigma = matrix(0.1), nobs = 100L)

pred(fit, samp = function(n, theta) rnorm(n, theta[1], 1))


Estimate of predictive interval of 'T|data' using Monte Carlo integration.

Description

Let 'T|x ~ f(t|x)“ be the pdf of vector 'T' given MLE 'x' and 'x ~ MVN(params(x),vcov(x))“ be the estimate of the sampling distribution of the MLE for the parameters of 'T'. Then, '(T,x) ~ f(t,x) = f(t|x) f(x) is the joint distribution of '(T,x)'. To find 'f(t)' for a fixed 't', we integrate 'f(t,x)' over 'x' using Monte Carlo integration to find the marginal distribution of 'T'. That is, we:

Usage

## S3 method for class 'mle_fit'
pred(x, samp, alpha = 0.05, R = 50000, ...)

Arguments

x

an 'mle_fit' object.

samp

The sampler for the distribution that is parameterized by the MLE 'x', i.e., 'T|x'.

alpha

(1-alpha)-predictive interval for 'T|x'. Defaults to 0.05.

R

number of samples to draw from the sampling distribution of 'x'. Defaults to 50000.

...

additional arguments to pass into 'samp'.

Details

1. Sample from MVN 'x' 2. Compute 'f(t,x)' for each sample 3. Take the mean of the 'f(t,x)' values asn an estimate of 'f(t)'.

The 'samp' function is used to sample from the distribution of 'T|x'. It should be designed to take

Value

Matrix with columns for mean, lower, and upper bounds of the predictive interval.

Examples

fit <- mle(theta.hat = c(mu = 5), sigma = matrix(0.1), nobs = 100L)

pred(fit, samp = function(n, theta) rnorm(n, theta[1], 1))


Print method for 'mle_fit' objects.

Description

Print method for 'mle_fit' objects.

Usage

## S3 method for class 'mle_fit'
print(x, ...)

Arguments

x

the 'mle_fit' object to print

...

additional arguments to pass

Value

Invisibly returns x.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
print(fit)

Function for printing a 'summary' object for an 'mle_fit' object.

Description

Function for printing a 'summary' object for an 'mle_fit' object.

Usage

## S3 method for class 'summary_mle_fit'
print(x, ...)

Arguments

x

the 'summary_mle_fit' object

...

pass additional arguments

Value

Invisibly returns x.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
print(summary(fit))

Objects exported from other packages

Description

These objects are imported from other packages. Follow the links below to see their documentation.

algebraic.dist

as_dist, cdf, conditional, expectation, inv_cdf, marginal, nparams, obs, params, rmap, sampler, sup


Computes the distribution of 'g(x)' where 'x' is an 'mle_fit' object.

Description

By the invariance property of the MLE, if 'x' is an 'mle_fit' object, then under the right conditions, asymptotically, 'g(x)' is normally distributed, g(x) ~ normal(g(params(x)),sigma) where 'sigma' is the variance-covariance of 'f(x)'

Usage

## S3 method for class 'mle_fit'
rmap(x, g, ..., n = 1000, method = c("mc", "delta"))

Arguments

x

an 'mle_fit' object

g

a deterministic, differentiable function mapping a numeric parameter vector to a numeric vector. The Jacobian is computed numerically via numDeriv::jacobian when method = "delta".

...

additional arguments to pass to the 'g' function

n

number of samples to take to estimate distribution of 'g(x)' if 'method == "mc"'.

method

method to use to estimate distribution of 'g(x)', "delta" or "mc".

Details

We provide two different methods for estimating the variance-covariance of 'f(x)': method = "delta" -> delta method method = "mc" -> monte carlo method

Value

An mle_fit object of class mle_fit_rmap representing the transformed MLE with variance estimated by the specified method.

Examples

# MLE for normal distribution
set.seed(123)
x <- rnorm(100, mean = 5, sd = 2)
n <- length(x)
fit <- mle(
  theta.hat = c(mu = mean(x), var = var(x)),
  sigma = diag(c(var(x)/n, 2*var(x)^2/n)),
  nobs = n
)

# Transform: compute MLE of standard deviation (sqrt of variance)
# Using delta method
g <- function(theta) sqrt(theta[2])
sd_mle <- rmap(fit, g, method = "delta")
params(sd_mle)
se(sd_mle)

Method for sampling from an 'mle_fit' object.

Description

It creates a sampler for the 'mle_fit' object. It returns a function that accepts a single parameter 'n' denoting the number of samples to draw from the 'mle_fit' object.

Usage

## S3 method for class 'mle_fit'
sampler(x, ...)

Arguments

x

the 'mle_fit' object to create sampler for

...

additional arguments to pass

Value

A function that takes parameter n and returns n samples from the asymptotic distribution of the MLE.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
s <- sampler(fit)
head(s(10))

Method for sampling from an 'mle_fit_boot' object.

Description

It creates a sampler for the 'mle_fit_boot' object. It returns a function that accepts a single parameter 'n' denoting the number of samples to draw from the 'mle_fit_boot' object.

Usage

## S3 method for class 'mle_fit_boot'
sampler(x, ...)

Arguments

x

the 'mle_fit_boot' object to create sampler for

...

additional arguments to pass (not used)

Details

Unlike the 'sampler' method for the more general 'mle_fit' objects, for 'mle_fit_boot' objects, we sample from the bootstrap replicates, which are more representative of the sampling distribution, particularly for small samples.

Value

A function that takes parameter n and returns n samples drawn from the bootstrap replicates.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
sampler(fit)(5)

Generic method for computing the score of an estimator object (gradient of its log-likelihood function evaluated at the MLE).

Description

Generic method for computing the score of an estimator object (gradient of its log-likelihood function evaluated at the MLE).

Usage

score_val(x, ...)

Arguments

x

the object to compute the score of.

...

pass additional arguments

Value

The score vector evaluated at the MLE.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), score = c(0.001, -0.002),
  loglike = -120, nobs = 100L)
score_val(fit)

Computes the score of an 'mle_fit' object (score evaluated at the MLE).

Description

If reguarlity conditions are satisfied, it should be zero (or approximately, if rounding errors occur).

Usage

## S3 method for class 'mle_fit'
score_val(x, ...)

Arguments

x

the 'mle_fit' object to compute the score of.

...

additional arguments to pass (not used)

Value

The score vector evaluated at the MLE, or NULL if not available.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), score = c(0.001, -0.002),
  nobs = 100L)
score_val(fit)

Generic method for obtaining the standard errors of an estimator.

Description

Generic method for obtaining the standard errors of an estimator.

Usage

se(x, ...)

Arguments

x

the estimator

...

additional arguments to pass

Value

Vector of standard errors for each parameter.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
se(fit)

Function for obtaining an estimate of the standard error of the MLE object 'x'.

Description

Function for obtaining an estimate of the standard error of the MLE object 'x'.

Usage

## S3 method for class 'mle_fit'
se(x, se.matrix = FALSE, ...)

Arguments

x

the MLE object

se.matrix

if 'TRUE', return the square root of the variance-covariance

...

additional arguments to pass (not used)

Value

Vector of standard errors, or matrix if se.matrix = TRUE, or NULL if variance-covariance is not available.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
se(fit)

Function for obtaining a summary of 'object', which is a fitted 'mle_fit' object.

Description

Function for obtaining a summary of 'object', which is a fitted 'mle_fit' object.

Usage

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

Arguments

object

the 'mle_fit' object

...

pass additional arguments

Value

An object of class summary_mle_fit.

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
summary(fit)

Support of the asymptotic distribution of an MLE.

Description

Support of the asymptotic distribution of an MLE.

Usage

## S3 method for class 'mle_fit'
sup(x)

Arguments

x

An mle_fit object.

Value

A support object (interval for normal distributions).

Examples

fit <- mle(theta.hat = c(mu = 5), sigma = matrix(0.1))
sup(fit)

Computes the variance-covariance matrix of 'mle_fit' object.

Description

Computes the variance-covariance matrix of 'mle_fit' object.

Usage

## S3 method for class 'mle_fit'
vcov(object, ...)

Arguments

object

the 'mle_fit' object to obtain the variance-covariance of

...

additional arguments to pass (not used)

Value

the variance-covariance matrix

Examples

fit <- mle(theta.hat = c(mu = 5, sigma2 = 4),
  sigma = diag(c(0.04, 0.32)), loglike = -120, nobs = 100L)
vcov(fit)

Computes the variance-covariance matrix of 'boot' object. Note: This impelements the 'vcov' method defined in 'stats'.

Description

Computes the variance-covariance matrix of 'boot' object. Note: This impelements the 'vcov' method defined in 'stats'.

Usage

## S3 method for class 'mle_fit_boot'
vcov(object, ...)

Arguments

object

the 'boot' object to obtain the variance-covariance of

...

additional arguments to pass into 'stats::cov'

Value

The variance-covariance matrix estimated from bootstrap replicates.

Examples

set.seed(1)
b <- boot::boot(rexp(50, 2), function(d, i) 1/mean(d[i]), R = 99)
fit <- mle_boot(b)
vcov(fit)