% RegressMultEx1.m % % Calculations for multiple regression example. % Data pts x coordinates are generated as corners of a rotated square and % a center point. % Then the square is offset by a constant vector added to all x pts. % The y values are created as: % -1's at the corners and +4 at the center, % the y values are scaled by a constant, % the y values are shifted by a plane y = a1*x1+a2*x2*b. % The plane in the last step is the multiple regression solution. % Create the corners of the square and the center point at the origin. X = [ 0, 0; 2, 1; -1, 2; -2, -1; 1, -2]; % Add offset to shift all x pts by vector. X = X + ones(5,1)*[3,4]; % Create y pts as +4 at center and -1's at corners of square. y = [4, -1, -1, -1, -1]'; % Scale y values by constant. y = y * 3; % Add offset and plane to y values. ba = [b, a1, a2]. ba = [-2, -1, 5]; ones_X = [ones(5,1),X]; y = y + ones_X*ba'; % Print values. data = [X,y] % Now compute the multiple regression solution. Xplus = pinv(ones_X); % Find the hyperplane fit. ba_soln = pinv(ones_X)*y % Print out the pts for the fit. fit = [X,ones_X*ba_soln] hold off % Make 3D plot. plot3(data(:,1)',data(:,2)',data(:,3)','+k') xlabel('x1'), ylabel('x2'), zlabel('y') axis([0, 6, 0, 6, 0, 25]) grid on hold on for index = 1 : 5 plot3([data(index,1);data(index,1)], ... [data(index,2);data(index,2)], ... [0,data(index,3)],'-k') end plot3(fit(:,1)',fit(:,2)',fit(:,3)','ok') hold off