Data Mining

Chapter 7
Regression, Ensembles and Predictive Analytics

Bachelors Program in Computer Science Engineering
Data Mining   •   Bachelor

Regression, Ensembles and Predictive Analytics

Introduction

Predictive analytics uses historical data to estimate unknown, future, or otherwise unobserved outcomes. In data mining, prediction commonly involves either classification, where the outcome is categorical, or regression, where the outcome is numerical and continuous. Predicting electricity demand, house prices, delivery time, customer lifetime value, or loan loss are regression problems.

This chapter develops regression models from the linear model to regularized regression and then examines ensemble methods: bagging, random forests, boosting, and stacking. It also connects model outputs to operational decisions such as forecasting, risk assessment, resource allocation, and intervention planning. A central theme is that predictive performance depends not only on the algorithm but also on data quality, validation design, appropriate evaluation metrics, interpretability, and the cost of errors.

Learning Objectives

After completing this chapter, students should be able to:

  • formulate continuous-outcome prediction problems;
  • fit and interpret linear and polynomial regression models;
  • explain overfitting, underfitting, bias, variance, and regularization;
  • distinguish bagging, random forests, boosting, and stacking;
  • select suitable regression metrics and validation procedures;
  • apply predictive models to forecasting and risk-related decisions;
  • identify ethical, governance, and operational limitations of predictive analytics.

1. Regression as Supervised Learning

Let a dataset contain n observations. Observation i has a feature vector xᵢ = (xᵢ₁, …, xᵢₚ) and a numerical response yᵢ. A regression algorithm estimates a function f such that:

ŷᵢ = f(xᵢ)

Here, ŷᵢ is the predicted value and f is learned from training data. The prediction error for an observation is eᵢ = yᵢ − ŷᵢ. A useful conceptual model is:

y = f(x) + ε

where ε represents random variation or measurement noise. This formulation assumes that the relationship represented by f is systematic and that the remaining variation cannot be completely explained by the available features.

Regression differs from descriptive correlation. Correlation measures association between variables, whereas regression constructs a predictive function. A strong predictive relationship does not by itself establish causation. For example, the number of online searches for umbrellas may predict rainfall, but searches do not cause rain.

Common regression applications

DomainResponse variablePossible predictors
RetailDaily salesPrice, promotions, season, inventory
FinanceCredit lossIncome, debt, repayment history
ManufacturingProduct lifetimeTemperature, vibration, material quality
HealthcareLength of stayAge, diagnosis, procedure, comorbidities
TransportationTravel timeDistance, traffic, weather, time of day

The response must be defined precisely. “Customer value” might mean revenue over 12 months, gross margin, or discounted future profit; each definition produces a different learning problem.

2. Linear Regression

In multiple linear regression, the conditional mean of the response is modeled as a linear combination of p predictors:

ŷ = β₀ + β₁x₁ + β₂x₂ + … + βₚxₚ

β₀ is the intercept and βⱼ is the coefficient associated with predictor xⱼ. The model is linear in its parameters, although predictors may be transformed or expanded. For example, x² and log(x) may be included while the coefficients remain linear parameters.

Ordinary least squares (OLS) estimates coefficients by minimizing the sum of squared residuals:

RSS = ∑ᵢ₌₁ⁿ (yᵢ − ŷᵢ)²

The estimated coefficients are those that produce the smallest RSS on the training data. Squaring penalizes large errors more strongly than small errors and makes the objective mathematically convenient.

In matrix notation, with design matrix X, response vector y, and coefficient vector β, the OLS estimator is:

β̂ = (XᵀX)⁻¹Xᵀy

This expression applies when XᵀX is invertible, which requires no exact linear dependence among the modeled columns and sufficient observations. In practice, numerical methods such as QR decomposition are often preferred to explicitly computing an inverse.

Interpretation

If all other predictors are held constant, βⱼ estimates the expected change in y associated with a one-unit increase in xⱼ. This interpretation is conditional and depends on the model specification. It should not be interpreted causally unless the study design and assumptions support causal inference.

