% Data from presentation "Statistical View of Regression" % Hint: Highlight code and press F9 to run in workspace clear all % Clears all data % The (x,y) data x =[ 0.0532 0.5323 -0.8114 0.6316 -0.6111 -0.1549 -0.5356 0.1772 -0.9918 0.2546 0.4079 0.7411 0.9125 -0.2603 -0.4453] Y =[ 7.8644 5.4082 1.1687 6.7322 4.8942 8.6170 6.4206 6.8425 0.2626 6.6100 7.1609 4.5558 3.1863 6.1780 5.6934] % Plot the data with a scatterplot figure, scatter(x,Y) title('Scatter Plot') xlabel('x') ylabel('y(x)') %% Estimate Parameters %% First order model (line fit) % Get data into matrix form X = [ones(n,1),x] % Estimate parameters b0,b1 b = inv(X'*X)*X'*Y % Built in Matlab commands % Backslash b = X\Y % regress function b = regress(Y,X) % to see more options with regress function help regress % or search in help window % Calculate Residuals Yhat = X*b e = Y - Yhat % Plot residuals figure, scatter(x,e) hold on, plot([-1,1],[0,0],'k') % Line at 0 title('Residual Scatter Plot - Linear') xlabel('x') ylabel('e(x)') %% Second order model (quadratic fit) X2 = [ones(n,1),x,x.^2] b2 = X2\Y Yhat2 = X2*b2 e2 = Y-Yhat2 figure, scatter(x,e2) hold on, plot([-1,1],[0,0],'k') % Line at 0 title('Residual Scatter Plot - Quadratic') xlabel('x') ylabel('e(x)') %% Plotting fits fit1=@(x) b(1) + b(2)*x fit2=@(x) b2(1) + b2(2)*x + b2(3)*x.^2 t=-1:.01:1; % Range for plotting figure, scatter(x,Y) hold on, plot(t,fit1(t)) plot(t,fit2(t),'r') title('Fitted functions') legend('Obs','Linear','Quadratic') %% Introduce curve fitting tool cftool