Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for conf.level in augment.lm #1191

Merged
merged 16 commits into from
May 6, 2024
Merged
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: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ Suggests:
mgcv,
mlogit,
modeldata,
modeltests,
modeltests (>= 0.1.6),
muhaz,
multcomp,
network,
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

* Corrected coefficients values in `tidy.varest()` output (#1174).

* Added support for `conf.level` in `augment.lm()` (#1191 by `@zietzm`).

# broom 1.0.5

Expand Down
11 changes: 9 additions & 2 deletions R/stats-lm-tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ tidy.lm <- function(x, conf.int = FALSE, conf.level = 0.95,
#' @template param_newdata
#' @template param_se_fit
#' @template param_interval
#' @param conf.level The confidence level to use for the interval created if
#' `interval` is `"confidence"` or `"prediction"`. Must be strictly greater
#' than 0 and less than 1. Defaults to 0.95, which corresponds to a 95
#' percent confidence/prediction interval.
#' @template param_unused_dots
#'
#' @evalRd return_augment(
#' ".hat",
Expand All @@ -151,11 +156,13 @@ tidy.lm <- function(x, conf.int = FALSE, conf.level = 0.95,
#' @seealso [augment()], [stats::predict.lm()]
#' @family lm tidiers
augment.lm <- function(x, data = model.frame(x), newdata = NULL,
se_fit = FALSE, interval = c("none", "confidence", "prediction"), ...) {
se_fit = FALSE, interval = c("none", "confidence", "prediction"),
conf.level = 0.95, ...) {
warn_on_subclass(x, "augment")
check_ellipses("level", "augment", "lm", ...)

interval <- match.arg(interval)
df <- augment_newdata(x, data, newdata, se_fit, interval)
df <- augment_newdata(x, data, newdata, se_fit, interval, level = conf.level)

if (is.null(newdata)) {
tryCatch(
Expand Down
6 changes: 6 additions & 0 deletions man/augment.lm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion tests/testthat/test-stats-lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ library(modeltests)
test_that("lm tidier arguments", {
check_arguments(tidy.lm)
check_arguments(glance.lm)
check_arguments(augment.lm)
# errors with "Tidiers with `conf.level` argument must have `conf.int` argument."
# check_arguments(augment.lm)
})

fit <- lm(mpg ~ wt, mtcars)
Expand Down Expand Up @@ -111,4 +112,32 @@ test_that("augment.lm", {
data = mtcars,
newdata = mtcars
)

# conf.level defaults to 0.95
aug <- augment(fit, newdata = mtcars, interval = "confidence")
pred <- predict(fit, newdata = mtcars, interval = "confidence", level = 0.95)
expect_equal(aug$.lower, pred[, "lwr"])
expect_equal(aug$.upper, pred[, "upr"])

# conf.level is respected
aug <- augment(fit, newdata = mtcars, interval = "confidence", conf.level = 0.75)
pred <- predict(fit, newdata = mtcars, interval = "confidence", level = 0.75)
expect_equal(aug$.lower, pred[, "lwr"])
expect_equal(aug$.upper, pred[, "upr"])

# conf.level works for prediction intervals as well
aug <- augment(fit, newdata = mtcars, interval = "prediction", conf.level = 0.25)
pred <- predict(fit, newdata = mtcars, interval = "prediction", level = 0.25)
expect_equal(aug$.lower, pred[, "lwr"])
expect_equal(aug$.upper, pred[, "upr"])

# conf.level is ignored when interval = "none"
aug <- augment(fit, newdata = mtcars, interval = "none", conf.level = 0.25)
expect_false(any(names(aug) %in% c(".lower", ".upper")))

# warns when passed as level rather than conf.level
expect_warning(
augment(fit, newdata = mtcars, interval = "confidence", level = 0.95),
"\\`level\\` argument is not supported in the \\`augment\\(\\)\\` method for \\`lm\\` objects"
)
})