Get the intercept of standard error form the LinregressResult given by stats.linregres

Viewed 24

So when I run this line on Jupyter notebook.

stats.linregress(xdata, data)

The result is

LinregressResult(slope=8.762662815890456, intercept=-583.1060100267368, rvalue=0.9764595396878868, pvalue=0.0, stderr=0.0710610032681328, intercept_stderr=31.20585863555493)

Then if I run this code

slope, intercept, r_value, p_value, std_err,std_err_intercept = stats.linregress(xdata, data)

There is an error:

ValueError: not enough values to unpack (expected 6, got 5)

But if you see there are 6 values to unpack and I need this value for my calculations could someone help, please.

1 Answers

This is a quirk of the evolving API of functions in scipy.stats. Old versions of linregress returned a tuple with length 5; they did not include the intercept_stderr in the result. New versions return an object with 6 attributes. However, for backwards compatibility, if you unpack that object, it will act like a tuple of length 5.

The simplest way to use the code is to write result = linregress(xdata, ydata), and then refer to the attributes as result.slope, result.intercept, etc. Then the standard error of the intercept is available as result.intercept_stderr.

If, for some reason, you must unpack the result, you can still write

slope, intercept, r_value, p_value, std_err = stats.linregress(xdata, data)

but you lose the intercept_stderr part of the result.

This is explained in the Notes section of the docstring.

Related