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 Y and one independent variable X using a straight line:
β0 is the intercept – the value of Y when X=0.
β1 is the slope – the change in Y for a one-unit change in X.
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 Yi and the predicted value Y^i from the model:
The Residual Sum of Squares is the sum of all squared residuals:
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 Y = GRE score
Independent variable X = CGPA
The best fit line yielded coefficients: β1≈6.8, β0≈261. So the equation becomes:
For a CGPA of 8.32:
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:
This represents a hyperplane in p-dimensional space. Each coefficient βj represents the expected change in Y for a one-unit change in Xj, holding all other variables constant.
Example: Predicting Car Mileage (MPG)
Suppose the goal is to predict miles per gallon (MPG) using:
X1 = weight
X2 = horsepower
X3 = number of cylinders
X4 = number of gears
The model would be:
Model Evaluation Metrics
R-squared (R2)
R-squared measures the proportion of variance in the dependent variable that is explained by the independent variables in the model.
SSres = sum of squared residuals (unexplained variance)
SStot = total sum of squares (variance of Y around its mean)
R2 ranges from 0 to 1. A value close to 1 indicates that the model explains most of the variability in Y. However, R2 always increases when more predictors are added, even if they are irrelevant.
Adjusted R-squared
Adjusted R2 penalizes the inclusion of unnecessary predictors:
where n is the number of observations and p 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 Y:
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=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:
Linearity – The relationship between Y and each X is linear. Check with scatter plots or residual vs. fitted plots.
Independence of errors – Residuals should not be correlated with each other (e.g., no time series dependence).
Homoscedasticity – The variance of residuals is constant across all levels of fitted values. A funnel-shaped residual plot indicates heteroscedasticity.
Normality of errors – Residuals should be approximately normally distributed (especially important for confidence intervals and p-values).
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.
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 Y 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 Y belongs to class 1, given the independent variables:
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.5, predict class 1.
If P(Y=1)<0.5, predict class 0.
Cost Function: Log Loss
Logistic regression estimates coefficients by minimizing log loss (also called cross-entropy loss):
where pi is the predicted probability for observation i. 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+TN+FP+FNTP+TN
Precision: TP+FPTP – among predicted positives, how many are actually positive?
Recall (Sensitivity): 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 R2 or accuracy; use adjusted R2, 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.