*! version 0.31

Title

matrix2stata -- converts roweq, rownames and content from a matrix into
Stata variables

Syntax

matrix2stata
matrixname
[,
options]

Main
clear This option perform the command clear before
creating variables from the matrix.

ziprows Orders and merges roweq and rownames into one label
column/variable.

zipsettings(string) A set of settings for the zip layout.

Option bold makes the equation values bold.

Description

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 matrices might have been generated from sumat.

Examples

To use as an alternative to collapse

`"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"'

Generate a matrix of means and CI of price, mpg, headroom, trunk, weight,
turn and displacement by the values of rep78 (number of repairs):

`"sumat price mpg headroom trunk weight turn displacement, statistics(mean ci) rowby(rep78)"'

Send the matrix to the data editor and clear the current dataset:

`"matrix2stata r(sumat), clear"'

To use as a base for a CI plot

`"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"'

Generate a matrix of mean and CI for price by rep78 (number of repairs)
and foreign (Origin of the car):

`"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)"'

Send the matrix to the data editor and prepare the matrix data for the
CI plot:

`"matrix2stata out, clear"'

`"strofnum out_names"'

`"replace out_names = subinstr(out_names, ", Price", "",.)"'

`"strtonum out_names"'

`"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""'

Make the CI plot without being limited to a subset of the
functionality of twoway:

`"twoway (scatter out_names out_mean, mlabel(lbl) mlabsize(vsmall) mlabposition(12)) (rcap out_ci95__lb out_ci95__ub out_names, hor), 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)"'

CI plots can also be made using the ziprows option. First data is generate again

`"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"'

Generate a matrix of mean and CI for price by rep78 (number of repairs)
and foreign (Origin of the car):

`"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)"'

Send the matrix to the data editor and prepare the matrix data for the
CI plot using the ziprows option:

`"matrix2stata out, clear ziprows"'

Generate labels and the Ci plot:

`"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)"'

Stored results

matrix2stata stores the following in r():

local

r(variable_names) The created variable names. If the matrix does
not exist " " is returned.

To see more examples

Authors and support

Author:

Niels Henrik Bruun,

Aalborg University Hospital

Support:

niels.henrik.bruun@gmail.com