For a model predicting monthly energy consumption in kilowatt-hours (kWh), a coefficient of 120 for household size means that, conditional on the other included variables, one additional occupant is associated with an estimated 120 kWh increase per month. The coefficient’s unit is kWh per occupant.

Assumptions and diagnostics

Classical inference for OLS commonly relies on the following assumptions:

  1. Linearity in parameters: the conditional mean is correctly represented by the chosen features and transformations.
  2. Independent observations or appropriately modeled dependence: observations should not be treated as independent when they are repeated measurements or time series.
  3. Exogeneity: the errors have conditional mean zero, written E[ε | X] = 0.
  4. Constant variance for conventional standard errors: Var(ε | X) is constant. This is called homoscedasticity.
  5. No perfect multicollinearity: predictors are not exact linear combinations of one another.
  6. Approximately normal errors: useful mainly for small-sample confidence intervals and hypothesis tests, not required for prediction itself.

Residual plots can reveal curvature, unequal variance, outliers, and dependence. A model may have a high training fit while failing badly on new data. Therefore, diagnostics and validation are more informative than training fit alone.

3. Model Complexity and the Bias–Variance Trade-off

A very simple model may fail to represent important structure. This is underfitting, often associated with high bias. A highly flexible model may fit random fluctuations in the training sample. This is overfitting, often associated with high variance.

For squared-error prediction, expected test error can be conceptually decomposed as:

E[(Y − f̂(X))²] = Bias²[f̂(X)] + Variance[f̂(X)] + σ²

The left side is expected prediction error at a particular input X. Bias² measures systematic error from an overly restrictive model, variance measures sensitivity to the training sample, and σ² is irreducible noise under the data-generating model. This decomposition is explanatory rather than a direct calculation from one dataset.

Increasing model complexity generally reduces bias but increases variance. The practical objective is not to minimize training error; it is to minimize expected error on future cases. Cross-validation, regularization, feature selection, early stopping, and ensemble methods are tools for controlling this trade-off.

4. Regularization

Regularization modifies the fitting objective by adding a penalty for large coefficients. The penalty discourages unnecessary complexity and can improve generalization.

Ridge regression

Ridge regression minimizes:

∑ᵢ₌₁ⁿ (yᵢ − β₀ − ∑ⱼ₌₁ᵖ βⱼxᵢⱼ)² + λ∑ⱼ₌₁ᵖ βⱼ²

λ ≥ 0 controls the penalty. The intercept is usually not penalized. When λ = 0, ridge regression becomes OLS. As λ increases, coefficients shrink toward zero but generally do not become exactly zero.

Ridge is particularly useful when predictors are numerous or strongly correlated. Because the penalty depends on coefficient magnitude, predictors should generally be standardized before fitting; otherwise, variables measured on large numerical scales may be penalized differently.

Lasso regression

Lasso, or least absolute shrinkage and selection operator, uses an absolute-value penalty:

∑ᵢ₌₁ⁿ (yᵢ − β₀ − ∑ⱼ₌₁ᵖ βⱼxᵢⱼ)² + λ∑ⱼ₌₁ᵖ |βⱼ|

Lasso can set some coefficients exactly to zero, producing a sparse model and performing feature selection. With highly correlated predictors, it may select one variable unpredictably or distribute importance unevenly. Thus, zero coefficients should not automatically be interpreted as proof that a variable is irrelevant.

Elastic net

Elastic net combines ridge and lasso penalties:

RSS + λ[α∑ⱼ₌₁ᵖ |βⱼ| + (1 − α)∑ⱼ₌₁ᵖ βⱼ²]

where 0 ≤ α ≤ 1. α = 1 gives lasso, while α = 0 gives ridge. Elastic net is useful when feature selection is desired but correlated groups of predictors should be handled more stably.

The penalty parameter λ and, for elastic net, α must be selected using validation data. They must not be chosen by examining test-set performance.

5. Nonlinear Regression and Trees

Nonlinear patterns can be modeled by transformations, interactions, splines, or tree-based methods. A polynomial regression model may be written as:

y = β₀ + β₁x + β₂x² + ε

Although the curve is nonlinear in x, the model remains linear in β. High-degree polynomials can oscillate and overfit, so complexity should be controlled.

