liveBook · Manning

Ordinary least squares

As explained in Effective Data Analysis

An ordinary least squares regression model estimates coefficients by minimizing the sum of squared differences between observed and predicted outcomes.

Ordinary least squares (OLS) estimates a linear outcome from one or more predictors by choosing coefficients that minimize squared prediction errors. Its fitted equation combines predictor values with coefficients and an intercept to produce estimated outcomes. section 9.1.2

Evaluate an OLS model through three complementary outputs:

  • Coefficients indicate the contribution of individual predictors. section 9.1.2
  • Evaluation metrics summarize overall predictive accuracy. section 9.1.2
  • Residuals show discrepancies between observed and predicted values and can be plotted against predictions or summarized as a distribution. section 9.1.2

Housing-price estimation

Use square footage and number of bedrooms as predictors of house price, add an intercept, fit OLS with statsmodels, and inspect the resulting summary. New predictor values can then generate an estimated price. section 9.1.2

import statsmodels.api as sm

import pandas as pd
housing_prices = pd.read_csv("housing_prices.csv")     #1

X = housing_prices[["sq_footage", "n_bedrooms"]]
y = housing_prices["price"]     #2
X = sm.add_constant(X)     #3

model = sm.OLS(y, X).fit()     #4
print(model.summary())

A compact Python workflow loads housing data, separates predictors and outcome, adds a constant, fits OLS, and prints diagnostic output. section 9.1.2

How Effective Data Analysis covers it

How deep it goes
Explains fitted equations, coefficient roles, predictive accuracy, residuals, and diagnostic plots without developing estimation internals.
Examples
Demonstrates housing-price prediction using square footage, bedrooms, fitted values, residual scatterplots, and residual histograms.
What you can do after
Apply it via statsmodels to fit a regression, generate predictions, inspect coefficients, and diagnose residual behavior.
How it connects
Connects linear regression with coefficients, predictors, predicted values, evaluation metrics, and residual analysis.

Also explained in

Compare how 4 books explain ordinary least squares →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Data Analysis. The section links point to the parts of the book it draws on.