Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# broom (development version)

* `tidy()` reuses the fitted standard errors when computing confidence intervals for ordinary `lm` models (@soodoku).

# broom 1.0.13

* Resolved an R CMD check WARNING on r-devel about namespace references in `R/sysdata.rda` by moving the `joineRML` test fixture to `tests/testthat/fixtures/`.
Expand Down
15 changes: 13 additions & 2 deletions R/stats-lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,19 @@ tidy.lm <- function(
}

if (conf.int) {
ci <- broom_confint_terms(x, level = conf.level)
ret <- dplyr::left_join(ret, ci, by = "term")
if (
identical(class(x), "lm") && x$df.residual > 0 &&
length(conf.level) == 1L && !anyDuplicated(ret$term)
) {
# confint.lm() uses these same standard errors and residual degrees of freedom.
tail <- (1 - conf.level) / 2
q <- stats::qt(c(tail, 1 - tail), df = x$df.residual)
ret$conf.low <- ret$estimate + ret$std.error * q[[1]]
ret$conf.high <- ret$estimate + ret$std.error * q[[2]]
} else {
ci <- broom_confint_terms(x, level = conf.level)
ret <- dplyr::left_join(ret, ci, by = "term")
}
}

if (exponentiate) {
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-stats-lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ test_that("tidy.lm works", {
expect_no_error(tidy(fit_na_row, conf.int = TRUE))
})

test_that("tidy.lm intervals agree with confint for weighted, aliased fits", {
fit <- lm(mpg ~ wt + I(2 * wt), data = mtcars, weights = hp)
td <- tidy(fit, conf.int = TRUE, conf.level = 0.8, exponentiate = TRUE)
ci <- exp(stats::confint(fit, level = 0.8))
expect_equal(td$term, rownames(ci))
expect_equal(td$conf.low, unname(ci[, 1]))
expect_equal(td$conf.high, unname(ci[, 2]))
})

test_that("glance.lm", {
gl <- glance(fit)
gl2 <- glance(fit2)
Expand Down