goodekat / ggResidpanel

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

Issue with Pearson residuals for heterogeneous variances #11

Open SimonShamusRiley opened 2 years ago

SimonShamusRiley commented 2 years ago

I am concerned that resid_panel might be improperly calculating Pearson residuals for lme models with heterogeneous variances. I believe the issue lies on line 39 of the helper_resid.R script. A short reproducible example:

library(agridat)
library(nlme)
library(ggResidpanel)

data('graybill.heteroskedastic')
str(graybill.heteroskedastic)

fit1 <- lme(fixed = yield ~ gen, 
           data = graybill.heteroskedastic, 
           random = ~1|env)

# With homogeneous variances, these are identical:
resid_panel(fit1, type = 'pearson')
resid_auxpanel(residuals = resid(fit1, type = 'pearson'),
               predicted = predict(fit1))

fit2 <- lme(fixed = yield ~ gen, 
            data = graybill.heteroskedastic, 
            random = ~1|env,
            weights = varIdent(form = ~1|gen))

# With heterogeneous variances, these are not:
resid_panel(fit2, type = 'pearson')
resid_auxpanel(residuals = resid(fit2, type = 'pearson'),
               predicted = predict(fit2))