如何更改statsmodels中的参数函数名称

发布于 2025-02-09 14:22:13 字数 1088 浏览 0 评论 0原文

我想看看在为截距和自变量使用“参数”时,是否有一种方法可以更改输出名称。目标是将其放入以后使用的数据框架中。我知道您可以在使用时更改XNAME model.summary(yname =“ status”,xname = ['alpha','beta'],title ='回归'),但我只希望参数不是整个摘要。

输出

Intercept    125.682063
SP50          -0.288299
dtype: float64 

这是我想将其更改为

Alpha        125.682063
Beta         -0.288299
dtype: float64

总体代码的

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()

,这是我要实现的目标。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月染过的梦 2025-02-16 14:22:13

params访问者返回pandas.Series,因此您可以像通常使用系列一样使用它。特别是在代码中,您需要做的就是使用此行:

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

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'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文