EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial OLS Regression in R
Secondary Sidebar
R programming Tutorial
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

OLS Regression in R

By Priya PedamkarPriya Pedamkar

OLS Regression in R

Introduction to OLS Regression in R

OLS Regression in R is a standard regression algorithm that is based upon the ordinary least squares calculation method.OLS regression is useful to analyze the predictive value of one dependent variable Y by using one or more independent variables X. R language provides built-in functions to generate OLS regression models and check the model accuracy. the R function such as lm() is used to create the OLS regression model. In the event of the model generates a straight line equation it resembles linear regression. OLS Regression is a good fit Machine learning model for a numerical data set.

The bivariate regression takes the form of the below equation.

Equation:

y = mx + c

  • y = is a dependent variable
  • m = gradient(slope)
  • x = independent variable
  • c = intercept

The OLS linear aggression allows us to predict the value of the response variable by varying the predictor values when the slope and coefficients are the best fit. To calculate the slope and intercept coefficients in R, we use lm() function. We need to input five variables to calculate slope and coefficient intercepts and those are standard deviations of x and y, means of x and y, Pearson correlation coefficients between x and y variables.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)

The mathematical formulas for both slope and intercept are given below.

Mathematical Formula:

slope <- cor(x, y) * (sd(y) / sd(x))
intercept <- mean(y) - (slope * mean(x))

To determine the linearity between two numeric values, we use a scatter plot that is best suited for the purpose. A scatter plot is easy to help us find out the strength and direction of a relationship. To perform OLS regression in R we need data to be passed on to lm() and predict() base functions. We also use ggplot 2 and dplyr packages which need to be imported.

Implementation of OLS

Here are some of the OLS implementation steps that we need to follow:

Step 1: To implement OLS through lm() function, we need to import the library required to perform OLS regression.

Syntax:

library(catools)

Catools library contains basic utility to perform statistic functions.

Step 2: After importing the required libraries, We import the data that is required for us to perform linear regression on. Below is the syntax.

Syntax:

data = read.csv(“path/filename”)

We import the data using the above syntax and store it in the variable called data.

Step 3: Once the data is imported, we analyze the data through str() function which displays the structure of the data that was imported.

Syntax :

str(data)

Step 4: We have seen the structure of the data, we will output the partial data for us to have a clear idea on the data set.

Syntax:

head(data)

Step 5: To understand the statistical features like mean, median and also labeling the data is important. We can use the summary () function to see the labels and the complete summary of the data.

Syntax:

summary(data)

Step 6: Now, once we have performed all the above steps. We now try to build a linear model from the data. We start by generating random numbers for simulating and modeling data.

Syntax:

set.seed(x)

We use seed() to generate random numbers for simulation and modeling where x, can be any random number to generate values.

Step 7: The significant step before we model data is splitting the data into two, one being the training data and the other being test data. Training data is 75% and test data is 25 %, which constitutes 100% of our data. This step is called a data division.

Syntax:

data_split = sample.split(data, SplitRatio = 0.75)
training <- subset(data, data_split == TRUE)
test <-subset(data, data_split == FALSE)

Step 8: The last step is to implement a linear data model using the lm() function.

Syntax:

model <- lm(X1.1 ~ X0.00631 + X6.572 + X16.3 + X25, data = training)

Step 9: Lastly, we display the summary of the model through a summary function.

Syntax:

Summary (model)

Important Command Used in OLS Model

Here we will discuss about some important commands of OLS Regression in R given below:

1. Reading the Data

Below are commands required to read data.

  • read.csv: To read data from a csv file.
  • read.table: To read data from text files.

2. Commands to Display Data

Below are the commands required to display data.

  • Head(): Displays the first six rows of the data
  • Str(): Shows the information of variables and their data types.
  • Rename(): Rename existing variables through the function.
  • Names(): Shows names of the variables.
  • Attach(): Used to attach data which makes it easier to search for variables.

3. Display Statistical Data

Below are the commands required to display statistical data.

  • mean(): Calculates the mean of variable x.
  • median(): Computes the median of variable x.
  • sd(x): Computes the standard deviation of variable x.
  • cor(matrix): Computes the correlation of the matrix.

4. Graphical Commands

Below are the commands required to display graphical data.

  • Hist(): Creates a histogram for the variable x
  • Boxplot(x): Creates box plot for the variable x.
  • Plot(x): Creates the scatter plot for x.
  • Stem(x): Creates a stem plot for the variable x.

OLS Diagnostics in R

Here are some of the diagnostic of OLS in the R language as follows:

  • After the OLS model is built, we have to make sure post-estimation analysis is done to that built model.
  •  We use diagnostics to create different graphs from the data to check what kind of data it is and the force behind the data that keeps it moving.
  • Outliers are important in the data as it is treated as unusual observations.
  • The ability to change the slope of the regression line is called Leverage.
  • The impact of the data is the combination of leverage and outliers.

Recommended Articles

This is a guide to OLS Regression in R. Here we discuss the introduction and implementation steps of OLS regression in r along with its important commands. You may also look at the following articles to learn more-

  1. Regression Testing Tools
  2. Simple Linear Regression
  3. Reverse Engineering Tools
  4. Cloud Security Tools
  5. What is Regression? | Types
  6. Simple Linear Regression in R | Types of Correlation Analysis
  7. Complete Guide to Regression in Machine Learning
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

Already registered !

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more