Skip to content

Reuse coefficient standard errors in tidy.lm confidence intervals - #1282

Open
soodoku wants to merge 1 commit into
tidymodels:mainfrom
finite-sample:perf/lm-confidence-intervals
Open

soodoku wants to merge 1 commit into
tidymodels:mainfrom
finite-sample:perf/lm-confidence-intervals

Conversation

@soodoku

@soodoku soodoku commented Nov 21, 2026

Copy link
Copy Markdown

For an ordinary linear model, tidy.lm() already has the coefficient estimates and standard errors from summary.lm(). Computing the two Student t endpoints there saves a second model summary inside confint.lm() and a join. Subclasses continue to dispatch to their own interval methods. Models with zero residual degrees of freedom or duplicate term names keep the existing path.

On an Apple Silicon Mac, I timed each installed revision in a fresh R process, alternating execution order across seven blocks. Each block timed 40 calls after a warm-up. The entries are median milliseconds per public call.

Operation Current main This PR
Tidy a fitted linear model with intervals, 99 rows 0.975 0.300
Tidy a fitted linear model with intervals, 19,999 rows 1.825 0.750
Fit the larger model and tidy with intervals 4.050 2.975
Tidy the larger fit with default settings 0.675 0.675

A minimal timing example, run in each installed revision:

set.seed(36017)
d <- data.frame(y = rnorm(20000), x = rnorm(20000), z = rnorm(20000))
d$x[7] <- NA
fit <- lm(y ~ x + z, d)
invisible(broom::tidy(fit, conf.int = TRUE))
system.time(for (i in seq_len(1000)) broom::tidy(fit, conf.int = TRUE))

The added regression test compares both exponentiated endpoints and coefficient labels to stats::confint() on a weighted, rank-deficient fit. Separately, I compared endpoints to a direct weighted least-squares calculation across six fits, four confidence levels, and both exponentiation settings (absolute error below 2e-12). A subclass with its own confidence interval method retained its custom endpoints. On an essentially perfect fit, removing the second summary also removes its duplicate warning.

R CMD check --no-manual finished with status OK on both revisions. The development test suite with NOT_CRAN=true passed locally on the proposed revision as well.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant