National University of Singapore crest
NUSNational University
of Singapore
Department of Statistics
and Data Science
Faculty of Science

Tutorial 4 (Access Code: r@inBow246

Can Regression Recover the Truth?

DSA3361 · Inferential Data Analytics
AY 2026/27 Semester 1
Zhu Xuelin

Can we trust a fitted line?

data
xy
1x1y1
2x2y2
100x100y100
We fit a line
sm.OLS(y,X).fit()

Can we trust a fitted line?

Falling-ball data, measurement error, and fitted regression line A plot that first shows the distribution of observed falling times squared. The controls then reveal observed times, ideal heights, measurement error, and an animated fitted line.
Same relationship:
+
Different measurements

Slightly different each time.

A world where we know the truth

We generate 100 sample points from

  • True .
import numpy as np

np.random.seed(123)
x = np.random.normal(
    loc=0, scale=1, size=100
)
eps = np.random.normal(
    loc=0, scale=np.sqrt(0.25), size=100
)
y = -1 + 0.5 * x + eps
yAssign toxeps

Fit the model and see what we get

We generate 100 sample points from

  • True .

We now fit the model :

import statsmodels.api as sm
Click each line to see what happens
import matplotlib.pyplot as plt
plt.scatter(x, y, color="black", alpha=0.75, label="Observed data")y_pred = model.fittedvalues # Compute predicted yorder = np.argsort(x)x_sorted, y_pred_sorted = x[order], y_pred[order]plt.plot(x_sorted, y_pred_sorted, color="red", linestyle="--", label="Fitted line")
plt.plot(x_sorted, -1 + 0.5 * x_sorted, color="blue", label="Population line")
plt.xlabel("x")plt.ylabel("y")plt.legend()plt.show()

Fit the model and see what we get

We generate 100 sample points from

  • True .

We now fit the model :

import statsmodels.api as sm
Click each line to see what happens

What if we add a quadratic term?

We generate 100 sample points from

  • True .

We now fit the model :

We now fit the model :

import statsmodels.api as sm
Click each line to see what happens

What if we add a quadratic term?

We generate 100 sample points from

  • True .

We now fit the model : Use the same code as before to plot.

ŷ = −0.9925 + 0.4929x − 0.0134x²

Refit the quadratic model under different error variances Observed data generated from a linear relationship and a red quadratic curve refitted after each new draw of the errors. Observed data True relationship Quadratic fit