Regression line for specified x-values
1 view (last 30 days)
Show older comments
Hi
I am searching for a way to plot regression lines, but only for given x values. I would also like to get r^2 values, change the color and linestyle to make the plot easy to understand.
My data along the x-axis is ranging from +12 to -8 and I would like to plot one line for the values from +12 to 0 and then another line from 0 to -8.
4 Comments
John D'Errico
on 11 Dec 2018
Again, you are not asking a question about MATLAB, just stating a long list of things you want to do. I'm not sure why you would expect that some very specific list of desires on your part should have a one line solution.
I told you several tools that return R^2 directly. And I gave you a link that shows how to compute it from what polyfit returns.
Again, you can plot a line. Surely you found out how to do that. Line will do it too. But, since you really want two different lines, then just plot two lines. If you look at the help docs for plot, it will show how to plot multiple lines, with different line specs for ech line. For example, this is one taken directly from doc plot.
plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')
You will see that you can set a specific color in the call to plot, although plot won't take a specific set of rgb code values as a line spec. You could do it using property/value pairs in the call to plot (but that is probably getting more complex than you can handle so far.) Or you can use handle graphics to change a color, linestyle etc.
If you are new to MATLAB, you won't learn how to use it unless you try. READ THE HELP DOCS. Try something, and see if it does what you expected. If it does not, then go back and read the help. It seems like so far, you tried a couple of random things (well, at least you tried something, better than a lot of people) but if all of that is too confusing and you are just trying random functions (refline?) then you really need to read the getting started tutorials, as you seem to be trying to run before you can walk.
So if you refuse to read the manual, then you need to go more slowly. Assuming that you understand polyfit, then it is not difficult to take what it returns and compute R^2, or use one of the alternative tools I suggested to do the regression, if you feel you really need to compute R^2.
Then learn how to use plot. You can use it to plot two lines in one call, as I show above. Or, by judicious use of the hold command, you can just call plot twice.
At some point, you may wish to learn about handle graphics. So try returning an argument from plot. See what it gies you. Then read the help for plot. For example, look at what this returns:
H = plot(rand(10,1),rand(10,1))
Now try things. Get your hands dirty. Think about what happens if you tried ths:
H.Color = 'r'
If you need to find the data points which lie below 0 in x, then what might you try? Would find help you? Try it. Better, read the help for find. Look at the examples in the doc for find.
I'm sorry that I did not tell you the exact commands to do exactly what it is that you want, but you won't learn that way. You will only learn how to dump a list of desires on Answers, and hope that someone out there feels sorry for you, thus solving whatever specific problem you have today. There is a huge amount of very well written documentation already there in place for you to use. Why not use it?
Answers (0)
See Also
Categories
Find more on Gaussian Process Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!