%Econ 241B
%HW 4 Solutions
%Prepared by Valerie Bostwick
%Last edited 2.20.2015

%% 

%Clear Memory
clear;
%Clear Command Window
clc;

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

%read in data
%columns are w1, w2, e1, e2, pubwage, period, wrk1id
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 (6) of Table 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%e1 =  beta0 + sum_i=1:3(beta_i*(w1=i) + beta4[(w1-w2)*(w1<w2)] + D_p + u
%create e1 vector
y=newdata(:,3);
%create wage and period dummies
%first find unique values of period
unique(newdata(:,6))
%create zero vectors for all the dummies
%Note: omitted categories are w1=0 and period=30
wdummies=zeros(n,3);
pdummies=zeros(n,29);
%create difference in wage variable
wdiff=newdata(:,1)-newdata(:,2);

for col=1:29
    for i=1:n
        %replace zero columns with indicators for the 30 possible period values
        if newdata(i,6)==col
            pdummies(i,col)=1;
        end
        %replace zero columns with indicators for the 3 possible wage values
        if newdata(i,1)==col
            wdummies(i,col)=1;
        end
        %multiply relative wage variable by indicator for w1<w2
        if newdata(i,1)>=newdata(i,2)
            wdiff(i,1)=0;
        end
    end
end

%Combine variables into X matrix
X=[ones(n,1), wdummies, wdiff, pdummies];
%estimate betas
betas6=inv(X'*X)*(X'*y)

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

%test the joint hypothesis that all 4 slope coefficients are equal
%create null hypothesis matrix
r=3
R=[0, 1, 0, 0, -1; 0, 0, 1, 0, -1; 0, 0, 0, 1, -1]
R=[R, zeros(r,29)];
%create variance of beta matrix
V=s2.*inv(X'*X);
%calculate F-statistic
F=(R*betas6)'*inv(R*V*R')*(R*betas6)./r
%calculate p-value
pvalue=(1-fcdf(abs(F),r, n-k))


%% 

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

%e1 = beta0 + sum_i=1:3(beta_i*(w1=i) + beta4[(w1-w2)*(w1<w2)] + D_p + D_i + u

%first find unique values of wrk1id
ids=unique(newdata(:,7))
idcount=size(ids,1)
%create zero vectors for all the dummies
%Note: omitted categories are wrk1id=134 and wrk1id=1
iddummies=zeros(n,idcount-2);

for col=2:idcount-1
    for i=1:n
        %replace zero columns with indicators for the 37 possible period values
        if newdata(i,7)==ids(col,1)
            iddummies(i,col-1)=1;
        end
    end
end

%Combine variables into X matrix
X=[X, iddummies];
%estimate betas
betas7=inv(X'*X)*(X'*y)

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

%test the joint hypothesis that all 4 slope coefficients are equal
%create null hypothesis matrix
r=3
R=[0, 1, 0, 0, -1; 0, 0, 1, 0, -1; 0, 0, 0, 1, -1]
R=[R, zeros(r,29+idcount-2)];
%create variance of beta matrix
V=s2.*inv(X'*X);
%calculate F-statistic
F=(R*betas7)'*inv(R*V*R')*(R*betas7)./r
%calculate p-value
pvalue=(1-fcdf(abs(F),r, n-k))


