Skip to content Skip to sidebar Skip to footer

How To Find Out The Slope Value By Applying Linear Regression On Trend Of A Data?

I have a time series data from which I am able to find out the trend.Now I need to put a regression line which fits the best for the trend data and would like the know whether the

Solution 1:

Your problem is here:

X = df.index.strftime('%Y-%m-%d')

X is thus a string, so you can't use it to fit a regression. You'll want something like

X = (df.index.astype(np.int64) // 10**9).values which will instead convert your datetimes to Unix seconds.

Alternatively if you'd rather use something like "days since initial value" for X, you can do

start_date = df.index[0]
X = (df.index - start_date).days.values

In either case, you'll want to print results.summary() rather than results as well.

Post a Comment for "How To Find Out The Slope Value By Applying Linear Regression On Trend Of A Data?"