DATA ARRANGEMENT FROM PANEL FORMAT

조회 수: 2 (최근 30일)
Saptorshee Chakraborty
Saptorshee Chakraborty 2020년 3월 17일
댓글: Saptorshee Chakraborty 2020년 3월 21일
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.

채택된 답변

Raunak Gupta
Raunak Gupta 2020년 3월 20일
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
Raunak Gupta
Raunak Gupta 2020년 3월 21일
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 2020년 3월 21일
Thank you very much indeed.
It solves my problem.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Saptorshee Chakraborty
Saptorshee Chakraborty 2020년 3월 20일
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by