📄 mf_panelsetup.hlp
字号:
{p 4 4 2}
{cmd:panelsetup(}{it:V}{cmd:,} {it:idcol}{cmd:,} ...{cmd:)} sets up panel
processing, returning a {it:K} {it:x} 2 matrix that contains a row for each
panel. The row records the first and last observation numbers (row numbers
in {it:V}) that correspond to the panel.
{p 4 4 2}
For instance, using our example, {cmd:panelsetup()} will return
{c TLC}{c -} {c -}{c TRC}
{c |} 1 3 {c |}
{c |} 4 5 {c |}
{c |} 6 9 {c |}
{c |} . . {c |}
{c |} . . {c |}
{c |} . . {c |}
{c BLC}{c -} {c -}{c BRC}
{p 4 4 2}
The first panel is recorded in observations 1 to 3; it contains 3-1+1=3
observations. The second panel is recorded in observations 4 to 5 and
it contains 5-4+1=2 observations, and so on.
We recorded the third panel as being observations 6 to 9, although we
did not show you enough of the original data for you to know
that 9 was the last observation with ID 3.
{p 4 4 2}
{cmd:panelsetup()} has lots of additional capabilities in constructing this
result, but it is important to appreciate that returning this
observation-number matrix is all {cmd:panelsetup()} does. This matrix
is all that other panel functions need to know. They work with the
information produced by {cmd:panelsetup()}, but they will equally well work
with any two-column matrix that contains observation numbers.
Correspondingly, {cmd:panelsetup()} engages in no secret behavior that ties up
memory, puts you in a mode, or anything else. {cmd:panelsetup()} merely
produces this matrix.
{p 4 4 2}
The number of rows of the matrix {cmd:panelsetup()} returns equals {it:K}, the
number of panels.
{p 4 4 2}
The syntax of {cmd:panelsetup()} is
{p 8 12 2}
{it:info} =
{cmd:panelsetup(}{it:V}{cmd:,}
{it:idcol}{cmd:,}
{it:minobs}{cmd:,}
{it:maxobs}{cmd:)}
{p 4 4 2}
The last two arguments are optional.
{p 4 4 2}
The required argument {it:V} specifies a matrix containing
at least the panel identification numbers and required argument {it:idcol}
specifies the column of {it:V} that contains that ID. In our case,
we will use the matrix {it:V1}, which contains only the identification number:
{it:info} = {cmd:panelsetup(}{it:V1}{cmd:,} {cmd:1)}
{p 4 4 2}
The two optional arguments are {it:minobs} and {it:maxobs}. {it:minobs}
specifies the minimum number of observations within panel that we are willing
to tolerate; if a panel has fewer observations, we want to omit it
entirely. For instance, were we to specify
{it:info} = {cmd:panelsetup(}{it:V1}{cmd:,} {cmd:1, 3)}
{p 4 4 2}
then the matrix {cmd:panelsetup()} would contain
fewer rows. In our example, the returned {it:info} matrix
would contain
{c TLC}{c -} {c -}{c TRC}
{c |} 1 3 {c |}
{c |} 6 9 {c |}
{c |} . . {c |}
{c |} . . {c |}
{c |} . . {c |}
{c BLC}{c -} {c -}{c BRC}
{p 4 4 2}
Observations 4 and 5 are now omitted because they
correspond to a two-observation panel, and we said only panels with three or
more observations should be included.
{p 4 4 2}
We chose three as a demonstration. In fact, it is most common to code
{it:info} = {cmd:panelsetup(}{it:V1}{cmd:,} {cmd:1, 2)}
{p 4 4 2}
because that eliminates the singletons (panels with one observation).
{p 4 4 2}
The final optional argument is {it:maxobs}. For example,
{it:info} = {cmd:panelsetup(}{it:V1}{cmd:,} {cmd:1, 2, 5)}
{p 4 4 2}
means to include only up to five observations per panel. Any observations
beyond five are to be trimmed. If we code
{it:info} = {cmd:panelsetup(}{it:V1}{cmd:,} {cmd:1, 3, 3)}
{p 4 4 2}
then all the panels contained in {it:info} would have three observations.
If a panel had less than three observations, it would be omitted entirely.
If a panel had more than three observations, only the first three would be
included.
{p 4 4 2}
Panel datasets with the same number of observations per panel are said to be
balanced. {it:panelsetup()} also provides panel-balancing capabilities. If
you specify {it:maxobs} as 0, then
{p 8 12 2}
1. {cmd:panelsetup()} first calculates the min(T_i) among the panels with
{it:minobs} observations or more. Call that number {it:m}.
{p 8 12 2}
2. {cmd:panelsetup()} then returns
{cmd:panelsetup(}{it:V1}{cmd:,} {it:idcol}{cmd:,} {it:m}{cmd:,} {it:m}{cmd:)},
thus creating balanced panels of size {it:m} and producing a
dataset that has the maximum number of within-panel observations
given it has the maximum number of panels.
{p 4 4 2}
If we coded
{it:info} = {cmd:panelsetup(}{it:V1}{cmd:,} {cmd:1, 2, 0)}
{p 4 4 2}
then {cmd:panelsetup()} would
create the maximum number of panels with the maximum number of within-panel
observations subject to the constraint of no singletons and the panels being
balanced.
{title:Using panelstats()}
{p 4 4 2}
{cmd:panelstats(}{it:info}{cmd:)} can be used on any 2-column matrix that
contains observation numbers. {cmd:panelstats()} returns a rowvector
containing
{cmd:panelstats()[1]} = number of panels (same as {cmd:rows(}{it:info}{cmd:)})
{cmd:panelstats()[2]} = number of observations
{cmd:panelstats()[3]} = min({it:T}_{it:i})
{cmd:panelstats()[4]} = max({it:T}_{it:i})
{title:Using panelsubmatrix()}
{p 4 4 2}
Having created an {it:info} matrix using {cmd:panelsetup()}, you can
obtain the matrix corresponding to the {it:i}th panel using
{it:X} = {cmd:panelsubmatrix(}{it:V}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}
{p 4 4 2}
It is not necessary that {cmd:panelsubmatrix()} be used with the same
matrix that was used to produce {it:info}.
Recall that we created matrix {it:V1} containing the ID numbers, and
we created matrix {it:V2} containing the {it:x} variables
{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}
and we create {it:info} using {it:V1}:
{it:info} = {cmd:panelsetup(}{it:V1}{cmd:,} {cmd:1)}
{p 4 4 2}
We can now create the corresponding {it:X} matrix by coding
{it:X} = {cmd:panelsubmatrix(}{it:V2}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}
{p 4 4 2}
and, had we created a {it:V3} matrix corresponding to {it:Y}_{it:i}, we could
also code
{it:Y} = {cmd:panelsubmatrix(}{it:V3}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}
{p 4 4 2}
and so on.
{title:Using panelsubview()}
{p 4 4 2}
{cmd:panelsubview()} works very much like {cmd:panelsubmatrix()}. The
difference is, rather than coding
{it:X} = {cmd:panelsubmatrix(}{it:V}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}
{p 4 4 2}
you code
{cmd:panelsubview(}{it:X}{cmd:,} {it:V}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}
{p 4 4 2}
The matrix to be defined becomes the first argument of {cmd:panelsubview()}.
That is because {cmd:panelsubview()} is especially designed to work with
views. {cmd:panelsubmatrix()} will work with views, but {cmd:panelsubview()}
does something special. Rather than returning an ordinary matrix (an array,
in the jargon), if {it:V} is a view, {cmd:panelsubview()} returns a view
in its first argument.
Views save memory.
{p 4 4 2}
Views can save lots of memory, so it would seem that you would always
want to use {cmd:panelsubview()} in place of {cmd:panelsubmatrix()}.
What is not always appreciated, however, is that it takes Mata longer
to access the data recorded in views, and so there is a tradeoff.
{p 4 4 2}
If the panels are likely to be large, you want to use {cmd:panelsubview()}.
Conserving memory trumps all other considerations.
{p 4 4 2}
In fact, the panels that occur in most datasets are not that large, even
when the dataset itself is. If you are going to make lots of
calculations on {it:X}, you may wish to use {cmd:panelsubmatrix()}.
{p 4 4 2}
Understand that both {cmd:panelsubmatrix()} and {cmd:panelsubview()}
work with view and nonview matrices. {cmd:panelsubview()} produces a
regular matrix when the base matrix {it:V} is not a view just as does
{cmd:panelsubmatrix()}. The difference is that {cmd:panelsubview()}
will produce a view when {it:V} is a view, whereas {cmd:panelsubmatrix()}
always produces a nonview matrix.
{title:Conformability}
{p 4 4 2}
{cmd:panelsetup(}{it:V}{cmd:,}
{it:idcol}{cmd:,}
{it:minobs}{cmd:,}
{it:maxobs}{cmd:)}:
{p_end}
{it:V}: {it:r x c}
{it:idcol}: 1 {it:x} 1
{it:minobs}: 1 {it:x} 1
{it:maxobs}: 1 {it:x} 1
{it:result}: {it:K x} 2, {it:K} = number of panels
{cmd:panelstats(}{it:info}{cmd:)}:
{it:info}: {it:K x} 2
{it:result}: 1 {it:x} 4
{cmd:panelsubmatrix(}{it:V}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}:
{it:V}: {it:r x c}
{it:i}: 1 {it:x} 1, 1 <= {it:i} <= {cmd:rows(}{it:info}{cmd:)}
{it:info}: {it:K x} 2
{it:result}: {it:t x c}, {it:t} = number of obs. in panel
{cmd:panelsubview(}{it:SV}{cmd:,} {it:V}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}:
{it:input:}
{it:SV}: irrelevant
{it:V}: {it:r x c}
{it:i}: 1 {it:x} 1, 1 <= {it:i} <= {cmd:rows(}{it:info}{cmd:)}
{it:info}: {it:K x} 2
{it:result}: {it:t x c}, {it:t} = number of obs. in panel
{it:output:}
{it:SV}: {it:t x c}, {it:t} = number of obs. in panel
{title:Diagnostics}
{p 4 4 2}
{cmd:panelsubmatrix(}{it:V}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}
and
{cmd:panelsubview(}{it:SV}{cmd:,} {it:V}{cmd:,} {it:i}{cmd:,} {it:info}{cmd:)}
abort with error if {it:i}<1 or {it:i}>{cmd:rows(}{it:info}{cmd:)}.
{p 4 4 2}
Note that {cmd:panelsetup()} can return a 0 {it:x} 2 result.
{title:Source code}
{p 4 4 2}
{view panelsetup.mata, adopath asis:panelsetup.mata},
{view panelstats.mata, adopath asis:panelstats.mata},
{view panelsubmatrix.mata, adopath asis:panelsubmatrix.mata},
{view panelsubview.mata, adopath asis:panelsubview.mata}
{title:Also see}
{p 4 13 2}
Manual: {hi:[M-5] panelsetup()}
{p 4 13 2}
Online: help for
{bf:{help m4_utility:[M-4] utility}}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -