📄 mf_cross.hlp
字号:
{cmd}: M = w = M2 = .
: st_view(M, ., ("w", "mpg", "weight", "foreign"), 0)
: st_subview(w, M, ., 1)
: st_subview(M2, M, ., (2\.))
:
: CP = cross(M,1 ,w, M,1)
: XX = CP[|3,3 \ .,.|]
: Xy = CP[|3,1 \ .,2|]
: b = invsym(XX)*Xy{txt}
{p 4 4 2}
Note how similar these solutions are to their unweighted counterparts.
The only
important difference is the appearance of {cmd:w} as the middle
argument of {cmd:cross()}. Since specifying the middle argument
as a scalar 1 is also allowed and produces unweighted estimates,
the above code could be
modified to produce unweighted or weighted estimates depending on
how {cmd:w} is defined.
{title:Example 7: Mean of a single variable}
{cmd}: x = .
: st_view(x, ., "mpg", 0)
:
: CP = cross(1,0 , x,1)
: mean = CP[1]/CP[2]{txt}
{p 4 4 2}
{it:Comments:}
An easier and every bit as good a solution would be
{cmd}: x = .
: st_view(x, ., "mpg", 0)
:
: mean = mean(x,1){txt}
{p 4 4 2}
{cmd:mean()} is implemented in terms of {cmd:cross()}.
Actually, {cmd:mean()} is implemented using the quad-precision version of
{cmd:cross()}; see
{bf:{help mf_quadcross:[M-5] quadcross()}}.
We could implement our solution in terms of {cmd:quadcross()}:
{cmd}: x = .
: st_view(x, ., "mpg", 0)
:
: CP = quadcross(1,0 , x,1)
: mean = CP[1]/CP[2]{txt}
{p 4 4 2}
{cmd:quadcross()} returns a double-precision result just as does
{cmd:cross()}. The difference is that {cmd:quadcross()}
uses quad precision internally in calculating sums.
{title:Example 8: Means of multiple variables}
{cmd}: X = .
: st_view(X, ., ("mpg", "weight", "displ"), 0)
:
: CP = cross(1,0 , X,1)
: n = cols(CP)
: means = CP[|1\n-1|] :/ CP[n]{txt}
{p 4 4 2}
{it:Comments:}
The above logic will work for a single variable, too.
Using {cmd:mean()}, the solution would be
{cmd}: X = .
: st_view(X, ., ("mpg", "weight", "displ"), 0)
:
: means = mean(X, 1){txt}
{title:Example 9: Weighted means of multiple variables}
{cmd}: M = w = X = .
: st_view(M, ., ("w", "mpg", "weight, "displ"), 0)
: st_subview(w, M, ., 1)
: st_subview(X, M, ., (2\.))
:
: CP = cross(1,0, w, X,1)
: n = cols(CP)
: means = CP[|1\n-1|] :/ CP[n]{txt}
{p 4 4 2}
{it:Comments:}
Note how similar this solution is to the unweighted solution: {cmd:w}
now appears as the middle argument of {cmd:cross()}.
The line {cmd:CP} {cmd:=} {cmd:cross(1,0, w, X,1)} could
also be coded
{cmd:CP} {cmd:=} {cmd:cross(w,0, X,1)}; it would make no difference.
{p 4 4 2}
The {cmd:mean()} solution to the problem is
{cmd}: M = w = X = .
: st_view(M, ., ("w", "mpg", "weight, "displ"), 0)
: st_subview(w, M, ., 1)
: st_subview(X, M, ., (2\.))
:
: means = mean(X, w){txt}
{title:Example 10: Variance matrix, traditional approach 1}
{cmd}: X = .
: st_view(X, ., ("mpg", "weight", "displ"), 0)
:
: n = rows(X)
: means = mean(X, 1)
: cov = (X'X/n - means'means)*(n/(n-1)){txt}
{p 4 4 2}
{it:Comments:}
This above is not 100% traditional since we used {cmd:mean()} to obtain
the means, but that does make the solution more understandable.
The solution requires calculating {cmd:X'}, requiring that the data matrix
be duplicated. In addition, we have used a numerically poor
calculation formula.
{title:Example 11: Variance matrix, traditional approach 2}
{cmd}: X = .
: st_view(X, ., ("mpg", "weight", "displ"), 0)
:
: n = rows(X)
: means = mean(X, 1)
: cov = (X:-means)'(X:-means) :/ (n-1){txt}
{p 4 4 2}
{it:Comments:}
We use a better calculation formula and, in the process, increase our
memory usage substantially.
{title:Example 12: Variance matrix using cross()}
{cmd}: X = .
: st_view(X, ., ("mpg", "weight, "displ"), 0)
:
: n = rows(X)
: means = mean(X, 1)
: XX = cross(X, X)
: cov = ((XX:/n)-means'means)*(n/(n-1)){txt}{txt}
{p 4 4 2}
{it:Comments:}
The above solution conserves memory, but uses the numerically poor
calculation formula. A related function, {cmd:crossdev()},
will calculate deviation crossproducts:
{cmd}: X = .
: st_view(X, ., ("mpg", "weight, "displ"), 0)
:
: n = rows(X)
: means = mean(X, 1)
: xx = crossdev(X,means, X,means)
: cov = xx:/(n-1){txt}
{p 4 4 2}
See {bf:{help mf_crossdev:[M-5] crossdev()}}.
The easiest solution, however, is
{cmd}: X = .
: st_view(X, ., ("mpg", "weight, "displ"), 0)
:
: cov = variance(X, 1){txt}
{p 4 4 2}
See {bf:{help mf_mean:[M-5] mean()}} for a description of the
{cmd:variance()} function.
{cmd:variance()} is implemented in terms of
{cmd:crossdev()}.
{title:Example 13: Weighted variance matrix, traditional approaches}
{cmd}: M = w = X = .
: st_view(M, ., ("w", "mpg", "weight", "displ"), 0)
: st_subview(w, M, ., 1)
: st_subview(X, M, ., (2\.))
:
: n = colsum(w)
: means = mean(X, w)
: cov = (X'diag(w)*X:/n - means'means)*(n/(n-1)){txt}
{p 4 4 2}
{it:Comments:}
Above we use the numerically poor formula. Using the better, deviation
formula, we would have
{cmd}: M = w = X = .
: st_view(M, ., ("w", "mpg", "weight", "displ"), 0)
: st_subview(w, M, ., 1)
: st_subview(X, M, ., (2\.))
:
: n = colsum(w)
: means = mean(X, w)
: cov = (X:-means)'diag(w)*(X:-means) :/ (n-1){txt}
{p 4 4 2}
The memory requirements include making a copy of the data with the
means removed and making an {it:N} {it:x} {it:N} diagonal matrix.
{title:Example 14: Weighted variance matrix using cross()}
{cmd}: M = w = X = .
: st_view(M, ., ("w", "mpg", "weight", "displ"), 0)
: st_subview(w, M, ., 1)
: st_subview(X, M, ., (2\.))
:
: n = colsum(w)
: means = mean(X, w)
: cov = (cross(X,w,X):/n - means'means)*(n/(n-1)){txt}
{p 4 4 2}
{it:Comments:}
As in example 12, the above solution conserves memory but uses a
numerically poor calculation formula. Better is to use
{cmd:crossdev()}:
{cmd}: M = w = X = .
: st_view(M, ., ("w", "mpg", "weight", "displ"), 0)
: st_subview(w, M, ., 1)
: st_subview(X, M, ., (2\.))
:
: n = colsum(w)
: means = mean(X, w)
: cov = crossdev(X,means, w, X,means) :/ (n-1){txt}
{p 4 4 2}
and easiest is to use {cmd:variance()}:
{cmd}: M = w = X = .
: st_view(M, ., ("w", "mpg", "weight", "displ"), 0)
: st_subview(w, M, ., 1)
: st_subview(X, M, ., (2\.))
:
: cov = variance(X, w){txt}
{p 4 4 2}
See
{bf:{help mf_crossdev:[M-5] crossdev()}}
and
{bf:{help mf_mean:[M-5] mean()}}.
{title:Comment concerning cross() and missing values}
{p 4 4 2}
{cmd:cross()} automatically omits rows containing missing values
in making its calculation. Depending on this feature, however, is
considered bad style because so many other Mata functions do not provide
that feature and it is easy to make a mistake.
{p 4 4 2}
The right way to handle missing values is to exclude them when constructing
views and subviews, as we have done above.
When we constructed a view, we invariable specified fourth argument 0
to {cmd:st_view()}. In formal programming situations, you will probably
specify the name of the {cmd:touse} variable you have previously constructed
in your ado-file that calls the your Mata function.
{title:Conformability}
{p 4 8 2}
{cmd:cross(}{it:X}{cmd:,}
{it:xc}{cmd:,}
{it:w}{cmd:,}
{it:Z}{cmd:,}
{it:zc}{cmd:)}:
{p_end}
{it:X}: {it:n x v1} or 1 {it:x} 1, 1 {it:x} 1 treated as if {it:n x} 1
{it:xc}: 1 {it:x} 1
{it:w}: {it:n x} 1 or 1 {it:x n} or 1 {it:x} 1
{it:Z}: {it:n x v2}
{it:zc}: 1 {it:x} 1
{it:result}: {it:(v1+(xc!=0)) x (v2+(zc!=0))}
{title:Diagnostics}
{p 4 4 2}
{cmd:cross(}{it:X}{cmd:,}
{it:xc}{cmd:,}
{it:w}{cmd:,}
{it:Z}{cmd:,}
{it:zc}{cmd:)} omits rows in {it:X} and {it:Z} that contain missing values.
{title:Source code}
{p 4 4 2}
Function is built-in.
{title:Also see}
{p 4 13 2}
Manual: {hi:[M-5] cross()}
{p 4 13 2}
Online: help for
{bf:{help mf_crossdev:[M-5] crossdev()}},
{bf:{help mf_quadcross:[M-5] quadcross()}},
{bf:{help mf_mean:[M-5] mean()}};
{bf:{help m4_stata:[M-4] stata}},
{bf:{help m4_utility:[M-4] utility}},
{bf:{help m4_statistical:[M-4] statistical}}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -