MATLAB: Have I plotted the residuals(residual sum of squares) correctly. Because it looks suspiciously wrong :/

linear regressionplotregressionresidual sum of squaresresiduals

y6=[2941, 2910, 2158, 2994, 2350, 2731, 2696, 2649, 2530, 2306, 2246, 2073, 1908, 1733, 1260, 1566, 1252, 1355, 1214, 1103, 1119];
p6=polyfit(x,y6,1);%p(1) is the slope and p(2) is the intercept
yfit6=polyval(p6,x); %saves you from writing the fit equation yourself:p(1)*x +p(2)
yresid6=y6-yfit6;
SSresid6 = sum(yresid6.^2);
SStotal6 = (length(y6)-1) * var(y6);
rsq6 = 1 - (SSresid6/SStotal6) %coefficient of determination
subplot(2,3,6)
plot(x,y6,'o',x,yfit6)
figure
plot(x,SSresid6,'+')
title('Plot of residual')
I just get a straight horizontal line. How do i fix it?
Thanks
EDIT:just realised i was plotting sum(SSresid.^2) I have added a command:
ssresid6=(y6-yfit6).^2
plot (x,ssresid6,'+')
I that the correct plot i am looking for or am i plottin something completley different here??

Best Answer

You're getting closer. First you were plotting the sum of the residuals (which is just a single number), but with your correction you are now plotting the square of the residuals for each x value. If you want the actual residuals themselves, then don't square the difference, just like dpb said.
ssresid6 = y6 - yfit6;
If you want just the distance of the actual from the fit, then you can take the absolute value with abs() so that all the numbers ore positive distances. Otherwise you'll have positive and negative residuals. It just depends on what you want to see.