Skip to content

Add residualize_over_grid()#386

Merged
strengejacke merged 43 commits intomainfrom
residualize_over_grid
Aug 18, 2025
Merged

Add residualize_over_grid()#386
strengejacke merged 43 commits intomainfrom
residualize_over_grid

Conversation

@strengejacke
Copy link
Copy Markdown
Member

@strengejacke strengejacke commented Feb 9, 2025

Fixes #129

@bwiernik
Copy link
Copy Markdown
Contributor

bwiernik commented Feb 9, 2025

Could we instead make this a model_residuals() function that takes a newdata argument, which could be a grid from get_datagrid()?

@strengejacke
Copy link
Copy Markdown
Member Author

Sure, but do we need newdata? The grid is extracted / taken from the estimate_means() (or related) result.

@DominiqueMakowski
Copy link
Copy Markdown
Member

It can still be useful for power-users to have a way to pass the data no?

@strengejacke strengejacke marked this pull request as ready for review August 16, 2025 17:25
@strengejacke strengejacke requested a review from Copilot August 16, 2025 17:26
@strengejacke
Copy link
Copy Markdown
Member Author

@gemini-code-assist review

This comment was marked as outdated.

gemini-code-assist[bot]

This comment was marked as outdated.

strengejacke and others added 2 commits August 16, 2025 19:29
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@strengejacke
Copy link
Copy Markdown
Member Author

This is the current state - though I'm not sure how to fix the last error, and I don't understand why ggplot complains about discrete values?

@strengejacke
Copy link
Copy Markdown
Member Author

library(modelbased)

model <- lm(Sepal.Length ~ Species * Sepal.Width, data = iris)

pr <- estimate_means(model, c("Sepal.Width", "Species"))
plot(pr, show_residuals = TRUE)

set.seed(1234)
x1 <- rnorm(200)
x2 <- rnorm(200)

# quadratic relationship
y <- 2 * x1 + x1^2 + 4 * x2 + rnorm(200)
d <- data.frame(x1, x2, y)

model <- lm(y ~ x1 + x2, data = d)

pr <- estimate_means(model, "x1")
plot(pr, show_residuals = TRUE)

model <- lm(y ~ x1 + x2, data = d)

pr <- estimate_means(model, c("x1", "x2=[sd]"))
plot(pr, show_residuals = TRUE)
#> Error in `scale_colour_continuous()`:
#> ! Discrete value supplied to a continuous scale.
#> ℹ Example values: -0.93, 0.07, and 1.08.

Created on 2025-08-16 with reprex v2.1.1

@strengejacke
Copy link
Copy Markdown
Member Author

It can still be useful for power-users to have a way to pass the data no?

I'm not quite sure at which point we should / can add newdata, since the data frame method already accepts a grid? Feel free to modify/add this function, where necessary.

@strengejacke strengejacke merged commit 164a4ee into main Aug 18, 2025
18 of 21 checks passed
@strengejacke strengejacke deleted the residualize_over_grid branch August 18, 2025 06:19
@marcora
Copy link
Copy Markdown

marcora commented Oct 14, 2025

Is there a way to get a table of partial residuals as easily as it is done here for plotting?

@strengejacke
Copy link
Copy Markdown
Member Author

Yes, should be residualize_over_grid().

@marcora
Copy link
Copy Markdown

marcora commented Oct 14, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Visualization: Partial residual plots

5 participants