绘制OLS回归参数

发布于 2025-01-20 09:34:18 字数 774 浏览 0 评论 0原文

我正在尝试使用 OLS 回归中的一些系数来构建条形图。我的模型名为 jun07_OLS。因此,我调用了 jun07_OLS.params 来获取:

Intercept    4.580174
jun06        0.616195
treat1      -0.520858
treat2       0.912693
treat3      -0.952914
dtype: float64

如何绘制在单个“jun07”列上分组的 treat1、treat2、treat3 的值?我想要一个列,因为我将在其他月份根据多个 OLS 回归结果构建此条形图。

这就是我到目前为止所得到的:

  def get_indicies(mylist):

    a_list = mylist.params
    indices_to_access = [2, 3, 4]

    a_series = pd.Series(a_list)
    accessed_series = a_series[indices_to_access]
    accessed_list = list(accessed_series)

    return accessed_list

jun_coefs = get_indicies(jun07_OLS)

ax = plt.subplot(111)
ax.bar('jun07', jun_coefs, width=0.2, color='b', align='center')
plt.show()

这正在生产一根酒吧。

I am trying to build a bar plot using a few coefficients from an OLS regression. My model is called jun07_OLS. So I have called jun07_OLS.params to get:

Intercept    4.580174
jun06        0.616195
treat1      -0.520858
treat2       0.912693
treat3      -0.952914
dtype: float64

How can I plot the values of treat1, treat2, treat3 grouped on a single 'jun07' column? I'd like a single column because I will be building this bar plot from multiple OLS regression results in other months.

This is what I have so far:

  def get_indicies(mylist):

    a_list = mylist.params
    indices_to_access = [2, 3, 4]

    a_series = pd.Series(a_list)
    accessed_series = a_series[indices_to_access]
    accessed_list = list(accessed_series)

    return accessed_list

jun_coefs = get_indicies(jun07_OLS)

ax = plt.subplot(111)
ax.bar('jun07', jun_coefs, width=0.2, color='b', align='center')
plt.show()

This is producing one bar.

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

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

发布评论

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

评论(2

难以启齿的温柔 2025-01-27 09:34:18

我不确定我是否误解了你,但似乎不需要你的第一个功能。请看下面:

import matplotlib.pyplot as plt;
import numpy as np
import matplotlib.pyplot as plt

yS = jun07_OLS.params[2:5] #This does what your first function does
objects = ('treat1', 'teart2', 'treat3')
y_pos = np.arange(len(objects))

plt.bar(y_pos, yS, align='center', alpha=0.5)
plt.xticks(y_pos, objects)

plt.show()

I am not sure if I am misunderstanding you, but it seems that your first function is not needed. Please see below:

import matplotlib.pyplot as plt;
import numpy as np
import matplotlib.pyplot as plt

yS = jun07_OLS.params[2:5] #This does what your first function does
objects = ('treat1', 'teart2', 'treat3')
y_pos = np.arange(len(objects))

plt.bar(y_pos, yS, align='center', alpha=0.5)
plt.xticks(y_pos, objects)

plt.show()
三生殊途 2025-01-27 09:34:18

您可以将每个 OLS 回归结果放入列表中(本例中为 regression_list),并使用 pd.Series 列表中的信息创建数据框。然后,为 x 轴设置正确的值 (set_index('date')) 并使用 df.plot 和参数 kind='酒吧'

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#setup
np.random.seed(2342)
regression_list=[]
for date in range(2,5):
    i = pd.Series(np.random.normal(size=(5))+3)
    i.index = ['Intercept',f'jun{date:02}','treat1','treat2','treat3']
    regression_list.append(i)

#code
d = {
'date' : [i.index[1] for i in regression_list],
'tread1' :  [i.loc['treat1'] for i in regression_list],
'tread2' :  [i.loc['treat2'] for i in regression_list],
'tread3' :  [i.loc['treat3'] for i in regression_list],
}
df = pd.DataFrame(d)
df = df.set_index('date')

df.plot(kind='bar', width=0.8)
plt.xticks(rotation=45)

grouped_bar_regression

You could place every OLS regression result you have in a list (regression_list in this example) and create a dataframe using the information inside the pd.Series list. Then, set the index with the correct value (set_index('date')) for the x-axis and use df.plot with the parameter kind='bar'.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#setup
np.random.seed(2342)
regression_list=[]
for date in range(2,5):
    i = pd.Series(np.random.normal(size=(5))+3)
    i.index = ['Intercept',f'jun{date:02}','treat1','treat2','treat3']
    regression_list.append(i)

#code
d = {
'date' : [i.index[1] for i in regression_list],
'tread1' :  [i.loc['treat1'] for i in regression_list],
'tread2' :  [i.loc['treat2'] for i in regression_list],
'tread3' :  [i.loc['treat3'] for i in regression_list],
}
df = pd.DataFrame(d)
df = df.set_index('date')

df.plot(kind='bar', width=0.8)
plt.xticks(rotation=45)

grouped_bar_regression

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