% example ch19ex4.m % illustrates use of prinicipal component regression % illustrates effect of noise how to construct a regression estimator load ch19ex2_reg_data reg=ch19ex2_reg_data m=reg(:,2)-100; % the second column is the manipulated variable x1=reg(:,3)-200; %the first secondary measurement x2=reg(:,4)-100; %the second secondary measurement y=reg(:,5)-90; % Now add some noise to the test data noise=input('Enter maximum noise value') dx1=x1-3.66*m+ randn(20,1)*noise; dx2=x2-5.92*m+ randn(20,1)*noise; dy=y-4.05*m+ randn(20,1)*noise; % do pcr regression A=[dx1 dx2]; estimator=pcr_bj(A,dy,1) % calculation PC regression estimator % Now load the test data and check Rsquared value load ch19ex2_test_data test=ch19ex2_test_data m=test(:,2)-100; % the second column is the manipulated variable x1=test(:,3)-200; %the first secondary measurement x2=test(:,4)-100; %the second secondary measurement y=test(:,5)-90+ randn(20,1)*noise; dx1=x1-3.66*m+ randn(20,1)*noise; dx2=x2-5.92*m+ randn(20,1)*noise; dy=y-4.05*m; A=[dx1 dx2]; error=dy-A*estimator; norm(error) rsquared=1-norm(error)^2/norm(dy)^2