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.
System information
Describe the problem
If the response variable in a linear model created with
lm()
is wrapped inI()
, then the residuals have typeAsIs
, which causes an error inautoplot.lm()
whenwhich = 2
.Source code / logs / plots
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.