Convenient way to show calculation for Seaborn regression line

Viewed 190

I'm searching for a convenient way to show the calculation for the regression line in a Seaborn regplot. I am using Python in Jupyter Notebook.

After looking into some other stackoverflow posts, I understand it as that you can't directly show the fit_reg calculation used in the plot.

Instead I've found recommendations of using the from scipy import stats module. However this does not seem to work for me:

from scipy import stats

#Read frame and create array of scores
read_frame = pd.DataFrame([each])
dates = list(read_frame.keys())
stock = dates.pop(0)
stock = each[stock]
stock_score = []
for key in dates:
    score = read_frame[key].to_list()
    stock_score.extend(score)

# y = dates, x = stock_score
slope = stats.linregress(dates, stock_score)
print(slope)

#[EDIT] Added data used for calculation.
# dates: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
# stock_score: [0.06933703703703704, 0.07373142857142856, 0.12000285714285716, 0.1634861111111112, 0.1726382978723404, 0.548125, 0.04427257383966246, 0.08823866666666677, 0.039461915367483294, -0.03524617834394902, 0.033837903225806445, 0.023088636363636363, -0.25486666666666663, 0.07539999999999997, 0.08250487804878054]

The output I recieve, as well as a plot that shows my points of data.

# Output
LinregressResult(slope=nan, intercept=nan, rvalue=nan, pvalue=nan, stderr=nan)

Plotted regression line for example stock

What I would like to do is obtain the calculation for the regression line in a convenient way.

Below is the whole segment of code used (including the code to plot).

# Scatterplot with regressionline for each stock

import seaborn as sns
from scipy import stats

colors = ['black', 'red', 'yellow', 'blue', 'green', 'orange', 'purple', 'pink', 'brown', 'coral', 'teal', 'navy', 'grey', 'violet', 'maroon']
ctr = 0
for each in new_stock_scores:
    read_frame = pd.DataFrame([each])
    dates = list(read_frame.keys())
    stock = dates.pop(0)
    stock = each[stock]
    stock_score = []
    for key in dates:
        score = read_frame[key].to_list()
        stock_score.extend(score)
    
    #Regression line
    slope = stats.linregress(dates, stock_score)
    print(slope)

    #Regression plot
    sns.set(style="whitegrid")
    sns.set_context("paper")
    ax = sns.regplot(x = dates, y = stock_score, color = colors[ctr], ci = None)
    ax.set_title(stock, weight ='bold').set_fontsize('16')
    ax.set(xticks = dates)
    ax.set(xlabel = 'Date', ylabel = 'Score')
    plt.xlim(0,16)
    plt.show()
    ctr += 1
1 Answers

It seems I cannot reproduce your issue

import scipy.stats as sps

dates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
stock_score = [0.06933703703703704, 0.07373142857142856, 0.12000285714285716, 0.1634861111111112, 0.1726382978723404, 0.548125, 0.04427257383966246, 0.08823866666666677, 0.039461915367483294, -0.03524617834394902, 0.033837903225806445, 0.023088636363636363, -0.25486666666666663, 0.07539999999999997, 0.08250487804878054]

linreg = sps.linregress(dates, stock_score)

print(linreg)

print('slope', linreg.slope)

that gives

LinregressResult(slope=-0.014006090719584158, intercept=0.19498288977241957, rvalue=-0.38523447967018376, pvalue=0.1561897736602469, stderr=0.009305431260444107, intercept_stderr=0.08460610819916589)

slope -0.014006090719584158

You may try to check your scipy version

import scipy
print(scipy.__version__)

1.6.1
Related