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

__Syntax__

Accumulate cross-product matrices to form X'X

__mat__**rix** __ac__**cum** *A* **=** *varlist* [*if*] [*in*] [*weight*] [**,** __nocons__**tant** __d__**eviations**
__m__**eans(***M***)** __abs__**orb(***varname***)**]

Accumulate cross-product matrices to form X'BX

__mat__**rix** __glsa__**ccum** *A* **=** *varlist* [*if*] [*in*] [*weight*]**,** __gr__**oup(***groupvar***)**
__gl__**smat(***W*|*stringvar***)** __r__**ow(***rowvar***)** [__nocons__**tant**]

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

__mat__**rix** **opaccum** *A* **=** *varlist* [*if*] [*in*] **,** __gr__**oup(***groupvar***)** __op__**var(***opvar***)**
[__nocons__**tant**]

Accumulate first variable against remaining variables

__mat__**rix** __veca__**ccum** *a* **=** *varlist* [*if*] [*in*] [*weight*] [**,** __nocons__**tant**]

*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.
**aweight**s, **fweight**s, **iweight**s, and **pweight**s 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'*B*X, 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 + ... + X*k*'e*k*e*k*'X*k*

where X*i* is a matrix of observations from the *i*th group of the *varlist*
variables and e*i* is a vector formed from the observations in the *i*th
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 **aweight**s) and
**matrix vecaccum** also store the sum of the weight in **r(sum_w)**, but **matrix**
**accum** does not.