📄 mf_panelsetup.hlp
字号:
{smcl}
{* 28mar2005}{...}
{cmd:help mata panelsetup()}
{hline}
{* index panelsetup() tt}{...}
{* index panelstats() tt}{...}
{* index panelsubmatrix() tt}{...}
{* index panelsubview() tt}{...}
{* index panel data}{...}
{title:Title}
{p 4 4 2}
{bf:[M-5] panelsetup() -- Panel-data processing}
{title:Syntax}
{p 16 16 2}
{it:info} =
{cmd:panelsetup(}{it:V}{cmd:,}
{it:idcol}{cmd:)}
{p 16 16 2}
{it:info} =
{cmd:panelsetup(}{it:V}{cmd:,}
{it:idcol}{cmd:,}
{it:minobs}{cmd:)}
{p 16 16 2}
{it:info} =
{cmd:panelsetup(}{it:V}{cmd:,}
{it:idcol}{cmd:,}
{it:minobs}{cmd:,}
{it:maxobs}{cmd:)}
{p 8 8 2}
{it:real rowvector}
{cmd:panelstats(}{it:info}{cmd:)}
{p 8 8 2}
{it:real matrix}{bind: }
{cmd:panelsubmatrix(}{it:V}{cmd:,}
{it:i}{cmd:,}
{it:info}{cmd:)}
{p 8 8 2}
{it:void}{bind: }
{cmd:panelsubview(}{it:SV}{cmd:,}
{it:V}{cmd:,}
{it:i}{cmd:,}
{it:info}{cmd:)}
{p 4 4 2}
where,
{it:V}: {it:real} or {it:string} {it:matrix}, possibly a view
{it:idcol}: {it:real scalar}
{it:minobs}: {it:real scalar}
{it:maxobs}: {it:real scalar}
{it:info}: {it:real matrix}
{it:i}: {it:real scalar}
{it:SV}: {it:matrix} to be created, possibly as view
{title:Description}
{p 4 4 2}
These functions assist with the processing of panel data. The idea is to
make it easy and fast to write loops like
{cmd:for (}{it:i}{cmd:=1;} {it:i}{cmd:<=}{it:number_of_panels}{cmd:;} {it:i}{cmd:++) {c -(}}
{it:X} {cmd:=} {it:matrix corresponding to panel i}
...
...{it:(calculations using X)}...
...
{cmd:{c )-}}
{p 4 4 2}
Using these functions, this loop could become
{cmd}st_view(Vid, ., "idvar", "touse")
st_view(V, ., ("x1", "x2"), "touse")
info = panelsetup(Vid, 1)
for (i=1; i<=rows(info); i++) {
X = panelsubmatrix(V, i, info)
{txt}...{cmd}
{txt}...{it:(calculations using} {cmd:X}{it:)}{txt}...{cmd}
{txt}...{cmd}
}{txt}
{p 4 4 2}
{cmd:panelsetup(}{it:V}{cmd:,} {it:idcol}{cmd:,} ...{cmd:)} sets up panel
processing. It returns a matrix ({it:info}) which is passed to other
panel-processing functions.
{p 4 4 2}
{cmd:panelstats(}{it:info}{cmd:)} returns a row vector
containing the number of panels, number of observations, minimum number of
observations per panel, and maximum number of observations per panel.
{p 4 4 2}
{cmd:panelsubmatrix(}{it:V}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}
returns a matrix containing the contents of {it:V} for panel {it:i}.
{p 4 4 2}
{cmd:panelsubview(}{it:SV}{cmd:,} {it:V}{cmd:,} {it:i}{cmd:,}
{it:info}{cmd:)}
does nearly the same thing. Rather than returning a matrix, however, it
places the matrix in {it:SV}. If {it:V} is a view, then the matrix placed
in {it:SV} will be a view.
{title:Remarks}
{p 4 4 2}
Remarks are presented under the headings
{bf:Definition of panel data}
{bf:Definition of problem}
{bf:Preparation}
{bf:Use of panelsetup()}
{bf:Use of panelstats()}
{bf:Using panelsubmatrix()}
{bf:Using panelsubview()}
{title:Definition of panel data}
{p 4 4 2}
Panel data includes multiple observations on subjects, countries, etc.:
subject ID "time" ID x1 x2
{hline 43}
1 1 4.2 3.7
1 2 3.2 3.7
1 3 9.2 4.2
2 1 1.7 4.0
2 2 1.9 5.0
3 1 9.5 1.3
. . . .
. . . .
{hline 43}
{p 4 4 2}
In the above dataset, there are three observations for subject 1, two for
subject 2, etc. We labeled the identifier within subject to be time, but that
is only suggestive, and in any case, the secondary identifier will play no
role in what follows.
{p 4 4 2}
If we speak about the first panel, we are discussing the first 3 observations
of this dataset. If we speak about the second, that corresponds to
observations 4 and 5.
{p 4 4 2}
It is common to refer to panel numbers using the letter {it:i}.
It is common to refer to the number of observations in the {it:i}th panel
as {it:T}_{it:i} even when the data within panel has nothing to do with
repeated observation over time.
{title:Definition of problem}
{p 4 4 2}
We want to calculate some statistic on panel data.
The calculation amounts to
K
Sum {it:f}({it:X}_{it:i})
i=1
{p 4 4 2}
where the sum is performed across panels, and {it:X}_{it:i} is the
data matrix for panel {it:i}. For instance, given the example in
the previous section
{c TLC}{c -} {c -}{c TRC}
{c |} 4.2 3.7 {c |}
{it:X}_1 = {c |} 3.2 3.7 {c |}
{c |} 9.2 4.2 {c |}
{c BLC}{c -} {c -}{c BRC}
{p 4 4 2}
and {it:X}_{it:2} is a similarly constructed 2 {it:x} 2 matrix.
{p 4 4 2}
Depending on the nature of the calculation, there will be problems for
which
{p 8 12 2}
1. We want to use all the panels.
{p 8 12 2}
2. We want to use only panels for which there are two or more
observations.
{p 8 12 2}
3. We want to use the same number of observations in all the panels
(balanced panels).
{p 4 4 2}
In addition to simple problems of the sort
K
Sum {it:f}({it:X}_{it:i})
i=1
{p 4 4 2}
you may also need to deal with problems of the form
K
Sum {it:f}({it:X}_{it:i}, {it:Y}_{it:i}, ...)
i=1
{p 4 4 2}
That is, you may need to deal with problems where there are multiple matrices
per subject.
{p 4 4 2}
We use the sum operator purely for illustration, although it is the most
common. Your problem might be
{it:F}({it:X}_1, {it:Y}_1, ..., {it:X}_2, {it:Y}_2, ..., ...)
{title:Preparation}
{p 4 4 2}
Before using the functions documented here, create a matrix or
matrices containing the data. For illustration, it will be sufficient to
create {it:V} containing all the data in our example problem:
{c TLC}{c -} {c -}{c TRC}
{c |} 1 2 4.2 3.7 {c |}
{c |} 1 2 3.2 3.7 {c |}
{c |} 1 3 9.2 4.2 {c |}
{c |} 2 1 1.7 4.0 {c |}
{it:V} = {c |} 2 2 1.9 5.0 {c |}
{c |} 3 1 9.5 1.3 {c |}
{c |} . . . . {c |}
{c |} . . . . {c |}
{c BLC}{c -} {c -}{c BRC}
{p 4 4 2}
But you will probably find it more convenient (and we recommend) if you
create at least two matrices, one containing the subject identifier
and the other containing the {it:x} variables (and omit the within-subject
"time" identifier altogether):
{c TLC}{c -} {c -}{c TRC} {c TLC}{c -} {c -}{c TRC}
{c |} 1 {c |} {c |} 4.2 3.7 {c |}
{c |} 1 {c |} {c |} 3.2 3.7 {c |}
{c |} 1 {c |} {c |} 9.2 4.2 {c |}
{c |} 2 {c |} {c |} 1.7 4.0 {c |}
{it:V1} = {c |} 2 {c |} {it:V2} = {c |} 1.9 5.0 {c |}
{c |} 3 {c |} {c |} 9.5 1.3 {c |}
{c |} . {c |} {c |} . . {c |}
{c |} . {c |} {c |} . . {c |}
{c BLC}{c -} {c -}{c BRC} {c BLC}{c -} {c -}{c BRC}
{p 4 4 2}
In the above, matrix {it:V1} contains the subject identifier, and
matrix {it:V2} contains the data for all the {it:X}_{it:i} matrices in
K
Sum {it:f}({it:X}_{it:i})
i=1
{p 4 4 2}
If your calculation is
K
Sum {it:f}({it:X}_{it:i}, {it:Y}_{it:i}, ...)
i=1
{p 4 4 2}
create additional {it:V} matrices, {it:V3} corresponding to {it:Y}_{it:i},
and so on.
{p 4 4 2}
To create these matrices, use {bf:{help mf_st_view:[M-5] st_view()}}
{cmd:st_view(}{it:V1}{cmd:, ., "idvar", "}{it:touse}{cmd:")}
{cmd:st_view(}{it:V2}{cmd:, ., ("x1", "x2"), "}{it:touse}{cmd:")}
{p 4 4 2}
although you could use {bf:{help mf_st_data:[M-5] st_data()}} if
you preferred. Using {cmd:st_view()} will save memory. Alternatively,
you can construct {it:V1}, {it:V2}, ..., however you wish; they are just
matrices. Be sure that the matrices align, for example, that row 4 of one
matrix corresponds to row 4 of another. We did that above by assuming a
{it:touse} variable had been included (or constructed) in the dataset.
{title:Use of panelsetup()}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -