The olsrr package provides following tools for teaching and learning OLS regression using R:
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
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
.
Plot to detect non-linearity, unequal error variances, and outliers.
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ols_plot_resid_fit(model)
DFBETAs measure the difference in each parameter estimate with and without the influential observation.
dfbetas_panel
creates plots to detect influential observations using DFBETAs.
model <- lm(mpg ~ disp + hp + wt, data = mtcars) ols_plot_dfbetas(model)
Plot to detect non-linearity, influential observations and outliers.
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ols_plot_resid_fit_spread(model)
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.
model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars) ols_test_breusch_pagan(model)
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ols_coll_diag(model)
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.
# stepwise regression model <- lm(y ~ ., data = surgical) ols_step_both_p(model)
model <- lm(y ~ ., data = surgical) k <- ols_step_both_p(model) plot(k)
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.
# stepwise aic backward regression model <- lm(y ~ ., data = surgical) k <- ols_step_backward_aic(model) k
model <- lm(y ~ ., data = surgical) k <- ols_step_backward_aic(model) plot(k)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.