On Fri, Sep 30, 2022 at 9:44 AM Riccardo (Jack) Lucchetti
<p002264(a)staff.univpm.it> wrote:
On Wed, 28 Sep 2022, Cottrell, Allin wrote:
> As Sven has said, this case is more awkward than the (more common) case for
> which gretl's stack() function is designed. [...]
>
> Here's hansl code that does the job [...]
It's a matter of taste, I know, but some people may find a little
variation of Allin's script more readable: the loop
<hansl>
NT = N * T
matrix X1 = zeros(NT, nvars)
r0 = 0
loop i=1..N
printf "original row %d\n", i
col = 1
loop j=1..nvars
printf " variable %d\n", j
loop t=1..T
printf " observation %d\n", t
X1[r0+t,j] = X0[i,col]
col++
endloop
endloop
r0 += T
endloop
</hansl>
could be replaced with more compact (although probably not as transparent)
syntax as follows:
<hansl>
matrix X1 = {}
# loop over the rows of the original dataset and reshape them
# as needed
loop i=1..N
printf "original row %d\n", i
X1 = X1 | mshape(X0[i,], T, nvars)
endloop
</hansl>
No question about it, Jack's use of mshape() is vastly superior to the
version I posted. For the uninitiated, here's what the mshape call is
doing:
Take a row vector of length N*T, of the form "T observations for unit
i, variable 1; T observations for unit i, variable 2,..." and reshape
it into a T x nvars matrix, which is precisely what we want for block
i of the final panel dataset.
The only caveat I have in relation to Jack's variant is that it
resizes the X1 matrix (which starts off 0 x 0) for each of the N
units. If N is of moderate size that's not an issue, but for large N
it wouldn't be very efficient. An alternative, which creates the
full-size X1 then fills it in, is as follows:
X1 = zeros(NT, nvars)
r0 = 1
loop i=1..N
printf "original row %d\n", i
X1[r0:r0+T-1,] = mshape(X0[i,], T, nvars)
r0 += T
endloop
Allin