ANALYSIS TOOL BOX

Hypothesis Testing

ConductLinearRegressionAnalysis

Run OLS linear regression with full diagnostics and interpretable output.

regressionolsstatisticsdiagnosticsmodeling

Introduction

How much does each predictor actually move a continuous outcome, and is that relationship strong enough to trust? ConductLinearRegressionAnalysis fits an Ordinary Least Squares model and hands back both the fitted statsmodels object and a readable summary of coefficients, significance, and fit statistics — with optional stepwise variable selection (forward, backward, or mixed) to narrow down a large predictor list, and diagnostic plots per predictor to check whether the linear assumption actually holds.

Teaching Note

Linear regression analysis is essential for:

  • Estimating the impact of independent variables on a continuous outcome
  • Predictive modeling for numerical values and trend forecasting
  • Testing scientific and economic hypotheses regarding variable associations
  • Analyzing the drivers of business metrics like revenue or customer satisfaction
  • Social science research on the effects of demographic or environmental factors
  • Identifying key features and their relative importance in a dataset
  • Validating the assumptions of linear relationships between variables

Parameters

ParameterTypeDefaultDescription
dataframerequiredThe pandas DataFrame containing the variables for the regression analysis.
outcome_variablerequiredName of the column in the DataFrame representing the dependent (outcome) variable.
list_of_predictorsrequiredA list of column names for the independent (predictor) variables to include in the model.
add_constantFalseIf True, adds an intercept (constant term) to the model. Defaults to False.
scale_predictorsFalseIf True, scales the predictor variables to have a mean of 0 and standard deviation of 1 using StandardScaler before fitting the model. Defaults to False.
variable_selection'none'Stepwise variable selection method to apply before fitting the final model. 'none' uses all predictors. 'forward' starts from the null model and greedily adds the predictor that most lowers AIC. 'backward' starts with all predictors and iteratively removes the one with the highest p-value until all remaining p-values fall below selection_p_threshold. 'mixed' combines forward addition by AIC with backward removal by p-value after each step. Defaults to 'none'.
selection_limit4Hard cap on the number of predictors kept by 'forward' or 'mixed' selection. Defaults to 4.
selection_p_threshold0.05P-value threshold used by 'backward' and 'mixed' selection. Predictors with p-value at or above this threshold are candidates for removal. Defaults to 0.05.
show_diagnostic_plots_for_each_predictorFalseIf True, displays diagnostic regression plots (e.g., partial regression plots) for each predictor in the model. Defaults to False.
show_helpTrueIf True, prints a quick guide to the console explaining how to access and interpret the output dictionary.

Returns

A dictionary with 'Fitted Model' (the statsmodels OLS results object — call .summary(), .params, .pvalues on it) and 'Model Summary' (a comprehensive statistical summary of the regression).

Example

python
from analysistoolbox.hypothesis_testing import ConductLinearRegressionAnalysis
import pandas as pd

# Analyze the impact of experience and education on salary
df = pd.DataFrame({
    'salary': [50000, 60000, 55000, 80000, 75000] * 10,
    'experience_years': [1, 5, 3, 10, 8] * 10,
    'education_level': [12, 16, 14, 18, 16] * 10
})
results = ConductLinearRegressionAnalysis(
    dataframe=df,
    outcome_variable='salary',
    list_of_predictors=['experience_years', 'education_level'],
    add_constant=True
)
print(results['Model Summary'])