Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoplot.lm(which = 2) fails when residuals have class AsIs #202

Closed
richierocks opened this issue May 12, 2020 · 1 comment · Fixed by #203
Closed

autoplot.lm(which = 2) fails when residuals have class AsIs #202

richierocks opened this issue May 12, 2020 · 1 comment · Fixed by #203

Comments

@richierocks
Copy link
Contributor

richierocks commented May 12, 2020

System information

  • R version, OS: R 4.0.0, macOS
  • ggfortify source: CRAN
  • ggfortify version: 0.4.10

Describe the problem

If the response variable in a linear model created with lm() is wrapped in I(), then the residuals have type AsIs, which causes an error in autoplot.lm() when which = 2.

Source code / logs / plots

library(ggplot2)
library(ggfortify)

# Create a linear regression model with response wrapped in I()
mdl_cars <- lm(I(dist ^ 2) ~ speed, data = cars)

# Automatic Q-Q plot
autoplot(mdl_cars, which = 2, ncol = 1)
#> Error in UseMethod("rescale"): no applicable method for 'rescale' applied to an object of class "AsIs"

# Remove the class of the residuals and try again
class(mdl_cars$residuals) <- NULL
autoplot(mdl_cars, which = 2, ncol = 1)

I think that the simplest fix is that autoplot.lm() should just drop the class of the residuals, so it is always dealing with a numeric vector.

@terrytangyuan
Copy link
Collaborator

Thanks for the detailed and reproducible issue! The suggested fix sounds good to me. Would you like to submit a pull request to fix that?

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 a pull request may close this issue.

2 participants