Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear all
- clc
- X=[1 2 3 4 5 6 7 8 9 10] %array X
- Y=[12 14 24 28 19 28 30 37 41 45] %array Y
- scatter(X,Y) %to obtain scatter plot
- %for axis labels
- xlabel('X')
- ylabel('Y')
- %to obtain line of best fit
- f=polyfit(X,Y,1);
- v=polyval(f,X);
- hold on
- grid on;
- plot(X,v)
- %calculation of estimates b0 and b1
- AvgX=mean(X); %mean value of array X
- AvgY=mean(Y); %mean value of array Y
- n=length(X); %setting n to 10(number of entries in arrays)
- sum1=sum(X.*Y); %summation of Xi*Yi
- sum2=sum(X); %summation of Xi
- sum3=sum(Y); %summation of Yi
- sum4=sum(X.^2); %summation of Xi^2
- sum5=sum2^2; %squaring the summation of Xi
- b1=(((n*sum1)-(sum2*sum3))/((n*sum4)-sum5)); %to obtain value of b1
- b0=AvgY-(b1*AvgX); %to obtain value of b0
- disp("Equation of line of best fit is: Y = "+b0+" + "+b1+"X") %to display equation
- disp(" ")%empty line in command window
- %computing R^2 coefficient
- sum6=sum((X-AvgX).*(Y-AvgY)); %summation of (Xi-AvgX)(Yi-AvgY)
- sum7=sum((X-AvgX).^2); %summation of (Xi-Avg(X))^2
- sum8=sum((Y-AvgY).^2); %summation of (Yi-Avg(Y))^2
- rsqrdcoeff = ((sum6)/(sqrt(sum7*sum8)))^2; %to obtain value of R^2 coefficient
- disp("R^2 Coefficient = "+rsqrdcoeff) %to display r^2 coefficient
- disp(" ") %empty line in command window
- %to calculate length of unstandardised residuals
- for k=1:length(X)
- residualss(k)=(((-b1)*X(k))+(Y(k))-b0)/(sqrt((b1^2)+(1^2)));
- end
- LengthOfResiduals = abs(residualss') %to display absolute value of lengths in a vector
- %to plot unstandardised residuals
- GradientOfResiduals=-1/(b1) %to obtain gradient of residuals
- %loop to obtain y-intercepts of residuals
- c=zeros(1,10);
- for i=1:10
- c(i)=Y(i)-(GradientOfResiduals*X(i));
- xInt=(c(i)- b0)/(b1-GradientOfResiduals);
- yInt=(GradientOfResiduals*xInt)+c(i);
- line([X(i) xInt],[Y(i) yInt], 'Color', 'b');
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement