Package {avlm}


Title: Safe Anytime Valid Inference for Linear Models
Version: 0.2.0
Description: Anytime-valid inference for linear models, namely, sequential t-tests, sequential F-tests, and confidence sequences with time-uniform Type-I error and coverage guarantees. This allows hypotheses to be continuously tested without sacrificing false positive guarantees. Heteroskedasticity-robust and cluster-robust versions are provided. It is based on the methods documented in Lindon et al. (2026) <doi:10.1080/01621459.2026.2692052> and Lindon (2026) <doi:10.48550/arXiv.2610.25613>.
License: MIT + file LICENSE
Encoding: UTF-8
Suggests: testthat (≥ 3.0.0)
Config/testthat/edition: 3
Config/roxygen2/version: 8.0.0
NeedsCompilation: no
Packaged: 2026-10-20 16:35:38 UTC; mlindon
Author: Michael Lindon [aut, cre]
Maintainer: Michael Lindon <michael.s.lindon@gmail.com>
Repository: CRAN
Date/Publication: 2026-10-21 16:50:25 UTC

Anytime-valid Conversion Generic Function

Description

This generic function converts a fitted model object into an anytime-valid version. The conversion is performed by the appropriate S3 method based on the class of the input model.

Usage

av(model, g = 1, ...)

Arguments

model

A fitted model object (e.g., an object of class aov or lm).

g

A positive precision parameter for anytime-valid inference (see av.lm). Defaults to 1.

...

Additional arguments passed to the method.

Value

An enhanced version of model with anytime-valid inference capabilities.


Convert an aov Object to Anytime-Valid aov (avaov)

Description

Converts an object of class aov to an anytime-valid version by setting the precision parameter g as an attribute and updating the class.

Usage

## S3 method for class 'aov'
av(model, g = 1, ...)

Arguments

model

An aov object resulting from an ANOVA analysis.

g

A positive precision parameter for anytime-valid inference (see av.lm). Default is 1.

...

Additional arguments passed to or from other methods.

Value

An object of class avaov with anytime-valid p-values.


Convert a Linear Model (lm) Object to Anytime-Valid lm (avlm)

Description

Converts an lm object into an anytime-valid version by storing a precision parameter g as an attribute and updating the object's class. The resulting object supports anytime-valid inference.

Usage

## S3 method for class 'lm'
av(model, g = 1, vcov_estimator = NULL, cluster = NULL, ...)

Arguments

model

An lm object from a linear model fit.

g

A positive precision parameter for the anytime-valid mixture (see Lindon et al., 2026, Section 4.3.1). It acts like a prior sample size: larger values favour smaller effects and give narrower intervals at large n, at the cost of wider intervals at small n. Use optimal_g to choose it for a target sample size. Default is 1.

vcov_estimator

Optional character string specifying the type of heteroskedasticity-robust standard errors to use. Must be one of "HC0", "HC1", "HC2", or "HC3". If NULL (default), no robust variance estimation is applied. Cannot be combined with cluster.

cluster

Optional cluster specification for cluster-robust standard errors. Either a vector of cluster identifiers, one per observation used in fitting model (e.g. a respondent or unit id); a one-sided formula such as ~ cyl; or a single column name such as "cyl". Formulas and column names are evaluated in the data the model was fit with, and rows dropped from the fit (by subset or missing values) are dropped from the clusters too. Only one-way clustering is supported. When supplied, standard errors are computed using the CR1 cluster-robust sandwich estimator (scaled by G/(G-1)), and the anytime-valid p-values and confidence intervals use G - 1 (clusters minus one) as the denominator degrees of freedom, rather than n - p (observations minus coefficients), and the number of clusters G as the sample size, so g is on the cluster scale. The cluster-robust e-values and confidence sequences are those of Lindon (2026). Cannot be combined with vcov_estimator.

...

Additional arguments passed to or from other methods.

Value

An enhanced lm object of class avlm with anytime-valid inference capabilities.

References

Lindon, M., Ham, D. W., Tingley, M., and Bojinov, I. (2026). Anytime-Valid Inference in Linear Models with Applications to Regression-Adjusted Causal Inference. Journal of the American Statistical Association. doi:10.1080/01621459.2026.2692052

Lindon, M. (2026). Anytime-Valid Inference in Linear Models with Clustered Outcomes: Exact Multivariate Tests and Cluster-Robust Confidence Sequences. arXiv:2610.25613. https://arxiv.org/abs/2610.25613

Examples

