*! version 1.2.2 15may2018

Title

submatrix -- Advanced Stata matrix subsetting, sorting and deleting.

Syntax

submatrix
A
[, options]

A is an existing Stata matrix

Main
*rownum(numlist) contains the numbers of the rows to be retained from A

*droprownum(numlist) contains the numbers of the rows to be dropped from A

*colnum(numlist) contains the numbers of the column to be retained from A

*dropcolnum(numlist) contains the numbers of the column to be dropped from A

*rownames(string) contains the names of the rows to be retained from A

*droprownames(string) contains the names of the rows to be dropped from A

*colnames(string) contains the names of the column to be retained from A

*dropcolnames(string) contains the names of the column to be dropped

rowvarlist specifies that arguments in droprownames and rownames are variables

colvarlist specifies that arguments in dropcolnames and colnames are variables

namesfirst return rows and columns based on their names first.

ignore ignore non existing row and columns names and numbers from the subsetting options (*) .

* subsetting options, duplicated arguments are allowed.

Description

submatrix returns subset of a Stata matrix based on row and columns names, numbers and equations.
Users can specify which row and/or column to keep or to drop.

Options

Main

rownames(string) controls the names of the rows to be retained from matrix A.
If string does not match any element of A
returns an error unless ignore is specified (see matrix_subscripting).
Double quotes may be used to enclose strings that contain spaces.

droprownames(string) controls the names of the rows to be dropped from matrix A.
If string does not match any element of A
returns an error unless ignore is specified (see matrix_subscripting).
Double quotes may be used to enclose strings that contain spaces.

colnames(string) controls the names of the columns to be retained from matrix A.
If string does not match any element of A
returns an error unless ignore is specified (see matrix_subscripting).
Double quotes may be used to enclose strings that contain spaces.

dropcolnames(string) controls the names of the columns to be dropped from matrix A.
If string does not match any element of A
returns an error unless ignore is specified (see matrix_subscripting).
Double quotes may be used to enclose strings that contain spaces.

rownum(numlist) controls the numbers of the rows to be retained from matrix A.
Exits with an error if any element of numlist is larger than the row number
of A unless ignore is specified (see matrix_subscripting).

droprownum(numlist) controls the numbers of the rows to be dropped from matrix A.
Exits with an error if any element of numlist is larger than the
row number of A unless ignore is specified (see matrix_subscripting).

colnum(numlist) controls the numbers of the columns to be retained from matrix A.
Exits with an error if any element of numlist is larger than the column
number of A unless ignore is specified (see matrix_subscripting).

dropcolnum(numlist) controls the numbers of the columns to be dopped from matrix A.
Exits with an error if any element of numlist is larger than the column
number of A unless ignore is specified (see matrix_subscripting).

colvarlist requests submatrix to treat the names in rownames
and droprownames as a varlist. This option enables factor variable
expansion and the use of the * character for matching
one or more characters.

rowvarlist requests submatrix to treat the names in colnames
and dropcolnames as a varlist. This option enables factor variable
expansion and the use of the * character for matching
one or more characters.

namesfirst prioritizes subsetting based on rownames and colnames rather than
using rownames and colnames first.

ignore requests submatrix to ignore any out of range element from A.
It affects rownames, droprownames, colnames, dropcolnames,rownum,
droprownum, colnum and dropcolnum. Using this option forces
submatrix to return a result anyway.

Example 1

Initializing matrix A

. matrix A=(1,3,4,6,7,8,10 \ 1,3,4,6,7,8,10)

Retaining columns 1, 5 and 7 (results in r(mat))

. submatrix A, colnum(1 5 7)

. matlist r(mat)

Same as above, using column names instead of numbers

. submatrix A, colnames(c1 c5 c7)

. matlist r(mat)

Dropping columns 2,3,4 and 6, using dropcolnum

. submatrix A, dropcolnum(2(1)4 6 )

. matlist r(mat)

Example 2

Loading data

. webuse nlswork, clear

A regression with a large number of coefficients and interactions

. xtreg ln_wage grade age c.age#c.age ttl_exp c.ttl_exp#c.ttl_exp tenure c.tenure#c.tenure 2.race not_smsa south i.year##(i.msp i.ind_code), be

Saving the result matrix

. matrix results=r(table)

Subsetting results using row and column names (note the use of colvarlist of factor variable notation to select all the interactions of .1.msp with .year)

. submatrix results, rownames(b pvalue) colnames(tenure c.tenure#c.tenure south i.year#1.msp) colvarlist

. matlist r(mat)', twidth(20)

Example 3

Loading data

. webuse sysdsn1, clear

Multinomial logit regression

. mlogit insure age male nonwhite i.site, base(3)

Saving the result matrix

. matrix b=e(b)

Selecting coefficients of age and male. for all equations but Uninsure (note the column operator).

. submatrix b, dropcolnames("Uninsure:") colnames(age male)

. matlist r(mat)

Resorting the columns by equation

. matrix b1=r(mat)

. submatrix b1, colnames( Indemnity: Prepaid:)

. matlist r(mat)

Stored results

submatrix stores the following in r():

Matrices

r(mat) the submatrix