How to extend OLS trendline?

Hi everyone!

I need help extending my OLS line of best fit - not sure how to do it? Please see below for details : )

Here is my plot:
Screen Shot 2019-12-06 at 1.06.40 PM

Now, with this scenario my R2(squared) is very high and I find this line of best fit valid per say, therefore, when scrolling further on my x/y-axes ranges I would like to make this line extend let’s say for up to ‘Jan 2020’ x-tick; how would I do that? Also, because my x-axis is ‘Date’ - my slope turns out to be gibberish and I cannot use the equation to calculate further values into the future (snippet of slope below):
Screen Shot 2019-12-06 at 1.11.03 PM

Please let me know how to approach this problem, all help is appreciated!

Hey @dgomonov, welcome to the forum! The string formatting for the slope does not work well indeed in your case. I’ve opened an issue https://github.com/plotly/plotly.py/issues/1984, the fix is easy so it should be corrected in the next release of plotly.py. At the moment there is no way to extend the trendline, and workarounds would be based on parsing the hovertemplate of the figure (fig.data[1].hovertemplate), but for this you need it first to display correctly!

1 Like

Hi @Emmanuelle! Thank you for quick response!
Yes, I have tried looking into fig.data, but fig.data[1].hovertemplate is what I would need to get my slope; however that gives me the whole string, how would I get the equation from it…?
Now, if I read your last statement correctly since the slope is gibberish as of now - I’m waiting for next release, correct? Any chance you could write what should it normally look like, in other words what should be output instead of ‘DATE’ in the equation (I’m a bit new to this and not sure how Dates are translated into equations)?

Can you share here an example (using dummy data so that we can reproduce it) reproducing your usecase, so that we can make sure it works ok in the next release?

Sure! Also, if possible please consider adding option (argument) to extend the line - I believe it could be very useful for visualizing forecasting. Here is a dummy dataframe you guys can try this on (with gibberish slope; btw it seems to be happening only when using dtype):

	DATE	VALUE
0	2017-01-06	1.3
1	2017-01-13	2.0
2	2017-01-20	2.1
3	2017-01-27	2.0
4	2017-02-03	2.1
5	2017-02-10	1.3
6	2017-02-17	2.0
7	2017-02-24	2.1
8	2017-03-03	2.0
9	2017-03-10	2.1

Here is the code to run:

ex_fig=go.Figure()
ex_fig=px.scatter(dummy_df, x='DATE', y='VALUE', trendline="ols")
ex_fig.show()

When is the next release may I ask in this case?

Hi Emmanuelle! I believe you never answered when the new release is coming out… (I think I forgot to @ you) Is there any approximate date or exact date? I am asking in regards of that ‘slope equation’ fix - just wondering :slight_smile:

Hi @dgomonov! First of all I learned that you can retrieve the statsmodel model which is stored within the figure, with px.get_trendline_results (see https://plot.ly/python/linear-fits/). You would do

model = px.get_trendline_results(fig)
params = model.px_fit_results.iloc[0].params

and with this you can extend the line (and also modify the hovertemplate so that it displays correctly).

The next release changing the formatting will probably be in January, we first need to make a couple of checks to be sure that changing from %f to %g formatting does not break stuff.

Hey,
im trying to do as instructed in the https://plot.ly/python/linear-fits/#linear-fit-trendlines-with-plotly-express link but all i get is a new variable called results that containes: “<statsmodels.regression.linear_model.RegressionResultsWrapper object at 0x000001DC03A79488>”

im using Spyder.

am i missing something?