A regression tree recursively partitions the feature space. In each terminal region Rₘ, the prediction is commonly the mean response of training observations within that region:

ŷ(x) = cₘ, where x ∈ Rₘ and cₘ = (1/|Rₘ|)∑ᵢ:xᵢ∈Rₘ yᵢ

Tree splits are selected to reduce within-node squared error. Trees naturally represent interactions and nonlinear thresholds, require little feature transformation, and can handle mixed feature types. A single deep tree, however, is usually unstable and prone to overfitting.

6. Ensemble Learning

An ensemble combines several models to obtain a prediction that is often more accurate or stable than that of one model. Ensembles work especially well when component models make different errors.

Bagging

Bootstrap aggregating, or bagging, creates B bootstrap datasets by sampling training observations with replacement. A model is fitted to each sample, and regression predictions are averaged:

f̂bag(x) = (1/B)∑b=1B f̂b(x)

Averaging can reduce variance when component predictors are unstable and not perfectly correlated. Bagged trees are less sensitive to individual observations than a single tree. Observations not selected in a particular bootstrap sample are called out-of-bag observations and can provide an internal performance estimate.

Random forests

A random forest is a bagged collection of decision trees with additional randomization. At each split, a random subset of features is considered rather than all features. This reduces correlation among trees, which strengthens the variance-reduction effect of averaging.

Important hyperparameters include the number of trees, the number of candidate features at each split, minimum leaf size, and maximum tree depth. More trees generally stabilize predictions, although computational cost increases. Random forests can estimate variable importance, but importance measures may be biased toward high-cardinality or correlated features; permutation-based and grouped analyses are often preferable.

Boosting

Boosting builds models sequentially. Each new model focuses on errors made by the current ensemble. In squared-error regression, a simplified gradient-boosting procedure begins with a constant prediction, often the training mean. At iteration m, residual-like negative gradients are calculated, a weak learner hₘ is fitted to them, and the ensemble is updated:

Fₘ(x) = Fₘ₋₁(x) + η hₘ(x)

η is the learning rate, with 0 < η ≤ 1 in common implementations. The number and complexity of weak learners, learning rate, subsampling, and regularization control overfitting. Smaller learning rates often require more trees but can improve generalization.

Gradient boosting is powerful for structured tabular data and captures nonlinearities and interactions. It can be sensitive to noisy labels, leakage, poor hyperparameter choices, and distribution shift. Modern implementations include gradient-boosted decision-tree systems such as XGBoost, LightGBM, and CatBoost, but the underlying principles remain sequential error correction and controlled complexity.

Stacking

Stacking combines different model families through a meta-model. Base learners may include linear regression, a random forest, and a boosting model. Their predictions become inputs to a second-level learner. To avoid leakage, meta-model training should use out-of-fold predictions: each training observation receives a prediction from a base model that was not trained on that observation.

Stacking is useful when models capture complementary patterns. It increases system complexity, so the performance gain should be demonstrated using nested or carefully separated validation.

MethodMain mechanismMain strengthMain risk
BaggingBootstrap and averageVariance reductionLess effective for highly biased learners
Random forestBagging plus random feature subsetsRobust nonlinear modelingLess transparent; importance can mislead
BoostingSequential error correctionHigh predictive accuracySensitive to tuning and noise
StackingMeta-model combines learnersExploits complementary modelsLeakage and implementation complexity

7. Evaluation and Validation

A regression model should be evaluated on data representing its intended future use. Randomly splitting data is inappropriate when the task is temporal forecasting, because future information may enter training through random mixing.

Metrics

Mean absolute error (MAE) is:

MAE = (1/n)∑ᵢ₌₁ⁿ |yᵢ − ŷᵢ|

It has the same units as the response and treats errors linearly.

Mean squared error (MSE) is:

MSE = (1/n)∑ᵢ₌₁ⁿ (yᵢ − ŷᵢ)²

Root mean squared error (RMSE) is:

RMSE = √MSE

RMSE has the response’s units but penalizes large errors more strongly. R², when evaluated with an intercept-based benchmark, is commonly defined as:

