By-group regression in base python


#1

How does one run a by-group regression analysis in base python?
Fit a regression model of Y on Xi’s by a group variable?


#2

@nsa,

Please provide a description about your problem, this will help me to understand your problem.

Regards,
Imran


#3
def ols_res(df, xcols,  ycol):
    return sm.OLS(df[ycol], df[xcols]).fit().predict()

Then, you can use groupby and apply like this

df.grouby('grp').apply(ols_res, xcols=['x1', 'x2'], ycol='y')

Or

df.grouby('grp').apply(ols_res, ['x1', 'x2'], 'y')

from

http://stackoverflow.com/questions/24544805/python-pandas-how-to-run-multiple-univariate-regression-by-group