%Econ 241B
%HW 2 Solutions
%Prepared by Valerie Bostwick
%Last edited 1.26.2015

%% 

%Clear Memory
clear;

%Set current directory
cd('C:\Users\bostwick\Dropbox\Econ241B_TA');

%read in data
data=csvread('hw2data.csv');

%get number of observations for future use
n=size(data,1)

%pubwage is saved in column 5
pubwage=data(:,5);

%create a new data matrix for only pubwage==1 observations
newdata=[];
for i=1:n
    if pubwage(i,1)==1
        newdata=[newdata;data(i,:)];
    end
end

%replace n with the new observation count
n=size(newdata,1)


%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Column (1) of Table 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%e1 = beta0 + beta1*w1 + u
%create e1 vector
y=newdata(:,3);
%create X matrix with a constant as the first column, w1 as second column
X=[ones(n,1), newdata(:,1)];
%estimate betas for column (1)
betas1=inv(X'*X)*(X'*y)

%estimate standard errors
k=2;
resid=y-X*betas1;
s2=(1/(n-k)).*(resid'*resid)
SEs1=sqrt(diag(s2.*inv(X'*X)))


%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Column (2) of Table 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%e1 = beta0 + beta1*w1 + beta2*(w1-w2) + u
%append relative wage to X matrix
X=[X, newdata(:,1)-newdata(:,2)];
%estimate betas for column (2)
betas2=inv(X'*X)*(X'*y)

%estimate standard errors
resid=y-X*betas2;
s2=(1/(n-k)).*(resid'*resid)
SEs2=sqrt(diag(s2.*inv(X'*X)))

%estimate t-statistics
t2=betas2(3,1)/SEs2(3,1)
%p=Pr(tdist(n-k)>tstat)*2
pvalue2=2*(1-tcdf(abs(t2),n-k))



%% 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Column (3) of Table 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%e1 = beta0 + beta1*w1 + beta2*(w1-w2)*(w1<w2) + u
%multiply relative wage variable by indicator for w1<w2
for i=1:n
    if newdata(i,1)>=newdata(i,2)
        X(i,3)=0;
    end
end
%estimate betas for column (2)
betas3=inv(X'*X)*(X'*y)

%estimate standard errors
resid=y-X*betas3;
s2=(1/(n-k)).*(resid'*resid)
SEs3=sqrt(diag(s2.*inv(X'*X)))

%estimate t-statistics
t3=betas3(3,1)/SEs3(3,1)
%p=Pr(tdist(n-k)>tstat)*2
pvalue3=2*(1-tcdf(abs(t3),n-k))




