Thanks to Kit Baum, a new package -matrixof- is
now available on SSC for Stata 7 users.
In an up-to-date Stata, you can get a
capsule description by typing
. ssc desc matrixof
although if you are interested, what follows
says more. You can install by typing
. ssc inst matrixof
The -matrixof- package contains two commands,
-matrixof- and -vectorof-.
-matrixof- and -vectorof- are another take on a perennial
problem, how to get a lot of similar results
at once with as little typing (and as little
thinking) as possible.
Naturally, many commands and many constructs
in Stata have been offered to help here,
both as part of official Stata and as
-foreach- and -forvalues- are perhaps
the most general constructs, and although
it may not be obvious from this account,
-matrixof- and -vectorof- are merely
wrappers for various calls to -foreach-
and -forvalues-. As the names should
imply, they apply when the result needed
in one matrix or vector of numbers --
except that we stretch to doing this
also for a matrix or vector of graphs.
Enough preamble. Let's cut to some examples.
You are thinking correlations, but rank
correlations, and you check out the
syntax for -ktau- or -spearman-. Frustration,
as these niggardly commands will give you results
for only one pair of variables at time!
You could grit your teeth and type
commands one at a time, but you know
that Stata should be doing this for you.
You could brush up your -foreach-
and cycle through a set of variables,
once as rows and once as columns. You
could think in terms of putting results
in a matrix, because then -matrix list-
can be used to show the results concisely
and tidily. That suggests initialising
a matrix and then replacing each element
after a call to -ktau-. There are other
wrinkles to take care of, such as being
consistent when faced with missing values
in some variables but not others, and
so on. The code to do all this from
first principles is not long, but
-matrixof- can do it in one line:
. matrixof ktau price-foreign, r(tau_b) format(%4.3f)
We want a _matrix of_ results from -ktau-.
The matrix will pick up all the values from r(tau_b) and
we are going to display it with 3 decimal places.
Similarly, if we want variations on what
-correlate- provides, such as a different format
. matrixof correlate price-foreign, r(rho) format(%3.2f)
or listwise results
. matrixof correlate price-foreign, r(rho) format(%3.2f) listwise
. matrixof correlate price-foreign, r(N) listwise
then -matrixof- can help. (Note in passing
that other Stata programs exist for
some of these needs.)
Or suppose you want just one column of
a correlation matrix, natural given
some possible covariates and one
response variable. This could be
a job for -vectorof- as the
results compose a vector
. vectorof correlate price-foreign, r(rho) y(mpg) format(%4.3f)
Note here the use of a -y()- option:
we imagine substituting -mpg- as
a variable paired with each of -price-foreign-.
Similarly, we might want just a block
of a larger correlation matrix.
Imagine calculating principal components
. factor x1-x6, pc
. score pc1-pc6
after which we should want to look
at the correlations between the original
variables and the PCs.
. correlate x1-x6 pc1-pc6
is likely to seem unwieldy -- and the
matrix for a larger number of variables
and components will surely seem
worse. Also, there is no scientific
interest in seeing that different
components have correlation zero,
although at one level it is reassuring.
The result needed is
. matrixof correlate x1-x6, cols(pc1-pc6) r(rho) format(%4.3f)
The idea is that the variable list supplied
with this syntax defines the rows of the matrix
and that supplied by -cols()- defines the columns.
When not asked to pick up an rclass result,
or an eclass result, or a global result, left
in its wake by some command, -matrixof-
and -vectorof- expect to be doing graphics.
Say you want to _compare_ graphs, for which it
helps to be able to see the graphs
within one window. The classic way to
do this in Stata is to save the
graphs in files g1, g2, g3, ... and then type
. graph using g1 g2 g3 ...
and unless you have a monitor half the size
of Texas you might find it helpful to
change the text size first:
. set textsize 140
-vectorof- will let you see a series of univariate graphs
all at once
. vectorof dotplot price-displacement
. vectorof quantile price-displacement
and you can also use -vectorof-
to get bivariate graphs with the same x variable
. vectorof graph arithmetic algebra geometry calculus, x(age)
or indeed the same y variable, using the -y()- option.
-matrixof- allows a display of pairwise graphs.
Bug reports to me please.
* For searches and help try: