1. Home
  2. Explore
  3. Mathematics
  4. Regression Analysis Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Regression Analysis Overview

Regression Analysis

Introduction

Regression analysis is a statistical method used to understand the relationship between a dependent variable (target) and one or more independent variables (predictors or features). It helps create a predictive model that can estimate the value of the dependent variable based on the independent variables.

Regression analysis helps businesses understand relationships between data points and make better decisions, such as predicting sales or understanding inventory levels.

For example, consider a survey of college students where the goal is to find if there is a relationship between a student's CGPA and their GRE score. By plotting CGPA on the x-axis and GRE score on the y-axis, a scatter plot reveals a positive linear trend – as CGPA increases, GRE score tends to increase. This visual inspection suggests a relationship, but to predict a specific GRE score given a CGPA of 8.32, a formal regression model is needed.


Linear Regression

Simple Linear Regression

Simple linear regression models the relationship between one dependent variable YYY and one independent variable XXX using a straight line:

Y=β0+β1XY = \beta_0 + \beta_1 XY=β0​+β1​X
  • β0\beta_0β0​ is the intercept – the value of YYY when X=0X = 0X=0.

  • β1\beta_1β1​ is the slope – the change in YYY for a one-unit change in XXX.

The goal is to find the line that best fits the data points. This is done by minimizing the Residual Sum of Squares (RSS).

Residuals and RSS

A residual is the difference between the observed value YiY_iYi​ and the predicted value Y^i\hat{Y}_iY^i​ from the model:

ei=Yi−Y^ie_i = Y_i - \hat{Y}_iei​=Yi​−Y^i​

The Residual Sum of Squares is the sum of all squared residuals:

RSS=∑i=1n(Yi−Y^i)2\text{RSS} = \sum_{i=1}^{n} (Y_i - \hat{Y}_i)^2RSS=∑i=1n​(Yi​−Y^i​)2

The best fit line is the one that produces the smallest RSS. This is commonly achieved using a method called ordinary least squares (OLS).

Best fit line minimizes the sum of squared vertical distances between data points and the line.

Example: Predicting GRE Score from CGPA

Using the student survey data:

  • Dependent variable YYY = GRE score

  • Independent variable XXX = CGPA

The best fit line yielded coefficients: β1≈6.8\beta_1 \approx 6.8β1​≈6.8, β0≈261\beta_0 \approx 261β0​≈261. So the equation becomes:

GRE=6.8⋅CGPA+261\text{GRE} = 6.8 \cdot \text{CGPA} + 261GRE=6.8⋅CGPA+261

For a CGPA of 8.32:

GRE=6.8×8.32+261≈317\text{GRE} = 6.8 \times 8.32 + 261 \approx 317GRE=6.8×8.32+261≈317

This demonstrates how regression provides a prediction from a known independent variable.

Multiple Linear Regression

Multiple linear regression extends the model to include more than one independent variable. The general form is:

Y=β0+β1X1+β2X2+⋯+βpXpY = \beta_0 + \beta_1 X_1 + \beta_2 X_2 + \dots + \beta_p X_pY=β0​+β1​X1​+β2​X2​+⋯+βp​Xp​

This represents a hyperplane in ppp-dimensional space. Each coefficient βj\beta_jβj​ represents the expected change in YYY for a one-unit change in XjX_jXj​, holding all other variables constant.

Example: Predicting Car Mileage (MPG)

Suppose the goal is to predict miles per gallon (MPG) using:

  • X1X_1X1​ = weight

  • X2X_2X2​ = horsepower

  • X3X_3X3​ = number of cylinders

  • X4X_4X4​ = number of gears

The model would be:

MPG=β0+β1⋅weight+β2⋅horsepower+β3⋅cylinders+β4⋅gears\text{MPG} = \beta_0 + \beta_1 \cdot \text{weight} + \beta_2 \cdot \text{horsepower} + \beta_3 \cdot \text{cylinders} + \beta_4 \cdot \text{gears}MPG=β0​+β1​⋅weight+β2​⋅horsepower+β3​⋅cylinders+β4​⋅gears

