Recently I had some very noisy data that I wanted to fit a curve to. The problem is there were different regions that were somewhat linear, but the overall data was far from linear. A spline or a polynomial did not capture the behavior in a global sense. What I wanted was the ability to fit a line to each region. SLM does that right out of the box.
I think it is very instructive to look at how this code was packaged up. There is a clearly labeled tutorial that was easy to scan looking for an example like what I wanted to do. All of the files have lots of comments. sometimes more comments that code I think. The files are named coherently so they make a nice package. These are all great things to make a package that other people can use.
13 CommentsOldest to Newest
Nice! I love it when I can learn about MATLAB functions that I did not know exist! It is a very large product, impossible to know it all!
Nice pick Doug – did you have to specify how many linear segments there are in your data? If so, do you have to specify the location of the knots?
I did specify, but the function chooses nice defaults if I did not.
Hi Doug, can you show me the codes you used to generate the fitting?
Yeah, code would have been nice – how can I learn this without an example to display some metacognitiion? Not trying to complain, but kind of lame.
The link to the code is in the first paragraph of the post.
I have confirmed it works.
MATLAB R2012a didn’t find “hpfilter” in help !!!
It is in the econometrics toolbox.
Hi Doug, great code. I was wondering if there was a way to specify a range for the interior knots. For example, if I have x-axis data ranging in value from 0 to 10, and I want 4 knots, the first at 0, the last at 10, and the middle 2 at or between 4 and 8. Is there a way to make this happen using your slm toolbox? The ‘interior knots’ just puts the knots at any number great than 0 or less than 10, I believe. Thanks!
Check with the original author. I am only promoting it.
Hi there, in your example, how did you decide the number and the position of “knots” of your fitting curve?
Hi Cheng, I’m afraid Doug Hull, the original author of this post has left the company. Do the default knot locations not work for you?