Stata 15 help for drawnorm

[D] drawnorm -- Draw sample from multivariate normal distribution

Syntax

drawnorm newvarlist [, options]

options Description ------------------------------------------------------------------------- Main clear replace the current dataset double generate variable type as double; default is float n(#) generate # observations; default is current number sds(vector) standard deviations of generated variables corr(matrix|vector) correlation matrix cov(matrix|vector) covariance matrix cstorage(full) store correlation/covariance structure as a symmetric k*k matrix cstorage(lower) store correlation/covariance structure as a lower triangular matrix cstorage(upper) store correlation/covariance structure as an upper triangular matrix forcepsd force the covariance/correlation matrix to be positive semidefinite means(vector) means of generated variables; default is means(0)

Options seed(#) seed for random-number generator -------------------------------------------------------------------------

Menu

Data > Create or change data > Other variable-creation commands > Draw sample from normal distribution

Description

drawnorm draws a sample from a multivariate normal distribution with desired means and covariance matrix. The default is orthogonal data with mean 0 and variance 1. The covariance matrix may be singular. The values generated are a function of the current random-number seed or the number specified with set seed(); see [R] set seed.

Options

+------+ ----+ Main +-------------------------------------------------------------

clear specifies that the dataset in memory be replaced, even though the current dataset has not been saved on disk.

double specifies that the new variables be stored as Stata doubles, meaning 8-byte reals. If double is not specified, variables are stored as floats, meaning 4-byte reals. See [D] data types.

n(#) specifies the number of observations to be generated. The default is the current number of observations. If n(#) is not specified or is the same as the current number of observations, drawnorm adds the new variables to the existing dataset; otherwise, drawnorm replaces the data in memory.

sds(vector) specifies the standard deviations of the generated variables. sds() may not be specified with cov().

corr(matrix|vector) specifies the correlation matrix. If neither corr() nor cov() is specified, the default is orthogonal data.

cov(matrix|vector) specifies the covariance matrix. If neither cov() nor corr() is specified, the default is orthogonal data.

cstorage(full|lower|upper) specifies the storage mode for the correlation or covariance structure in corr() or cov(). The following storage modes are supported:

full specifies that the correlation or covariance structure is stored (recorded) as a symmetric k*k matrix.

lower specifies that the correlation or covariance structure is recorded as a lower triangular matrix. With k variables, the matrix should have k(k+1)/2 elements in the following order:

C(11) C(21) C(22) C(31) C(32) C(33) ... C(k1) C(k2) ... C(kk)

upper specifies that the correlation or covariance structure is recorded as an upper triangular matrix. With k variables, the matrix should have k(k+1)/2 elements in the following order:

C(11) C(12) C(13) ... C(1k) C(22) C(23) ... C(2k) ... C(k-1k-1) C(k-1k) C(kk)

Specifying cstorage(full) is optional if the matrix is square. cstorage(lower) or cstorage(upper) is required for the vectorized storage methods. See storage modes for examples.

forcepsd modifies the matrix C to be positive semidefinite (psd), and so be a proper covariance matrix. If C is not positive semidefinite, it will have negative eigenvalues. By setting negative eigenvalues to 0 and reconstructing, we obtain the least-squares positive-semidefinite approximation to C. This approximation is a singular covariance matrix.

means(vector) specifies the means of the generated variables. The default is means(0).

+---------+ ----+ Options +----------------------------------------------------------

seed(#) specifies the initial value of the random-number seed used by the runiform() function. The default is the current random-number seed. Specifying seed(#) is the same as typing set seed # before issuing the drawnorm command.

Examples

Generate 2,000 independent observations (x,y); x with mean 2 and standard deviation .5; y with mean 3 and standard deviation 2

. matrix m = (2,3) . matrix sd = (.5,2) . drawnorm x y, n(2000) means(m) sds(sd) . summarize

Draw a sample of 1,000 observations from a bivariate standard normal distribution, with correlation 0.5

. clear . matrix C = (1, .5 \ .5, 1) . drawnorm x y, n(1000) corr(C) . summarize

Equivalently,

. clear . matrix C = (1, .5, 1) . drawnorm x y, n(1000) corr(C) cstorage(lower) . summarize


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