Mata course: Views

Syntax

matrix to be created --+
|
+++ +-+ +-+ +
+
st_view(|V|, |i|, |j| [, |selectvar|])
+-+ +++ +++ +----+----+
| | |
| | +-- optional
| |
| +-- variables (columns)
observations (rows) --+ matrix is to contain
matrix is to contain

Example

st_view(mymat, ., .) will create mymat containing all variables
and all observations:

. sysuse auto, clear
(1978 Automobile Data)

. sort turn

. keep mpg weight foreign

. keep in 1/5
(69 observations deleted)

. list

+---------------
+
| mpg weight foreign |
+---------------
+
1. | 22 3,180 Domestic |
2. | 35 2,020 Foreign |
3. | 30 1,980 Foreign |
4. | 28 1,800 Domestic |
5. | 24 2,280 Foreign |
+---------------
+

. mata

mata (type end to exit)

: st_view(mymat, ., .)

: mymat
1 2 3
+
+
1 | 22 3180 0 |
2 | 35 2020 1 |
3 | 30 1980 1 |
4 | 28 1800 0 |
5 | 24 2280 1 |
+
+

: mymat'mymat
[symmetric]
1 2 3
+
+
1 | 3969 |
2 | 305180 26551600 |
3 | 89 6280 3 |
+
+

The last little bit (mymat'mymat) is just to show that mymat is
just a matrix: You use it just as you would any other matrix.

mymat consumes only 16 bytes!

: mata describe mymat

# bytes type name and extent

16 real matrix mymat[5,3]

This is surprising because

1.
mymat is 5 x 3, so storing the data ought to take 5*3*8=120 bytes.

2.
mymat fits into 16 bytes because it is a view onto the Stata dataset,
not a copy.

3.
If our dataset contained 120,253 observations, mymat would still
require only 16 bytes!

Disclaimers:

D1.
Not mentioned is 64 bytes of overhead. The 64 bytes is fixed.

D2.
The amounts are so small because we are looking at all variables and
all observations.

D3.
If we selected k variables and selected n observations, the total
would be 4*(k+n), which is still small.

D4. If we selected k variables and took all observations, the total
would be 4*k.

D5. If we selected n observations and took all variables, to total
would be 4*n.

mymat really is a view!

If you change an element of mymat, you change the underlying data:

. list

+---------------
+
| mpg weight foreign |
+---------------
+
1. | 22 3,180 Domestic |
2. | 35 2,020 Foreign |
3. | 30 1,980 Foreign |
4. | 28 1,800 Domestic |
5. | 24 2,280 Foreign |
+---------------
+

. mata: mymat[1,1] = 100

. list

+---------------
+
| mpg weight foreign |
+---------------
+
1. | 100 3,180 Domestic |
2. | 35 2,020 Foreign |
3. | 30 1,980 Foreign |
4. | 28 1,800 Domestic |
5. | 24 2,280 Foreign |
+---------------
+

Specifying variables

The third argument, j does that:

+-+ +-+ +-+ +
+
st_view(|V|, |i|, |j| [, |selectvar|])
+-+ +-+ +++ +---------+
|
|
+-- variables (columns)
matrix is to contain

j meaning

. all variables

(1,2,3) first three variables

("mpg", "weight", "foreign") three named variables

(1,9,2) variables 1, 9, and 2

(1,9,2,9) variables 1, 9, 2, and 9 again

("mpg", "weight", "mpg") variables mpg, weight, and mpg again

Warning, do not forget the parentheses:

st_view(mymat, ., "mpg", "weight", "foreign")

is an error -- it looks as if you specified five arguments.

Type

st_view(mymat, ., ("mpg", "weight", "foreign"))

That's three arguments, with

("mpg", "weight", "foreign")

being the
third.

Specifying observations

The second argument, i does that:

+-+ +-+ +-+ +
+
st_view(|V|, |i|, |j| [, |selectvar|])
+-+ +++ +-+ +---------+
|
|
observations (rows) --+
matrix is to contain

i meaning

. all observations

(1\2\3\4\5\6\7\8) obs. 1 to 8

(1,8) obs. 1 to 8

((1,7)\ (9,9)\ (10,20)) obs 1 to 7, 9, and 10 to 20

(1\2\4\9\10) obs. 1, 2, 4, 9, and 10

(1\2\4\9\4) obs 1, 2, 4, 9, and 4 again

Do not forget the parentheses:

_st_view(mymat, 1,8, .)

is an error -- it looks as if you specified four arguments.

Type

_st_view(mymat, (1,8), .)

Specifying observations 2: subsetting the data

The optional fourth argument also has to do with observations:

+-+ +-+ +-+ +
+
st_view(|V|, |i|, |j| [, |selectvar|])
+-+ +-+ +-+ +----+----+
|
+-- optional

selectvar meaning

(not specified) all observations

. same

0 nonmissing observations

2 obs. for which var 2 != 0

"myvar" obs. for which myvar != 0

For instance, specify

st_view(mymat, ., ., 0)

and you will get all observations that have no missing values.

Specify

st_view(mymat, ., ("mpg", "weight"), 0)

and you will get all observations for which the variables mpg and weight do not
contain missing.

Specify

st_view(mymat, (1,50), ("mpg", "weight"), 0)

and you will get all observations within 1 to 50 for which the variables
mpg and weight do not contain missing.

Specify

st_view(mymat, ., ("mpg", "weight"), "touse")

and you will get all observations for which variable touse does not
contain 0.

Specifying the first argument

The first argument specifies the name of the matrix to be created:

matrix to be created --+
|
+++ +-+ +-+ +
+
st_view(|V|, |i|, |j| [, |selectvar|])
+-+ +-+ +-+ +---------+

Follow what you think are the rules, however, and you may be surprised:

: st_view(mymat, ., .)
<istmt>: 3499 mymat not found
r(3499);

Arguments to functions must exist, even when the only exist to be
recreated. Thus, type

: mymat = .

: st_view(mymat, ., .)

or type

: st_view(mymat=., ., .)

It does not matter what mymat contains, just so long as it contains
something (it exists). We just make it contain a missing value.

Top