How do I get the columns that a statsmodels / patsy formula depends on?
问题 Suppose I have a pandas dataframe: df = pd.DataFrame({'x1': [0, 1, 2, 3, 4], 'x2': [10, 9, 8, 7, 6], 'x3': [.1, .1, .2, 4, 8], 'y': [17, 18, 19, 20, 21]}) Now I fit a statsmodels model using a formula (which uses patsy under the hood): import statsmodels.formula.api as smf fit = smf.ols(formula='y ~ x1:x2', data=df).fit() What I want is a list of the columns of df that fit depends on, so that I can use fit.predict() on another dataset. If I try list(fit.params.index) , for example, I get: [