load WAGE1.mat;
dd = WAGE1;
n=size(dd,1);

  %Obs:   526

  %1. wage                     average hourly earnings
  %2. educ                     years of education
  %3. exper                    years potential experience
  %4. tenure                   years with current employer
  %5. nonwhite                 =1 if nonwhite
  %6. female                   =1 if female
  %7. married                  =1 if married
  %8. numdep                   number of dependents
  %9. smsa                     =1 if live in SMSA
 %10. northcen                 =1 if live in north central U.S
 %11. south                    =1 if live in southern region
 %12. west                     =1 if live in western region
 %13. construc                 =1 if work in construc. indus.
 %14. ndurman                  =1 if in nondur. manuf. indus.
 %15. trcommpu                 =1 if in trans, commun, pub ut
 %16. trade                    =1 if in wholesale or retail
 %17. services                 =1 if in services indus.
 %18. profserv                 =1 if in prof. serv. indus.
 %19. profocc                  =1 if in profess. occupation
 %20. clerocc                  =1 if in clerical occupation
 %21. servocc                  =1 if in service occupation
 %22. lwage                    log(wage)
 %23. expersq                  exper^2
 %24. tenursq                  tenure^2
 
% Vector y of regressands
y = dd(:,22);

% Vectors with different regressors

one = ones(size(y,1),1);
x_1 = dd(:,2); %EDUC
x_2 = dd(:,3); %EXPER
x_3 = dd(:,4);
x_4 = dd(:,8);
x_5 = dd(:,23); %EXPER^2

%Part a)

% Matrix X ofregressors 

X=[one x_1 ]; 

%OLS estimator

betahat = (X'*X)\(X'*y);
betahat

% Variance estimator
uhat = y-X*betahat;
n = size(y,1);
colsX = size(X,2);
dof = n-colsX;
sigmahat2 =(1/dof)*uhat'*uhat;
sigmahat2 


% alpha - confidence interval for inidividual parameters

% A is the inverse of X'*X
A=(X'*X)\eye(colsX);

% 1-alpha is the desired level of confidence
alpha = 0.05;

% cint is a placeholder matrix
cint = zeros(colsX,2);

for i=1:colsX
    
    cint(i,:)=[betahat(i)-tinv(1-alpha/2,dof)*sqrt(sigmahat2*A(i,i)) betahat(i)+tinv(1-alpha/2,dof)*sqrt(sigmahat2*A(i,i))];
end

% cint is the confidence interval

cint

% Part b)

X=[one x_1 x_2 ]; 

%OLS estimator

betahat = (X'*X)\(X'*y);
betahat

% Variance estimator
uhat = y-X*betahat;
n = size(y,1);
colsX = size(X,2);
dof = n-colsX;
sigmahat2 =(1/dof)*uhat'*uhat;
sigmahat2 


% alpha - confidence interval for inidividual parameters

% A is the inverse of X'*X
A=(X'*X)\eye(colsX);

% 1-alpha is the desired level of confidence
alpha = 0.05;

% cint is a placeholder matrix
cint = zeros(colsX,2);

for i=1:colsX
    
    cint(i,:)=[betahat(i)-tinv(1-alpha/2,dof)*sqrt(sigmahat2*A(i,i)) betahat(i)+tinv(1-alpha/2,dof)*sqrt(sigmahat2*A(i,i))];
end

% cint is the confidence interval

cint

% Part c)

X=[one x_1 x_2 x_5 ]; 

%OLS estimator

betahat = (X'*X)\(X'*y);
betahat

% Variance estimator
uhat = y-X*betahat;
n = size(y,1);
colsX = size(X,2);
dof = n-colsX;
sigmahat2 =(1/dof)*uhat'*uhat;
sigmahat2 


% alpha - confidence interval for inidividual parameters

% A is the inverse of X'*X
A=(X'*X)\eye(colsX);

% 1-alpha is the desired level of confidence
alpha = 0.05;

% cint is a placeholder matrix
cint = zeros(colsX,2);

for i=1:colsX
    
    cint(i,:)=[betahat(i)-tinv(1-alpha/2,dof)*sqrt(sigmahat2*A(i,i)) betahat(i)+tinv(1-alpha/2,dof)*sqrt(sigmahat2*A(i,i))];
end

% cint is the confidence interval

cint




