---
title: "Introduction to olsrr"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to olsrr}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

## Introduction

The olsrr package provides following tools for teaching and learning OLS regression using R:

- comprehensive regression output
- residual diagnostics
- measures of influence
- heteroskedasticity tests
- collinearity diagnostics
- model fit assessment
- variable contribution assessment
- variable selection procedures

```{r, echo=FALSE, message=FALSE}
library(olsrr)
library(ggplot2)
library(gridExtra)
library(nortest)
library(goftest)
```

This document is a quickstart guide to the tools offered by **olsrr**. Other vignettes provide more details on specific topics:

- Residual Diagnostics: Includes plots to examine residuals to validate OLS assumptions

- Variable selection: Differnt variable selection procedures such as all possible regression, best subset regression, stepwise regression, stepwise forward regression and stepwise backward regression

- Heteroskedasticity: Tests for heteroskedasticity include bartlett test, breusch pagan test,
score test and f test

- Measures of influence: Includes 10 different plots to detect and identify influential observations

- Collinearity diagnostics: VIF, Tolerance and condition indices to detect collinearity and plots for assessing mode fit and contributions of variables


## Regression

```{r regress}
ols_regress(mpg ~ disp + hp + wt + qsec, data = mtcars)
```

In the presence of interaction terms in the model, the predictors are
scaled and centered before computing the standardized betas. `ols_regress()` will detect interaction terms automatically but in case you have created a new variable instead of using the inline function `*`, you can indicate the presence of interaction terms by setting `iterm` to `TRUE`.

## Residual vs Fitted Values Plot

Plot to detect non-linearity, unequal error variances, and 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)
```

## DFBETAs Panel

DFBETAs measure the difference in each parameter estimate with and without the influential observation.
`dfbetas_panel` creates plots to detect influential observations using DFBETAs.

```{r dfbpanel, fig.width=7, fig.height=7, fig.align='center'}
model <- lm(mpg ~ disp + hp + wt, data = mtcars)
ols_plot_dfbetas(model)
```

## Residual Fit Spread Plot

Plot to detect non-linearity, influential observations and outliers.

```{r rfsplot, fig.width=10, fig.height=5, fig.align='center'}
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_plot_resid_fit_spread(model)
```

## Breusch Pagan Test

Breusch Pagan test is used to test for herteroskedasticity (non-constant error variance). It tests whether the variance of the errors from a regression is dependent on the values of the independent variables. It is a $\chi^{2}$ test.

```{r bp1}
model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_test_breusch_pagan(model)
```

## Collinearity Diagnostics

```{r colldiag}
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_coll_diag(model)
```

## Stepwise Regression

Build regression model from a set of candidate predictor variables by entering and removing predictors based on 
p values, in a stepwise manner until there is no variable left to enter or remove any more.

### Variable Selection

```{r stepwise1}
# stepwise regression
model <- lm(y ~ ., data = surgical)
ols_step_both_p(model)
```

### Plot

```{r stepwise2, fig.width=10, fig.height=15, fig.align='center'}
model <- lm(y ~ ., data = surgical)
k <- ols_step_both_p(model)
plot(k)
```


## Stepwise AIC Backward Regression

Build regression model from a set of candidate predictor variables by removing predictors based on 
Akaike Information Criteria, in a stepwise manner until there is no variable left to remove any more.

### Variable Selection

```{r stepaicb1}
# stepwise aic backward regression
model <- lm(y ~ ., data = surgical)
k <- ols_step_backward_aic(model)
k
```

### Plot

```{r stepaicb2, fig.width=5, fig.height=5, fig.align='center'}
model <- lm(y ~ ., data = surgical)
k <- ols_step_backward_aic(model)
plot(k)
```
