Skip to content
Hands-on Mentor
ENTR
Back to blog

Linear regression explained: a simple example in Python

Simple linear regression fits a line to predict one variable from another. Learn to read the slope, R-squared and the limits of a prediction in Python.

4 min read

Simple linear regression draws one straight line through the points of two numeric variables and uses that line to predict one variable from the other. The line has two parts: a starting value (the intercept) and a slope. The slope tells you how much y changes, on average, when x goes up by one unit. R-squared shows how much of the variation in y the line explains.

Correlation answers "how strong is the link?" Regression goes one step further and answers "if I change x by this much, what happens to y?" with a concrete number. This post uses 181 days of marketing data from Bazaar, a fictional online store.

How does a line fit the data?

Bazaar's marketing team sees a strong link (r ≈ 0.80) between daily ad spend (in dollars) and visitors that day. The team wants a concrete figure: "If we spend $100 more, how many more visitors will we get?"

Regression picks the line that sits closest to the points. "Closest" means it squares the vertical distance from each point to the line (the residual), adds those squares up, and finds the line with the smallest total. This method is called least squares. You write the line as:

visitors = intercept + slope × ad_spend

How do you fit a regression in Python?

In the statsmodels library you build the model with a formula. The variable on the left of ~ is the one you want to predict (y). The one on the right explains it (x):

import pandas as pd
import statsmodels.formula.api as smf

df = pd.read_csv("marketing.csv")
model = smf.ols("visitors ~ ad_spend", data=df).fit()
print(model.params.round(4))
print(round(model.rsquared, 3))
OutputIntercept    1200.1417
ad_spend        1.6270
dtype: float64
0.635

How do you read the slope and R-squared?

  • Intercept, 1200: When ad spend is close to zero, the line shows about 1,200 visitors. You can think of these as visitors who arrive without ads. The data has very few days near zero spend, so treat this number as rough.
  • Slope, 1.627: Each extra $1 is linked to about 1.63 more visitors on average. To make the unit easier to picture, multiply by 100: each extra $100 is linked to about 163 more visitors.
  • R-squared, 0.635: About 63% of the variation in daily visitors is explained by ad spend. The other 37% comes from other factors (day of the week, campaigns, season) and chance.

The word "linked" is deliberate. Regression does not prove that spending causes visitors. It only turns "they move together" into a number. See Correlation vs causation for why that difference matters.

How do you make a prediction with the line?

You give the model new spend values and it returns the matching points on the line:

new = pd.DataFrame({"ad_spend": [50, 150, 350]})
print(model.predict(new).round(0))
Output0    1281.0
1    1444.0
2    1770.0
dtype: float64

At $150 of daily spend, the line expects about 1,444 visitors. This is an average. Individual days land above or below the line. The standard deviation of the residuals is about 126 visitors, so a real day that misses the prediction by 126 visitors would not be surprising.

Where does a prediction stop being reliable?

In this data, daily ad spend runs from $0.50 to $354. The line is trustworthy in that range. If you ask it about $1,000 of daily spend, you are predicting in a region you never observed. The effect of ads may level off after some point, and a straight line cannot see that. This is called extrapolation, and you should avoid it.

Common mistakes

  • Reading the slope without units: "The slope is 1.627" says little by itself. Say "each extra $100 is linked to about 163 more visitors."
  • Treating R-squared as accuracy: An R-squared of 0.635 does not mean 63% of predictions are right. It is the share of variation explained.
  • Claiming causation: A regression slope measures a link, not a cause.
  • Skipping the residuals: If the points follow a curve, the straight line is the wrong model. A residual plot shows what the model missed.
  • Predicting outside the data range: The line is reliable only where you have observed data.

What to read next

  • #statistics
  • #regression
  • #python
  • #statsmodels
All posts