Contents

Introduction

% Brock University - Economics 4F10 - Ivan Medovikov
% www.medovikov.me

% Practical Worksheet #1

% The following worksheet demonstrates how to estimate a simple linear
% regression model in MATLAB of the form y = Xbeta + u, where X is a nxk
% data matrix and beta is a kx1 vector of model coefficients.

Set parameters and load data

clear; % clear memory
% load data: this dataset contains two varibles: X and y, where X is a 1000
% by 2 data matrix containing 2 regressors, and y is a 1000 by 1 vector
% containing realizations of the dependent variable.
load SampleRegressionData;
n = size(X,1); % set the number of observations to the # of rows in X
% set the number of coefficients in model to # of columns plus one for the
% constant
k = size(X,2) + 1;

Estimate the model y = Xbeta + u using OLS

% Add a column of ones to the data matrix to account for constant
xmat = [ones(n,1) X];

% Obtain OLS estimate of the coefficients vector beta
betahat = inv(xmat'*xmat)*(xmat'*y);

Estimate the covariance matrix of betahat

% First, estimate the residuals
uhat = y - xmat*betahat;

% Next, estimate the variance of the residuals
sigma2hat = (uhat'*uhat)/(n-k);

% Lastly, estimate the covariance matrix of betahat
covarbetahat = sigma2hat * inv(xmat'*xmat);

Calculate the t-ratios for regression coefficients

tratio_constant = betahat(1) / sqrt(covarbetahat(1,1));
tratio_beta2 = betahat(2) / sqrt(covarbetahat(2,2));
tratio_beta3 = betahat(3) / sqrt(covarbetahat(3,3));

Store results in table object and display table

regression_results = table;
regression_results.coefficient(1,1) = {'Constant'};
regression_results.coefficient(2,1) = {'Beta 2'};
regression_results.coefficient(3,1) = {'Beta 3'};
regression_results.estimate(1,1) = betahat(1);
regression_results.estimate(2,1) = betahat(2);
regression_results.estimate(3,1) = betahat(3);
regression_results.tratio(1,1) = tratio_constant;
regression_results.tratio(2,1) = tratio_beta2;
regression_results.tratio(3,1) = tratio_beta3;

disp(regression_results);
    coefficient    estimate    tratio
    ___________    ________    ______

    'Constant'     1.0834      15.231
    'Beta 2'       0.3646      5.1239
    'Beta 3'       2.4169      33.954