Trevor Seibert
Trevor Seibert

Reputation: 119

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

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

Upvotes: 0

Views: 361

Answers (1)

Always Right Never Left
Always Right Never Left

Reputation: 1481

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'])

Upvotes: 2

Related Questions