Skip to content

ENH stepwise regression - #4813

Closed
anishgoyal24 wants to merge 5 commits into
statsmodels:mainfrom
anishgoyal24:BackwardElimination
Closed

anishgoyal24 wants to merge 5 commits into
statsmodels:mainfrom
anishgoyal24:BackwardElimination

Conversation

@anishgoyal24

@anishgoyal24 anishgoyal24 commented Jul 26, 2018 •

Copy link
Copy Markdown

This python file provides the functionality to optimise the dataset for Multiple Regression Models using Backward Elimination. Columns are first dropped using the adjusted R-Squared values and then to further optimise the data, p-values of the columns are also checked.

Pass the features(X) followed by the labels(y) and the significance level you want to consider into the backward_eliminaton function. It will return the optimised dataset.

NOTE: No need to pass the dataset with the constant column already added. It will automatically add the column.

@coveralls

coveralls commented Jul 26, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) to 83.691% when pulling b8e0f90 on anishgoyal24:BackwardElimination into ed6631a on statsmodels:master.

@codecov-io

codecov-io commented Jul 26, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #4813 into master will decrease coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4813      +/-   ##
==========================================
- Coverage   81.14%   81.14%   -0.01%     
==========================================
  Files         581      581              
  Lines       89895    89901       +6     
  Branches    10041    10041              
==========================================
+ Hits        72944    72946       +2     
- Misses      14673    14675       +2     
- Partials     2278     2280       +2
Impacted Files Coverage Δ
statsmodels/tsa/statespace/sarimax.py 93.69% <0%> (-0.48%) ⬇️
statsmodels/stats/power.py 80.08% <0%> (ø) ⬆️
statsmodels/iolib/summary.py 54.16% <0%> (ø) ⬆️
statsmodels/discrete/tests/test_discrete.py 98.4% <0%> (ø) ⬆️
statsmodels/tsa/tests/test_arima.py 99.32% <0%> (ø) ⬆️
statsmodels/tsa/tests/test_stattools.py 97.36% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ed6631a...77b8373. Read the comment docs.

Comment thread BackwardElimination.py Outdated
bestModel = r_sq_elimination(bestModel, y)
return bestModel
else:
return bestModel

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case, bestModel needs to returned, could this be used without needing else check?

if foundNew == True:
        bestModel = r_sq_elimination(bestModel, y)

return bestModel

Comment thread BackwardElimination.py Outdated
for i in range(0, noCols):
ols_regeressor = sm.OLS(exog=x, endog=y).fit()
pValues = ols_regeressor.pvalues
print(pValues)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you intend to print these values? If this was meant for debugging, you can remove them?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Left them by mistake. Removed thanks!

Comment thread BackwardElimination.py
bestModel = pd.DataFrame(x)
foundNew = False
for i in range(1, noOfColumn):
temp = x.drop(x.columns[i], axis=1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could loop through column names like

for col in x.columns:
    temp = x.drop(col, axis=1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to have reverted back?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I ran and realised that something was going wrong when I changed it. And secondly I do not want to work on the 0th column. So this is saving me resources.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, which could be done with slicing btw.

for col in x.columns[1:]

Comment thread BackwardElimination.py
noCols = x.shape[1]
for i in range(0, noCols):
ols_regeressor = sm.OLS(exog=x, endog=y).fit()
pValues = ols_regeressor.pvalues

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUIC, you're doing the ols_regeressor = sm.OLS(exog=x, endog=y).fit() same check, any reason for looping it noCols times?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Actually I am dropping columns with pvalues > sl sequentially and not all at once.

@josef-pkt

Copy link
Copy Markdown
Member

LGTM pull request analysis was skipped for b44a141 by anishgoyal24. Analysis of future commits will happen as normal.


Comment posted by LGTM.com

Comment thread BackwardElimination.py
@@ -0,0 +1,46 @@
import pandas as pd

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename is not good. Should be backward_elimination.py to conform to pep.

@josef-pkt

Copy link
Copy Markdown
Member

LGTM pull request analysis was skipped for 77b8373 by anishgoyal24. Analysis of future commits will happen as normal.


Comment posted by LGTM.com

@josef-pkt

Copy link
Copy Markdown
Member

This needs some design discussion/decision.
E,g, how general do we want to make this both in terms of models and selection criteria.

The current module location is wrong, it is on the top level AFAICS. It needs to move "somewhere" depending on the models that can be used with it.

@josef-pkt josef-pkt changed the title Add files via upload ENH stepwise regression Jul 25, 2025
@anishgoyal24 anishgoyal24 closed this by deleting the head repository Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants