Regression Model Graph(plot), Deflection Problem(sharp point or cusp)
조회 수: 12 (최근 30일)
이전 댓글 표시

I implemented the first, second, and third regression models for the data and showed them using the plot function.
But, for the 3rd regression model, the graph was bent, such as the part in red.
How can we solve this problem?
댓글 수: 6
답변 (1개)
Scott MacKenzie
2021년 6월 25일
편집: Scott MacKenzie
2021년 6월 27일
You are getting the sharp bend because you are calculating YFIT using the x sample points (which are sparce at the inflexion point). To see the curvature in the model, you need to calculate YFIT using a set of x query points (xq) that are finely spaced over the full range of interest (including about the inflexion point):
load accidents
x = hwydata(:,6); % population of states
y = hwydata(:,4); % accidents per state
scatter(x,y,'filled')
hold on;
% use xq for plotting (to expose curvature in models)
xq = linspace(min(x), max(x));
X = [ones(size(x)) x];
b = regress(y,X)
hold on
YFIT = b(1) + b(2)*xq; % use xq, not x
plot(xq,YFIT);
hold on;
X2 = [ones(size(x)) x x.^2];
b2 = regress(y,X2);
YFIT2 = b2(1) + b2(2)*xq + b2(3)*xq.^2 ; % use xq, not x
plot(xq,YFIT2);
X3 = [ones(size(x)) x x.^2 x.^3];
b3 = regress(y,X3);
YFIT3 = b3(1) + b3(2)*xq + b3(3)*xq.^2 + b3(4)*xq.^3 ; % use xq, not x
plot(xq,YFIT3) ;
xlabel('Registered vehicles[thousands](x)');
ylabel('Accidents per state(y)');
legend('Data','1st Order','2nd Order', '3rd Order', 'location', 'southeast');

BTW, consider using polyfit (along with polyval) instead of regress. It's a bit easier overall:
pf1 = polyfit(x, y, 1)
yfit1 = polyval(pf1, xq)
plot (xq, yfit1)
pf2 = polyfit(x, y, 2)
yfit2 = polyval(pf2, xq)
plot (xq, yfit2)
pf3 = polyfit(x, y, 3)
yfit3 = polyval(pf3, xq)
plot (xq, yfit3)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!