Movatterモバイル変換


[0]ホーム

URL:


Residual plots for linear models

Residual plots using thenullabor package

Thenullabor package provides functions to drawresidual plots for linear regression models using the lineuppackage.

library(nullabor)

First, fit a linear model:

data(tips)x<-lm(tip~ total_bill,data = tips)

Thelineup_residuals function can now be used togenerate four types of residual lineup plots.

The first residual plot shows the residuals versus the fitted values.It is used to test the hypothesis that the response variable is a linearcombination of the predictors. If you can spot the true data in theplot, you can formally reject the null hypothesis with p-value 0.05(Buja et al., 2009; Li et al., 2024). After running the code below, runthedecrypt message(e.g. decrypt("XSKz 5xQx Vd Z3jVQV3d ww")) printed in the RConsole to see which dataset is the true data.

lineup_residuals(x,type =1)

The second plot is a normal Q-Q plot for the residuals, used to testthe hypothesis that the errors are normal:

lineup_residuals(x,type =2)
## Warning in lineup_residuals(x, type = 2): Method "rotate" does not generate## normal residuals. Using method = "pboot" instead.

The third plot is a scale-location plot used to test the hypothesisthat the errors are homoscedastic:

lineup_residuals(x,type =3)

The fourth plot shows leverage, and is used to identify points withhigh residuals and high leverage, which are likely to have a stronginfluence on the model fit:

lineup_residuals(x,type =4)

The plots are created usingggplot2 and can be modifiedin the same way as other ggplots. In addition,lineup_residuals has arguments for changing the colorsused:

library(ggplot2)lineup_residuals(x,type =3,color_points ="skyblue",color_trends ="darkorange")+theme_minimal()

If the null hypothesis in the type 1 plot is violated, consider usinga different model. If the null hypotheses in the type 2 or 3 plots areviolated, consider using bootstrap p-values; seeSection8.1.5 of Thulin (2024) for details and recommendations.

References

Buja, A., Cook, D., Hofmann, H., Lawrence, M., Lee, E.-K., Swayne, D.F, Wickham, H. (2009) Statistical Inference for Exploratory DataAnalysis and Model Diagnostics, Royal Society Philosophical TransactionsA, 367:4361–4383, DOI: 10.1098/rsta.2009.0120.

Li, W., Cook, D., Tanaka, E., & VanderPlas, S. (2024). A plot isworth a thousand tests: Assessing residual diagnostics with the lineupprotocol. Journal of Computational and Graphical Statistics, 1-19.

Thulin, M. (2024)Modern Statistics with R. Boca Raton: CRCPress. ISBN 9781032512440.https://www.modernstatisticswithr.com/


[8]ページ先頭

©2009-2025 Movatter.jp