R² = 1 − [∑ᵢ(yᵢ − ŷᵢ)² / ∑ᵢ(yᵢ − ȳ)²]

where ȳ is the mean of observed responses in the evaluation set. R² can be negative on test data when a model performs worse than the mean benchmark. It is not a percentage accuracy measure.

Percentage metrics require care when actual values are zero or near zero. MAPE, for example, is undefined when yᵢ = 0 and can become excessively large near zero. WAPE, scaled errors, or domain-specific loss functions may be preferable.

Validation design

  • Holdout validation: reserve a validation set for tuning and a final test set for one-time reporting.
  • k-fold cross-validation: divide training data into k folds, train on k − 1 folds, and validate on the remaining fold; repeat until each fold has served as validation.
  • Repeated cross-validation: repeats the process with different partitions to assess variability.
  • Time-series validation: use rolling or expanding training windows so that training precedes validation chronologically.
  • Nested cross-validation: uses inner folds for tuning and outer folds for unbiased performance estimation, particularly when data are limited and many hyperparameters are tested.

All preprocessing steps, including imputation, scaling, feature selection, and target encoding, must be fitted within each training fold. Otherwise, information from validation observations leaks into the model.

8. Predictive Analytics for Decisions

Prediction is not the final objective. A predictive system supports a decision. A forecast of demand may determine inventory; a predicted failure probability may trigger maintenance; a credit-risk estimate may influence approval limits.

A useful decision framework includes:

  1. Define the decision, prediction horizon, and unit of analysis.
  2. Specify the target and the time at which predictions will be made.
  3. Identify the costs of false positives, false negatives, overprediction, and underprediction.
  4. Construct features using only information available at prediction time.
  5. Validate using a deployment-realistic split.
  6. Select a threshold or action policy based on costs and capacity.
  7. Monitor accuracy, calibration, drift, fairness, and operational outcomes.

Suppose a model predicts demand d̂ and the organization chooses order quantity q. Underage cost cᵤ is the cost of ordering too little, and overage cost cₒ is the cost of ordering too much. A simple decision rule is to order near the demand quantile with service level:

p = cᵤ / (cᵤ + cₒ)

This rule assumes a known demand distribution, linear overage and underage costs, and no additional constraints such as capacity, perishability, or minimum order quantities. It illustrates why a point prediction alone may be insufficient: decisions depend on uncertainty and consequences.

For risk systems, calibration is important. A group of cases assigned predicted probability 0.20 should experience the event approximately 20% of the time if the model is calibrated for that population and horizon. Discrimination and calibration are different: a model may rank cases effectively while producing probabilities that are systematically too high or too low.

9. Illustrative Case: Predictive Maintenance in a Manufacturing Plant

Background

A factory operates 180 industrial pumps. Unexpected pump failures interrupt production and create emergency repair costs. Sensors record vibration, temperature, pressure, operating hours, and recent maintenance history every hour.

Problem

The plant wants to predict whether a pump will fail within the next seven days. Although the outcome is binary, the operational system also estimates expected repair cost and remaining useful life, making regression and predictive analytics relevant. The objective is not simply maximum accuracy; maintenance capacity allows technicians to inspect only 25 pumps per day.

Analysis and approach

Engineers first define a prediction timestamp and ensure that all features are available at that time. Records created after failure, such as emergency work-order codes, are excluded because they would cause leakage. Data are split chronologically: earlier months are used for training, a subsequent month for tuning, and the latest month for testing.

A regularized logistic model provides a transparent baseline for failure probability. A random forest captures nonlinear interactions such as high vibration combined with elevated temperature. A gradient-boosted model is tuned using rolling validation. Since failures are relatively rare, MAE and RMSE are not sufficient for the probability component; calibration, precision among the 25 daily inspections, recall, and the cost of missed failures are also examined.

The final policy ranks pumps by predicted risk and sends the highest-risk pumps for inspection, subject to technician capacity. A calibrated probability is combined with estimated downtime and repair cost to prioritize cases. Explanations show recent vibration trends, temperature excursions, and operating hours, but engineers are warned that feature importance is not causal proof.

Outcome

