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
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)

* `augment.glm()` now keeps zero-weight rows aligned with their influence
diagnostics (@M-Colley, #1250).

# 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
11 changes: 7 additions & 4 deletions R/stats-glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ augment.glm <- function(
{
infl <- influence(x, do.coef = FALSE)
df$.resid <- residuals(x, type = type.residuals) |> unname()
df <- add_hat_sigma_cols(df, x, infl)
df$.std.resid <- rstandard(x, infl = infl, type = type.residuals) |>
unname()
df$.cooksd <- cooks.distance(x, infl = infl) |> unname()
df <- add_hat_sigma_cols(df, x, infl, type = type.residuals)
if (inherits(x$na.action, "exclude")) {
# These diagnostics can include padding for excluded observations.
df$.std.resid <- rstandard(x, infl = infl, type = type.residuals) |>
unname()
df$.cooksd <- cooks.distance(x, infl = infl) |> unname()
}
},
error = data_error
)
Expand Down
4 changes: 2 additions & 2 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ safe_response <- purrr::possibly(response, NULL)
# in weighted regressions, influence measures should be zero for
# data points with zero weight
# helper for augment.lm and augment.glm
add_hat_sigma_cols <- function(df, x, infl) {
add_hat_sigma_cols <- function(df, x, infl, ...) {
df$.hat <- 0
df$.sigma <- 0
df$.cooksd <- 0
Expand All @@ -399,7 +399,7 @@ add_hat_sigma_cols <- function(df, x, infl) {

df$.hat[nonzero_idx] <- infl$hat |> unname()
df$.sigma[nonzero_idx] <- infl$sigma |> unname()
df$.std.resid[nonzero_idx] <- rstandard(x, infl = infl) |> unname()
df$.std.resid[nonzero_idx] <- rstandard(x, infl = infl, ...) |> unname()
df$.cooksd[nonzero_idx] <- cooks.distance(x, infl = infl) |> unname()
df
}
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-stats-glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ test_that("augment.glm", {
)
})

test_that("augment.glm aligns diagnostics with zero-weight observations", {
df <- data.frame(
x = rep(0:2, each = 4),
y = rep(c(0, 1, 1, 0), 3),
w = c(1, 1, 0, 1, 1, 1, 2, 1, 1, 0, 1, 1)
)
fit <- glm(y ~ x, df, family = binomial(), weights = w)
nonzero <- df$w != 0
infl <- influence(fit, do.coef = FALSE)

for (type in c("deviance", "pearson")) {
aug <- augment(fit, type.residuals = type)

expect_equal(nrow(aug), nrow(df))
expect_equal(aug$.fitted, unname(predict(fit)))
expect_equal(aug$.resid, unname(residuals(fit, type = type)))
expect_equal(
aug$.std.resid[nonzero],
unname(rstandard(fit, infl = infl, type = type))
)
expect_equal(aug$.std.resid[!nonzero], c(NA_real_, NA_real_))
expect_equal(aug$.cooksd[nonzero], unname(cooks.distance(fit, infl = infl)))
expect_equal(aug$.cooksd[!nonzero], c(0, 0))
}
})

test_that("glm tidiers warn informatively with glm.fit2 input", {
gfit <- glm(am ~ wt, mtcars, family = "binomial")
gfit$method <- "glm.fit2"
Expand Down