% Exampleols.m
% Regression analysis using matrix algegra and Matlab
nobs = 6;
nvars = 2;
y = [8,7,13,11,16,15]';
ones = [1,1,1,1,1,1]';
x1 = [1,2,3,4,5,6]';
X = [ones,x1];
betahat = inv(X'*X)*X'*y; % OLS coefficients
betahat
yhat = X*betahat;
yhat
resid = y - yhat; % OLS residuals
resid
ssr = resid'*resid;  % sum of squared residuals
ssr
sigmasq = ssr/(nobs-nvars); % estimate of error variance
ser = sqrt(sigmasq);  % standard error of regression
covbeta = sigmasq*inv(X'*X);
covbeta
sebeta = sqrt(diag(covbeta)); % standard errors of betas
sebeta
tbeta = betahat ./ sebeta;  % t-statitics of beta
tbeta
ym = y - mean(y);
rsqr1 = ssr;
rsqr2 = ym'*ym;
rsqr = 1.0 - rsqr1/rsqr2; % R-Squared
rsqr
term1 = (nobs - nvars)/(nvars - 1);  
term2 = rsqr/(1-rsqr);
foverall = term1*term2;  % overall F-statistic
foverall
scatter(x1,y);