In an illustrative deployment, the ensemble identifies more impending failures within the inspection capacity than the baseline, while the regularized model remains useful for audit and communication. The plant monitors performance monthly because sensor replacement, production changes, and seasonal conditions can alter the data distribution.

Learning points

  • The target and prediction time must be defined before feature engineering.
  • A random split can produce unrealistic performance in temporal maintenance data.
  • Ranking, calibration, costs, and capacity all matter in operational deployment.
  • Ensembles may improve accuracy, but a transparent baseline supports governance.
  • Monitoring is necessary because predictive models can degrade after deployment.

10. Limitations, Ethics, and Governance

Regression and ensemble models inherit limitations from their data. Missing values, biased sampling, measurement changes, and historical discrimination can produce unreliable or unfair predictions. A model trained on one region, customer segment, or machine type may not generalize to another.

Important governance practices include data minimization, access control, documentation of feature definitions, reproducible training pipelines, versioning, audit logs, and clear ownership of decisions. Sensitive attributes may require special legal protection. Removing a sensitive feature does not guarantee fairness because proxy variables can encode similar information.

Fairness evaluation should be matched to the application. Relevant questions may include whether error rates differ across groups, whether calibration is adequate, and whether a model changes access to important resources. There is no universally best fairness criterion; some criteria conflict when base rates differ. Technical evaluation must therefore be combined with legal, social, and domain review.

Interpretability is also contextual. Coefficients are relatively direct to interpret in a well-specified linear model, whereas tree ensembles require tools such as permutation importance, partial dependence, or local explanations. These tools describe model behavior, not necessarily causal mechanisms, and can be unstable when predictors are correlated.

Contemporary systems increasingly use automated machine-learning pipelines, gradient boosting, probabilistic forecasting, and uncertainty estimation. Automation can accelerate experimentation but does not remove the need for sound target definitions, leakage controls, validation, and human accountability.

11. Best Practices

  1. Establish a simple baseline before using complex ensembles.
  2. Define the target, horizon, and decision context precisely.
  3. Use chronological validation for temporal problems.
  4. Fit preprocessing only on training data within each fold.
  5. Standardize predictors for penalized linear models when appropriate.
  6. Tune hyperparameters using validation data, not the final test set.
  7. Report several metrics and include uncertainty or variability estimates.
  8. Match the loss function to the consequences of errors.
  9. Inspect residuals, subgroup performance, calibration, and drift.
  10. Preserve interpretability and documentation when models affect people or essential services.

Chapter Summary

Regression estimates continuous outcomes from features and can be expressed as a systematic function plus unexplained noise. Ordinary least squares minimizes squared residuals, while ridge, lasso, and elastic net control complexity through coefficient penalties. The bias–variance trade-off explains why a model with excellent training performance may generalize poorly.

Bagging reduces variance by averaging models trained on bootstrap samples. Random forests add random feature selection to reduce correlation among trees. Boosting builds learners sequentially to correct errors, and stacking combines heterogeneous models through a meta-model trained without leakage. Model evaluation requires appropriate metrics, realistic validation, and careful preprocessing.

Predictive analytics connects predictions to decisions involving costs, uncertainty, capacity, and risk. Effective deployment requires monitoring, calibration, governance, fairness assessment, and accountability. No algorithm compensates for a poorly defined target, leaked information, biased data, or an operational process that ignores model limitations.

Key Terms

Regression: Prediction of a numerical response variable.

Response variable: The outcome a model attempts to predict.

Feature: An input variable used for prediction.

Residual: The difference between an observed and predicted value.

Ordinary least squares: Estimation by minimizing the sum of squared residuals.

Regularization: Penalization used to control model complexity.

Ridge regression: Regression using an L2, or squared-coefficient, penalty.

Lasso: Regression using an L1, or absolute-coefficient, penalty.

Elastic net: A model combining lasso and ridge penalties.

Overfitting: Fitting noise or idiosyncrasies that do not generalize.

Underfitting: Failure to capture important systematic structure.

Bias: Systematic prediction error caused by restrictive assumptions.

Variance: Sensitivity of a fitted model to the training sample.

Bagging: Bootstrap aggregation of multiple model predictions.

Random forest: Bagged randomized decision trees.

