Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change params function names in statsmodels

I wanted to see if there was a way to change the output names when using the ‘params’ for the intercept and independent variable. The goal is to put it into a data frame to use later. I know you can change the xnames when using
model.summary(yname="Status", xname=[‘Alpha’, ‘Beta’], title=’Regression’) but I only want the params not the whole summary.

Here is the output

Intercept    125.682063
SP50          -0.288299
dtype: float64 

Here is what I want to change it to

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Alpha        125.682063
Beta         -0.288299
dtype: float64

Here is the code

df = pd.read_excel("dataset\Special_Proj.xlsx") 
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%y')
tickers = ['FDX', 'BRK', 'MSFT', 'NVDA', 'INTC', 'AMD', 'JPM', 'T', 'AAPL', 'AMZN', 'GS']

def rolling_regression_stats():
    first52 = df[(df['Date'] <= '2000-12-22')]

    for t in tickers:
        model = smf.ols(f'{t} ~ SP50', data=first52).fit()
        coef_and_intercept = model.params
        print(coef_and_intercept,'\n\n')
        

rolling_regression_stats()

Overall, Here is what I’m trying to achieve.

Table of Alphas and Betas of Tickers

>Solution :

params accessor returns pandas.Series, so you can work with it like you usually work with series. Specifically in your code all you need to do is to work with this line:

coef_and_intercept = model.params.set_axis(['Alpha', 'Beta'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading