{smcl} {bf:Mata course: Views} {hline} {title:Syntax} matrix to be created {c -}{c -}{c TRC} {c |} {c TLC}{c BT}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{hline 9}{c TRC} {cmd:st_view(}{c |}{it:V}{c |}, {c |}{it:i}{c |}, {c |}{it:j}{c |} [, {c |}{it:selectvar}{c |}]{cmd:)} {c BLC}{c -}{c BRC} {c BLC}{c TT}{c BRC} {c BLC}{c TT}{c BRC} {c BLC}{hline 4}{c TT}{hline 4}{c BRC} {c |} {c |} {c |} {c |} {c |} {c BLC}{c -}{c -} optional {c |} {c |} {c |} {c BLC}{c -}{c -} variables (columns) observations (rows) {c -}{c -}{c BRC} matrix is to contain matrix is to contain {title:Example} {p 4 4 2} {cmd:st_view(mymat, ., .)} will create {cmd:mymat} containing all variables and all observations: {com}. sysuse auto, clear {txt}(1978 Automobile Data) {com}. sort turn {txt} {com}. keep mpg weight foreign {txt} {com}. keep in 1/5 {txt}(69 observations deleted) {com}. list {txt} {c TLC}{hline 5}{c -}{hline 8}{c -}{hline 10}{c TRC} {c |} {res}mpg weight foreign {txt}{c |} {c LT}{hline 5}{c -}{hline 8}{c -}{hline 10}{c RT} 1. {c |} {res} 22 3,180 Domestic {txt}{c |} 2. {c |} {res} 35 2,020 Foreign {txt}{c |} 3. {c |} {res} 30 1,980 Foreign {txt}{c |} 4. {c |} {res} 28 1,800 Domestic {txt}{c |} 5. {c |} {res} 24 2,280 Foreign {txt}{c |} {c BLC}{hline 5}{c -}{hline 8}{c -}{hline 10}{c BRC} {com}. mata {txt}{hline 49} mata (type {cmd:end} to exit) {hline} {com}: st_view(mymat, ., .) {res} {com}: mymat {res} {txt} 1 2 3 {c TLC}{hline 22}{c TRC} 1 {c |} {res} 22 3180 0{txt} {c |} 2 {c |} {res} 35 2020 1{txt} {c |} 3 {c |} {res} 30 1980 1{txt} {c |} 4 {c |} {res} 28 1800 0{txt} {c |} 5 {c |} {res} 24 2280 1{txt} {c |} {c BLC}{hline 22}{c BRC} {com}: mymat'mymat {res}{txt}[symmetric] 1 2 3 {c TLC}{hline 34}{c TRC} 1 {c |} {res} 3969 {txt} {c |} 2 {c |} {res} 305180 26551600 {txt} {c |} 3 {c |} {res} 89 6280 3{txt} {c |} {c BLC}{hline 34}{c BRC} {p 4 4 2} The last little bit ({cmd:mymat'mymat}) is just to show that {cmd:mymat} is just a matrix: You use it just as you would any other matrix. {title:mymat consumes only 16 bytes!} {com}: mata describe mymat {txt}# bytes type name and extent {hline 71} {res} 16 {txt}real matrix {res}mymat{txt}[5,3] {hline 71} {p 4 4 2} This is surprising because {p 8 12 2} 1. {cmd:mymat} is 5 {it:x} 3, so storing the data ought to take 5*3*8=120 bytes. {p 8 12 2} 2. {cmd:mymat} fits into 16 bytes because it is a view onto the Stata dataset, not a copy. {p 8 12 2} 3. If our dataset contained 120,253 observations, {cmd:mymat} would still require only 16 bytes! {p 4 4 2} Disclaimers: {p 7 12 2} D1. Not mentioned is 64 bytes of overhead. The 64 bytes is fixed. {p 7 12 2} D2. The amounts are so small because we are looking at all variables and all observations. {p 7 12 2} D3. If we selected {it:k} variables and selected {it:n} observations, the total would be 4*({it:k}+{it:n}), which is still small. {p 7 12 2} D4. If we selected {it:k} variables and took all observations, the total would be 4*{it:k}. {p 7 12 2} D5. If we selected {it:n} observations and took all variables, to total would be 4*{it:n}. {title:mymat really is a view!} {p 4 4 2} If you change an element of mymat, you change the underlying data: {com}. list {txt} {c TLC}{hline 5}{c -}{hline 8}{c -}{hline 10}{c TRC} {c |} {res}mpg weight foreign {txt}{c |} {c LT}{hline 5}{c -}{hline 8}{c -}{hline 10}{c RT} 1. {c |} {res} 22 3,180 Domestic {txt}{c |} 2. {c |} {res} 35 2,020 Foreign {txt}{c |} 3. {c |} {res} 30 1,980 Foreign {txt}{c |} 4. {c |} {res} 28 1,800 Domestic {txt}{c |} 5. {c |} {res} 24 2,280 Foreign {txt}{c |} {c BLC}{hline 5}{c -}{hline 8}{c -}{hline 10}{c BRC} {com}. mata: mymat[1,1] = 100 {res}{txt} {com}. list {txt} {c TLC}{hline 5}{c -}{hline 8}{c -}{hline 10}{c TRC} {c |} {res}mpg weight foreign {txt}{c |} {c LT}{hline 5}{c -}{hline 8}{c -}{hline 10}{c RT} 1. {c |} {res}100 3,180 Domestic {txt}{c |} 2. {c |} {res} 35 2,020 Foreign {txt}{c |} 3. {c |} {res} 30 1,980 Foreign {txt}{c |} 4. {c |} {res} 28 1,800 Domestic {txt}{c |} 5. {c |} {res} 24 2,280 Foreign {txt}{c |} {c BLC}{hline 5}{c -}{hline 8}{c -}{hline 10}{c BRC}{txt} {title:Specifying variables} {p 4 4 2} The third argument, {it:j} does that: {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{hline 9}{c TRC} {cmd:st_view(}{c |}{it:V}{c |}, {c |}{it:i}{c |}, {c |}{it:j}{c |} [, {c |}{it:selectvar}{c |}]{cmd:)} {c BLC}{c -}{c BRC} {c BLC}{c -}{c BRC} {c BLC}{c TT}{c BRC} {c BLC}{hline 4}{c -}{hline 4}{c BRC} {c |} {c |} {c BLC}{c -}{c -} variables (columns) matrix is to contain {it:j} meaning {hline} {cmd:.} all variables {cmd:(1,2,3)} first three variables {cmd:("mpg", "weight", "foreign")} three named variables {cmd:(1,9,2)} variables 1, 9, and 2 {cmd:(1,9,2,9)} variables 1, 9, 2, and 9 again {cmd:("mpg", "weight", "mpg")} variables mpg, weight, and mpg again {hline} {p 4 4 2} Warning, do not forget the parentheses: {cmd:st_view(mymat, ., "mpg", "weight", "foreign")} {p 4 4 2} is an error -- it looks as if you specified five arguments. {p 4 4 2} Type {cmd:st_view(mymat, ., ("mpg", "weight", "foreign"))} {p 4 4 2} That's three arguments, with {cmd:("mpg", "weight", "foreign")} {p 4 4 2} being the third. {title:Specifying observations} {p 4 4 2} The second argument, {it:i} does that: {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{hline 9}{c TRC} {cmd:st_view(}{c |}{it:V}{c |}, {c |}{it:i}{c |}, {c |}{it:j}{c |} [, {c |}{it:selectvar}{c |}]{cmd:)} {c BLC}{c -}{c BRC} {c BLC}{c TT}{c BRC} {c BLC}{c -}{c BRC} {c BLC}{hline 4}{c -}{hline 4}{c BRC} {c |} {c |} observations (rows) {c -}{c -}{c BRC} matrix is to contain {it:i} meaning {hline} {cmd:.} all observations {cmd:(1\2\3\4\5\6\7\8)} obs. 1 to 8 {cmd:(1,8)} obs. 1 to 8 {cmd:((1,7)\ (9,9)\ (10,20))} obs 1 to 7, 9, and 10 to 20 {cmd:(1\2\4\9\10)} obs. 1, 2, 4, 9, and 10 {cmd:(1\2\4\9\4)} obs 1, 2, 4, 9, and 4 again {hline} {p 4 4 2} Do not forget the parentheses: {cmd:_st_view(mymat, 1,8, .)} {p 4 4 2} is an error -- it looks as if you specified four arguments. {p 4 4 2} Type {cmd:_st_view(mymat, (1,8), .)} {title:Specifying observations 2: subsetting the data} {p 4 4 2} The optional fourth argument also has to do with observations: {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{hline 9}{c TRC} {cmd:st_view(}{c |}{it:V}{c |}, {c |}{it:i}{c |}, {c |}{it:j}{c |} [, {c |}{it:selectvar}{c |}]{cmd:)} {c BLC}{c -}{c BRC} {c BLC}{c -}{c BRC} {c BLC}{c -}{c BRC} {c BLC}{hline 4}{c TT}{hline 4}{c BRC} {c |} {c BLC}{c -}{c -} optional {it:selectvar} meaning {hline} {it:(not specified)} all observations {cmd:.} same {cmd:0} nonmissing observations {cmd:2} obs. for which var 2 != 0 {cmd:"myvar"} obs. for which myvar != 0 {hline} {p 4 4 2} For instance, specify {cmd:st_view(mymat, ., ., 0)} {p 4 4 2} and you will get all observations that have no missing values. {p 4 4 2} Specify {cmd:st_view(mymat, ., ("mpg", "weight"), 0)} {p 4 4 2} and you will get all observations for which the variables mpg and weight do not contain missing. {p 4 4 2} Specify {cmd:st_view(mymat, (1,50), ("mpg", "weight"), 0)} {p 4 4 2} and you will get all observations within 1 to 50 for which the variables mpg and weight do not contain missing. {p 4 4 2} Specify {cmd:st_view(mymat, ., ("mpg", "weight"), "touse")} {p 4 4 2} and you will get all observations for which variable touse does not contain 0. {title:Specifying the first argument} {p 4 4 2} The first argument specifies the name of the matrix to be created: matrix to be created {c -}{c -}{c TRC} {c |} {c TLC}{c BT}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{hline 9}{c TRC} {cmd:st_view(}{c |}{it:V}{c |}, {c |}{it:i}{c |}, {c |}{it:j}{c |} [, {c |}{it:selectvar}{c |}]{cmd:)} {c BLC}{c -}{c BRC} {c BLC}{c -}{c BRC} {c BLC}{c -}{c BRC} {c BLC}{hline 4}{c -}{hline 4}{c BRC} {p 4 4 2} Follow what you think are the rules, however, and you may be surprised: {com}: st_view(mymat, ., .) {res}{err} : 3499 mymat not found {txt}{search r(3499):r(3499);} {p 4 4 2} Arguments to functions must exist, even when the only exist to be recreated. Thus, type {com}: mymat = .{txt} {com}: st_view(mymat, ., .){txt} {p 4 4 2} or type {com}: st_view(mymat=., ., .){txt} {p 4 4 2} It does not matter what mymat contains, just so long as it contains something (it exists). We just make it contain a missing value. {hline} {bf:{view talk.smcl:Top}}