strengejacke / ggeffects

Estimated Marginal Means and Marginal Effects from Regression Models for ggplot2
https://strengejacke.github.io/ggeffects
Other
544 stars 35 forks source link

"Could not find model object to extract residuals." fails with a pipe #526

Closed Deleetdk closed 4 months ago

Deleetdk commented 4 months ago
#debugging reprex

library(tidyverse)
library(ggeffects)

#add_data works
lm(Sepal.Length ~ Petal.Width + Species, data = iris) %>% 
  ggpredict(terms = "Petal.Width") %>% 
  plot(add.data = T) +
  ggtitle(NULL) +
  theme_bw()
#> Data points may overlap. Use the `jitter` argument to add some amount of
#>   random variation to the location of data points and avoid overplotting.


#but show_residuals does not
lm(Sepal.Length ~ Petal.Width + Species, data = iris) %>% 
  ggpredict(terms = "Petal.Width") %>% 
  plot(show_residuals = T) +
  ggtitle(NULL) +
  theme_bw()
#> Could not find model object to extract residuals.


#without a pipe it works
lm_fit = lm(Sepal.Length ~ Petal.Width + Species, data = iris)

ggpredict(lm_fit, terms = "Petal.Width") %>% 
  plot(show_residuals = T) +
  ggtitle(NULL) +
  theme_bw()
#> Data points may overlap. Use the `jitter` argument to add some amount of
#>   random variation to the location of data points and avoid overplotting.

Created on 2024-05-16 with reprex v2.1.0

strengejacke commented 4 months ago

Which version of ggeffects are you using? For me, your example works fine.

library(ggplot2)
library(ggeffects)
lm(Sepal.Length ~ Petal.Width + Species, data = iris) |> 
  ggpredict(terms = "Petal.Width") |> 
  plot(show_residuals = TRUE) +
  ggtitle(NULL) +
  theme_bw()
#> Data points may overlap. Use the `jitter` argument to add some amount of
#>   random variation to the location of data points and avoid overplotting.

Created on 2024-05-18 with reprex v2.1.0

Please run ggeffects::install_latest() and try again - does that help?

strengejacke commented 4 months ago

Oh, it's an issue of the magrittr-pipe:

library(magrittr)
library(ggplot2)
library(ggeffects)
lm(Sepal.Length ~ Petal.Width + Species, data = iris) %>%
  ggpredict(terms = "Petal.Width") %>%
  plot(show_residuals = TRUE) +
  ggtitle(NULL) +
  theme_bw()
#> Could not find model object to extract residuals.

Created on 2024-05-18 with reprex v2.1.0

I would close this as "invalid" - it works with the native R pipe, or maybe it can be fixed in magrittr?

Deleetdk commented 4 months ago

Interesting. Good idea to check native pipe. I think closing is fine.

strengejacke commented 4 months ago

Ok, thanks!