What is -matrix2stata-

The command -matrix2stata- converts roweq, rownames and content from a matrix into variables. This makes it eg possible to do graphs of the content of one or more matrices. The difference from -svmat- is that it adds roweq and rownames as variables for grouping.

The matrices might have been generated from -sumat-.

-matrix2stata- is a part of the package matrixtools.

Syntax

The syntax is: matrix2stata matrixname [, options]

Here "matrixname" is the matrix to convert.

The styled matrix can be save as file using the using modifier.

Options

Stored results

-matrix2stata- stores the following in r():

local

Versions

-matrix2stata- is tested in version 12.1 ic, 13.1 ic, and 14.2 ic.

Installation

To install use the command: ssc install matrixtools

A demonstration of -matrix2stata-

The dataset

The dataset in this example is auto with a value label:


sysuse auto, clear
label define rep78 1 "1 repair" 2 "2 repairs" 3 "3 repairs" 4 "4 repairs" 5 "5 repairs"
label values rep78 rep78

The command -sumat- is used to generate a matrix with some content to plot:


sumat price  if foreign == "Foreign":origin, statistics(mean ci) rowby(rep78) roweq(Foreign) full
matrix out = r(sumat)
sumat price  if foreign == "Domestic":origin, statistics(mean ci) rowby(rep78) roweq(Domestic) full
matrix out = out \ r(sumat)

And the matrix looks like


matprint out

--------------------------------------------------------------
                                      mean  ci95% lb  ci95% ub
--------------------------------------------------------------
Foreign   rep78(1 repair), Price         .         .         .
          rep78(2 repairs), Price        .         .         .
          rep78(3 repairs), Price  4828.67   3373.89   6283.45
          rep78(4 repairs), Price  6261.44   5022.69   7500.20
          rep78(5 repairs), Price  6292.67   4485.82   8099.51
Domestic  rep78(1 repair), Price   4564.50   3840.29   5288.71
          rep78(2 repairs), Price  5967.63   3487.30   8447.95
          rep78(3 repairs), Price  6607.07   5226.06   7988.09
          rep78(4 repairs), Price  5881.56   4841.46   6921.66
          rep78(5 repairs), Price  4204.50   3772.33   4636.67
--------------------------------------------------------------

Now -matrix2stata- is used to move the content of the matrix out to the dataset. The option clear clears the dataset before inserting the matrix.


matrix2stata out, clear

The command -matrix2stata- returns the following stored results:


display "`:r(macros)'"

variable_names

display "`r(variable_names)'"

out_eq out_names out_mean out_ci95__lb out_ci95__ub

The current dataset now looks like:


list

     +------------------------------------------------------------------------+
     |   out_eq                 out_names    out_mean   out_ci~lb   out_ci~ub |
     |------------------------------------------------------------------------|
  1. |  Foreign    rep78(1 repair), Price           .           .           . |
  2. |  Foreign   rep78(2 repairs), Price           .           .           . |
  3. |  Foreign   rep78(3 repairs), Price   4828.6667   3373.8855   6283.4478 |
  4. |  Foreign   rep78(4 repairs), Price   6261.4444   5022.6874   7500.2015 |
  5. |  Foreign   rep78(5 repairs), Price   6292.6667   4485.8221   8099.5113 |
     |------------------------------------------------------------------------|
  6. | Domestic    rep78(1 repair), Price      4564.5   3840.2933   5288.7067 |
  7. | Domestic   rep78(2 repairs), Price    5967.625   3487.3029   8447.9471 |
  8. | Domestic   rep78(3 repairs), Price   6607.0741   5226.0617   7988.0865 |
  9. | Domestic   rep78(4 repairs), Price   5881.5556   4841.4554   6921.6557 |
 10. | Domestic   rep78(5 repairs), Price      4204.5   3772.3279   4636.6721 |
     +------------------------------------------------------------------------+

The variable name Price appears unnecessary in the variable out_names. This is remedied by the utility commands -strofnum- and -strtonum-:


strofnum out_names
replace out_names = subinstr(out_names, ", Price", "",.)
strtonum out_names

To create a CI plot

To add estimate and CI as labels a marker label variable is made and some labelling is done:


generate lbl = string(out_mean, "%6.0f") + " (" + string(out_ci95__lb, "%6.0f") + "; " + string( out_ci95__ub, "%6.0f") + ")"
label variable out_eq "Origin"
label variable out_names "Repair record 1978"

Now the graph can be generated:


twoway  (scatter out_names out_mean, mlabel(lbl) mlabsize(vsmall) mlabposition(12)) ///
                (rcap out_ci95__lb out_ci95__ub out_names, horizontal) ///
                , yscale(range(.5 5.5)) ///
                ylabel(1(1)5, angle(zero) valuelabel) ///
                by(out_eq, legend(off) cols(1)) ytitle(Mean price and 95% CI) name(g1, replace)

A CI plot example 1

To create a slightly different CI plot

An alternative using the option ziprows is:


sysuse auto, clear
label define rep78 1 "1 repair" 2 "2 repairs" 3 "3 repairs" 4 "4 repairs" 5 "5 repairs"
label values rep78 rep78
sumat price  if foreign == "Foreign":origin, statistics(mean ci) rowby(rep78) roweq(Foreign) full
matrix out = r(sumat)
sumat price  if foreign == "Domestic":origin, statistics(mean ci) rowby(rep78) roweq(Domestic) full
matrix out = out \ r(sumat)
matrix2stata out, clear ziprows

Now the dataset looks like:


list

     +------------------------------------------------+
     | out_rowe~s    out_mean   out_ci~lb   out_ci~ub |
     |------------------------------------------------|
  1. |    Foreign           .           .           . |
  2. | rep78(1 re           .           .           . |
  3. | rep78(2 re           .           .           . |
  4. | rep78(3 re   4828.6667   3373.8855   6283.4478 |
  5. | rep78(4 re   6261.4444   5022.6874   7500.2015 |
     |------------------------------------------------|
  6. | rep78(5 re   6292.6667   4485.8221   8099.5113 |
  7. |   Domestic           .           .           . |
  8. | rep78(1 re      4564.5   3840.2933   5288.7067 |
  9. | rep78(2 re    5967.625   3487.3029   8447.9471 |
 10. | rep78(3 re   6607.0741   5226.0617   7988.0865 |
     |------------------------------------------------|
 11. | rep78(4 re   5881.5556   4841.4554   6921.6557 |
 12. | rep78(5 re      4204.5   3772.3279   4636.6721 |
     +------------------------------------------------+

And the graph is generated by:


generate lbl = string(out_mean, "%6.0f") + " (" + string(out_ci95__lb, "%6.0f") + "; " + string( out_ci95__ub, "%6.0f") + ")"
twoway      (scatter out_roweqnames out_mean, mlabel(lbl) mlabsize(vsmall) mlabposition(12)) ///
                (rcap out_ci95__lb out_ci95__ub out_roweqnames, horizontal) ///
                , ylabel(1(1)6 8(1)13, angle(zero) valuelabel) ///
                legend(off) ytitle(Mean price and 95% CI) name(g2, replace)

A CI plot example 2


The do file for this document

Last update: 2017-06-03, Stata version 14.2