OLS Regression with Python

Scikit-learn

Scikit-learn can be used to run a normal regression, however, its focus as a library is on machine learning and prediction, not statistical inference, so it doesnt provide a full set of regression statistics. See statsmodel implementation below.

from sklearn.linear_model import LinearRegression ols = LinearRegression() X = [[1,1],[1,3]] y = [[1],[2]] # X and y are numpy matrices ols.fit(X, y) intercept = ols.intercept_ coefficients = ols.coef_

Stats Models

The statsmodels library provides an implementation of the various regression statistics. The following regress function can be used to regress a dataset consisting of a list of dictionaries.

import pandas as pd import statsmodels.api as sm def regress(data_list, x_cols, y_col): """ Fits a statsmodels OLS model on a list of dictionaries. Parameters: - data_list (list): A list of dictionaries containing the dataset. - x_cols (list): List of strings matching the independent variable keys. - y_col (str): String matching the dependent variable key. Returns: - dict: A dictionary containing mapped coefficients, p-values, R², Adj R², and F-statistic. """ # 1. Convert the list of dicts to a pandas DataFrame df = pd.DataFrame(data_list) # 2. Separate X and y features X = df[x_cols] y = df[y_col] # 3. Add a constant column to account for the intercept X_with_constant = sm.add_constant(X) # 4. Fit the OLS model model = sm.OLS(y, X_with_constant).fit() # 5. Extract and package all target metrics metrics = { "coefficients": model.params.to_dict(), "p_values": model.pvalues.to_dict(), "r_squared": float(model.rsquared), "adj_r_squared": float(model.rsquared_adj), "f_stat": float(model.fvalue) } return metrics if __name__ == '__main__': data = [{'factor1':1, 'factor2':1, 'y':1},{'factor1':1, 'factor2':3, 'y':2},] test = regress(data, ['factor1','factor2'], 'y') pass