I have some data that looks almost linear. I need to make a fit in order to evaluate the slope. The thing is, the slope is changing if I change the fit range (not all the data is linear so I need to choose myself the range) I wondered how would you suggest to evaluate the added error from the arbitrary selection of the range. For example, maybe I should make a few fits for a few ranges, and take the "range of slopes"/stdv as the added uncertainty?

  • 1
    $\begingroup$ Hi, welcome to OR.SE. Could you provide an example to illustrate the mentioned behavior? So that the question is understood more clearly. $\endgroup$ – dhasson Jun 15 '20 at 18:03
  • $\begingroup$ Are you required to come up with a linear fit (single constant slope), or can you do a piecewise-linear fit as Kevin Dalmeijer suggests? Also, are there just two variables (the one you are predicting and a single explanatory variable) or is it multidimensional? $\endgroup$ – prubin Jun 15 '20 at 19:10

There exist methods for fitting a piece-wise linear function to data in an optimal way, for various interpretations of what it means for this fit to be optimal. These methods may still require you to specify the number of segments in advance, but playing around with the number of segments seems less arbitrary to me than selecting the ranges by yourself.

This question on StackOverflow discusses Python libraries to do piece-wise linear fitting, including pwlf. You may also have a look at the Wikipedia entry for Segmented regression.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.