I have the below code which goes through my dataframe (df_main) and runs one regression (applying
sm.OLS()) per each unique value of project_x. Thus, the number of regressions corresponds to the number of unique values in project_x.
Question: How can I store the estimated (exponentiated!) coefficients from all regressions that I ran in one single matrix with the below dimensions?
- Number of rows=number of unique
project_xvalues - Number of columns=number of regression coefficients estimated
The matrix would therefore have the unique project_x values as rows and the regression coefficients estimated as columns with the intersection of each being the corresponding estimated regression coefficient.
My code:
import statsmodels.api as sm
for x in df_main.project_x.unique():
df_holder=df_main[df_main.project_x == x]
X = df_holder.drop(['unneeded1', 'unneeded2','unneeded3'], axis=1)
X['constant']=1
Y = df_holder['sales']
eq=sm.OLS(y, X)
results=eq.fit()
exp_coefficients=np.exp(results.params)
print(exp_coefficients)