Stata 15 help for mkmat

[P] matrix mkmat -- Convert variables to matrix and vice versa

Syntax

Create matrix from variables

mkmat varlist [if] [in] [, matrix(matname) nomissing rownames( varname) roweq(varname) rowprefix(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;

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

3) arbitrary names when the explicit option is specified.

To reset problem-size limits, see [R] matsize.

Menu

mkmat

Data > Matrices, ado language > Convert variables to matrix

svmat

Data > Matrices, ado language > Convert matrix to variables

Description

mkmat 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, ....

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(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.

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

Create matrix X with columns consisting of the values for foreign, weight, and displacement . mkmat foreign weight displacement, matrix(X)

List the contents of matrix X . matrix list X

Create vector b . matrix b = invsym(X'*X) * X'*mpg

Run a linear regression . regress mpg foreign weight displacement, noconstant

Create matrix c containing the transpose of the coefficient vector . matrix c = e(b)'

List 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

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

Create vector f by appending vector c to the end of vector b . matrix f = b\c

Rename row two of f "wgt", where "wgt" does not currently exist as a variable . matname f wgt, rows(2) explicit

List the contents of vector f . matrix list f

Correspondence analysis of indicator matrix Setup . webuse ca_smoking, clear

Create indicator variables S1 and S2 for smoking . tab smoking, gen(S)

Create indicator variables for rank . tab rank, gen(R)

Create matrix ISR with columns consisting of the values of the indicator variables . mkmat S* R*, matrix(ISR)

List the contents of matrix ISR . mat list ISR

Perform simple correspondence analysis on ISR . camat ISR, dim(3)


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index