'Plotting OLS regression params

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.



Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 le_camerone
Solution 2 n1colas.m