# Fit a linear model on the built-in mtcars dataset
fit <- lm(mpg ~ wt + hp, data = mtcars)

# Convert the standard lm object to an anytime-valid avlm object
av_fit <- av(fit, g = 1)

# Print the summary of the anytime-valid model
summary(av_fit)

# With cluster-robust standard errors, clustering on number of cylinders
av_fit_cl <- av(fit, g = 1, cluster = ~ cyl)
summary(av_fit_cl)

# Equivalent ways of specifying the clusters
av_fit_cl <- av(fit, g = 1, cluster = "cyl")
av_fit_cl <- av(fit, g = 1, cluster = mtcars$cyl)


Coefficient Covariance and Degrees of Freedom for Anytime-Valid Inference

Description

Computes, for an avlm object, the coefficient estimates, their covariance matrix and the two quantities the anytime-valid e-values need: the denominator degrees of freedom nu and the sample size n on which the precision g acts.

Usage

av_vcov(object)

Arguments

object

An avlm object.

Details

Aliased (NA) coefficients are dropped. For weighted fits, the rows of X and the residuals are scaled by \sqrt{w} and observations with zero weight are dropped.

Value

A list with elements coefficients (non-aliased estimates), vcov, nu, n, type ("classical", an HC type, or "CR1") and G (number of clusters, or NULL).


Validate the Precision Parameter g

Description

Validate the Precision Parameter g

Usage

check_g(g)

Arguments

g

Candidate value of g.

Value

g, invisibly, if it is a single finite positive number.


Look Up a Cluster Variable in the Data a Model Was Fit With

Description

Look Up a Cluster Variable in the Data a Model Was Fit With

Usage

cluster_from_data(model, cluster)

Arguments

model

A fitted lm object.

cluster

A one-sided formula such as ~ cyl, or a single column name.

Value

The cluster variable, subset to the observations used in the fit.


Confidence Intervals for Anytime-Valid lm (avlm) Objects

Description

Computes confidence intervals for the coefficients of an avlm object by adjusting the standard errors using the precision parameter g and an anytime-valid approach.

Usage

## S3 method for class 'avlm'
confint(object, parm, level = 0.95, ...)

Arguments

object

An avlm object.

parm

A specification of which parameters are to be given confidence intervals; can be a vector of numbers or names.

level

The confidence level required for the intervals. Defaults to 0.95.

...

Additional arguments passed to or from other methods.

Value

A matrix with the lower and upper confidence limits for the specified parameters.

Examples

# Fit a simple linear model using the mtcars dataset
fit <- lm(mpg ~ wt + hp, data = mtcars)

# Convert the standard lm object to an anytime-valid avlm object with precision parameter g = 1
av_fit <- av(fit, g = 1)

# Calculate and print confidence intervals for the coefficients
conf_intervals <- confint(av_fit)
print(conf_intervals)


Check Whether a Covariance Matrix Is Numerically Singular

Description

Scale-invariant: checks the smallest eigenvalue of the implied correlation matrix.

Usage

is_singular_vcov(V, tol = 1e-10)

Arguments

V

A symmetric covariance matrix.

tol

Tolerance for the smallest eigenvalue of the correlation matrix.

Value

TRUE if V is numerically singular.


Log Cluster-Robust e-Value for a Joint Wald Test

Description

The cluster-robust e-process of Lindon (2026), eq. (ECR), with \nu = G - 1 and exponent G/2 for any number q of tested coefficients:

E_G = \left(\frac{g}{g+G}\right)^{q/2} \left(\frac{1 + Q/(G-1)}{1 + \frac{g}{g+G} Q/(G-1)}\right)^{G/2}.

Usage

log_E_CR(Q, q, G, g)

Arguments

Q

The cluster-robust Wald statistic.

q

The number of tested coefficients.

G

The number of clusters.

g

The precision parameter, on the cluster scale.

Value

The log e-value.

References

Lindon, M. (2026). Anytime-Valid Inference in Linear Models with Clustered Outcomes: Exact Multivariate Tests and Cluster-Robust Confidence Sequences. arXiv:2610.25613. https://arxiv.org/abs/2610.25613


Computes the value of g such that width of the 1-\alpha confidence interval at sample size n is minimized

Description

Minimizes the radius R(g, n, 1, \alpha) of the asymptotic t confidence sequence (Lindon et al., 2026, Section 5.1) over g.

Usage

optimal_g(n, number_of_coefficients, alpha, nu = n - number_of_coefficients)

Arguments

n

A positive sample size integer. For cluster-robust inference, the number of clusters.

