%% Panel (Michele Piffer, LSE, 15/2/2012)

clc 
clear 

%% DGP

T = 40;
N = 1500; % total individuals
NT = N*T;

id = kron([1 : 1 : N ]', ones(T,1));
year = kron(ones(N,1), [1990 : 1 : 1990+T-1]');

% constant
aa = ones(NT,1); 

% regressor
x1 = [];  % long regressor
xx = randi([-15 15], T, N);  % x for each individual
for i=1:N
    x1 = [x1; xx(:, i)];
end

% error
uIID = 10*randn(NT,1); % if iid

uRW = zeros(NT,1);
for i=2:NT
    uRW(i) = uRW(i-1) + uIID(i);
end

u = uIID;

% individual shock

for i=1:N
    xbars(i) = sum(xx(:,i));
end

endog = 0.30;  % controls endogeneity of x in c

c = 5*randn(N,1) + endog*xbars';
cc = kron(c,ones(T,1));
display('endogeneity problem')
x1'*cc/NT + (sum(x1)/NT)*sum(cc)/NT 
 

% model
X = [aa, x1]; % regressors only

beta_true = [2 ; 3];

y = X*beta_true + cc + u;


% Export data

data = [id, year y, x1, [beta_true; zeros(NT-2,1)], cc, u];

xlswrite('panel.xls', data, 'output', 'A1')

