필터 지우기
필터 지우기

How do i use lsline to get linear regression

조회 수: 6 (최근 30일)
Belal Abboushi
Belal Abboushi 2015년 2월 12일
답변: Star Strider 2015년 2월 12일
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,

채택된 답변

Star Strider
Star Strider 2015년 2월 12일
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’.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by