Ordinary Least Squares (OLS) regression is one of the most widely used statistical methods for understanding the relationship between one or more independent variables and a dependent variable. It is employed in various fields including economics, biology, engineering, and social sciences to model and predict real-world systems. This article aims to provide an in-depth guide on how to perform OLS regression in R.
Table of Contents
- Introduction to OLS Regression
- Data Preparation
- Implementing OLS in R
- The
lm()
function - Model Diagnostics
- Visualizing the Model
- Predicting New Data Points
- The
- Advanced Topics
- Multiple Linear Regression
- Handling Categorical Variables
- Polynomial Regression
- Conclusion
1. Introduction to OLS Regression
Ordinary Least Squares (OLS) regression seeks to find the best-fitting line through a scatter plot of data points. In a simple linear regression model with one independent variable X and one dependent variable Y, the OLS method estimates the coefficients β0 and β1 such that the sum of the squared differences between observed values and predicted values is minimized. The equation for simple linear regression is:

Where:
- Y is the dependent variable
- X is the independent variable
- β0 is the y-intercept
- β1 is the slope of the line
- ϵ is the error term
2. Data Preparation
Before running any regression model, it is essential to understand the data you are working with. Often, data comes in a messy format and requires cleaning, transformation, or normalization. R’s tidyverse
package suite is an excellent tool for these tasks. To install the tidyverse
, run the following command in your R console:
install.packages("tidyverse")
3. Implementing OLS in R
3.1 The lm()
Function
R’s base package comes with a function, lm()
, which stands for “linear model.” This function is the workhorse for running not just simple but also multiple linear regressions.Here’s a basic example using R’s built-in mtcars
dataset:
# Load the dataset
data(mtcars)
# Run the OLS regression
model <- lm(mpg ~ wt, data = mtcars)
# Show the summary statistics
summary(model)
3.2 Model Diagnostics
After fitting a model, it’s crucial to evaluate its quality and assumptions. Several diagnostic plots are available in R to assist with this:
# Diagnostic plots
plot(model)
This will display a series of four plots that help assess the validity of the model’s assumptions.
3.3 Visualizing the Model
Visualization can help to understand the model’s behavior. You can plot the regression line along with the data points using ggplot2
, which is a part of the tidyverse
:
# Load ggplot2
library(ggplot2)
# Create the plot
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)

3.4 Predicting New Data Points
To make predictions using your model, you can use the predict()
function:
new_data <- data.frame(wt = c(3, 4))
predict(model, newdata = new_data)
4. Advanced Topics
4.1 Multiple Linear Regression
To include more than one predictor variable, simply add them to the formula in the lm()
function:
model_multiple <- lm(mpg ~ wt + hp + qsec, data = mtcars)
summary(model_multiple)
4.2 Handling Categorical Variables
If you have categorical predictors, you can include them by converting them into factors:
mtcars$am <- as.factor(mtcars$am)
model_cat <- lm(mpg ~ wt + am, data = mtcars)
summary(model_cat)
4.3 Polynomial Regression
To fit a polynomial regression model, you can include polynomial terms of the predictor variables:
model_poly <- lm(mpg ~ wt + I(wt^2), data = mtcars)
summary(model_poly)
5. Conclusion
This article covered how to perform OLS regression in R, starting from understanding the basic theory to executing code for model estimation, evaluation, and prediction. R’s rich ecosystem of statistical packages makes it a robust tool for regression analysis, providing not just the basic functionalities but also a wide array of diagnostic and visualization tools. By understanding how to implement OLS regression in R effectively, you open doors to more complex statistical modeling and data analysis tasks.