# Residual Diagnostics" In olsrr: Tools for Building OLS Regression Models

```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.

```model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_plot_resid_qq(model)
```

## Residual Normality Test

Test for detecting violation of normality assumption.

```model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_test_normality(model)
```

Correlation between observed residuals and expected residuals under normality.

```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.
```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.

```model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_plot_resid_hist(model)
```

## Try the olsrr package in your browser

Any scripts or data that you put into this service are public.

olsrr documentation built on Feb. 10, 2020, 5:07 p.m.