Does 'statsmodels' or another Python package offer an equivalent to R's 'step' function?

orome

Is there a statsmodels or other Python equivalent for R's step functionality for selecting a formula-based model using AIC?

Kostya

I really suspect that you are doing the same online course as I do -- the following allows you to get the right answers. If the task at hand is not very computationally heavy (and it isn't in the course), then we can sidestep all the smart details of the step function, and just try all the subsets of the predictors.

For each subset we can calculate AIC as ACI = 2*nvars - 2*result.llf.
And then we just find a subset with the minimal AIC:

import itertools
import numpy as np
import pandas as pd
import statsmodels.api as sm
AICs = {}
for k in range(1,len(predictorcols)+1):
    for variables in itertools.combinations(predictorcols, k):
        predictors = train[list(variables)]
        predictors['Intercept'] = 1
        res = sm.OLS(target, predictors).fit()
        AICs[variables] = 2*(k+1) - 2*res.llf
pd.Series(AICs).idxmin()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Equivalent of R's factor function in Pandas

From Dev

equivalent of R's View for Python's pandas

From Dev

Python equivalent of R's head and tail function

From Dev

What is python's equivalent of R's NA?

From Dev

Python equivalent of R "split"-function

From Dev

Is there a Python equivalent to MATLAB's pearsrnd function?

From Dev

Python's equivalent for R's dput() function

From Dev

Python equivalent for R's 'zoo' package

From Dev

Python numpy or pandas equivalent of the R function sweep()

From Dev

Is there an equivalent to Python's all function in JavaScript or jQuery?

From Dev

Python equivalent of daisy() in the cluster package of R

From Dev

Equivalent of R's createDataPartition in Python

From Dev

What is the Java equivalent to Python's reduce function?

From Dev

R equivalent to the Python function "dir"?

From Dev

Does Python have an equivalent of Perl's qq?

From Dev

Equivalent of Python's dir function in PHP

From Dev

Matlab equivalent of Python's 'reduce' function

From Dev

Does Rust have an equivalent to Python's unichr() function?

From Dev

Equivalent of R's removeSparseTerms in Python

From Dev

Is there a Python equivalent to R's sample() function?

From Dev

Is there an equivalent to Python's exec() function in Java?

From Dev

Pandas equivalent of Python's readlines function

From Dev

Is there a python ReportLab equivalent of TCPDF's 'annotate' function?

From Dev

Does python have an equivalent to Javascript's 'btoa'

From Dev

R: Is there an equivalent of Stata's ibn. function?

From Dev

Is there a C# equivalent to python's type() function?

From Dev

Python equivalent to Matlab's set function

From Dev

What is the Python Equivalent of Batch's PAUSE function

From Dev

Matlab equivalent of Python's 'reduce' function

Related Related

  1. 1

    Equivalent of R's factor function in Pandas

  2. 2

    equivalent of R's View for Python's pandas

  3. 3

    Python equivalent of R's head and tail function

  4. 4

    What is python's equivalent of R's NA?

  5. 5

    Python equivalent of R "split"-function

  6. 6

    Is there a Python equivalent to MATLAB's pearsrnd function?

  7. 7

    Python's equivalent for R's dput() function

  8. 8

    Python equivalent for R's 'zoo' package

  9. 9

    Python numpy or pandas equivalent of the R function sweep()

  10. 10

    Is there an equivalent to Python's all function in JavaScript or jQuery?

  11. 11

    Python equivalent of daisy() in the cluster package of R

  12. 12

    Equivalent of R's createDataPartition in Python

  13. 13

    What is the Java equivalent to Python's reduce function?

  14. 14

    R equivalent to the Python function "dir"?

  15. 15

    Does Python have an equivalent of Perl's qq?

  16. 16

    Equivalent of Python's dir function in PHP

  17. 17

    Matlab equivalent of Python's 'reduce' function

  18. 18

    Does Rust have an equivalent to Python's unichr() function?

  19. 19

    Equivalent of R's removeSparseTerms in Python

  20. 20

    Is there a Python equivalent to R's sample() function?

  21. 21

    Is there an equivalent to Python's exec() function in Java?

  22. 22

    Pandas equivalent of Python's readlines function

  23. 23

    Is there a python ReportLab equivalent of TCPDF's 'annotate' function?

  24. 24

    Does python have an equivalent to Javascript's 'btoa'

  25. 25

    R: Is there an equivalent of Stata's ibn. function?

  26. 26

    Is there a C# equivalent to python's type() function?

  27. 27

    Python equivalent to Matlab's set function

  28. 28

    What is the Python Equivalent of Batch's PAUSE function

  29. 29

    Matlab equivalent of Python's 'reduce' function

HotTag

Archive