help mata mm_colvar()-------------------------------------------------------------------------------

Title

mm_colvar() -- Variance, by column

Syntax

real rowvectormm_colvar(X[,w])

real matrixmm_meancolvar(X[,w])

real matrixmm_variance0(X[,w])

real matrixmm_meanvariance0(X[,w])

real matrixmm_mse(X,w,mu)

real rowvectormm_colmse(X,w,mu)

real matrixmm_sse(X,w,mu)

real rowvectormm_colsse(X,w,mu)

where

X:real matrix X(rows are observations, columns variables)

w:real colvector w

mu:real rowvector mu

Description

mm_colvar(X,w)returns the variance of each column ofX. Essentially,

mm_colvar(X,w)=diagonal(variance(X,w))'See help for

mean(). However,mm_colvar()does not compute the covariances and is therefore much faster thandiagonal(variance())ifXcontains more than one column. Furthermore, note thatmm_colvar()omits missing values inXby column, whereasvariance()omits missing values casewise.

mm_meancolvar(X,w)returns a matrix containing the mean and the variance of each column ofX. (Means in row one, variances in row two.)

mm_variance0(X,w)returns the population variance matrix ofX.mm_variance0()differs from official Stata'svariance()(see help formean()) in that it divides the deviation cross products by N instead of N-1, where N is the number of observations. Essentially,

mm_variance0(X,w)=variance(X,w)* (N-1)/NHowever,

mm_variance0()also produces correct results if N==1.

mm_meanvariance0(X,w)returnsmean(X,w)\mm_variance0(X,w).

mm_mse(X,w,mu)computes the mean squared errors matrix, where errors are defined asX:-mu.

mm_colmse()computes mean squared errors by column.

mm_sse()andmm_colsse()compute the sum of squared errors.

wspecifies the weights. Specifywas 1 to obtain unweighted results.

RemarksExamples for

mm_colvar():: x = invnormal(uniform(10000,3)) : mm_colvar(x, 1) 1 2 3 +-------------------------------------------+ 1 | 1.00018384 1.002621747 1.003480729 | +-------------------------------------------+

: mm_meancolvar(x, 1) 1 2 3 +----------------------------------------------+ 1 | -.0024994158 -.0091972878 -.0035865732 | 2 | 1.00018384 1.002621747 1.003480729 | +----------------------------------------------+

The formula implemented in

mm_mse()andmm_colmse()computes the mean squared error as the sum of squared errors divided by N, where N is the number of observations (or sum of weights ifw!=1).

Conformability

mm_colvar(X,w):X:n x kw:n x 1or1 x 1result:1 x k

mm_meancolvar(X,w):X:n x kw:n x1 or 1x1result: 2xk

mm_variance0(X,w):X:n x kw:n x 1or1 x 1result:k x k

mm_meanvariance0(X,w):X:n x kw:n x1 or 1x1result: (k+1)xk

mm_mse(X,w,mu),mm_sse(X,w,mu):X:n x kw:n x 1or1 x 1mu: 1x kresult:k x k

mm_colmse(X,w,mu),mm_colsse(X,w,mu):X:n x kw:n x1 or 1x1mu: 1x kresult: 1xk

Diagnostics

mm_variance0(),mm_meanvariance0(),mm_mse(), andmm_sse()omit from calculation rows ofXorwthat contain missing values (casewise deletion). If all rows contain missing values, then the returned result contains all missing values.Contrarily,

mm_colvar(),mm_meancolvar(),mm_colmse(), andmm_colsse()omit missing values by column (i.e. not casewise).

Source codemm_colvar.mata, mm_variance0.mata, mm_mse.mata

AuthorBen Jann, ETH Zurich, jann@soz.gess.ethz.ch

Also seeOnline: help for

[M-5] mean(),moremata