Skip to content

Commit

Permalink
include more information for glance.lm with one predictor and 0-int…
Browse files Browse the repository at this point in the history
…ercept (#1209)



---------

Co-authored-by: simonpcouch <simonpatrickcouch@gmail.com>
  • Loading branch information
jrob95 and simonpcouch authored Sep 26, 2024
1 parent 7f3d795 commit 42b6ad5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# broom (development version)

* `glance.lm()` now returns non-`NA` values for `statistic`, `p.value`, and `df`
for models fitted with a single predictor and no intercept (@jrob95, #1209).

# broom 1.0.6

## New Features
Expand Down
2 changes: 1 addition & 1 deletion R/stats-lm-tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ glance.lm <- function(x, ...) {

# check whether the model was fitted with only an intercept, in which
# case drop the fstatistic related columns
int_only <- nrow(summary(x)$coefficients) == 1
int_only <- nrow(summary(x)$coefficients) == 1 & "(Intercept)" %in% row.names(summary(x)$coefficients)

with(
summary(x),
Expand Down
12 changes: 11 additions & 1 deletion tests/testthat/test-stats-lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test_that("lm tidier arguments", {
fit <- lm(mpg ~ wt, mtcars)
fit2 <- lm(mpg ~ wt + log(disp), mtcars)
fit3 <- lm(mpg ~ 1, mtcars)
fit4 <- lm(mpg ~ 0 + cyl, mtcars)

# zero weights used to break influence columns in augment.lm
wts <- c(0, rep(1, nrow(mtcars) - 1))
Expand Down Expand Up @@ -60,8 +61,9 @@ test_that("glance.lm", {
gl <- glance(fit)
gl2 <- glance(fit2)
gl3 <- glance(fit3)
gl4 <- glance(fit4)

check_glance_outputs(gl, gl2, gl3)
check_glance_outputs(gl, gl2, gl3, gl4)
})

test_that("augment.lm", {
Expand Down Expand Up @@ -141,3 +143,11 @@ test_that("augment.lm", {
"\\`level\\` argument is not supported in the \\`augment\\(\\)\\` method for \\`lm\\` objects"
)
})

test_that("glance.lm returns non-NA entries with 0-intercept model (#1209)", {
fit <- lm(mpg ~ 0 + cyl, mtcars)
fit_glance <- glance(fit)
expect_false(is.na(fit_glance[["statistic"]]))
expect_false(is.na(fit_glance[["p.value"]]))
expect_false(is.na(fit_glance[["df"]]))
})

0 comments on commit 42b6ad5

Please sign in to comment.