% cd to the directory where the data file Table11_1 is
cd C:\Gerda\csc186
load Table11_1 -ascii %load the ascii file Table11_1
X = Table11_1(:,1);
Y = Table11_1(:,2);
clear Table11_1

% [b,bint,r,rint,stats] = regress(y,X) returns an estimate of the 
% regression coefficients in b, a 95% confidence 
% interval for  in the p-by-2 vector bint. The residuals are returned in r and a 95% 
% confidence interval for each residual is returned in the n-by-2 vector rint. The 
% vector stats contains the R2 statistic along with the F and p values for the 
% regression. 
%
% [b,bint,r,rint,stats] = regress(y,X,alpha) gives 100(1 - alpha)% confidence intervals
% for bint and rint. For example, alpha = 0.2 gives 80% confidence intervals. 
%
% X should include a column of ones so that the model contains a constant term. The 
% F statistic and p value are computed under the assumption that the model contains
% a constant term, and they are not correct for models without a constant. 
% The R-square value is one minus the ratio of the error sum of squares to the total
% sum of squares. This value can be negative for models without a constant, which 
% indicates that the model is not appropriate for the data.

alpha = 0.01;
V = [ones(size(X)) X]; % pad with a first column of 1's
[b,bint,r,rint,stats] = regress(Y,V,alpha); % compute

[b,bint,r,rint,stats] = csc186regress(Y,V,alpha); % compute and display

whichstat = {'fstat' 'rsquare' 'beta' 'mse' 'yhat' 'r'}; %chose parameters to store
%  fstat includes: SSE SSR DFe DFr f Pval
%  rsquare is the coefficient of determination = 1 - SSE/SST
%  beta is the vector of the regression coefficients the free term is first, beta(1)
%  mse = SSE/DFe , mean square error
%  yhat are the estimated response values
%  r is the vector of residuals

analysistat=regstats(Y,X,'linear',whichstat);
who %will display the variabl;es in the workspace (including all of the above

analysisstat

%analysistat = 

%     source: 'regstats'
%       beta: [2x1 double]
%       yhat: [33x1 double]
%          r: [33x1 double]
%        mse: 10.4299
%   rsquare: 0.9129
%     fstat: [1x1 struct]

 analysistat.fstat

%ans = 

%     sse: 323.3273
%    dfe: 31
%    dfr: 1
%    ssr: 3.3906e+003
%      f: 325.0795
%   pval: 0

 analysistat.beta

%ans =

%    3.8296
%    0.9036
