diff --git a/DESCRIPTION b/DESCRIPTION index 02adeddce..2f02c2ab3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: marginaleffects Title: Predictions, Comparisons, Slopes, Marginal Means, and Hypothesis Tests -Version: 0.22.0.5 +Version: 0.22.0.6 Authors@R: c(person(given = "Vincent", family = "Arel-Bundock", diff --git a/NEWS.md b/NEWS.md index 3e2848166..cc9b7d842 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,6 +15,7 @@ Bugs: * `hypotheses(joint = TRUE)` would throw an error if sample sizes could not be computed, even if they were not needed. Thanks to Noah Greifer. * `hypotheses(joint = TRUE)` respects the `vcov` argument. Thanks to @kennchua for report #1214. * `ordbetareg` models in `glmmTMB` are now supported. Thanks to @jgeller112 for code contribution #1221. +* `tidymodels()`: Indexing overrode the value of predictors in the output data frame. The numerical estimates were unaffected. Thanks to @agmath for report #1209. ## 0.22.0 diff --git a/R/methods_tidymodels.R b/R/methods_tidymodels.R index 2129439a2..281c4ff75 100644 --- a/R/methods_tidymodels.R +++ b/R/methods_tidymodels.R @@ -41,10 +41,10 @@ get_predict.model_fit <- function(model, newdata, type = NULL, ...) { if (type == "numeric") { v <- intersect(c(".pred", ".pred_res"), colnames(out))[1] - out <- data.frame(rowid = seq_along(out), estimate = out[[v]]) + out <- data.frame(rowid = seq_len(nrow(out)), estimate = out[[v]]) } else if (type == "class") { - out <- data.frame(rowid = seq_along(out), estimate = out[[".pred_class"]]) + out <- data.frame(rowid = seq_len(nrow(out)), estimate = out[[".pred_class"]]) } else if (type == "prob") { colnames(out) <- substr(colnames(out), 7, nchar(colnames(out))) diff --git a/inst/tinytest/test-pkg-tidymodels.R b/inst/tinytest/test-pkg-tidymodels.R index 9f3fd4353..cc18f8ff4 100644 --- a/inst/tinytest/test-pkg-tidymodels.R +++ b/inst/tinytest/test-pkg-tidymodels.R @@ -78,6 +78,24 @@ p <- plot_comparisons(fit, expect_inherits(p, "data.frame") +# Issue 1209 +nobs <- 50 +my_data <- tibble( + x = runif(nobs, 0, 10), + y = -(x - 11)^2 + 100 + rnorm(nobs, 0, 25) +) +lr_spec <- linear_reg() +lr_rec <- recipe(y ~ x, data = my_data) |> + step_poly(x, degree = 2) +lr_wf <- workflow() |> + add_model(lr_spec) |> + add_recipe(lr_rec) +lr_fit <- lr_wf |> + fit(my_data) +mfx <- slopes(lr_fit, newdata = my_data, variable = "x") +expect_equivalent(mfx$x, my_data$x) +expect_equivalent(mfx$y, my_data$y) + rm(list = ls())