

{"id":4877,"date":"2018-01-15T08:55:28","date_gmt":"2018-01-15T08:55:28","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=4877"},"modified":"2021-08-25T17:25:42","modified_gmt":"2021-08-25T11:55:42","slug":"ols-regression-in-r","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/","title":{"rendered":"OLS Regression in R &#8211; 8 Simple Steps to Implement OLS Regression Model"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:2171,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Ordinary_least_squares&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250930222842\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Ordinary_least_squares&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-11 01:04:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-27 01:27:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-04 11:58:07&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-12 18:02:07&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-20 08:43:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-26 16:10:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-31 22:10:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-05 13:59:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-14 18:21:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-18 16:38:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-22 10:58:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-26 22:06:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-03 14:53:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-09 20:11:13&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-24 17:53:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-30 01:41:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-03 10:14:14&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-08 10:22:38&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-14 11:15:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-18 04:43:38&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-21 09:48:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-04 14:37:54&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-13 07:23:19&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-28 03:22:43&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-05-28 03:22:43&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:2172,&quot;href&quot;:&quot;https:\\\/\\\/drive.google.com\\\/file\\\/d\\\/19djlrmVysJjPh5Bi30BuTxkq7qXoyBPA\\\/view&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250504111801\\\/https:\\\/\\\/drive.google.com\\\/file\\\/d\\\/19djlrmVysJjPh5Bi30BuTxkq7qXoyBPA\\\/view&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-11 01:05:03&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-27 01:27:30&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-04 11:58:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-12 18:02:09&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-20 08:44:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-26 16:10:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-31 22:10:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-07 01:54:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 07:23:18&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-23 17:00:33&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-26 22:07:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-03 14:53:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-09 20:11:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-24 21:32:07&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-30 01:41:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-03 10:14:18&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-14 11:15:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-18 04:43:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-21 09:48:03&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-04 14:37:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-13 07:23:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-28 03:22:47&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-05-28 03:22:47&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p><em><strong>Struggling to implement OLS regression In R?\u00a0<\/strong><\/em><\/p>\n<p>Forget all your troubles, you have landed on the most relevant page. This article is a complete guide of Ordinary Least Square (OLS) Regression Modeling. It will make you an expert in executing commands and implementing OLS regression in R programming.<\/p>\n<h2>What is OLS Regression in R?<\/h2>\n<p><em>OLS Regression in R programming is a type of statistical technique, that is used for modeling. <\/em>It is also used for the analysis of linear relationships between a response variable.\u00a0If the relationship between the two variables is linear, a straight line can be drawn to model their relationship. This will also fit accurately to our dataset.<\/p>\n<p>The linear equation for a bivariate regression takes the following form:<br \/>\n<b><\/b><\/p>\n<p style=\"text-align: center\"><em><b>y = mx + c<\/b><\/em><\/p>\n<p><span style=\"font-weight: 400\">where, y = response(dependent) variable<\/span><\/p>\n<p><span style=\"font-weight: 400\">m = gradient(slope)<\/span><\/p>\n<p><span style=\"font-weight: 400\">x = predictor(independent) variable<\/span><\/p>\n<p><span style=\"font-weight: 400\">c = the intercept<\/span><\/p>\n<p><em><b>Wait! Have you checked &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/r-data-types\/\">R Data Types<\/a><\/b><\/em><\/p>\n<h2>OLS\u00a0in R &#8211; Linear Model Estimation using Ordinary Least Squares<\/h2>\n<h3>1. Keywords<\/h3>\n<p><span style=\"font-weight: 400\">Models, regression<\/span><\/p>\n<h3>2. Usage<\/h3>\n<p><span style=\"font-weight: 400\">ols(formula, data, weights, subset, na.action=na.delete,<\/span><\/p>\n<p><span style=\"font-weight: 400\">method=&#8221;qr&#8221;, model=FALSE,<\/span><\/p>\n<p><span style=\"font-weight: 400\">x=FALSE, y=FALSE, se.fit=FALSE, linear.predictors=TRUE,<\/span><\/p>\n<p><span style=\"font-weight: 400\">penalty=0, penalty.matrix, tol=1e-7, sigma,<\/span><\/p>\n<p><span style=\"font-weight: 400\">var.penalty=c(&#8216;simple&#8217;,&#8217;sandwich&#8217;), \u2026)<\/span><\/p>\n<h3>3. Arguments<\/h3>\n<p>These are the arguments used in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ordinary_least_squares\">OLS<\/a> in R programming:<\/p>\n<ul>\n<li><b>Formula &#8211; <\/b>An S formula object, for example:<\/li>\n<\/ul>\n<p>Y ~ rcs(x1,5)*lsp(x2,c(10,20))<br \/>\n<b><\/b><\/p>\n<ul>\n<li><b>Data &#8211;\u00a0<\/b>It is the name of an S data frame containing all needed variables.<\/li>\n<li><b>Weights &#8211;\u00a0<\/b>We use it in the fitting process.<\/li>\n<li><b>Subset &#8211; <\/b>It is an expression that defines a subset of the observations to use in the fit. The default is to use all observations.<\/li>\n<li><b>na.action &#8211;\u00a0<\/b>This specifies an S function to handle missing data.<\/li>\n<li><b>Method &#8211;\u00a0<\/b>This specifies a particular fitting method, or &#8220;model.frame&#8221;.<\/li>\n<li><b>Model &#8211;\u00a0<\/b>The default is FALSE. It is set to TRUE. This attribute returns the model frame in the form of an element that is able to fit the object.<\/li>\n<li><b>X &#8211;\u00a0<\/b>The default is FALSE. It is set to TRUE to return the expanded design matrix as element x of the returned fit object. First, set both x=TRUE, if you are going to use the residuals function.<\/li>\n<li><b>Y &#8211;\u00a0<\/b>The default is FALSE.\u00a0 It is set to TRUE to return the vector of response values as element y of the fit.<\/li>\n<li><b>Se.fit &#8211;\u00a0<\/b>The default is FALSE. It is set to TRUE that computes the estimated standard errors of the estimate of X\u03b2. And, also store them in element se.fit of the fit.<\/li>\n<li><b>Linear.predictors &#8211;\u00a0<\/b>It is set FALSE as default. It is used to cause predicted values not to be stored.<\/li>\n<li><b>Penalty penalty.matrix &#8211;\u00a0<\/b>see lrm<\/li>\n<li><b>Tol &#8211; <\/b>Tolerance for information matrix singularity.<\/li>\n<li><b>Sigma &#8211;\u00a0<\/b>If sigma is given, then we can use it as the actual root mean squared error parameter for the model. We also use sigma as an estimation from the data that consists of the usual formulas.<\/li>\n<li><b>Var.penalty &#8211;\u00a0<\/b>It is the type of variance-covariance matrix that is to be stored in the var component of the fit when penalization is used.<\/li>\n<li><b>p. &#8211; <\/b>We pass the arguments to lm.wfit or lm.fit.<\/li>\n<\/ul>\n<p><em><strong>Do you know &#8211;\u00a0<a href=\"https:\/\/data-flair.training\/blogs\/r-matrix-operations-applications\/\">How to Create &amp; Access R Matrix<\/a>?<\/strong><\/em><\/p>\n<h2>OLS Data Analysis: Descriptive Stats<\/h2>\n<p>Several built-in commands for describing data has been present in R.<\/p>\n<ul>\n<li>We use <em>list()<\/em> command to get the output of all elements of an object.<\/li>\n<li>The <em>summary()<\/em> command is used to describe all variables contained within a data frame.<\/li>\n<li>We use<em> summary()<\/em> command with individual variables.<\/li>\n<li>Simple plots can also provide familiarity with the data.<\/li>\n<li>The<em> hist()<\/em> command produces a histogram for any given data values.<\/li>\n<li>We use the<em> plot()<\/em> command which produces both univariate and bivariate plots for any given objects.<\/li>\n<\/ul>\n<h3>1. Other Useful Commands:<\/h3>\n<ul>\n<li>sum<\/li>\n<li>min<\/li>\n<li>max<\/li>\n<li>mean<\/li>\n<li>median<\/li>\n<li>var<\/li>\n<li>sd<\/li>\n<li>cor<\/li>\n<li>range<\/li>\n<li>Summary<\/li>\n<\/ul>\n<h2>OLS Regression Commands for\u00a0Data Analysis<\/h2>\n<p>These are useful OLS regression commands for data analysis:<\/p>\n<ul>\n<li><b>lm<\/b>\u00a0&#8211; Linear Model<\/li>\n<\/ul>\n<ul>\n<li><b>lme<\/b> &#8211; Mixed effects<\/li>\n<\/ul>\n<ul>\n<li><b>glm<\/b> &#8211; General lm<\/li>\n<\/ul>\n<ul>\n<li><b>Multinomial <\/b>&#8211; Multinomial Logit<\/li>\n<\/ul>\n<ul>\n<li><b>Optim<\/b> &#8211; General Optimizer<\/li>\n<\/ul>\n<p><em><strong>You must definitely check the <a href=\"https:\/\/data-flair.training\/blogs\/generalized-linear-models-in-r\/\">Generalized Linear Regression in R<\/a><\/strong><\/em><\/p>\n<h2>How to Implement OLS Regression in R<\/h2>\n<p>To implement OLS in R, we will use the <em>lm<\/em> command that performs linear modeling. The dataset that we will be using is the UCI Boston Housing Prices that are openly available.<\/p>\n<p><em>For the implementation of OLS regression in R, we use &#8211;\u00a0<strong><a href=\"https:\/\/drive.google.com\/file\/d\/19djlrmVysJjPh5Bi30BuTxkq7qXoyBPA\/view\">Data (CSV)<\/a><\/strong><\/em><\/p>\n<p>So, let\u2019s start with the steps with our first R linear regression model.<\/p>\n<p><strong>Step 1:\u00a0<\/strong>\u00a0First, we import the important library that we will be using in our code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; library(caTools)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/library-ca-Tools.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63936\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/library-ca-Tools.jpg\" alt=\"library ca Tools - OLS Regression in R\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/library-ca-Tools.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/library-ca-Tools-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/library-ca-Tools-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/library-ca-Tools-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/library-ca-Tools-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/library-ca-Tools-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Step 2:\u00a0<\/strong>Now, we read our data that is present in the .csv format (<em>CSV stands for Comma Separated Values<\/em>).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; data = read.csv(\"\/home\/admin1\/Desktop\/Data\/hou_all.csv\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data-read.csv_.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63937\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data-read.csv_.jpg\" alt=\"data read.csv\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data-read.csv_.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data-read.csv_-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data-read.csv_-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data-read.csv_-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data-read.csv_-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data-read.csv_-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Step 3:\u00a0<\/strong>Now, we will display the compact structure of our data and its variables with the help of <em>str()<\/em> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; str(data)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/srt-data.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63938\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/srt-data.jpg\" alt=\"srt data - OLS Regression in R\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/srt-data.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/srt-data-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/srt-data-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/srt-data-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/srt-data-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/srt-data-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Step 4:\u00a0<\/strong>Then to get a brief idea about our data, we will output the first 6 data values using the <em>head()<\/em> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; head(data)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/head-data.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63939\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/head-data.jpg\" alt=\"head data - OLS Regression in R\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/head-data.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/head-data-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/head-data-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/head-data-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/head-data-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/head-data-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Step 5:\u00a0<\/strong>Now, in order to have an understanding of the various statistical features of our labels like mean, median, 1st Quartile value etc., we use the <em>summary()<\/em> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; summary(data)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-data.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63940\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-data.jpg\" alt=\"summary data\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-data.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-data-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-data-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-data-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-data-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-data-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Step 6:\u00a0<\/strong>Now, we will take our first step towards building our linear model. Firstly, we initiate the set.seed() function with the value of 125. In R,<em> set.seed()<\/em> allows you to randomly generate numbers for performing simulation and modeling.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; set.seed(125)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/set.seed_.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63941\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/set.seed_.jpg\" alt=\"set.seed\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/set.seed_.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/set.seed_-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/set.seed_-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/set.seed_-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/set.seed_-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/set.seed_-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Step 7:\u00a0<\/strong>The next important step is to divide our data into training data and test data. We set the percentage of data division to 75%, meaning that 75% of our data will be training data and the rest 25% will be the test data.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; data_split = sample.split(data, SplitRatio = 0.75)\r\n&gt; train &lt;- subset(data, data_split == TRUE)\r\n&gt; test &lt;-subset(data, data_split == FALSE)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data_split.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63942\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data_split.jpg\" alt=\"data_split - OLS Regression in R\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data_split.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data_split-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data_split-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data_split-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data_split-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/data_split-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Step 8:\u00a0<\/strong>Now that our data has been split into training and test set, we implement our linear modeling model as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">model &lt;- lm(X1.1 ~ X0.00632 + X6.575 + X15.3 + X24, data = train) #DataFlair<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/model-lm.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63943\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/model-lm.jpg\" alt=\"model lm\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/model-lm.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/model-lm-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/model-lm-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/model-lm-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/model-lm-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/model-lm-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>Lastly, we display the summary of our model using the same <em>summary()<\/em> function that we had implemented above.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; summary(model)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-model.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63944\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-model.jpg\" alt=\"summary model - OLS Regression in R\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-model.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-model-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-model-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-model-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-model-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/summary-model-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>And, that\u2019s it! You have implemented your first OLS regression model in R using linear modeling!<\/strong> <\/em><\/p>\n<h2>OLS Diagnostics in R<\/h2>\n<ul>\n<li>Post-estimation diagnostics are key to data analysis.<\/li>\n<\/ul>\n<ul>\n<li>We can use diagnostics which allows us the opportunity to show off some of the R\u2019s graphs. What else could be driving our driving our data?<\/li>\n<\/ul>\n<p><b>-Outlier<\/b>: Basically, it is an unusual observation.<br \/>\n<b><\/b><\/p>\n<p><b>-Leverage<\/b>: It has the ability to change the slope of the regression line.<br \/>\n<b><\/b><\/p>\n<p><b>-Influence<\/b>: The combined impact of strong leverage and outlier status.<\/p>\n<p><em><strong>It&#8217;s the right time to uncover the <\/strong><strong><a href=\"https:\/\/data-flair.training\/blogs\/logistic-regression-in-r\/\">Logistic Regression in R<\/a><\/strong><\/em><\/p>\n<h2>Summary<\/h2>\n<p>We have seen how OLS regression in R using ordinary least squares exist. Also, we have learned its usage as well as its command. Moreover, we have studied diagnostic in R which helps in showing graph. Now, you are an expert in OLS regression in R with knowledge of every command.<\/p>\n<p>If you have any suggestion or feedback, please comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Struggling to implement OLS regression In R?\u00a0 Forget all your troubles, you have landed on the most relevant page. This article is a complete guide of Ordinary Least Square (OLS) Regression Modeling. It will&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":63935,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[19694,9207,20646,19692],"class_list":["post-4877","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-create-ols-in-r","tag-ols-data-analysis-in-r","tag-ols-regression-in-r","tag-ols-regression-modeling"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>OLS Regression in R - 8 Simple Steps to Implement OLS Regression Model - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn to create OLS regression in R with examples, commands, keywords, arguments used in Ordinary Least Square regression modeling in R programming.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OLS Regression in R - 8 Simple Steps to Implement OLS Regression Model - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn to create OLS regression in R with examples, commands, keywords, arguments used in Ordinary Least Square regression modeling in R programming.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-15T08:55:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T11:55:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/OLS-Regression-in-R-01-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OLS Regression in R - 8 Simple Steps to Implement OLS Regression Model - DataFlair","description":"Learn to create OLS regression in R with examples, commands, keywords, arguments used in Ordinary Least Square regression modeling in R programming.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/","og_locale":"en_US","og_type":"article","og_title":"OLS Regression in R - 8 Simple Steps to Implement OLS Regression Model - DataFlair","og_description":"Learn to create OLS regression in R with examples, commands, keywords, arguments used in Ordinary Least Square regression modeling in R programming.","og_url":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-15T08:55:28+00:00","article_modified_time":"2021-08-25T11:55:42+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/OLS-Regression-in-R-01-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"OLS Regression in R &#8211; 8 Simple Steps to Implement OLS Regression Model","datePublished":"2018-01-15T08:55:28+00:00","dateModified":"2021-08-25T11:55:42+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/"},"wordCount":1154,"commentCount":7,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/OLS-Regression-in-R-01-1.jpg","keywords":["Create OLS in R","OLS data analysis in R","OLS Regression in R","OLS regression modeling"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/","url":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/","name":"OLS Regression in R - 8 Simple Steps to Implement OLS Regression Model - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/OLS-Regression-in-R-01-1.jpg","datePublished":"2018-01-15T08:55:28+00:00","dateModified":"2021-08-25T11:55:42+00:00","description":"Learn to create OLS regression in R with examples, commands, keywords, arguments used in Ordinary Least Square regression modeling in R programming.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/OLS-Regression-in-R-01-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/OLS-Regression-in-R-01-1.jpg","width":802,"height":420,"caption":"OLS-Regression-in-R"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/ols-regression-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"R Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/r\/"},{"@type":"ListItem","position":3,"name":"OLS Regression in R &#8211; 8 Simple Steps to Implement OLS Regression Model"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=4877"}],"version-history":[{"count":13,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4877\/revisions"}],"predecessor-version":[{"id":63945,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4877\/revisions\/63945"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/63935"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=4877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=4877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=4877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}