## Stata 15 help for matrix accum

```
[P] matrix accum -- Form cross-product matrices

Syntax

Accumulate cross-product matrices to form X'X

matrix accum A = varlist [if] [in] [weight] [, noconstant deviations
means(M) absorb(varname)]

Accumulate cross-product matrices to form X'BX

matrix glsaccum A = varlist [if] [in] [weight], group(groupvar)
glsmat(W|stringvar) row(rowvar) [noconstant]

Accumulate cross-product matrices to form sum[(X_i)'e_i(e_i)'X_i]

matrix opaccum A = varlist [if] [in] , group(groupvar) opvar(opvar)
[noconstant]

Accumulate first variable against remaining variables

matrix vecaccum a = varlist [if] [in] [weight] [, noconstant]

varlist in matrix accum and in matrix vecaccum may contain factor
variables (except for the first variable in matrix vecaccum varlist);
see fvvarlist.
varlist may contain time-series operators; see tsvarlist.
aweights, fweights, iweights, and pweights are allowed; see weight.

Description

matrix accum accumulates cross-product matrices from the data to form A =
X'X.

matrix glsaccum accumulates cross-product matrices from the data by using
a specified inner weight matrix to form A = X'BX, where B is a block
diagonal matrix.

matrix opaccum accumulates cross-product matrices from the data by using
an inner weight matrix formed from the outer product of a variable in the
data to form

A = X1'e1e1'X1 + X2'e2e2'X2 + ... + Xk'ekek'Xk

where Xi is a matrix of observations from the ith group of the varlist
variables and ei is a vector formed from the observations in the ith
group of the opvar variable.

matrix vecaccum accumulates the first variable against the remaining
variables in varlist to form a row vector of accumulated inner products
to form a = x1'X, where X = (x2, x3,...).

Also see [M-5] cross() for other routines for forming cross-product
matrices.

Options

noconstant suppresses the addition of a "constant" to the X matrix.  If
noconstant is not specified, it is as if a column of 1s is added to X
before the accumulation begins.

deviations, allowed only with matrix accum, causes the accumulation to be
performed in terms of deviations from the mean.  If noconstant is not
specified, the accumulation of X is done in terms of deviations, but
the added row and column of sums are not in deviation format (in
which case they would be zeros).  With noconstant specified, the
resulting matrix divided through by N-1, where N is the number of
observations, is a covariance matrix.

means(M), allowed only with matrix accum, creates matrix M:  1 x (p+1) or
1 x p (depending on whether noconstant is also specified) containing
the means of X.

absorb(varname), allowed only with matrix accum, specifies that matrix
accum compute the accumulations in terms of deviations from the mean
within the absorption groups identified by varname.

group(groupvar) is required with matrix glsaccum and matrix opaccum and
is not allowed otherwise.  In the two cases where it is required, it
specifies the name of a variable that identifies groups of
observations.  The data must be sorted by groupvar.

In matrix glsaccum, groupvar identifies the observations to be
individually weighted by glsmat().

In matrix opaccum, groupvar identifies the observations to be
weighted by the outer product of opvar().

glsmat(W|stringvar), required with matrix glsaccum and not allowed
otherwise, specifies the name of the matrix or the name of a string
variable in the dataset that contains the name of the matrix that is
to be used to weight the observations in group().  stringvar must be
str8 or less.

row(rowvar), required with matrix glsaccum and not allowed otherwise,
specifies the name of a numeric variable containing the row numbers
that specify the row and column of the glsmat() matrix to use in the
inner-product calculation.

opvar(opvar), required with matrix opaccum, specifies the variable used
to form the vector whose outer product forms the weighting matrix.

Examples

. sysuse auto
. matrix accum A = price weight mpg
. matrix list A
. matrix accum Cov = price weight mpg, deviations noconstant
. matrix Cov = Cov/(r(N)-1)
. matrix list Cov

. webuse maccumxmpl
. xtdescribe, patterns(11)
. sort id t
. matrix opaccum B = x1 x2, group(id) opvar(e)

Stored results

matrix accum, matrix glsaccum, matrix opaccum, and matrix vecaccum store
the number of observations in r(N).  matrix accum stores the number of
absorption groups in r(k_absorb).  matrix glsaccum (with aweights) and
matrix vecaccum also store the sum of the weight in r(sum_w), but matrix
accum does not.

```