*! version 1.1.12 15may2018

Title

mkmatfv -- Version of mkmat which accepts factor variables

Syntax

Create matrix from variables

mkmatfv varlist [if] [in]
[, matrix:(matname) nomissing
rownames(varname)
roweq(varname)
rowprefix(strings:string)
obs
nchar(#)
*
]

Create variables from matrix

svmat [type] A [,
names:(col|eqcol|matcol|string)]

Rename rows and columns of matrix

matname A namelist [,
rows:(range) columns:(range)
explicit]

where A is the name of an existing matrix, type is a storage type
for new variables, and namelist is one of

1) a varlist, that is, names of existing variables possibly
abbreviated, may include factor variables;

2) _cons and the names of existing variables possibly
abbreviated;

3) arbitrary names when the explicit option is specified.

Menu

mkmat

User contributed, and therefore not on Stata's menu

svmat

Data > Matrices, ado language > Convert matrix to variables

Description

mkmatfv stores the variables listed in varlist in column vectors of
the same name, that is, N x 1 matrices, where N = _N, the number of
observations in the dataset. Optionally, they can be stored as an N x k
matrix, where k is the number of variables in varlist. The variable
names are used as column names. By default, the rows are named r1,
r2, ....

In addition to the functionality of mkmat, mkmatfv accommodates
Stata's factor variables by expanding the variable list using
fvrevar. In addition to mkmat's options, mkmatfv
also accepts fvrevar options.

svmat takes a matrix and stores its columns as new variables. It is
the reverse of the mkmat command, which creates a matrix from existing
variables.

matname renames the rows and columns of a matrix. matname
differs from the matrix rownames and matrix colnames commands in
that matname expands varlist abbreviations and allows a
restricted range for the rows and columns. See
[P] matrix rownames.

Options

matrix(matname) requests that the vectors be combined
in a matrix instead of creating the column vectors.

nomissing specifies that observations with missing values in any of
the variables be excluded ("listwise deletion").

rownames(varname) and roweq(varname)
specify that the row names and row equations of the created matrix or
vectors be taken from varname. varname should be a string
variable or an integer positive-valued numeric variable. (Value labels
are ignored; use decode if you want to use value labels.) Within
the names, spaces and periods are replaced by an underscore (_).

rowprefix(strings:string) specifies that the string string be
prefixed to the row names of the created matrix or column vectors. In the
prefix, spaces and periods are replaced by an underscore (_). If
rownames() is not specified, rowprefix() defaults to r, and
to nothing otherwise.

obs specifies that the observation numbers be used as row names.
This option may not be combined with rownames().

nchar(#) specifies that row names be truncated to # characters,
1<=#<=32. The default is nchar(32).

names(col|eqcol|matcol|string)
specifies how the new variables are to be named.

names(col) uses the column names of the matrix to name the variables.

names(eqcol) uses the equation names prefixed to the column names.

names(matcol) uses the matrix name prefixed to the column names.

names(string) names the variables
string1, string2, ..., stringn, where string
is a user-specified string and n is the number of columns of the
matrix.

If names() is not specified, the variables are named
A1, A2, ..., An, where A is the name of the
matrix.

rows(range) and columns(range)
specify the rows and columns of the matrix to rename. The number of rows
or columns specified must be equal to the number of names in
namelist. If both rows() and columns() are given, the
specified rows are named namelist, and the specified columns are also
named namelist. The range must be given in one of the following forms:

rows(.) renames all the rows

rows(2..8) renames rows 2 through 8

rows(3) renames only row 3

rows(4...) renames row 4 to the last row

If neither rows() nor columns() is given,
rows(.) columns(.) is the default. That is, the matrix must
be square, and both the rows and the columns are named namelist.

explicit suppresses the expansion of varlist abbreviations
and omits the verification that the names are those of existing variables.
That is, the names in namelist are used explicitly and can be any valid
row or column names.

* Additional options passed to help fvrevar}. See fvrevar
for a description of those options.

Remarks on mkmat

Although cross-products of variables can be loaded into a matrix with the
matrix accum command, programmers may sometimes find it more
convenient to work with the variables in their datasets as vectors instead of
as cross-products. mkmat allows the user a simple way to load specific
variables into matrices in Stata's memory.

Examples

Setup

. sysuse auto

Store mpg in column vector mpg

. mkmat mpg

List the contents of vector mpg

. matrix list mpg

Use mkmat to create matrix X1 with columns consisting of the values for
foreign, weight, and displacement

. mkmat foreign weight displacement, matrix(X)

List the contents of matrix X1

. matrix list X1

Use mkmatfv to create matrix X2 with columns consisting of the values for
the interactions among
foreign and weight as well as for the variable displacement

. mkmatfv i.foreign##c.weight displacement, matrix(X2)

Compare the list of the contents of X2 to those of X1

. matrix list X2

Create and list vector b

. matrix b = invsym(X2'*X2) * X2'*mpg

. matrix list b

Use matname to apply rownames to b

. matname b domestic foreign, rows(1..2) explicit

. matname b dom_x_wgt for_x_wgt, rows(4..5) explicit

. matrix list b

Run a linear regression using the factor variable syntax

. regress mpg i.foreign##c.weight displacement, noconstant

Create matrix c containing the transpose of the coefficient
vector

. matrix c = e(b)'

Compare the vectors b and c

. matrix list b

. matrix list c

Create matrix D, where the first column contains vector
b

. matrix D = b, c

List the contents of matrix D

. matrix list D

Use svmat to save the columns of D as variables in the dataset with names
reg1 and reg2

. svmat D, names(reg)

List the result

. list make price reg1 reg2 in 1/5

Authors

This help file is a slightly edited copy of the help file for Stata's mkmat command.

Mead Over added factor-variable
functionality in Aug, 2017 & June, 2021. Contact him at:
Email: MOver@CGDev.Org if you observe any
problems.