Skip to content

Commit

Permalink
Add support for conf.level in augment.lm (#1191)
Browse files Browse the repository at this point in the history
* take argument as `conf.level` rather than `level`

Co-authored-by: simonpcouch <simonpatrickcouch@gmail.com>
  • Loading branch information
zietzm and simonpcouch committed May 6, 2024
1 parent 8a30408 commit e11cd4a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
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 @@ -14,6 +14,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"
)
})

0 comments on commit e11cd4a

Please sign in to comment.