Skip to content

multiindex with stats.api.ols: unexpected formatting #11390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DSLituiev opened this issue Oct 20, 2015 · 2 comments
Closed

multiindex with stats.api.ols: unexpected formatting #11390

DSLituiev opened this issue Oct 20, 2015 · 2 comments

Comments

@DSLituiev
Copy link

With MultiIndex, stats.api.ols returns prediction of a different shape than outcome variable:

"set up the example"
x1 = np.arange(0,4)
x2 = 2*np.random.rand(4)
y = x1+x2 +  .2*np.random.randn(4)

df = pd.DataFrame( np.c_[y,x1, x2] ,
             columns = ["y", "x1","x2"],
             index = pd.MultiIndex.from_tuples([("chr1", 1), ("chr1",2), ("chr2",2), ("chr2", 4)]))
df.index.names = ["chr", "pos"]
df["y"]

"set up the model"
from pandas.stats.api import ols
res = ols(y = df["y"] , x = df[["x1","x2"]])
unexpected_output = res.predict()
unexpected_output

following post-formating recovers expected shape:

cols = unexpected_output.columns
unexpected_output.reset_index()
expected_output = pd.melt(unexpected_output.reset_index(), 
                     id_vars= res.y.index.names[0]  \
                    ).dropna().set_index(res.y.index.names).loc[res.y.index.get_values()]
expected_output
@jreback
Copy link
Contributor

jreback commented Oct 20, 2015

pandas.stats.api is basically deprecated (and will be in the next version)

use statsmodels instead

@jreback
Copy link
Contributor

jreback commented Oct 20, 2015

see #6077

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants