Title
mz_panrowshape() -- Reorganizes, in rows, a panel data matrix for which the panels are presented by columns
Syntax
transmorphic matrix mz_panrowshape(V, idcol, f [, d ])
where
V: transmorphic matrix containing panel data
idcol: real scalar indicating the column containing the subject identifier
f: string scalar indicating how the user wants the panels to be presented
d: real rowvector containing the columns to be removed and is optional
Description
mz_panrowshape() reshapes a (K * t) x c panel data set matrix V, to a u x (v * K) panel data matrix, (u <= t and v <= c) if the argument f takes the value "CK". K is the number of panels, t the number of observations in panels and c the number of columns of matrix V. The "CK" means that each panel (see the help for [M-5] panelsetup() for the definition of panel data in mata) is merged to the preceding until the exhaustion of the panels.
mz_panrowshape() returns a u x (K * v) matrix, (u <= t and v <= c) if the argument f takes the value "KC". The "KC" means that each column is merged to itself by panel until the exhaustion of panels. This action is repeated for all columns of V and the resulting sub-matrices are themselves merged to each other.
mz_panrowshape() always returns balanced panels, same number of observations in all the panels, even if V is not balanced. The balancing criterion used is panelsetup(V, idcol, 2, 0). See [M-5] panelsetup() for more details.
It should be noted that the real multiplications (v * K) and (K * v) in u x (v * K) and u x (K * v) respectively give the same value if u and v are identical in the two cases but they are used here to emphasize the difference in the arrangement of columns in the returned matrices.
Remarks
Remarks are presented under the following headings:
Case where f takes the value "CK" The matrix V is balanced The matrix V is unbalanced Case where f takes the value "KC" String matrix
Case where f takes the value "CK"
: V 1 2 3 4 +---------------------+ 1 | 1 1 3.1 3.7 | 2 | 1 2 9.2 4.2 | 3 | 1 3 2.5 6.5 | 4 | 1 4 8.4 7 | 5 | 1 5 4.6 3.9 | 6 | 2 1 6.2 1.5 | 7 | 2 2 5 3.2 | 8 | 2 3 7.8 9.6 | 9 | 2 4 5.4 7.7 | 10 | 2 5 3.4 9.2 | 11 | 3 1 2.1 2.3 | 12 | 3 2 9 1.5 | 13 | 3 3 1.4 6.7 | 14 | 3 4 4.8 3.3 | 15 | 3 5 9.5 9.7 | +---------------------+
The first column of V contains the subject identifier. There are three panels, each of them has five observations.
: mz_panrowshape(V, 1, "CK") 1 2 3 4 5 6 7 8 9 10 11 12 +-------------------------------------------------------------+ 1 | 1 1 3.1 3.7 2 1 6.2 1.5 3 1 2.1 2.3 | 2 | 1 2 9.2 4.2 2 2 5 3.2 3 2 9 1.5 | 3 | 1 3 2.5 6.5 2 3 7.8 9.6 3 3 1.4 6.7 | 4 | 1 4 8.4 7 2 4 5.4 7.7 3 4 4.8 3.3 | 5 | 1 5 4.6 3.9 2 5 3.4 9.2 3 5 9.5 9.7 | +-------------------------------------------------------------+
If we want to drop the second column of V, we set
: d = 2 : d : 2
Then
: mz_panrowshape(V, 1, "CK", d) 1 2 3 4 5 6 7 8 9 +-------------------------------------------------+ 1 | 1 3.1 3.7 2 6.2 1.5 3 2.1 2.3 | 2 | 1 9.2 4.2 2 5 3.2 3 9 1.5 | 3 | 1 2.5 6.5 2 7.8 9.6 3 1.4 6.7 | 4 | 1 8.4 7 2 5.4 7.7 3 4.8 3.3 | 5 | 1 4.6 3.9 2 3.4 9.2 3 9.5 9.7 | +-------------------------------------------------+
If we want to drop the first two columnns, we set
: d = (1,2) : d : 1 2 +---------+ 1 | 1 2 | +---------+
Then
: mz_panrowshape(V, 1, "CK", d) 1 2 3 4 5 6 +-------------------------------------+ 1 | 3.1 3.7 6.2 1.5 2.1 2.3 | 2 | 9.2 4.2 5 3.2 9 1.5 | 3 | 2.5 6.5 7.8 9.6 1.4 6.7 | 4 | 8.4 7 5.4 7.7 4.8 3.3 | 5 | 4.6 3.9 3.4 9.2 9.5 9.7 | +-------------------------------------+
: V 1 2 3 4 +---------------------+ 1 | 1 2 9.2 4.2 | 2 | 1 3 2.5 6.5 | 3 | 1 4 8.4 7 | 4 | 1 5 4.6 3.9 | 5 | 2 1 6.2 1.5 | 6 | 2 2 5 3.2 | 7 | 3 1 2.1 2.3 | 8 | 3 2 9 1.5 | 9 | 3 3 1.4 6.7 | +---------------------+
: mz_panrowshape(V, 1,"CK") 1 2 3 4 5 6 7 8 9 10 11 12 +-------------------------------------------------------------+ 1 | 1 2 9.2 4.2 2 1 6.2 1.5 3 1 2.1 2.3 | 2 | 1 3 2.5 6.5 2 2 5 3.2 3 2 9 1.5 | +-------------------------------------------------------------+
As the minimum number of observations in panels is two (panel number 2), the balancing criterion makes that the result is a panel data set with only two observations per panel.
If V had only one observation in panel number 2,
: V 1 2 3 4 +---------------------+ 1 | 1 2 9.2 4.2 | 2 | 1 2 9.2 4.2 | 3 | 1 4 8.4 7 | 4 | 1 5 4.6 3.9 | 5 | 2 1 6.2 1.5 | 6 | 3 1 2.1 2.3 | 7 | 3 2 9 1.5 | 8 | 3 3 1.4 6.7 | +---------------------+
we would have
: mz_panrowshape(V, 1, "CK") 1 2 3 4 5 6 7 8 +-----------------------------------------------+ 1 | 1 2 9.2 4.2 3 1 2.1 2.3 | 2 | 1 3 2.5 6.5 3 2 9 1.5 | 3 | 1 4 8.4 7 3 3 1.4 6.7 | +-----------------------------------------------+
Panel number 2 is dropped because it is a singleton. See [M-5] panelsetup() for more details on balancing panel data sets with mata.
Case where f takes the value "KC"
: V 1 2 3 4 +---------------------+ 1 | 1 1 3.1 3.7 | 2 | 1 2 9.2 4.2 | 3 | 1 3 2.5 6.5 | 4 | 1 4 8.4 7 | 5 | 1 5 4.6 3.9 | 6 | 2 1 6.2 1.5 | 7 | 2 2 5 3.2 | 8 | 2 3 7.8 9.6 | 9 | 2 4 5.4 7.7 | 10 | 2 5 3.4 9.2 | 11 | 3 1 2.1 2.3 | 12 | 3 2 9 1.5 | 13 | 3 3 1.4 6.7 | 14 | 3 4 4.8 3.3 | 15 | 3 5 9.5 9.7 | +---------------------+
: mz_panrowshape(V, 1, "KC") 1 2 3 4 5 6 7 8 9 10 11 12 +-------------------------------------------------------------+ 1 | 1 2 3 1 1 1 3.1 6.2 2.1 3.7 1.5 2.3 | 2 | 1 2 3 2 2 2 9.2 5 9 4.2 3.2 1.5 | 3 | 1 2 3 3 3 3 2.5 7.8 1.4 6.5 9.6 6.7 | 4 | 1 2 3 4 4 4 8.4 5.4 4.8 7 7.7 3.3 | 5 | 1 2 3 5 5 5 4.6 3.4 9.5 3.9 9.2 9.7 | +-------------------------------------------------------------+ If we want to drop the first two columnns
: d = (1,2) : d : 1 2 +---------+ 1 | 1 2 | +---------+
: mz_panrowshape(V, 1, "KC", d) 1 2 3 4 5 6 +-------------------------------------+ 1 | 3.1 6.2 2.1 3.7 1.5 2.3 | 2 | 9.2 5 9 4.2 3.2 1.5 | 3 | 2.5 7.8 1.4 6.5 9.6 6.7 | 4 | 8.4 5.4 4.8 7 7.7 3.3 | 5 | 4.6 3.4 9.5 3.9 9.2 9.7 | +-------------------------------------+
: V 1 2 3 +-----------------+ 1 | A a11 a12 | 2 | A a21 a22 | 3 | A a31 a32 | 4 | B b11 b12 | 5 | B b21 b22 | 6 | B b31 b32 | 7 | C c11 c12 | 8 | C c21 c22 | 9 | C c31 c32 | +-----------------+
: d = 1 : d : 1
: mz_panrowshape(V, 1, "KC", d) 1 2 3 4 5 6 +-------------------------------------+ 1 | a11 b11 c11 a12 b12 c12 | 2 | a21 b21 c21 a22 b22 c22 | 3 | a31 b31 c31 a32 b32 c32 | +-------------------------------------+
Conformability
mz_panrowshape(V, idcol, f, d) V: (K * t) x c idcol: 1 x 1 f: 1 x 1 d: 1 x m, m < c (optional) result: u x (v * K) or u x (K * v), u <= t, v <= c, t = number of obs. in panel, K = number of panels
Diagnostics
mz_panrowshape() aborts with error if d is equal to all columns of V or if V is not a panel data matrix.
Source code
mz_panrowshape.mata
Author
Diallo Ibrahima Amadou, zavren@gmail.com
Also see
Online: help for [M-5] panelsetup(), mz_pancolshape(), ltimbimata