0

I am trying to find possible linear relationships in my data. However, sometimes, rather than just a single linear trend throughout the whole dataset, there is a possibility that the data may contain multiple linear relationships at different portions of the data.

I know that linear regression is used for finding linear relationships, however I am unable to figure out how to perform a check to see if there is any linear relation in the first place. The only method I could think of was calculate the gradient between 2 consecutive points and see if subsequent pairs of lines fall into a tolerated deviation of gradients as a form of checking, but this sounds very inefficient, especially for larger sets of data. Is there any library or method I could use for this specific purpose?

For reference, this is the test code I have:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

#cut off quadratic curve
x = np.array([-10,-9,-8,-7,-6,-5,2,3,4,5,6,7,8,9,10])
y = np.array([100,81,64,49,36,25,4,9,16,25,36,49,64,81,100])

#perform check for any linear relationships?? (if possible set max deviation for gradient)
#if any found, save ranges of x values into array and perform linear regression

#for (range of x values) in array:
#linear regression
linreg = LinearRegression()
x = x.reshape(-1,1)
linreg.fit(x,y)

#predicted trend values of y
y_trend = linreg.predict(x)

#plot out data
plt.scatter(x,y)
plt.plot(x,y_trend, color='red')
plt.show()

This is what I currently obtain:

Current outcome (Best fit line for whole dataset)

This is what I want to obtain (or something like it):

Wanted outcome (Find possible trends)

Where the red and green lines indicate different linear relationships in the data.


UPDATE:

Thanks to 祖tp's response, I have learnt that what I am looking for is known as piecewise linear regression. After searching around for a bit, I found a library on github that was specifically made for DISCONTINUOUS piecewise linear regression. Playing around with it gave me the following result:

Piecewise Linear Regression result

So while it is not exactly what I wanted, I should be able to build on from here.

Oboros
  • 47
  • 5

1 Answers1

1

You may want to check out piece-wise lienear regression that fits linear regression models on different segment of your data. See this post for more detail. And perhaps this can also be helpful.

Azuuu
  • 853
  • 8
  • 17