number_of_coefficients

A positive integer of coefficients in the full model

alpha

A positive numeric scalar in (0,1) for nominal Type I error.

nu

Denominator degrees of freedom. Defaults to n - number_of_coefficients; for cluster-robust inference (av(..., cluster = )) use nu = n - 1 with n the number of clusters.

Value

A positive numeric scalar representing the optimal g that minimizes the CI width.

Examples

n <- 10000
alpha <- 0.05
g_star <- optimal_g(n, 5, alpha)
cat("The optimal g is:", g_star, "\n")

# cluster-robust inference with 200 clusters
optimal_g(200, alpha = 0.05, nu = 199)


Print Method for summary.avaov Objects

Description

This method prints the summary of an avaov object. It captures the output from the default printing method, substitutes the header "Pr(>F)" with "p value", and adds a note indicating that anytime-valid inference is used.

Usage

## S3 method for class 'summary.avaov'
print(
  x,
  digits = max(3L, getOption("digits") - 3L),
  signif.stars = getOption("show.signif.stars"),
  ...
)

Arguments

x

An object of class summary.avaov.

digits

The number of significant digits to use when printing. Defaults to a value based on options.

signif.stars

Logical indicating whether significance stars should be printed.

...

Additional arguments passed to or from other methods.

Value

Invisibly returns the summary object.


Print Method for summary.avlm Objects

Description

Custom print method for summary.avlm objects that mimics the standard summary.lm output, but replaces p-value column headers and includes anytime-valid annotations.

Usage

## S3 method for class 'summary.avlm'
print(
  x,
  digits = max(3L, getOption("digits") - 3L),
  signif.stars = getOption("show.signif.stars"),
  ...
)

Arguments

x

An object of class summary.avlm containing the summary of an anytime-valid lm object.

digits

The number of significant digits to use when printing. Defaults based on system options.

signif.stars

Logical indicating whether significance stars should be printed.

...

Additional arguments passed to or from other methods.

Value

Invisibly returns the summary object.


Resolve a Cluster Argument into a Factor Aligned with the Fitted Model

Description

Resolve a Cluster Argument into a Factor Aligned with the Fitted Model

Usage

resolve_cluster_factor(model, cluster)

Arguments

model

A fitted lm object.

cluster

NULL, a vector of cluster identifiers with one entry per observation used in the fit, a one-sided formula such as ~ cyl, or a single column name such as "cyl". Formulas and column names are looked up in the data the model was fit with and subset to the observations used in the fit.

Value

A factor of cluster identifiers, or NULL if cluster is NULL.


Summary Method for Anytime-Valid aov Objects

Description

This method produces a summary for objects of class avaov. It first calls the default summary.aov method and then replaces the standard p-values with anytime-valid p-values calculated using the precision parameter g.

Usage

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

Arguments

object

An object of class avaov created by av.aov.

...

Additional arguments passed to or from other methods.

Value

A summary object of class summary.avaov that includes the anytime-valid p-values.

Examples


# Fit an ANOVA model to the iris dataset.
# This model tests whether the sepal length differs by species.
fit_aov <- aov(Sepal.Length ~ Species, data = iris)

# Convert the standard aov object to an anytime-valid aov (avaov) with precision parameter g = 1.
av_fit_aov <- av(fit_aov, g = 1)

# Print the summary of the anytime-valid ANOVA model.
# The summary replaces standard p-values with anytime-valid p-values.
summary(av_fit_aov)


Summary Method for Anytime-Valid lm (avlm) Objects

Description

Computes a summary for an avlm object, a linear model enhanced with anytime-valid inference. In addition to the standard lm summary statistics, the p-values for the coefficient tests are recalculated using an anytime-valid method.

Usage

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

Arguments

object

An avlm object generated by av.lm.

...

Additional arguments passed to or from other methods.

Value

A summary object of class summary.avlm that includes updated p-values for the coefficients.


Covariance Matrix for Anytime-Valid lm (avlm) Objects

Description

Returns the covariance matrix used for anytime-valid inference: the classical one, or the heteroskedasticity-robust or cluster-robust one requested in av.lm.

Usage

## S3 method for class 'avlm'
vcov(object, complete = TRUE, ...)

Arguments

object

An avlm object.

complete

Logical; if TRUE (the default), aliased coefficients are included with NA rows and columns, as in vcov.

...

Unused.

Value

A covariance matrix.

Examples

fit <- av(lm(mpg ~ wt + hp, data = mtcars), cluster = ~ cyl)
vcov(fit)