tidymodels / broom

Convert statistical analysis objects from R into tidy format
https://broom.tidymodels.org
Other
1.45k stars 302 forks source link

test failure re: `augment.mfx()` #1218

Closed simonpcouch closed 4 days ago

simonpcouch commented 4 days ago
── Error ('test-mfx.R:86:3'): augment.mfx ──────────────────────────────────────
Error in `data.frame(..., check.names = FALSE)`: arguments imply differing number of rows: 6, 1000
Backtrace:
    ▆
 1. └─modeltests::check_augment_function(...) at test-mfx.R:86:3
 2.   └─modeltests:::check_augment_data_specification(...)
 3.     └─broom (local) aug(model, data = head_dl$tibble, newdata = head_new_dl$tibble)
 4.       └─broom:::augment.betareg(...) at broom/R/mfx-tidiers.R:300:3
 5.         └─broom::augment_columns(...) at broom/R/betareg-tidiers.R:80:3
 6.           ├─tibble::as_tibble(cbind(original, ret)) at broom/R/utilities.R:319:5
 7.           └─base::cbind(original, ret) at broom/R/utilities.R:319:5
 8.             └─base::cbind(deparse.level, ...)
 9.               └─base::data.frame(..., check.names = FALSE)
simonpcouch commented 4 days ago

Errors at the same place as #1216. No predictions from the mfx object, so augment_columns() tries to use fitted().

library(broom)
library(mfx)

# simulate data
set.seed(12345)
n <- 1000
x <- rnorm(n)

# beta outcome
y_beta <- rbeta(n, shape1 = plogis(1 + 0.5 * x), shape2 = (abs(0.2 * x)))
y_beta <- (y_beta * (n - 1) + 0.5) / n # Smithson and Verkuilen correction

df <- data.frame(y_beta, x)

fit_betamfx <- betamfx(y_beta ~ x | x, data = df)

augment(fit_betamfx, head(df))
#> Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 6, 1000

predict(fit_betamfx, head(df))
#> Error in UseMethod("predict"): no applicable method for 'predict' applied to an object of class "betamfx"

Created on 2024-09-26 with reprex v2.1.1

simonpcouch commented 4 days ago

No mfx update in the last 5 years, so not due to any changes there.

Actually first surfaced here:

https://github.com/tidymodels/broom/blob/d112dd779e804ec7b65e0f8d6f940524a6c55c51/tests/testthat/test-mfx.R#L81-L90

These tidiers dispatch directly to augment.betareg(), so this is quite related to #1216.

augment_columns() is receiving several type.predict() values, resulting in:

predict(model, newdata = newdata, type = c("response", "link"))
#> Error in match.arg(type, c("response", "link", "precision", "variance",  : 
#>  'arg' must be of length 1