Model Evaluation Metrics

R-squared (R2R^2R2)

R-squared measures the proportion of variance in the dependent variable that is explained by the independent variables in the model.

R2=1−SSresSStotR^2 = 1 - \frac{SS_{res}}{SS_{tot}}R2=1−SStot​SSres​​
  • SSresSS_{res}SSres​ = sum of squared residuals (unexplained variance)

  • SStotSS_{tot}SStot​ = total sum of squares (variance of YYY around its mean)

R2R^2R2 ranges from 0 to 1. A value close to 1 indicates that the model explains most of the variability in YYY. However, R2R^2R2 always increases when more predictors are added, even if they are irrelevant.

Adjusted R-squared

Adjusted R2R^2R2 penalizes the inclusion of unnecessary predictors:

Radj2=1−(1−R2)(n−1)n−p−1R^2_{adj} = 1 - \frac{(1-R^2)(n-1)}{n-p-1}Radj2​=1−n−p−1(1−R2)(n−1)​

where nnn is the number of observations and ppp is the number of predictors. It can decrease when a useless variable is added, making it a better metric for model comparison.

Root Mean Squared Error (RMSE)

RMSE gives the average prediction error in the same units as YYY:

RMSE=1n∑i=1n(Yi−Y^i)2\text{RMSE} = \sqrt{\frac{1}{n} \sum_{i=1}^{n} (Y_i - \hat{Y}_i)^2}RMSE=n1​∑i=1n​(Yi​−Y^i​)2​

Lower RMSE indicates a better fit.

F-statistic and p-values

  • The F-statistic tests whether the overall model is statistically significant (i.e., whether at least one predictor has a non-zero coefficient).

  • p-values for individual coefficients test the null hypothesis H0:βj=0H_0: \beta_j = 0H0​:βj​=0 (no linear relationship). A low p-value (typically < 0.05) suggests the predictor is statistically significant.

p-value indicates the probability of observing such an extreme coefficient if the true coefficient were zero. Low p-values suggest a real relationship.


Assumptions and Diagnostics of Linear Regression

For reliable inference and prediction, linear regression makes several key assumptions:

  1. Linearity – The relationship between YYY and each XXX is linear. Check with scatter plots or residual vs. fitted plots.

  2. Independence of errors – Residuals should not be correlated with each other (e.g., no time series dependence).

  3. Homoscedasticity – The variance of residuals is constant across all levels of fitted values. A funnel-shaped residual plot indicates heteroscedasticity.

  4. Normality of errors – Residuals should be approximately normally distributed (especially important for confidence intervals and p-values).

  5. No multicollinearity – Independent variables should not be highly correlated with each other. Multicollinearity inflates standard errors and makes coefficient estimates unstable. It can be detected using Variance Inflation Factor (VIF) or correlation matrices.

  6. No influential outliers – Outliers can disproportionately affect the regression line. Use studentized residuals or Cook's distance to identify them.

Violations of assumptions may require transformations (e.g., log transform), using robust standard errors, or switching to a different modeling approach.


Logistic Regression

Concept and Use Case

Logistic regression is used for binary classification problems, where the dependent variable YYY takes only two values (e.g., 0 and 1, representing default/no default, diabetic/non-diabetic). Despite its name, it is a classification algorithm.

It models the probability that YYY belongs to class 1, given the independent variables:

P(Y=1∣X)=11+e−(β0+β1X1+⋯+βpXp)P(Y=1 | X) = \frac{1}{1 + e^{-(\beta_0 + \beta_1 X_1 + \dots + \beta_p X_p)}}P(Y=1∣X)=1+e−(β0​+β1​X1​+⋯+βp​Xp​)1​

This is called the sigmoid function or logistic function. It maps any real-valued input to a value between 0 and 1.

