Pros and Cons of Linear Regression
Linear regression offers simplicity, high interpretability via direct coefficient weights, low variance, and an exact closed-form Ordinary Least Squares solution. Its primary drawbacks are the assumption of a linear relationship in features, strong sensitivity to outliers under squared error loss, coefficient instability under multicollinearity, and unreliable extrapolation beyond the training range.
Study Card: Pros and Cons of Linear Regression
Direct Answer
Linear regression fits a linear function by minimizing the sum of squared residuals, admitting a closed-form Ordinary Least Squares (OLS) solution when is invertible:
Pros:
- Simplicity & Speed: Fast to fit ( exact solution or per gradient descent step) with low variance.
- Interpretability: Each coefficient quantifies the marginal effect of feature on the target holding other features constant.
- Baseline & Statistical Inference: Supports hypothesis tests and confidence intervals under classical regression assumptions.
Cons:
- Linearity Assumption: Underfits complex non-linear patterns and feature interactions unless explicit basis/polynomial features are engineered.
- Outlier Sensitivity: Squared error penalizes large residuals quadratically, allowing extreme outliers to pull the fitted line strongly.
- Multicollinearity: Highly correlated features make ill-conditioned or singular, inflating coefficient variance and flipping signs.
- Poor Extrapolation: Linear trends can produce implausible predictions far outside the observed feature domain.
Key Terms
- Linearity: Assumption that the expected target is linear in the model parameters (i.e., ).
- Independent Variables (Features): Input predictors used to explain or forecast the target.
- Dependent Variable (Target): Continuous outcome variable being predicted.
- Coefficients (): Weights multiplying each feature, representing its marginal rate of change on .
- Intercept (): Expected output when all feature values equal zero.
Example
Consider predicting house prices from square footage. When price scales roughly linearly with size inside a single neighborhood, linear regression provides a transparent baseline where directly measures price per square foot. However, if neighborhood effects, property age curves (e.g., historic vs. brand-new), and non-linear luxury interactions strongly influence price, plain linear regression underfits without feature engineering or non-linear models, and a single ultra-luxury mansion outlier can skew the slope for typical homes.
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# Generate sample data with a linear relationship + noise
np.random.seed(0)
X = np.random.rand(100, 1) * 10
y = 2 * X + 1 + np.random.randn(100, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Fit linear regression
model = LinearRegression()
model.fit(X_train, y_train)
# Predict and evaluate on test set
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse:.4f}")
print(f"Coefficients: {model.coef_}")
print(f"Intercept: {model.intercept_}")
# Sort test points before plotting the regression line cleanly
order = np.argsort(X_test[:, 0])
plt.scatter(X, y, label="Data")
plt.plot(X_test[order], y_pred[order], color='red', label="Regression Line")
plt.xlabel("House Size")
plt.ylabel("House Price")
plt.title("Linear Regression Example")
plt.legend()
plt.show()
Related Concepts
- Polynomial & Basis Regression: Adds non-linear feature transformations (polynomials, splines) while keeping the model linear in ; high-degree polynomials risk severe overfitting and Runge oscillations.
- Regularization (L1, L2): Ridge () stabilizes ill-conditioned matrices via ; Lasso () performs feature selection.
- Robust Regression: Huber loss or RANSAC mitigates sensitivity to outliers compared to OLS squared error.
- Gauss-Markov Assumptions: Under linearity, zero conditional error mean, independence, and homoscedasticity (constant error variance), OLS is the Best Linear Unbiased Estimator (BLUE) without requiring Gaussian errors; error normality is additionally assumed for exact finite-sample -tests, confidence intervals, and MLE equivalence.
- Multicollinearity Diagnostics: Variance Inflation Factor (VIF) and condition numbers detect correlated predictors, addressable via feature removal, PCA, or Ridge regularization.