📄 mf_crossdev.hlp
字号:
{smcl}
{* 28mar2005}{...}
{cmd:help mata crossdev()}
{hline}
{* index crossdev()}{...}
{* index deviation cross product}{...}
{* index cross product}{...}
{* index product}{...}
{title:Title}
{p 4 8 2}
{bf:[M-5] crossdev() -- Deviation cross products}
{title:Syntax}
{p 8 12 2}
{it:real matrix}
{cmd:crossdev(}{it:X}{cmd:,}
{it:x}{cmd:,}
{it:Z}{cmd:,}
{it:z}{cmd:)}
{p 8 12 2}
{it:real matrix}
{cmd:crossdev(}{it:X}{cmd:,}
{it:x}{cmd:,}
{it:w}{cmd:,}
{it:Z}{cmd:,}
{it:z}{cmd:)}
{p 8 12 2}
{it:real matrix}
{cmd:crossdev(}{it:X}{cmd:,}
{it:xc}{cmd:,}
{it:x}{cmd:,}
{it:Z}{cmd:,}
{it:zc}{cmd:,}
{it:z}{cmd:)}
{p 8 12 2}
{it:real matrix}
{cmd:crossdev(}{it:X}{cmd:,}
{it:xc}{cmd:,}
{it:x}{cmd:,}
{it:w}{cmd:,}
{it:Z}{cmd:,}
{it:zc}{cmd:,}
{it:z}{cmd:)}
{p 4 8 2}
where
{it:X}: {it:real matrix X}
{it:xc}: {it:real scalar xc}
{it:x}: {it:real rowvector x}
{it:w}: {it:real vector w}
{it:Z}: {it:real matrix Z}
{it:zc}: {it:real scalar zc}
{it:z}: {it:real rowvector z}
{title:Description}
{p 4 4 2}
{cmd:crossdev()} makes calculations of the form
({it:X}:-{it:x})'({it:X}:-{it:x})
({it:X}:-{it:x})'({it:Z}:-{it:z})
({it:X}:-{it:x})'diag({it:w})({it:X}:-{it:x})
({it:X}:-{it:x})'diag({it:w})({it:Z}:-{it:z})
{p 4 4 2}
{cmd:crossdev()} is a variation on
{bf:{help mf_cross:[M-5] cross()}}.
{cmd:crossdev()}
mirrors {cmd:cross()} in every respect except that it has two
additional arguments: {it:x} and {it:z}.
{it:x} and {it:z} record the amount by which {it:X} and {it:Z} are to be
deviated. In most cases, {it:x} and {it:z} contain the (appropriately
weighted) column means of {it:X} and {it:Z}.
{title:Remarks}
{p 4 4 2}
{it:x} usually contains the same number of rows as {it:X} but, if
{it:xc}!=0, {it:x} may contain an extra element on the right
recording the amount from which the constant 1 should be deviated.
{p 4 4 2}
The same applies to {it:z}: it usually contains the same number of rows
as {it:Z} but, if {it:zc}!=0, {it:z} may contain an extra element on the
right.
{title:Example 1: Linear regression using a single view}
{cmd}: M = .
: st_view(M, ., ("mpg", "weight", "foreign"), 0)
:
: means = mean(M, 1)
: CP = crossdev(M,means , M,means)
: xx = CP[|2,2 \ .,.|]
: xy = CP[|2,1 \ .,1|]
: b = invsym(xx)*xy
: b = b \ means[1]-means[|2\.|]*b{txt}
{p 4 4 2}
Compare this solution with Example 3 in
{bf:{help mf_cross:[M-5] cross()}}.
{title:Example 2: Linear regression using subviews}
{cmd}: M = X = y = .
: st_view(M, ., ("mpg", "weight", "foreign"), 0)
: st_subview(y, M, ., 1)
: st_subview(X, M, ., (2\.))
:
: xmean = mean(X, 1)
: ymean = mean(Y, 1)
: xx = crossdev(X,xmean , X,xmean)
: xy = crossdev(X,xmean , y,ymean)
: b = invsym(xx)*xy
: b = b \ ymean-xmean*b{txt}
{p 4 4 2}
Compare this solution with Example 4 in
{bf:{help mf_cross:[M-5] cross()}}.
{title:Example 3: Weighted linear regression}
{cmd}: M = X = y = w = .
: st_view(M, ., ("w", "mpg", "weight", "foreign"), 0)
: st_subview(w, M, ., 1)
: st_subview(y, M, ., 2)
: st_subview(X, M, ., (3\.))
:
: xmean = mean(X, w)
: ymean = mean(Y, w)
: xx = crossdev(X,xmean, w, X,xmean)
: xy = crossdev(X,xmean, w, y,ymean)
: b = invsym(xx)*xy
: b = b \ ymean-xmean*b{txt}
{p 4 4 2}
Compare this solution with Example 6 in
{bf:{help mf_cross:[M-5] cross()}}.
{title:Example 4: Variance matrix}
{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}
This is exactly what {cmd:variance()} does; see
{bf:{help mf_mean:[M-5] mean()}}.
Compare this solution with Example 12 in
{bf:{help mf_cross:[M-5] cross()}}.
{title:Example 5: Weighted variance matrix}
{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}
This is exactly what {cmd:variance()} does with weighted data; see
{bf:{help mf_mean:[M-5] mean()}}.
Compare this solution with Example 14 in
{bf:{help mf_cross:[M-5] cross()}}.
{title:Conformability}
{p 4 8 2}
{cmd:crossdev(}{it:X}{cmd:,}
{it:xc}{cmd:,}
{it:x}{cmd:,}
{it:w}{cmd:,}
{it:Z}{cmd:,}
{it:zc}{cmd:,}
{it:z}{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:x}: 1 {it:x v1} or 1 {it:x} {it:v1}+({it:xc}!=0)
{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:z}: 1 {it:x v2} or 1 {it:x} {it:v2}+({it:zc}!=0)
{it:result}: {it:(v1+(xc!=0)) x (v2+(zc!=0))}
{title:Diagnostics}
{p 4 4 2}
{cmd:crossdev(}{it:X}{cmd:,}
{it:xc}{cmd:,}
{it:x}{cmd:,}
{it:w}{cmd:,}
{it:Z}{cmd:,}
{it:zc}{cmd:,}
{it:z}{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] crossdev()}
{p 4 13 2}
Online: help for
{bf:{help mf_cross:[M-5] cross()}},
{bf:{help mf_quadcross:[M-5] quadcross()}};
{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 + -