I've got some regressions results from running statsmodels.formula.api.ols
. Here's a toy example:
import pandas as pd
import numpy as np
import statsmodels.formula.api as smf
example_df = pd.DataFrame(np.random.randn(10, 3))
example_df.columns = ["a", "b", "c"]
fit = smf.ols('a ~ b', example_df).fit()
I'd like to apply the model to column c
, but a naive attempt to do so doesn't work:
fit.predict(example_df["c"])
Here's the exception I get:
PatsyError: Error evaluating factor: NameError: name 'b' is not defined
a ~ b
^
I can do something gross and create a new, temporary DataFrame
in which I rename the column of interest:
example_df2 = pd.DataFrame(example_df["c"])
example_df2.columns = ["b"]
fit.predict(example_df2)
Is there a cleaner way to do this? (short of switching to statsmodels.api
instead of statsmodels.formula.api
)