goodekat / ggResidpanel

An R package for creating a panel of diagnostic plots for residuals from a model.
Other
36 stars 3 forks source link

Error for polynomial regression #9

Open somakd opened 4 years ago

somakd commented 4 years ago

When poly is used to fit a polynomial via the lm function, resid_panel cannot produce the residual panel.

Here's a reproducible example:

library("ggResidpanel")
set.seed(123)
x = rnorm(100)
y= 1 + x + x^2 + rnorm(100)
fit = lm(y ~ poly(x,2))
resid_panel(fit)

Produces the following error message: Error in `[<-.data.frame`(`*tmp*`, , i, value = c("poly(x, 2): -0.0716640223708324", : replacement has 200 rows, data has 100

However, if I() is used instead, resid_panel works fine:

fit2 = lm(y ~ x + I(x^2))
resid_panel(fit2) # Works fine
goodekat commented 4 years ago

Good find! I'll add this to my to do list.

RenePosma commented 1 year ago

Similar error for glm(y ~ ns(x)) and glmer(y ~ ns(x))

The error is in the function helper_plotly_label when dissecting, it might be due to the fact that: plotly_data <- model$model returns multiple columns within a list when poly(x) or ns(x) is used within the model, while just one column is returned with I(x^2). model$model$ns function provides an error, but is reachable by model$model[,numberofcolumn]. Unlisting does not work.