Boosting: Sequential construction of learners that correct previous errors.

Stacking: Combining base-model predictions with a meta-model.

Data leakage: Use of information unavailable at prediction time.

Calibration: Agreement between predicted probabilities and observed frequencies.

Concept drift: Change in the relationship between features and outcomes over time.

Review Questions

Short Answer Questions

  1. What is the difference between a regression problem and a classification problem?
  2. Why should predictors usually be standardized before ridge or lasso regression?
  3. What is the main purpose of bootstrap aggregation?
  4. Distinguish RMSE from MAE.
  5. Why is chronological validation important for forecasting?

Long Answer Questions

  1. Explain the bias–variance trade-off and discuss how regularization addresses it.
  2. Compare bagging, random forests, boosting, and stacking in terms of construction and typical strengths.
  3. State the main assumptions of ordinary least squares and describe useful diagnostic procedures.
  4. Explain how data leakage can occur during preprocessing and model validation.
  5. Discuss how predictive model outputs should be integrated into operational decisions.

Analytical / Application Questions

  1. A retailer predicts weekly demand using a random train–test split, although the model will forecast future weeks. Identify the methodological problem and design a better validation strategy.
  2. A lasso model removes several correlated features, while a ridge model retains all of them. Explain why this occurs and recommend how the analyst should interpret the results.
  3. A maintenance model has excellent ranking performance but systematically predicts probabilities twice as large as observed failure frequencies. Explain why this is a deployment problem and propose corrective actions.

Multiple Choice Questions with Answers

Question 1

Which objective does ordinary least squares minimize?

A. The sum of absolute residuals

B. The sum of squared residuals

C. The number of predictors

D. The variance of the response only

Correct Answer: B. The sum of squared residuals

Question 2

What is a principal effect of increasing the ridge penalty parameter λ?

A. Coefficients are generally shrunk toward zero

B. All coefficients become exactly zero immediately

C. The training sample becomes larger

D. The response variable becomes categorical

Correct Answer: A. Coefficients are generally shrunk toward zero

Question 3

Which method can set some regression coefficients exactly to zero?

A. Ridge regression

B. Lasso regression

C. Ordinary least squares only

D. Bagging only

Correct Answer: B. Lasso regression

Question 4

Why does a random forest usually consider only a random subset of features at each split?

A. To increase correlation among trees

B. To reduce diversity among trees

C. To reduce correlation among trees and improve averaging

D. To eliminate the need for validation

Correct Answer: C. To reduce correlation among trees and improve averaging

Question 5

How does boosting generally construct an ensemble?

A. By fitting all models independently and selecting the first one

B. By sequentially fitting learners that address previous errors

C. By removing all weak learners

D. By averaging only the response variable

Correct Answer: B. By sequentially fitting learners that address previous errors

Question 6

Which metric has the same units as the response and does not square errors?

A. MAE

B. MSE

C. R²

D. The ridge penalty

Correct Answer: A. MAE

Question 7

What is data leakage?

A. Random variation in the response

B. Using information that would not be available when making the prediction

C. Using too few trees in a forest

D. Standardizing a feature

Correct Answer: B. Using information that would not be available when making the prediction

Question 8

For a time-series forecasting task, which validation approach is most appropriate?

A. Randomly mixing past and future observations

B. Training on future observations and testing on past observations

C. Using rolling or expanding chronological windows

D. Evaluating only on the training data

Correct Answer: C. Using rolling or expanding chronological windows

Question 9

What does calibration of predicted probabilities assess?

A. Whether numerical features have equal units

B. Whether predicted probabilities agree with observed frequencies

C. Whether every coefficient is positive

D. Whether the model has no residuals

Correct Answer: B. Whether predicted probabilities agree with observed frequencies

Question 10

In stacking, why are out-of-fold predictions used to train the meta-model?

A. To prevent the meta-model from receiving overly optimistic predictions from models trained on the same observations

B. To guarantee that every base model is linear

C. To remove the need for a test set

D. To convert regression into clustering

Correct Answer: A. To prevent the meta-model from receiving overly optimistic predictions from models trained on the same observations

Data Mining
Chapter 7 of 15