Model Selection

Two systematic methods for variable selection. Both use the same call pattern as ravix.ols() — pass a formula and DataFrame directly.

Stepwise — ravix.stepwise()

SW1 = ravix.stepwise("Price ~ .", df, direction = "both")
SW1.summary()
Summary of OLS Regression Analysis:
======================================================

Coefficients:
------------------------------------------------------
              Estimate  Std. Error  t-value    p-value
Intercept       15.568      3.8002    4.097   4.23e-05 ***
Accommodates    12.798     0.51440   24.880    < 2e-16 ***
Bathrooms       12.657      1.0325   12.259    < 2e-16 ***
HostListings   0.39391    0.024465   16.101    < 2e-16 ***
Deposit       0.060696   0.0052196   11.629    < 2e-16 ***
Bedrooms        8.5485     0.94133    9.081    < 2e-16 ***
MinNights       2.0375     0.35092    5.806   6.58e-09 ***
ExtraPeople   -0.17785    0.033987   -5.233   1.70e-07 ***
Beds            3.2672     0.66959    4.879   1.08e-06 ***
ResponseRate   -12.962      3.5793   -3.621     0.0003 ***
CleaningFee   0.078094    0.023349    3.345     0.0008 ***
FeeMissing      2.2486     0.93516    2.404     0.0162 *
MaxNights    -0.0014906   0.0007393   -2.016     0.0438 *

Model Statistics:
------------------------------------------------------
Residual Std. Error: 40.3219
R-squared:      0.4764          AIC: 106115.38
Adj. R-squared: 0.4758          BIC: 106209.59
F-statistic: 785.2638 on 12 and 10357 DF, p-value: < 2e-16
======================================================
SW2 = ravix.stepwise("Price ~ .", df, direction = "both", metric="bic", verbose = True)
Initial BIC = 112808.1938
Step 1: add Accommodates (BIC=107077.3266)
Step 2: add Bathrooms (BIC=106822.1724)
Step 3: add HostListings (BIC=106607.1197)
Step 4: add Deposit (BIC=106411.2468)
Step 5: add Bedrooms (BIC=106277.3118)
Step 6: add MinNights (BIC=106241.1347)
Step 7: add ExtraPeople (BIC=106219.6968)
Step 8: add Beds (BIC=106205.0822)
Step 9: add ResponseRate (BIC=106200.2787)
No improvement found. Stopping at step 10.
Final BIC = 106200.2787

Parameters

Parameter Default Description
method "ols" "ols", "logistic", or "poisson"
direction "backward" "both", "forward", or "backward"
metric "aic" "aic", "bic", "adjr2", or "pvalue"

Best subset — ravix.bsr()

bsr_model = ravix.bsr("Price ~ .", df)

Plot best subset results — ravix.plot_bsr()

ravix.plot_bsr(bsr_model)

Ravix best subset regression model selection plot

AIC vs BIC

Metric Penalty Best for
aic 2k Predictive accuracy
bic k · ln(n) Parsimony and interpretation

Textbook reference: Model selection is covered in Chapter 9 of Applied Linear Regression for Business Analytics with Python.

On this page