% load data load ('smoke.txt') smoking = smoke(:,1); mort = smoke(:,2); % make a scatterplot figure plot(smoking,mort,'bo'); title('Relationship between smoking and lungcancer'); ylabel('Mortality Index'); xlabel('Smoking Index'); n = length(smoking); % create the design matrix X = [ones(n,1),smoking]; % find the least square estimate of beta betahat = inv(X'*X)*(X'*mort); % find the fitted values and the residuals fitted = X*betahat; res = mort - X*betahat; % plot the regression line figure plot(smoking,mort,'k.',smoking,fitted,'c'); title('Smoking and Lung Cancer'); ylabel('Mortality Index'); xlabel('Smoking Index'); legend('Data','Fitted Model',0); % look at the residual plots to see if any model assumptions % are violated figure plot(res,'bo'); xlabel('Observation Number'); ylabel ('Residual'); title('Residual Plot'); figure plot(fitted,res,'bo'); xlabel('Fitted Values'); ylabel ('Residual'); title('Residual Plot'); % see how good our model fits the data figure plot(smoking,mort,'k.',smoking,fitted,'c',smoking,fitted,'r*'); title('Smoking and Lung Cancer'); ylabel('Mortality Index'); xlabel('Smoking Index'); legend('Data','Fitted Model',0);