The linear combination inside the exponent is the same as in linear regression, but the output is transformed into a probability. The decision boundary is typically set at 0.5:

  • If P(Y=1)≥0.5P(Y=1) \ge 0.5P(Y=1)≥0.5, predict class 1.

  • If P(Y=1)<0.5P(Y=1) < 0.5P(Y=1)<0.5, predict class 0.

Cost Function: Log Loss

Logistic regression estimates coefficients by minimizing log loss (also called cross-entropy loss):

Log Loss=−1n∑i=1n[yilog⁡(pi)+(1−yi)log⁡(1−pi)]\text{Log Loss} = -\frac{1}{n} \sum_{i=1}^{n} \big[ y_i \log(p_i) + (1-y_i) \log(1-p_i) \big]Log Loss=−n1​∑i=1n​[yi​log(pi​)+(1−yi​)log(1−pi​)]

where pip_ipi​ is the predicted probability for observation iii. This function heavily penalizes confident wrong predictions.

Evaluation: Confusion Matrix and Metrics

For classification, accuracy alone can be misleading, especially with imbalanced classes. A confusion matrix tabulates actual vs. predicted classes:


Predicted Positive

Predicted Negative

Actual Positive

True Positive (TP)

False Negative (FN)

Actual Negative

False Positive (FP)

True Negative (TN)

Key metrics derived from the confusion matrix:

  • Accuracy: TP+TNTP+TN+FP+FN\frac{TP + TN}{TP + TN + FP + FN}TP+TN+FP+FNTP+TN​

  • Precision: TPTP+FP\frac{TP}{TP + FP}TP+FPTP​ – among predicted positives, how many are actually positive?

  • Recall (Sensitivity): TPTP+FN\frac{TP}{TP + FN}TP+FNTP​ – among actual positives, how many are captured?

  • F1-score: harmonic mean of precision and recall.

For imbalanced datasets (e.g., few diabetic cases), recall for the minority class is often more important than overall accuracy.

Example: Pima Indians Diabetes Dataset

In this dataset, the goal is to predict whether a female patient has diabetes (1) or not (0) based on features like pregnancies, plasma glucose, blood pressure, etc. The data shows that many attributes overlap heavily between the two classes, and the minority class (diabetic) is underrepresented. A basic logistic regression model may achieve reasonable overall accuracy (around 77%) but low recall for the diabetic class (around 55%). This illustrates the importance of careful data handling and evaluation.

Handling Class Imbalance

  • Up-sampling the minority class (duplicating instances) or down-sampling the majority class.

  • Adjusting the decision threshold – lowering the probability cutoff for the minority class can improve recall at the cost of precision.

  • Using class weights in the algorithm to penalize misclassification of the minority class more heavily.

Advantages and Limitations

Advantages:

  • Simple to implement and interpret – coefficients can be interpreted as log-odds ratios.

  • Provides probability estimates, not just class labels.

  • Fast to train, even on large datasets.

  • Less prone to overfitting than complex models when data is clean.

Limitations:

  • Assumes a linear decision boundary – may underperform when classes are not linearly separable.

  • Sensitive to outliers and multicollinearity.

  • Can struggle with highly imbalanced datasets without proper handling.

  • Requires careful feature engineering for non-linear relationships.


Key Takeaways

  • Linear regression predicts a continuous target by fitting a line or hyperplane that minimizes RSS.

  • Logistic regression predicts a binary target by fitting a sigmoid curve that minimizes log loss.

  • Both are linear models that assume linear relationships between predictors and the (transformed) target.

  • Model evaluation must go beyond R2R^2R2 or accuracy; use adjusted R2R^2R2, RMSE, confusion matrices, and domain-specific metrics.

  • Assumptions such as linearity, independence, homoscedasticity, and no multicollinearity must be checked.

  • Data preparation – handling missing values, outliers, and class imbalance – often determines model success more than the algorithm choice.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent