PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 7, 2022

[FIXED] How to change params function names in statsmodels

 October 07, 2022     pandas, python, scikit-learn, statistics, statsmodels     No comments   

Issue

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


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


Answered By - Always Right Never Left
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing