Write your own function file as \"mylinearregression\" which perform linear regr
ID: 3210289 • Letter: W
Question
Write your own function file as "mylinearregression" which perform linear regression without using in-built MATLAB function, in-order to return following values: slope of the regression line(an, intercept of the regression line(ao), Mean square error (MSE), standard error for the data (SE), Linear regression correlation coefficient (r), standard error for intercept (SE(ao) and standard error for slope (SE(a)), where ao and ai are the intercept and slope of the linear line. Display your results as “ The slope of the regression line (al) is given below" to display slope of the regression line. In similar way display as their respective name for all the unknown quantities that you have been asked to compute. Your function file must look as below: function [al, ao, MSE, SE, R2, SEa0, SEa1] = mylinearregression(X,F) %%% Your code goes here. endExplanation / Answer
%%%% Matlab function
function [a1 a0 MSE SE R2 SEa0 SEa1]= mylinearregression(x,F)
y1=F;
N=length(x);
k1=sum(x);
k2=sum(y1);
k3=sum(x.*y1);
k4=sum(x.^2);
a1=(N*(k3)-k1*k2)/(N*k4-k1^2); %%% slope
a0=(k2-a1*k1)/N; %%% intercept
R2=(a0*sqrt(var(x)/var(y1)))^2; %%% corealtion coefficient
y_est=a1*x+a0;
SE=abs(sum(y1-y_est))/N;
MSE=(sum(y1-y_est).^2)/N;
fprintf('The slope of regression line : %f ',a1);
fprintf('The intercept of regression line : %f ',a0);
fprintf('The Mean square error of regression line : %f ',MSE);
fprintf('The standard error for data : %f ',SE);
fprintf('The regression coefficient : %f ',R2);
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.