%%%%%%%%%%%%(c) Jan Kabatek, Universiteit van Tilburg %%%%%%%%%%%%%%%%%%%%%
% This is a resulting m-file for 3rd tutorial of Econometrics 1 for %
% research masters. %
% %
% Contents: basic functions, simulating data, regression output %
% %
% IMPORTANT: before asking me about a specific command in this code(or any%
% code) consult a HELP FILE by parsing: help 8
sum(ourmat > 8)
sum(sum(ourmat > 8)) % how many points satisfy the condition
censormat = ourmat.*(ourmat > 8) % censoring the matrix according to our
% condition. The dot operator stands for array multiplication. Only terms
% at the same positions are multiplied.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(2)% DATA GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all ; % Remember - the more variables you
% store in the memory, the less of it can be used for actual computation
%%%% set ANR number as a seed ! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s = RandStream('mcg16807','Seed',123456)
RandStream.setDefaultStream(s)
%%%% data generating parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N = 100 ; %number of obs
K = 4 ; %number of regressors
beta = [1 2 3 4]' ; %true regressor values
sigma = .5 ; %sd of error terms
%%%% data generating process %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X = [ones(N,1) randn(N,K-1)] ; %first col is constant term
E = sigma*randn(N,1) ;
Y = X*beta+E ;
%%%% basic statistical functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mean(X,1) %mean value over rows (2=cols)
var(X,1)
std(X,1)
cov(X(:,2),Y)
%%% OLS by command %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% help regress
regress(Y,X)
[B,BINT,R,RINT,STATS] = regress(Y,X) ;
% X should include a column of ones so
% that the model contains a constant term, otherwise it's a regression
% through origin!!!
% one way how to include it is (for an X matrix without constat term)
% X = [ones(size(,1),1) , X ]
% Or be creative: Y~=NaN will do the same job as "ones" command!
% NaN = not a number
%%% small loop example %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% we're gonna examine how do the values of beta change as we run ols on new
% draws from the DGP
nloop = 1000
keepbeta = NaN(K,1000) ; % defining our storage matrix for
% Betas - it is useful to define it in advance, as it is more efficient in
% terms of memory allocation.
% parsing keepbeta=[keepbeta,newbeta] 1000 times is very memory-intensive.
for i=1:nloop
% generate new draw
X = [ones(N,1) randn(N,K-1)] ;
E = sigma*randn(N,1) ;
Y = X*beta+E ;
%run regression
newbeta=regress(Y,X) ;
keepbeta(:,i) = newbeta ;
end
% plotting trajectories of beta
figure(1), plot(keepbeta') ;
%this is what we would call Monte Carlo (MC) simulation. In fact, we could
%get an approximation of covariance matrix of betas by cov(keepbeta').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(3)% IMPORTING DATA% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% most safe way is to use Import data wizard accessible through selection
% File->Import data in the main Matlab window.
% a direct command is
% A = importdata(filename) check the help file for further info