-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathresidual_diagnostics.Rmd
More file actions
76 lines (55 loc) · 2.29 KB
/
Copy pathresidual_diagnostics.Rmd
File metadata and controls
76 lines (55 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
title: "Residual Diagnostics"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Residual Diagnostics}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, echo=FALSE, message=FALSE}
library(olsrr)
library(ggplot2)
library(gridExtra)
library(nortest)
library(goftest)
```
# Introduction
olsrr offers tools for detecting violation of standard regression assumptions. Here we take a look
at residual diagnostics. The standard regression assumptions include the following about residuals/errors:
- The error has a normal distribution (normality assumption).
- The errors have mean zero.
- The errors have same but unknown variance (homoscedasticity assumption).
- The error are independent of each other (independent errors assumption).
## Residual QQ Plot
Graph for detecting violation of normality assumption.
```{r qqresid, fig.width=5, fig.height=5, fig.align='center'}
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_plot_resid_qq(model)
```
## Residual Normality Test
Test for detecting violation of normality assumption.
```{r normtest}
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_test_normality(model)
```
Correlation between observed residuals and expected residuals under normality.
```{r corrtest}
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_test_correlation(model)
```
## Residual vs Fitted Values Plot
It is a scatter plot of residuals on the y axis and fitted values on the x axis to detect non-linearity, unequal error variances, and outliers.
**Characteristics of a well behaved residual vs fitted plot:**
- The residuals spread randomly around the 0 line indicating that the relationship is linear.
- The residuals form an approximate horizontal band around the 0 line indicating homogeneity of error variance.
- No one residual is visibly away from the random pattern of the residuals indicating that there are no outliers.
```{r rvsfplot, fig.width=5, fig.height=5, fig.align='center'}
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_plot_resid_fit(model)
```
## Residual Histogram
Histogram of residuals for detecting violation of normality assumption.
```{r residhist, fig.width=5, fig.height=5, fig.align='center'}
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_plot_resid_hist(model)
```