DATA ARRANGEMENT FROM PANEL FORMAT

2 views (last 30 days)
Hello,
I have a data of 5 variables (x1, x2, x3, x4, x5) for 24 countries of 28 years. (N = 24, T = 28, d = 5) in panel format. I want to arrange X = (x_{11}, ..., x_{1T}, ..., x_{N1}, ..., x_{NT})'.
How do I proceed.

Accepted Answer

Raunak Gupta
Raunak Gupta on 20 Mar 2020
Hi,
I assume you have 28 tables corresponding to 28 years with 5 variables for all 24 countries based on my understanding of panel format data. So, each table contains all 24 countries and 5 variables for it. You can create a matrix first for each variable X1, X2, X3, X4, X5. Then can read each of the 28 tables and assign the columns to corresponding variable column. Then you can flatten each variable matrix in row major form to get the output. You may find below code useful.
% Let say tablei is the table having 24 rows and 5 column
% corresponding to x1,x2,x3,x4,x5
% Lets say you have tables stored as .csv file and all the names of .csv
% files are stored in vector tableName.
N = 24;
T = 28;
d = 5;
X1mat = zeros(N,T);
X2mat = zeros(N,T);
X3mat = zeros(N,T);
X4mat = zeros(N,T);
X5mat = zeros(N,T);
for i=1:size(tableName,1) % For the number of years
tablei = readtable(tabelName(i));
X1mat(:,i) = tablei.x1;
X2mat(:,i) = tablei.x2;
X3mat(:,i) = tablei.x3;
X4mat(:,i) = tablei.x4;
X5mat(:,i) = tablei.x5;
end
% Creating vector by Reshaping in row major form
X1 = reshape(X1mat.',1,[]);
X2 = reshape(X2mat.',1,[]);
X3 = reshape(X3mat.',1,[]);
X4 = reshape(X4mat.',1,[]);
X5 = reshape(X5mat.',1,[]);
  2 Comments
Raunak Gupta
Raunak Gupta on 21 Mar 2020
Hi,
Based on the data you have provided lets say XOriginal is the matrix of dimension (NTxd) and for arranging this matrix, for arranging XOriginal in the given format try the below code.
dummyX = reshape(XOriginal,[28,24,5]);
Xfinal = permute(dummyX,[2,1,3]);
Also for arranging Y you can just reshape the vector in [28,24] then can take transpose of that array.
dummyY = reshape(YOriginal,[28,24]);
Yfinal = dummyY.';
Hope this provide the solution you want.
Saptorshee Chakraborty
Saptorshee Chakraborty on 21 Mar 2020
Thank you very much indeed.
It solves my problem.

Sign in to comment.

More Answers (1)

Saptorshee Chakraborty
Saptorshee Chakraborty on 20 Mar 2020
Sorry this do not solve my problem, have a look at the picture. I have five x's (x1, x2, x3, x4, x5) in panel format. I want them arranged like this X = (x_{11}, ..., x_{1T}, ..., x_{N1}, ..., x_{NT})' [Observations on explanatory variables, NT-by-d matrix]..
i.e., X should be somewhat like a matrix of 5 x's of 24*28*5, but the five X's should not become one single X, so that the data can differentiate while reading it. Since my Y is a 24*28 matrix, similarly my X has to be 24*28*5 matrix and not something else.
Thannk you.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!