How do i use lsline to get linear regression

49 views (last 30 days)
Hi, I'm analyzing a survey and will need to use lsline to look at the relationship between two sets of data from that survey:
scatter(N,fruits,'bx')
scatter(N,sweets,'ro')
I tried lsline(N, fruits, sweets) but it didn't work. Any suggestions would be greatly appreciated!
Thanks,

Accepted Answer

Star Strider
Star Strider on 12 Feb 2015
The lsline function requires a bit of extra effort if you want to get information out of it. It calculates the linear fits on the current axes, or whatever axes you give it.
This works:
N = 1:10; % Created Data
fruits = 2*N + randn(1,10); % Created Data
sweets = 3*N + randn(1,10); % Created Data
figure(1)
scatter(N,fruits,'bx')
hold on
scatter(N,sweets,'ro')
hold off
h = lsline; % Fit Data
Fit_sweets = polyfit(h(1).XData, h(1).YData, 1); % Parameters: ‘sweets’
Fit_fruits = polyfit(h(2).XData, h(2).YData, 1); % Parameters: ‘fruits’
It seems to allocate to ‘h’ the x and y data from the topmost line as h(1) and the others in descending order, so you have to look at the fit (derived by polyfit here) to determine which is which. In this instance, the two do not overlap, but you will have to look at the fits that polyfit returns to decide which line is ‘sweets’ and which line is ‘fruits’ when you use your data. I cannot guarantee that they will be the same as in my example code, because the documentation on lsline does not mention how it assigns the lines to ‘h’.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!