我正试图找到一种方法来查找Stata中矩阵的特定(总是相同)列中的最小值,然后让它给出相应元素的行号。
答案 0 :(得分:0)
这是一些技巧。请注意,通常,列中的最小值或最大值可能出现在一行或多行中。
. webuse lutkepohl2
(Quarterly SA West German macro data, Bil DM, from Lutkepohl 1993 Table E.1)
. varsoc dln_inv dln_inc dln_consump
Selection-order criteria
Sample: 1961q2 - 1982q4 Number of obs = 87
+---------------------------------------------------------------------------+
|lag | LL LR df p FPE AIC HQIC SBIC |
|----+----------------------------------------------------------------------|
| 0 | 696.398 2.4e-11 -15.9402 -15.9059 -15.8552* |
| 1 | 711.682 30.568 9 0.000 2.1e-11 -16.0846 -15.9477* -15.7445 |
| 2 | 724.696 26.028 9 0.002 1.9e-11* -16.1769* -15.9372 -15.5817 |
| 3 | 729.124 8.8557 9 0.451 2.1e-11 -16.0718 -15.7294 -15.2215 |
| 4 | 738.353 18.458* 9 0.030 2.1e-11 -16.0771 -15.632 -14.9717 |
+---------------------------------------------------------------------------+
Endogenous: dln_inv dln_inc dln_consump
Exogenous: _cons
. matrix li r(stats)
r(stats)[5,9]
lag LL LR df p FPE
r1 0 696.39806 .b .b .b 2.398e-11
r2 1 711.68216 30.568191 9 .00035098 2.076e-11
r3 2 724.69624 26.02817 9 .00202142 1.894e-11
r4 3 729.12407 8.8556522 9 .45070419 2.109e-11
r1 4 738.35331 18.458484 9 .03021188 2.105e-11
AIC HQIC SBIC
r1 -15.940185 -15.905946 -15.855154
r2 -16.084647 -15.947689 -15.744522
r3 -16.176925 -15.937249 -15.581706
r4 -16.071818 -15.729423 -15.221505
r1 -16.077088 -15.631974 -14.971681
. mata
------------------------------------------------- mata (type end to exit) -----
: stats = st_matrix("r(stats)")
: select((1::rows(stats)), (stats[,7] :== colmax(stats[,7])))
1
: select((1::rows(stats)), (stats[,9] :== colmax(stats[,9])))
5
答案 1 :(得分:0)
到目前为止,她就是我的。我知道可能有更好的方法来编写它,但这就是我现在能做的所有事情。
foreach c of local gracauseloop {
varsoc rppi_chst `c' , maxlag(5)
matrix A = r(stats)
svmat A, name(col)
drop lag LL LR df p FPE AIC HQIC
egen minSB = min (SBIC)
egen lags = fill(0 1 2 3 4 5)
gen varlag = lags if minSB == SBIC
egen lag = total(varlag)
local vlag "lag"
var rppi_chst `c' , lags(1/`vlag')
vargranger
clear matrix
drop SBIC minSB lags varlag lag
}