Ordinary Least Squares (OLS) in R

OLS regression, or ordinary least squares regression, is a statistical method that is used to fit a linear relationship between a dependent variable and one or more independent variables. The OLS regression model minimizes the sum of the squared residuals, which are the differences between the observed values of the dependent variable and the predicted values from the model.

The OLS regression model is fitted using a method called least squares estimation. Least squares estimation is a method that finds the parameters of a model that minimize the sum of the squared residuals.

OLS regression can be used to solve a variety of problems. For example, it can be used to:

  1. Predict the value of a dependent variable based on the values of one or more independent variables. Determine the strength of the relationship between a dependent variable and one or more independent variables.
  2. Identify outliers in the data.
  3. Make inferences about the population.

OLS regression is a powerful tool that can be used to make predictions about the relationship between variables. However, it is important to note that OLS regression is only a model and it is not perfect. The results of OLS regression should always be interpreted with caution.

Ols Regression in R

OLS regression can be performed in R using the lm() function. The lm() function takes three arguments:

  1. formula: A formula that specifies the relationship between the dependent variable and the independent variables. The formula is in the following format:
y ~ x1 + x2 + x3 + ... + n

where y is the dependent variable and x1, x2, x3, ..., n are the independent variables.

  1. data: A data frame that contains the data for the dependent variable and the independent variables.
  2. subset: An optional argument that specifies the subset of data to use.

For example, the following code performs an OLS regression to model the relationship between the height and weight of a group of people:

height <- c(160, 170, 180, 190, 200) weight <- c(60, 70, 80, 90, 100) data <- data.frame(height, weight) model <- lm(weight ~ height, data)

The output of the lm() function is an object that contains the results of the OLS regression. You can use the summary() function to view the results of the OLS regression:

#Output: Call: lm(formula = weight ~ height, data = data) Residuals: Min 1Q Median 3Q Max -15.875 -3.445 -0.816 2.325 13.515 Coefficients: Estimate Std. Error t value Pr(>t) (Intercept) 70.450 10.309 6.826 1.05e-09 height 1.500 0.250 6.000 1.44e-07 Residual standard error: 5.81 on 4 degrees of freedom Multiple R-squared: 0.816, Adjusted R-squared: 0.780 F-statistic: 36.00 on 1 and 4 DF, p-value: 1.44e-07

The summary of the OLS regression shows that the coefficient of the independent variable, height, is 1.50. This means that for every 1 unit increase in height, there is a 1.50 unit increase in weight. The p-value for the coefficient of the independent variable is less than 0.05, which means that the coefficient is statistically significant. The R-squared value is 0.816, which means that 81.6% of the variation in weight is explained by the OLS regression model.


OLS regression is a fundamental statistical tool for understanding and modeling relationships between variables, making predictions, and assessing the impact of independent variables on the dependent variable. It serves as the basis for more complex regression techniques like multiple regression.