hansen检验 stata 回归指令公式是什么 stata

 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
stata命令大全(全)
下载积分:2500
内容提示:stata命令大全(全)
文档格式:DOC|
浏览次数:1994|
上传日期: 14:26:49|
文档星级:
全文阅读已结束,如果下载本文需要使用
 2500 积分
下载此文档
阅读此文档的用户还读了
stata命令大全(全)
官方公共微信君,已阅读到文档的结尾了呢~~
广告剩余8秒
文档加载中
STATA的简单命令
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
STATA的简单命令
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口Hansen_1999_面板门槛_01_连玉君-原创视频-搜狐视频
Hansen_1999_面板门槛_01_连玉君
视频介绍:
深入浅出地介绍了Hansen(1999)提出的面板门槛模型的估计、检验方法以及Stata实现过程。视频资料来自于中山大学连玉君副教授主讲的“stata学术论文专题”视频教程。
推荐出品人mata clear
mata drop *()
mata set matastrict on
mata set mataoptimize on
mata set matalnum off
* Copyright David Roodman 2005-15. May be distributed free.
* Mata code for xtabond2 version 3.6.3 30 September 2015
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
struct IVinst {
real scalar mz, passthru, equation
string scalar ivstyle, BaseVarNames
real matrix Base
// instrumenting variables
pointer (struct IVinst scalar) scalar next
struct GMMinst {
real scalar equation, passthru, FullInstSetEq, NumInsts, NumBaseVars, collapse, FullInstSetDiffed, MakeExtraInsts, InstOrthogonal
string scalar gmmstyle, BaseVarNames
real matrix Laglim, Base
real colvector BaseNameTs // time index of each instrumen, before lagging
pointer (real matrix) colvector BaseAll
pointer (string colvector) colvector BaseNamesAll
pointer (struct GMMinst scalar) scalar next
struct ClustInfo {
real colvector ID
real scalar N, sign, OneVar
pointer (real matrix) colvector pZe1i
real scalar xtabond2_mata() {
real scalar artests, arlevels, steps, h, consopt, r, r2, small, robust, onestepnonrobust,
j, j0, k, NObs, NObsEff, NGroups, diffsargan, NOtherInsts,
Nnottouse, NIVOptions, NGMMOptions, NDiffSargans, tmin,
N, T, NT, SystemGMM, RowsPerGroup, SystemHeight, j_GMM, j_IV, weights, wttot, rc, svmat, svvar, tdelta, pca, components, KMO, MinNClust
real matrix tmp, Xi, X0, Y0, Subscripts, Z_IV, Z_GMM, SubscriptsStart, SubscriptsStep, eigenvectors
string scalar idName, tName, LevelArg, optionsArg, touseName, bname, Vname, idSampleName, wtvarname, wtype, wexp, LabelName, tsfmt
string rowvector VarlistNames, Xnames, InstOptTxt, ClustVars
string matrix Stripe, ZGMMnames, ZIVnames, eigenvectorBasisNames
real colvector p, p_IV, p_GMM, p_AllIV, p_AllGMM, p_System, touseVar, idVar, tVar0, tVar, SortByIDEq, SortByEqID, ErrorEq, nottouse, Fill, touse, Complete,
wt, wt0, _wt, wtvar, ideqt
struct ClustInfo colvector clusts
pointer(real rowvector) matrix InstOptInd
real rowvector TiMinMax, DFm_keep, mz, passthru, equation, collapse, orthogonal, keep, eigenvalues
pointer(struct GMMinst scalar) GMM, GMMinsts
pointer (struct IVinst scalar) IV, IVinsts
real colvector e1, e2, b1, b2, Ze, ZeDiffSargan, ARz, ARp, A1diag, Xcons, b
real matrix S, D, ZXi, Z, Zi, A1Ze, A2Ze, H, V1, V2, A1, A2, App, V1robust, V2robust, XZA, VXZA, m2VZXA,
diffsargans, X, Y, ZX, ZXp, ZY, laglimits, V
real scalar c, i, sig2, g, sarganDF, sargan, sarganp, hansen, hansenp, DFm, DFr, F, Fp, Wald, Waldp
pointer (real matrix) pA, pV, pVrobust
pointer (real matrix) colvector pei, pZe1i
pointer (real colvector) pe, pwe
pointer (real matrix function) pfnXform
pragma unset NIVO pragma unset NGMMO pragma unset j_GMM; pragma unset Z_IV; pragma unset InstOptTxt
pragma unset touseV pragma unset idV pragma unset tVar0; pragma unset InstOptInd
prag pragm pragma unset ZGMM pragma unset ZIV pragma unset GMMinsts
pragma unset ARz; pragma unset ARp
if (favorspeed())
printf(`"{txt}Favoring speed over space. To switch, type or click on {stata "mata: mata set matafavor space, perm"}.\n"')
printf(`"{txt}Favoring space over speed. To switch, type or click on {stata "mata: mata set matafavor speed, perm"}.\n"')
if (st_nobs() = 10 ? st_numscalar("r(tdelta)") : 1
if ((tsfmt=st_global("r(tsfmt)")) == "%tc") tsfmt = "%9.0g"
stata("markout "+ touseName + " " + idName)
stata("bysort " + idName + ": egen " + (idSampleName=st_tempname()) + "=max(" + touseName + ")")
st_view(touseVar, ., touseName, idSampleName)
if (!any(touseVar)) {
printf("{err}No observations.\n")
return(2000)
st_view(tVar0, .,
tName, idSampleName)
st_view(idVar, ., idName, idSampleName)
if (missing(tVar0)) {
printf("{err}Missing values in time variable (%s).\n", tName)
return (459)
tVar = (tVar0 :- (tmin = min(tVar0))) / tdelta
T = max(tVar) + 1
VarlistNames = tokens(st_local("varlist"))
if (weights = strlen(wtype = st_local("weight"))) {
stata("quietly generate double " + (wtvarname = st_tempname()) + (wexp = st_local("exp")) + " if " + touseName)
st_view(wtvar, ., wtvarname, idSampleName)
st_local("0", "," + st_local("options"))
stata("syntax, [Robust Cluster(varlist) TWOstep noConstant noLeveleq ORthogonal ARtests(integer 2) SMall H(integer 3) DPDS2 Level(integer $S_level) ARLevels noDiffsargan SVMat SVVar pca COMPonents(integer 0) *]")
arlevels = st_local("arlevels")!= ""
small = st_local("small")!= ""
steps = 1 + (st_local("twostep")=="twostep")
if ((pca = st_local("pca")!= "") & favorspeed() == 0) {
printf("{err}pca not available in space-favoring mode.\n")
return(198)
components = strtoreal(st_local("components"))
if ((ClustVars = st_local("cluster")) != "" & favorspeed() == 0) {
printf("{err}cluster() not available in space-favoring mode.\n")
return(198)
if (((svmat = st_local("svmat") != "") | (svvar = st_local("svvar") != "")) & favorspeed() == 0)
printf("{res}In space-favoring mode, svmat and svvar will not save Z matrix.\n")
robust = ClustVars != "" | st_local("robust") != "" | (substr(wtype,1,1) == "p" & steps == 1)
onestepnonrobust = steps == 1 & robust == 0
diffsargan = st_local("diffsargan") == ""
pfnXform = (orthogonal = st_local("orthogonal") != "") ? &_Orthog() : &_Difference()
h = strtoreal(st_local("h"))
artests = strtoreal(st_local("artests"))
SystemGMM = st_local("leveleq") == ""
if (SystemGMM == 0 & arlevels) {
printf("{err}arlevels option invalid for difference GMM estimation.\n")
return (198)
if (h != 1 & h != 2 & h != 3) {
printf("{err}h(%f) invalid.\n", h)
return (198)
// Compute row number for each observation once the data set is filled out to NT rows
Fill = J(rows(tVar), 1, 1)
for (r = 2; r
tmp = st_data(., Xnames = VarlistNames[|2 \ .|], idSampleName)
(X0 = J(NT, cols(tmp), .))[Fill, .] = tmp
Xnames = J(1, 0, "")
X0 = J(NT, 0, .)
if (consopt = (SystemGMM & st_local("constant") == "")) {
Xnames = Xnames, "_cons"
X0 = X0, (Xcons = J(NT, 1, 1))
} else if (cols(Xnames) == 0) {
printf("{err}No regressors.\n")
return (481)
RowsPerGroup = (1 + SystemGMM) * T
SubscriptsStart = SystemHeight-RowsPerGroup+1,. \ SystemHeight,.
SubscriptsStep = J(2, 1, RowsPerGroup), J(2, 1, 0)
if (SystemGMM) { // reorder rows by t, equation rather than equation, t
SortByIDEq = 0 :: SystemHeight-1
SortByIDEq = trunc(SortByIDEq :/ RowsPerGroup) :* T + (mod(SortByIDEq, RowsPerGroup) :>= T) :* NT + mod(SortByIDEq, T) :+ 1
SortByEqID = invorder(SortByIDEq)
ErrorEq = J(N,1,(h!=1\h==1)#J(T,1,1)) // dummy for equation whose errors to use for sig2 and serial correlation test--tranformed eq unless h=1
Complete = !(rowmissing(X0) :| rowmissing(Y0))
if (j_IV = consopt) {
IVinsts = &(IVinst())
IVinsts->next = NULL
// add new IV inst group to linked list
IVinsts->equation = IVinsts->mz = IVinsts->passthru = 0
IVinsts->ivstyle = ""
IVinsts->Base = Xcons
IVinsts->BaseVarNames = "_cons"
IVinsts = NULL
rc = _ParseInsts(j_IV, j_GMM, NIVOptions, NGMMOptions, IVinsts, GMMinsts, SystemGMM, Complete, N, T, NT, idSampleName, Fill, orthogonal)
if (rc) return (rc)
pca = pca & j_GMM
NDiffSargans = NIVOptions + NGMMOptions + SystemGMM
_MakeIVinsts(InstOptInd, InstOptTxt, g, Z_IV, j_IV, SystemHeight, N, T, NT, NIVOptions, IVinsts, pfnXform, Complete, NDiffSargans, SystemGMM, ZIVnames)
if (NGMMOptions & diffsargan) {
p_System = J(1, 0, 0)
i = 0; GMM = GMMinsts
while (GMM != NULL) {
p = 0..i , i+1+GMM->NumInsts..j_GMM+1
InstOptInd[g, 2] = &(cols(p)>2 ? p[|2 \ cols(p) - 1|] : J(1, 0, .))
InstOptTxt[g--] = GMM->gmmstyle
if (SystemGMM & GMM->FullInstSetEq == 0) // mark GMM instruments for difference equation, for inclusion in diff-Sargan test of levels instruments
p_System = p_System , i+1 .. i+GMM->NumInsts - (GMM->MakeExtraInsts? (GMM->collapse? 1 : T - GMM->FullInstSetDiffed) * GMM->NumBaseVars : 0)
i = i + GMM->NumInsts
GMM = GMM->next
if (SystemGMM) {
InstOptInd[1, 2] = &p_System
InstOptTxt[1] = "GMM instruments for levels"
if (SystemGMM) {
if (weights) wt = wt0 \ wt0
touse = (orthogonal? _lag(touse, 1) : touse) \ touse
X = (*pfnXform)(X0, N, T, NT, Complete, 1) \ X0
Y = (*pfnXform)(Y0, N, T, NT, Complete, 1) \ Y0
if (svmat) ideqt = ideqt \ (ideqt[|.,.\.,1|], J(NT, 1, 1), ideqt[|.,3\.,.|])
X = (*pfnXform)(X0, N, T, NT, Complete, 1)
Y = (*pfnXform)(Y0, N, T, NT, Complete, 1)
if (orthogonal) __lag(touse, 1)
if (weights) wt = wt0
touse = touse :& !(rowmissing(Z_IV) :| rowmissing(X) :| rowmissing(Y))
touseVar[,] = (SystemGMM? touse[|NT+1 \ .|] : touse)[Fill]
if ((optionsArg=st_local("options")) != "") {
printf("{err}%s invalid.", optionsArg)
return (198)
if (ClustVars != "") {
ClustVars = strCombs(tokens(ClustVars))[|2,.\.,.|]
clusts = ClustInfo(rows(ClustVars), 1)
MinNClust = .
for (i=rows(ClustVars); i--) {
stata("tempvar clust")
stata("qui egen long " + st_local("clust") + " = group(" + invtokens(ClustVars[i,]) + ") if " + touseName + ", missing")
st_view(p, ., st_local("clust"), idSampleName)
(clusts[i].ID = J(NT, 1, .))[Fill] = p
if (SystemGMM) clusts[i].ID = clusts[i].ID \ clusts[i].ID
if ((clusts[i].N = colmax(p)) < MinNClust) MinNClust = clusts[i].N
j = rowsum(ClustVars[i,]:!="")
clusts[i].OneVar = j==1
clusts[i].sign = 2*mod(j, 2) - 1
clusts[i].pZe1i = J(clusts[i].N, 1, NULL)
if (favorspeed())
Z_GMM = _MakeGMMinsts(N, T, NT, SystemHeight, orthogonal, RowsPerGroup, j_GMM, touse, GMMinsts, ZGMMnames, tsfmt, tmin, tdelta)
// Zero out excluded observations
nottouse = J(Nnottouse = SystemHeight - sum(touse), 1, 0)
r2 = Nnottouse
for (r = SystemH r--) if (touse[r] == 0) nottouse[r2--] = r
Y = Y :* touse
if (weights) wt = wt :/ touse
if (svmat) ideqt = ideqt :/ touse
for (i=rows(clusts); i--) clusts[i].ID = clusts[i].ID :* touse
X[nottouse,] = J(Nnottouse, cols(X), 0)
Z_IV[nottouse,] = J(Nnottouse, j_IV, 0)
if (favorspeed())
Z_GMM[nottouse,] = J(Nnottouse, j_GMM, 0)
_editmissing(X, 0)
_editmissing(Y, 0)
_editmissing(Z_IV, 0)
k = cols(keep = _rmcoll(X, consopt, 1, Xnames))
if (k == 0) {
printf("{err}No regressors.\n")
return (481)
if (k 1) wttot = sum(wt[|.\NT|])
wt = wt * (NObsEff / wttot)
wttot = SystemGMM & h>1? sum(touse[|.\NT|]) : NObsEff // sum of weights in Xformed eq
wt = wt * (NObs / wttot)
wttot = SystemGMM & h>1? sum(touse[|.\NT|]) : NObs // sum of weights in Xformed eq
X = X :* wt
Y = Y :* wt
wttot = SystemGMM & h>1? sum(touse[|.\NT|]) : NObs
if (pca) {
tmp = colsum(Z_GMM:!=0):!=0
Z_GMM = select(Z_GMM, tmp)
eigenvectorBasisNames = select(ZGMMnames, tmp')
j_GMM = cols(Z_GMM)
S = quadcorrelation(Z_GMM, weights? wt : touse)
KMO = 1 /(1 + quadsum(lowertriangle(corr(invsym(S)),0):^2) / quadsum(lowertriangle(S,0):^2))
_symeigensystem(S, eigenvectors, eigenvalues)
_edittozerotol(eigenvalues, 1e-12)
j_GMM = components? min((components, j_GMM)) : max((sum(eigenvalues:>.999999), k-j_IV))
Z_GMM = Z_GMM * eigenvectors[|.,. \ ., j_GMM|]
ZGMMnames = J(j_GMM, 1, ""), (stataversion()>=1100? strofreal(eigenvalues'[|.\j_GMM|], "%32.12f") : J(j_GMM, 1, " "))
if ((j = j_GMM + j_IV)
printf("{res}Warning: Number of instruments may be large relative to number of observations.\n")
A1Ze = A1 * (Ze = _Ze(e1, ., ., N, T, NT, SystemHeight, orthogonal, RowsPerGroup, j_GMM, touse, GMMinsts,
SubscriptsStart, SubscriptsStep, Z_IV, Z_GMM, tsfmt, tmin, tdelta))
sarganp = chi2tail(sarganDF = j0 - k, sargan = quadcross(Ze, A1Ze))
if (onestepnonrobust) {
// correct H by sig2 too and prepare to do Hansen as well as Sargan
H = H * sig2
S = S * sig2
if (rows(clusts)) {
if (SystemGMM | clusts[rows(clusts)].N!=NObs) S = J(j, j, 0)
for (c=rows(clusts); c--) {
if (clusts[c].N==NObs & !SystemGMM) { // efficient code for clustering by obs/het-robust case
Z = Z_IV, Z_GMM
S = (small? clusts[c].sign*clusts[c].N/(clusts[c].N-1) : clusts[c].sign) * quadcross(Z, e1:*e1, Z)
tmp = J(j, j, 0)
for (i=clusts[c].N; i--) {
p = clusts[c].ID:==i
e2 = select(e1, p)
clusts[c].pZe1i[i] = &( (j_IV? quadcross(e2, select(Z_IV, p)) : J(1, 0, 0)) , (j_GMM? quadcross(e2, select(Z_GMM, p)) : J(1, 0, 0)) )
tmp = tmp + quadcross(*clusts[c].pZe1i[i], *clusts[c].pZe1i[i])
S = S + (small? clusts[c].sign*clusts[c].N/(clusts[c].N-1) : clusts[c].sign) * tmp
S = J(j, j, 0)
pZe1i = J(N, 1, NULL); Subscripts = SubscriptsStart
for (i = N; i--) {
e2 = e1[|Subscripts|]
pZe1i[i] = &( (j_IV? quadcross(e2, Z_IV[|Subscripts|]) : J(1, 0, 0)) , (j_GMM? quadcross(e2,
favorspeed()? Z_GMM[|Subscripts|] : _MakeGMMinsts(N, T, NT, SystemHeight, orthogonal, RowsPerGroup, j_GMM, touse, GMMinsts, ZGMMnames, tsfmt, tmin, tdelta, i)) : J(1, 0, 0)) )
S = S + quadcross(*pZe1i[i], *pZe1i[i])
Subscripts = Subscripts - SubscriptsStep
if (robust) {
VXZA = V1 * quadcross(ZX, A1)
V1robust = VXZA * S * VXZA'
A2 = invsym(S)
if (diag0cnt(A2)) {
printf("{res}Warning: Two-step estimated covariance matrix of moments is singular.\n")
printf("{res}
Using a generalized inverse to calculate %s.\n", steps==2? "optimal weighting matrix for two-step estimation" :
"robust weighting matrix for Hansen test")
if (diffsargan) printf("{res}
Difference-in-Sargan/Hansen statistics may be negative.\n")
// even in one-step robust, get Hansen
V2 = invsym((XZA = quadcross(ZX, A2)) * ZX)
e2 = Y - X * (b2 = (VXZA = V2 * XZA) * ZY)
if (steps == 2) {
pwe = &(weights? e2:/sqrt(wt) : e2)
if (SystemGMM) pwe = &(*pwe :* ErrorEq)
sig2 = quadcross(*pwe,*pwe) / (2 - (orthogonal | h==1)) / wttot
A2Ze = A2 * (Ze = _Ze(e2, . , ., N, T, NT, SystemHeight, orthogonal, RowsPerGroup, j_GMM, touse, GMMinsts,
SubscriptsStart, SubscriptsStep, Z_IV, Z_GMM, tsfmt, tmin, tdelta))
if (steps==2 & robust) {
// Windmeijer-corrected variance matrix for two-step
Need to compute matrix whose jth column is
// [sum_i Z_i'(xj_i*e1_i'+e1_i*xj_i')Z_i]*A2*Z'e2 (where xj = jth col of X)
= sum_i (Z_i'xj_i*e1_i'Z_i*A2*Z'e2 + Z_i'e1_i*xj_i'Z_i*A2*Z'e2).
Since e1_i'Z_i*A2*Z'e2 and xj_i'Z_i*A2*Z'e2 are scalars, they can be transposed and swapped with
adjacent terms. So this is:
matrix whose jth col is sum_i (e1_i'Z_i*A2*Z'e2 + Z_i'e1_i*e2'Z*A2)Z_i'xj_i
= sum_i (e1_i'Z_i*A2*Z'e2 + Z_i'e1_i*e2'Z*A2)Z_i'X_i
(transformation reverse engineered from DPD.)
if (rows(clusts)) {
if (SystemGMM | clusts[rows(clusts)].N!=NObs) D = J(j, k, 0)
for (c=rows(clusts); c--)
if (clusts[c].N==NObs & !SystemGMM) // efficient code for clustering by obs/het-robust case
D = (small? clusts[c].sign*clusts[c].N/(clusts[c].N-1) : clusts[c].sign) * 2*quadcross(Z, e1 :* Z*A2Ze, X)
tmp = J(j, k, 0)
for (i=clusts[c].N; i--) {
p = clusts[c].ID:==i
Xi = select(X, p)
ZXi = (j_IV? quadcross(select(Z_IV, p), Xi) : J(0, k, 0)) \ (j_GMM? quadcross(select(Z_GMM, p), Xi) : J(0, k, 0))
tmp = tmp + (*clusts[c].pZe1i[i] * A2Ze) * ZXi + quadcross(A2Ze * *clusts[c].pZe1i[i], ZXi)
D = D + (small? clusts[c].sign*clusts[c].N/(clusts[c].N-1) : clusts[c].sign) * tmp
D = J(j, k, 0)
Subscripts = SubscriptsStart
for (i = N; i--) {
Xi = X[|Subscripts|]
ZXi = (j_IV? quadcross(Z_IV[|Subscripts|], Xi) : J(0, k, 0)) \
(j_GMM? quadcross(favorspeed()? Z_GMM[|Subscripts|] :
_MakeGMMinsts(N, T, NT, SystemHeight, orthogonal, RowsPerGroup, j_GMM, touse, GMMinsts, ZGMMnames, tsfmt, tmin, tdelta, i), Xi) : J(0, k, 0))
D = D + (*pZe1i[i] * A2Ze) * ZXi + quadcross(A2Ze * *pZe1i[i], ZXi)
Subscripts = Subscripts - SubscriptsStep
D = VXZA * D
V2robust = V2 + D * V1robust * D' + (D + D) * V2
hansenp = chi2tail(sarganDF, hansen = quadcross(Ze, A2Ze))
if (steps == 1) {
pV = robust? &V1robust : &V1
pV = robust? &V2robust : &V2
V = (*pV + *pV')/2
if (steps == 1) {
pVrobust = &V1robust
pVrobust = &V2robust
m2VZXA = (-2 * *pV) * quadcross(ZX, *pA)
if (robust)
pV = pVrobust
if (onestepnonrobust == 0) { // preserve estimation residuals for AR tests
pei = J(N, 1, NULL)
Subscripts = SubscriptsStart
for (i = N; i--) {
pei[i] = &((*pe)[|Subscripts|])
Subscripts = Subscripts - SubscriptsStep
if (small) {
tmp = wttot/(wttot - k)
V = V * (onestepnonrobust? tmp : (rows(clusts)? (NObs-1)/(NObs-k) : (NObs-1)/(NObs-k)*NGroups/(NGroups-1)) )
sig2 = sig2 * tmp
// In case constant is in column space of X, even despite noconstant, bump it out for F/Wald test
DFm_keep = _rmcoll(SystemGMM?
// for system GMM, drop difference equation from model fit test
X[SortByEqID[|NT+1 \ .|], .] :
consopt, 0)
if (cols(DFm_keep) > consopt) {
DFm = cols(DFm_keep = DFm_keep[|.\cols(DFm_keep)-consopt|])
b2 = b[DFm_keep]
if (small)
Fp = Ftail(DFm, DFr = (onestepnonrobust? NObsEff - DFm : (rows(clusts)? MinNClust : NGroups)) - consopt,
= quadcross(b2, invsym(V[DFm_keep', DFm_keep])) * b2 / DFm)
Waldp = chi2tail(DFm, Wald = quadcross(b2, invsym(V[DFm_keep', DFm_keep])) * b2)
if (small)
DFr = (onestepnonrobust? NObsEff : (rows(clusts)? MinNClust : NGroups)) - consopt
if (diffsargan & !pca) {
diffsargans = J(5, NDiffSargans, .)
A1diag = diagonal(A1)
= j_IV ? 1..j_IV
: J(1, 0, 0)
p_AllGMM = j_GMM? 1..j_GMM : J(1, 0, 0)
for (g = NDiffS g--) {
p_IV = *InstOptInd[g, 1]
p_GMM = *InstOptInd[g, 2]
== . ? p_AllIV
: p_IV ) ,
(p_GMM == . ? p_AllGMM : p_GMM) :+ j_IV
NOtherInsts = sum(A1diag[p] :!= 0)
if (NOtherInsts >= k & NOtherInsts < j0) { // invalid if # remaining insts < # of regressors
XZA = quadcross(ZXp = ZX[p,], App = invsym(S[p,p]))
ZeDiffSargan = _Ze(Y - X * (invsym(XZA * ZXp) * (XZA * ZY[p])), p_IV, p_GMM, N, T, NT, SystemHeight, orthogonal, RowsPerGroup,
j_GMM, touse, GMMinsts, SubscriptsStart, SubscriptsStep, Z_IV, Z_GMM, tsfmt, tmin, tdelta)
diffsargans[2, g] = (hansen==.? sargan : hansen) - (diffsargans[1, g] = quadcross(ZeDiffSargan, App * ZeDiffSargan)) // unrestricted Sargan, and difference
diffsargans[3, g] = j0 - NOtherInsts
// # insts in group
diffsargans[4, g] = chi2tail(sarganDF - diffsargans[3, g], diffsargans[1, g]) // p value
diffsargans[5, g] = chi2tail(diffsargans[3, g], diffsargans[2, g])
// p value
NDiffSargans = rows(InstOptTxt = select(InstOptTxt, diffsargans[1,]' :!= .))
diffsargans = select(diffsargans, diffsargans[1,] :!= .)
_ARTests(arlevels, artests, onestepnonrobust, h, N, T, NT, SystemHeight, RowsPerGroup, sig2, orthogonal, SystemGMM, j, j_IV, j_GMM, touse, SortByEqID, Complete,
X, X0, Y0, Z_IV, Z_GMM, b, weights, wt, wt0, pe, pei, ARz, ARp, SubscriptsStep, SubscriptsStart, GMMinsts, ZGMMnames, tsfmt, tmin, tdelta, m2VZXA, keep, pV)
st_local("b", bname=st_tempname())
st_matrix(bname, b')
Stripe = J(k, 1, ""), Xnames'
st_matrixcolstripe(bname, Stripe)
st_local("V", Vname=st_tempname())
st_matrix(Vname, V)
st_matrixcolstripe(Vname, Stripe)
st_matrixrowstripe(Vname, Stripe)
stata("est post " + bname + " " + Vname + "," + (small? "dof(" + strofreal(DFr)+")" : "") + " obs(" + strofreal(NObs) +
") esample(" + touseName +") depname(" + VarlistNames[1] + ")")
st_numscalar("e(sargan)", sargan)
st_numscalar("e(sar_df)", sarganDF)
st_numscalar("e(sarganp)", sarganp)
st_matrix("e(Ze)", Ze)
if (hansen != .) {
st_numscalar("e(hansen)", hansen)
st_numscalar("e(hansen_df)", sarganDF)
st_numscalar("e(hansenp)", hansenp)
if (cols(diffsargans)) {
st_matrix("e(diffsargan)", diffsargans)
st_matrixcolstripe("e(diffsargan)", (J(NDiffSargans, 1, ""), substr(subinstr(InstOptTxt, ".", ""), 1, 32)))
st_matrixrowstripe("e(diffsargan)", (J(5, 1, ""),
("Unrestricted Sargan/Hansen"\"Difference in Sargan"\"Instruments generated"\"Unrestricted Sargan p"\"Difference in Sargan p")))
bname = ""
for (i=1; i<=rows(InstOptTxt); i++) st_global("e(diffgroup"+strofreal(i)+")", InstOptTxt[i])
st_matrix("e(A1)", A1)
if (A2 != .) st_matrix("e(A2)", A2)
if (pca) {
st_global("e(pca)", "pca")
st_numscalar("e(components)", j_GMM)
st_matrix("e(eigenvalues)", eigenvalues)
st_numscalar("e(pcaR2)", quadsum(eigenvalues[|.,.\.,j_GMM|])/cols(eigenvalues))
st_numscalar("e(kmo)", KMO)
st_global("e(depvar)", VarlistNames[1])
st_numscalar("e(N)", NObs)
st_numscalar("e(sig2)", sig2)
st_numscalar("e(sigma)", sqrt(sig2))
st_numscalar("e(artests)", artests)
st_global("e(transform)", orthogonal? "orthogonal deviations" : "first differences")
st_numscalar("e(g_min)", TiMinMax[1])
st_numscalar("e(g_max)", TiMinMax[2])
st_numscalar("e(N_g)", NGroups)
st_numscalar("e(df_m)", DFm)
st_numscalar("e(h)", h)
if (rows(clusts)) {
st_global("e(clustvar)", st_local("cluster"))
for (c=i=1; i<=rows(clusts); i++)
if (clusts[i].OneVar)
st_numscalar("e(Nclust"+strofreal(c++)+")", clusts[i].N)
if (strlen(wexp)) {
st_global("e(wtype)", wtype)
st_global("e(wexp)", wexp)
if (small) {
st_numscalar("e(F)", F)
st_numscalar("e(F_p)", Fp)
st_numscalar("e(df_r)", DFr)
st_global("e(small)", "small")
st_numscalar("e(chi2)", Wald)
st_numscalar("e(chi2p)", Waldp)
st_numscalar("e(g_avg)", NObs / NGroups)
for (i=1; iBaseVarNames)
equation = equation, IV->equation
passthru = passthru, IV->passthru
IV = IV->next
st_matrix("e(ivequation)", equation)
st_matrix("e(ivpassthru)", passthru)
st_matrix("e(ivmz)",
if (favorspeed() & (svmat | svvar)) Z_GMM = Z_IV, Z_GMM
if (svmat) {
LabelName = st_tempname()
stata("egen " + st_tempname() + "= group(" + idName + "), lname(" + LabelName + ")")
Stripe = substr(vec(st_vlmap(LabelName, ((1..N)#J(1,SystemGMM+1,1))) :+ J(1, SystemGMM+1, J(1, N, ", ":+strofreal((0::T-1)*tdelta:+tmin, tsfmt)))), 1, 32)
Stripe = J(rows(Stripe), 1, ""), Stripe
st_vldrop(LabelName)
st_matrix("e(X)", X)
st_matrixcolstripe("e(X)", substr((J(k, 1, ""), Xnames'), 1, 32))
st_matrixrowstripe("e(X)", Stripe)
st_matrix("e(Y)", Y)
st_matrixcolstripe("e(Y)", ("", substr(VarlistNames[1], 1, 32)))
st_matrixrowstripe("e(Y)", Stripe)
st_matrix("e(H)", H)
st_matrix("e(ideqt)", ideqt)
st_matrixcolstripe("e(ideqt)", ("","id"\"","eq"\"","t"))
if (weights) {
st_matrix("e(wt)", wt)
st_matrixrowstripe("e(wt)", Stripe)
for (c=i=1; i= 1100) st_matrixcolstripe("e(eigenvectors)", (J(cols(eigenvalues), 1, ""), strofreal(eigenvalues',"%32.12f")))
st_matrixrowstripe("e(eigenvectors)", substr(eigenvectorBasisNames, 1, 32))
if (svvar) {
stata("gsort -" + idSampleName + " +" + idName + " +" + tName)
Vname = orthogonal? "orthog" : "diff"
p = SystemGMM? (J(N,1,1::T) + 2*T*(0::N-1)#J(T,1,1))[Fill] : Fill
stata("capture matrix rename sample" + Vname + " " + (bname=st_tempname()))
st_matrix("sample"+Vname, touse[p])
stata("capture noisily svmat double sample"+Vname)
stata("capture matrix drop sample"+Vname)
stata("capture matrix rename " + bname + " sample"+Vname)
stata("capture matrix rename y" + Vname + " " + (bname=st_tempname()))
st_matrix("y"+Vname, Y[p])
stata("capture noisily svmat double y"+Vname)
stata("capture matrix drop y"+Vname)
stata("capture matrix rename " + bname + " y"+Vname)
stata("capture matrix rename x" + Vname + " " + (bname=st_tempname()))
st_matrix("x"+Vname, X[p,])
stata("capture noisily svmat double x"+Vname)
stata("capture matrix drop x"+Vname)
stata("capture matrix rename " + bname + " x"+Vname)
if (favorspeed()) {
stata("capture matrix rename z" + Vname + " " + (bname=st_tempname()))
Z_IV = Z_GMM[p,]
Z_IV = select(Z_IV, colsum(Z_IV:!=0))
if (cols(Z_IV)) {
st_matrix("z"+Vname, select(Z_IV, colsum(Z_IV:!=0)))
stata("capture noisily svmat double z"+Vname)
stata("capture matrix drop z"+Vname)
stata("capture matrix rename " + bname + " z"+Vname)
if (SystemGMM) {
p = p :+ T
stata("capture matrix rename ylev " + (bname=st_tempname()))
st_matrix("samplelev", touse[p])
stata("capture noisily svmat double samplelev")
stata("capture matrix drop samplelev")
stata("capture matrix rename " + bname + " samplelev")
stata("capture matrix rename ylev " + (bname=st_tempname()))
st_matrix("ylev", Y[p])
stata("capture noisily svmat double ylev")
stata("capture matrix drop ylev")
stata("capture matrix rename " + bname + " ylev")
stata("capture matrix rename xlev " + (bname=st_tempname()))
st_matrix("xlev", X[p,])
stata("capture noisily svmat double xlev")
stata("capture matrix drop xlev")
stata("capture matrix rename " + bname + " xlev")
if (favorspeed()) {
stata("capture matrix rename zlev " + (bname=st_tempname()))
Z_IV = Z_GMM[p,]
Z_IV = select(Z_IV, colsum(Z_IV:!=0))
if (cols(Z_IV)) {
st_matrix("zlev", select(Z_IV, colsum(Z_IV:!=0)))
stata("capture noisily svmat double zlev")
stata("capture matrix drop zlev")
stata("capture matrix rename " + bname + " zlev")
i = 1; GMM = GMM equation = passthru = collapse = orthogonal = J(1, 0, 0); laglimits = J(2, 0, 0)
while (GMM != NULL) {
st_global("e(gmminsts"+strofreal(i++)+")", GMM->BaseVarNames)
= equation,
GMM->equation
= passthru,
GMM->passthru
= collapse,
GMM->collapse
= laglimits,
GMM->Laglim'
orthogonal = orthogonal, GMM->InstOrthogonal
GMM = GMM->next
st_matrix("e(gmmequation)",
st_matrix("e(gmmpassthru)",
st_matrix("e(gmmcollapse)",
st_matrix("e(gmmlaglimits)",
laglimits)
st_matrix("e(gmmorthogonal)", orthogonal)
st_global("e(ivar)", idName)
st_global("e(tvar)", tName)
st_numscalar("e(j)", j0)
st_numscalar("e(j0)", j)
st_global("e(esttype)", SystemGMM? "system" : "difference")
st_global("e(artype)", arlevels? "levels" : "first differences")
st_local("level", LevelArg)
st_global("e(marginsok)", "XB default")
st_global("e(predict)", "xtab2_p")
st_global("e(cmd)", "xtabond2")
real scalar _ParseInsts(real scalar j_IV, real scalar j_GMM, real scalar NIVOptions, real scalar NGMMOptions, pointer(struct IVinst scalar) IV,
pointer(struct GMMinst scalar) GMM, real scalar SystemGMM,
real colvector Complete, real scalar N, real scalar T, real scalar NT, string scalar idSampleName, real colvector Fill, real scalar orthogonal) {
string scalar ivstyle, gmmstyle, optionsArg, LaglimArg, EquationArg
string rowvector LaglimStr
string colvector BaseNames
pointer (string colvector) pBaseNames
real scalar split, steps, EquationTokenCount
real matrix Base, tmp
pointer(real matrix) scalar pBase
pointer next
struct GMMinst scalar sGMM
st_local("0", "," + st_local("options"))
stata("syntax [, IVstyle(string) *]")
NIVOptions = j_IV
while ((ivstyle = st_local("ivstyle")) != "") {
NIVOptions++
// Insert vector of paramaters and data describing IV inst set at head of linked list of such vectors
next = IV; (*(IV = &(IVinst()))).next = next
// add new IVinst group to linked list
optionsArg = st_local("options")
st_local("0", ivstyle)
stata("capture syntax varlist(numeric ts" + (st_numscalar("c(stata_version)") >= 11 ? " fv" : "") + "), [Equation(string) Passthru MZ]")
stata("loc _rc = _rc")
if (st_local("_rc") != "0") {
printf("{err}ivstyle(%s) invalid.\n", ivstyle)
return (198)
if (st_numscalar("c(stata_version)") >= 11) {
stata("fvexpand " + st_local("varlist"))
IV->BaseVarNames = st_global("r(varlist)")
IV->BaseVarNames = st_local("varlist")
st_local ("0", "," + (EquationArg = st_local("equation")))
EquationTokenCount = cols(tokens(EquationArg))
stata("capture syntax, [Diff Level Both]")
stata("loc _rc = _rc")
if (EquationTokenCount > 1 | st_local("_rc") != "0") {
printf("{err}equation(%s) invalid.\n", EquationArg)
return (198)
// 0=eq(level), 1=eq(diff), 2=eq(both) (default)
IV->equation = !EquationTokenCount | st_local("both")!="" ? 2 : st_local("level")==""
if (!(SystemGMM | IV->equation)) {
printf ("{txt}Instruments for levels equations only ignored since noleveleq specified.\n")
IV->passthru = st_local("passthru") != ""
IV->mz = st_local("mz") != ""
if (IV->passthru & IV->equation==2 & SystemGMM) {
printf ("{err}passthru not valid with equation(both) in system GMM.\n")
return (198)
IV->ivstyle = "iv(" + IV->BaseVarNames
if (IV->mz | IV->passthru | IV->equation != 2) {
IV->ivstyle = IV->ivstyle + ","
if (IV->passthru)
IV->ivstyle = IV->ivstyle + " passthru"
if (IV->mz)
IV->ivstyle = IV->ivstyle + " mz"
if (IV->equation !=2) IV->ivstyle = IV->ivstyle + " eq(" + (IV->equation? "diff" : "level") + ")"
IV->ivstyle = IV->ivstyle + ")"
tmp = st_data(., tokens(IV->BaseVarNames), idSampleName)
(IV->Base = J(NT, cols(tmp), .))[Fill, .] = tmp
Complete = Complete :& !rowmissing(IV->Base)
j_IV = j_IV + cols(IV->Base)
st_local("0", "," + optionsArg)
stata("syntax [, IVstyle(string) *]")
st_local("0", "," + st_local("options"))
stata("syntax [, GMMstyle(string) *]")
GMM = NULL; j_GMM = 0
NGMMOptions = 0
while ((gmmstyle = st_local("gmmstyle")) != "") {
next = GMM; (*(GMM = &(GMMinst()))).next = next
// add new GMMinst group to linked list
optionsArg = st_local("options")
st_local ("0", gmmstyle)
stata("capture syntax anything, [SPlit *]") // strip out this suboption first so it won't appear in diff-sargan reports
stata("local _rc = _rc")
if (st_local("_rc") != "0") {
printf("{err}gmmstyle(%s) invalid.\n", gmmstyle)
return (198)
split = strlen(st_local("split")) > 0
if (strlen(gmmstyle = st_local("options")))
gmmstyle = st_local("anything") + ", " + gmmstyle
gmmstyle = st_local("anything")
st_local ("0", gmmstyle)
stata("capture syntax varlist(numeric ts" + (st_numscalar("c(stata_version)") >= 11 ? " fv" : "") + "), [Equation(string) Laglimits(string) Collapse Passthru Orthogonal]")
stata("loc _rc = _rc")
if (st_local("_rc") != "0") {
printf("{err}gmmstyle(%s) invalid.\n", gmmstyle)
return (198)
GMM->passthru
= st_local("passthru") != ""
GMM->collapse
= st_local("collapse") != ""
GMM->InstOrthogonal = st_local("orthogonal") != ""
if (stataversion()>= 1100) {
stata("fvexpand " + st_local("varlist"))
GMM->BaseVarNames = st_global("r(varlist)")
GMM->BaseVarNames = st_local("varlist")
st_local ("0", "," + (EquationArg = st_local("equation")))
EquationTokenCount = cols(tokens(EquationArg))
stata("capture syntax, [Diff Level Both]")
stata("loc _rc = _rc")
if (EquationTokenCount > 1 | st_local("_rc") != "0") {
printf("{err}equation(%s) invalid.\n", EquationArg)
return (198)
// 0=eq(level), 1=eq(diff), 2=eq(both) (default)
GMM->equation = !EquationTokenCount | st_local("both")!="" ? 2 : st_local("level")==""
if (GMM->InstOrthogonal & !orthogonal & GMM->equation) {
printf("{res}Warning: backward-orthogonal-deviations are usually not valid unless forward-orthgonal-deviations regressors are specified.\n")
printf("{res}
I.e., {inp}orthogonal{res} option should accompany the {inp}orthogonal{res} suboption of {inp}gmmstyle(){res} option.\n")
if (split & GMM->equation != 2) {
printf("{res}Warning: split has no effect in combination with equation(%s).\n", EquationArg)
if (split & !SystemGMM) {
printf("res}Warning: split has no effect in Difference GMM.\n")
if (!(SystemGMM | GMM->equation)) {
printf ("{txt}Instruments for levels equations only ignored since noleveleq specified.\n")
GMM = next
if (SystemGMM & GMM->passthru & GMM->equation==2) {
printf("{err}passthru not valid with equation(both) in system GMM.\n")
return (198)
if (cols(LaglimStr = tokens(LaglimArg = st_local("laglimits")))) {
if (cols(LaglimStr) != 2) {
printf("{err}Laglimits(%s) must have two arguments.\n", LaglimArg)
return (198)
if (missing(GMM->Laglim = strtoreal(LaglimStr)) > sum(LaglimStr :== ".")) {
printf("{err}Laglimits(%s) invalid.\n", LaglimArg)
return (198)
if (GMM->Laglim[1] == .)
GMM->Laglim[1] = 1
if (GMM->Laglim[1] > GMM->Laglim[2])
GMM->Laglim = GMM->Laglim[(2,1)]
GMM->Laglim = 1, .
GMM->gmmstyle = "gmm(" + GMM->BaseVarNames + ","
if (GMM->collapse)
GMM->gmmstyle = GMM->gmmstyle + " collapse"
if (GMM->passthru)
GMM->gmmstyle = GMM->gmmstyle + " passthru"
if (GMM->InstOrthogonal) GMM->gmmstyle = GMM->gmmstyle + " orthogonal"
if (split)
GMM->equation = 1
if (GMM->equation != 2) GMM->gmmstyle = GMM->gmmstyle + " eq(" + (GMM->equation? "diff" : "level") + ")"
GMM->gmmstyle = GMM->gmmstyle + " lag(" + strofreal(GMM->Laglim[1]) + " " + strofreal(GMM->Laglim[2]) + ")"
// Get base vars filled out to NT rows
tmp = st_data(. , tokens(GMM->BaseVarNames), idSampleName)
(Base = J(NT, cols(tmp), .))[Fill, .] = tmp
pBase = &(GMM->InstOrthogonal? _Orthog(Base, N, T, NT, Complete, 0, 0) : Base)
BaseNames = tokens(GMM->BaseVarNames)'
if (!GMM->collapse) {
pBase = &_Explode(*pBase, N, T, NT)
GMM->BaseNameTs = (0::T-1) # J(rows(BaseNames), 1, 1)
BaseNames = J(T, 1, BaseNames :+ (stataversion()InstOrthogonal? &("D.":+BaseNames) : xtabond2_clone(BaseNames)
GMM->MakeExtraInsts = GMM->equation==2 & SystemGMM
// Need to make extra instruments, as in standard Blundell-Bond?
GMM->BaseAll = SystemGMM | !(GMM->equation | GMM->passthru)?
&editmissing(*pBase, 0) \ (GMM->collapse?
&_Difference(Base, N, T, NT, Complete, 0, 0) :
&_Explode(_Difference(Base, N, T, NT, Complete, 0, 0), N, T, NT)) :
&editmissing(*pBase, 0)
GMM->BaseNamesAll = SystemGMM | !(GMM->equation | GMM->passthru)? pBaseNames \ &("D.":+*pBaseNames) : pBaseNames
for (steps= steps>=0; steps--) { // run twice for split groups
NGMMOptions++
GMM->FullInstSetEq = !GMM->equation
// Should the full instrument set apply to levels equation?
GMM->FullInstSetDiffed = !(GMM->equation | GMM->passthru) // Exploded instrument based on differences?
j_GMM = j_GMM + (GMM->NumInsts = cols(Base) * _GMMinstPerBaseVar(GMM, T))
GMM->gmmstyle = GMM->gmmstyle + (split? (GMM->equation?" eq(diff)":" eq(level)")+" lag("+strofreal(GMM->Laglim[1])+" "+strofreal(GMM->Laglim[2])+")" : "") + ")"
GMM->NumBaseVars = cols(Base)
if (split & steps) { // run at end of 1st iteration of split group
sGMM = *GMM
sGMM.next = GMM
GMM = &sGMM
GMM->equation = 0
GMM->Laglim = J(1, 2, GMM->Laglim[1] - 1)
// levels inst for gmm(X, lag(a b)) specified by gmm(X, lag(a-1 a-1) eq(lev))
st_local("0", "," + optionsArg)
stata("syntax [, GMMstyle(string) *]")
return (0)
void _MakeIVinsts(pointer(real rowvector) matrix InstOptInd, string rowvector InstOptTxt, real scalar g, real matrix Z_IV, real scalar j_IV,
real scalar SystemHeight, real scalar N, real scalar T, real scalar NT, real scalar NIVOptions, pointer(struct IVinst scalar) scalar IVinsts,
pointer (real matrix function) pfnXform, real colvector Complete, real scalar NDiffSargans, real scalar SystemGMM, string matrix ZIVnames) {
pointer(struct IVinst scalar) scalar IV
InstOptInd = J(NDiffSargans, 2, &.) // holds col indices for complements of each instument group--IV and GMM separate
InstOptTxt = J(NDiffSargans, 1, " ")
g = NDiffSargans
Z_IV = J(SystemHeight, j_IV, 0); ZIVnames = J(0, 1, "")
if (NIVOptions) {
IV = IVinsts
while (IV != NULL) {
ZIVnames = ZIVnames \ tokens(IV->BaseVarNames)'
if (IV->equation)
Z_IV[|1,i+1 \ NT, i+cols(IV->Base)|] = IV->mz?
editmissing((IV->passthru? IV->Base :
(*pfnXform)(IV->Base, N, T, NT, Complete, 1)),0) :
(IV->passthru? IV->Base : (*pfnXform)(IV->Base, N, T, NT, Complete, 1))
if (SystemGMM & IV->equation != 1)
Z_IV[|NT+1,i+1 \ SystemHeight, i+cols(IV->Base)|] = IV->mz? editmissing(IV->Base,0) : IV->Base
if (strlen(IV->ivstyle)) { // leave constant term out of diff-sargan testing
p = 0..i , i+1+cols(IV->Base)..j_IV+1
InstOptInd[g, 1] = &(cols(p)>2 ? p[|2 \ cols(p) - 1|] : J(1, 0, .))
InstOptTxt[g--] = IV->ivstyle
i = i + cols(IV->Base)
IV = IV->next
ZIVnames = J(rows(ZIVnames), 1, ""), ZIVnames
// AR test. e=full residual vector. w = (weighted) diff or levels residuals tested for AR(). wl = unweighted lagged residuals
void _ARTests (real scalar arlevels, real scalar artests, real scalar onestepnonrobust, real scalar h, real scalar N, real scalar T, real scalar NT, real scalar SystemHeight, real scalar RowsPerGroup,
real scalar sig2, real scalar orthogonal, real scalar SystemGMM, real scalar j, real scalar j_IV, real scalar j_GMM, real colvector touse, real colvector SortByEqID, real colvector Complete,
real matrix X, real matrix X0, real colvector Y0, real matrix Z_IV, real matrix Z_GMM, real colvector b, real scalar weights, real colvector wt, real colvector wt0,
pointer (real colvector) pe, pointer (real matrix) colvector pei, real colvector ARz, real colvector ARp, real matrix SubscriptsStep, real matrix SubscriptsStart,
pointer(struct GMMinst scalar) GMMinsts, string matrix ZGMMnames, string scalar tsfmt, real scalar tmin, real scalar tdelta, real matrix m2VZXA,
real rowvector keep, pointer (real matrix) pV) {
real colvector p, touse2, w, wl, ZHw, _wt, wli
real matrix H, psit, psiw, sum_wwli, Subscripts, tmp
pointer (real matrix) pX
real scalar lag, wHw, i
if (onestepnonrobust) {
H = _H(arlevels? 1 : h, 0, 0, 0, T) * sig2
psit = _H(h, 0, orthogonal, 0, T) * sig2 //psi'
if (SystemGMM) psit = arlevels? J(T, T, 0), psit : psit, J(T, T, 0)
touse2 = colshape(SystemGMM? touse[p = SortByEqID[|arlevels? NT+1 \ SystemHeight : . \ NT|]] : touse, T)'
if (orthogonal & arlevels == 0) { // Get residuals in first differences for AR() test
wl = _Difference(Y0 - (X0 = X0[, keep]) * b, N, T, NT, Complete, 0)
pX = &_Difference(X0, N, T, NT, Complete, 0)
if (weights) {
pX = &(*pX :* wt0)
w = colshape(wl :* wt0, T)'
wl = colshape(wl, T)'
wl = w = colshape(wl, T)'
if (SystemGMM) {
w = (*pe)[p] // get residuals subject to AR() test
pX = &X[p,]
if (weights) {
wl = colshape(w :/ wt0, T)'
w = colshape(w, T)'
wl = w = colshape(w, T)'
ARz = ARp = J(artests, 1, .)
for (lag=1; lagFullInstSetEq & GMM->FullInstSetDiffed)
// the most one could lag in the "full" instrument set
ForwardMax = T - 2 + GMM->FullInstSetEq
// the most one could "forward" therein
GMM->Laglim = max((-ForwardMax, GMM->Laglim[1])) , min((LagMax, GMM->Laglim[2]))
if (GMM->collapse)
return (GMM->Laglim[2] - GMM->Laglim[1] + 1 + GMM->MakeExtraInsts)
if (GMM->Laglim[1] > 0 | GMM->Laglim[2] Laglim[1] >= 0) {
N1 = LagMax - GMM->Laglim[1] + 1
N2 = LagMax - GMM->Laglim[2]
N1 = ForwardMax + GMM->Laglim[2] + 1
N2 = ForwardMax + GMM->Laglim[1]
InstsPerBaseVar = (N1*(N1+1) - N2*(N2+1))/2
N1 = ForwardMax + GMM->Laglim[1]
N2 = LagMax - GMM->Laglim[2]
InstsPerBaseVar = (LagMax+1)*(ForwardMax+1) - (N1*(N1+1) + N2*(N2+1))/2
return (GMM->MakeExtraInsts? InstsPerBaseVar + T - GMM->FullInstSetDiffed : InstsPerBaseVar)
real matrix _MakeGMMinsts(real scalar N, real scalar T, real scalar NT, real scalar SystemHeight, real scalar orthogonal, real scalar RowsPerGroup,
real scalar j_GMM, real colvector touse, pointer(struct GMMinst scalar) scalar GMMinsts, string matrix ZGMMnames, string scalar tsfmt,
real scalar tmin, real scalar tdelta,
| real scalar id) // If id!=., restricts Z to just individual id
real scalar c, Lag, LagStop, SearchDir, InstOffset, Zeros, _N, _NT, _SystemHeight
real matrix Z, SubscriptsBase, SubscriptsInst, SubscriptsStep, NewInsts
real colvector p
pointer(real matrix) colvector BaseAll
pointer(struct GMMinst scalar) scalar GMM
if (id != .) {
_SystemHeight = RowsPerGroup
_SystemHeight = SystemHeight
Z = J(_SystemHeight, j_GMM, 0)
ZGMMnames = J(j_GMM, 2, " ")
SubscriptsInst = J(2, 2, 1)
GMM = GMMinsts
while (GMM != NULL) {
BaseAll = GMM->BaseAll
if (id != .)
for (c=1; ccollapse? 0 : GMM->NumBaseVars)
SubscriptsBase = GMM->Laglim[1]+GMM->FullInstSetEq > 0? 1+GMM->FullInstSetDiffed \ _NT-GMM->Laglim[1] : 2-GMM->FullInstSetEq-GMM->Laglim[1] \ _NT
SubscriptsBase = SubscriptsBase, (GMM->collapse? 1 \ GMM->NumBaseVars : (SubscriptsBase[1]-1) * GMM->NumBaseVars + 1 \ (SubscriptsBase[2] + T - _NT) * GMM->NumBaseVars)
InstOffset = GMM->FullInstSetEq * _NT + GMM->Laglim[1]
for (Lag = GMM->Laglim[1]; Lag Laglim[2]; Lag++) {
SubscriptsInst = SubscriptsBase[,1] :+ InstOffset++ , SubscriptsBase[,2] :+ SubscriptsInst[1,2]-SubscriptsBase[1,2]
NewInsts = (*BaseAll[GMM->FullInstSetDiffed+1])[|SubscriptsBase|]
if (Lag & _N > 1) {
// 0 out rows that shifted into adjacent group
Zeros = abs(Lag) + (!GMM->FullInstSetEq & LagFullInstSetEq? "Levels" : (orthogonal? "Orthog eq" : "Diff eq")) ,
_LF(Lag) :+ (*GMM->BaseNamesAll[GMM->FullInstSetDiffed+1]:+(GMM->collapse? "" : strofreal((GMM->BaseNameTs:+Lag)*tdelta:+tmin,tsfmt)))[|SubscriptsBase[,2]|]
SubscriptsInst[1,2] = SubscriptsInst[2,2] + 1
// To move base frame, raise the top end toward t=1, or, if it's flush against t=1, raise its bottom end
if (Lag>=0) SubscriptsBase[2,] = SubscriptsBase[2,] - SubscriptsStep
if (LagFullInstSetDiffed) SubscriptsBase[1,] = SubscriptsBase[1,] - SubscriptsStep
if (GMM->MakeExtraInsts) {
if (GMM->Laglim[1] > 0) { // if both lags positive, start at lag Laglim[1]-1 (as is standard) and search to deeper lags if neceesary
Lag = GMM->Laglim[1] - 1
LagStop = GMM->Laglim[2] - 1
SearchDir = 1
if (GMM->Laglim[2] > 0) { // if lags straddle 0, start at lag 0 and search to deeper lags (which could miss workable negative lags, but we only need one)
LagStop = GMM->Laglim[2] - 1
SearchDir = 1
} else { // if both lags non-positive, start at lag Laglim[2]-1 and search to deeper forwards if neceesary
Lag = GMM->Laglim[2] - 1
SearchDir = -1
LagStop = GMM->Laglim[1] - 1
SubscriptsBase[,1] = Lag >= 0? 1 \ _NT-Lag : 1-Lag \ _NT
SubscriptsBase[,2] = GMM->collapse? 1 \ GMM->NumBaseVars : (SubscriptsBase[1,1]-1) * GMM->NumBaseVars + 1 \ (SubscriptsBase[2,1] + T - _NT) * GMM->NumBaseVars
SubscriptsInst = SubscriptsBase[,1] :+ Lag+_NT , SubscriptsBase[,2] :+ SubscriptsInst[1,2]-SubscriptsBase[1,2]
NewInsts = (*BaseAll[2])[|SubscriptsBase|]
if (Lag & _N > 1) {
// 0 out rows that shifted into adjacent group
Zeros = abs(Lag)
p = 0 :: Zeros * (_N - 1) - 1
p = trunc(p :/ Zeros) :* T + mod(p, Zeros) :+ T - Zeros + 1
NewInsts[p, .] = J(rows(p), cols(NewInsts), 0)
Z[|SubscriptsInst|] = NewInsts
ZGMMnames[|SubscriptsInst[,2],(.\.)|] = J(cols(NewInsts), 1, "Levels eq"), _LF(Lag):+((*GMM->BaseNamesAll[2]):+(GMM->collapse? "" : (GMM->collapse? "" : strofreal((GMM->BaseNameTs:+Lag)*tdelta:+tmin, tsfmt))))[|SubscriptsBase[,2]|]
SubscriptsInst[1,2] = SubscriptsInst[2,2] + 1
// If any of these instruments happens to be 0 for all included observations, try shifting it to other t's in the instrument matrix
if (SearchDir == 1)
for (c=SubscriptsInst[1,2]; c<=SubscriptsInst[2,2]; c++)
for (; Lag<LagStop & !any(Z[,c] :& touse); Lag++) {
Z[|2,c \ .,c|] = Z[|1,c \ _SystemHeight-1, c|]
Z[_NT+1, c] = 0
for (c=SubscriptsInst[1,2]; cLagStop & !any(Z[,c] :& touse); Lag--) {
Z[|1,c \ _SystemHeight-1, c|] = Z[|2,c \ .,c|]
Z[_SystemHeight, c] = 0
GMM = GMM->next
return (Z)
real rowvector _rmcoll(real matrix X, real scalar hascons, real scalar nocons, | string rowvector varnames) {
real matrix U,
real scalar i, pointer(real matrix) scalar pX
if (cols(X)<=1)
return (cols(X))
if (rows(X)) {
if (nocons) {
if (hascons) {
t = X[,cols(X)]
pX = &(X - quadcross(X, t)'/sum(t) # t) // partial out constant term, which in Sys GMM has both 0's and 1's, to prevent it being picked for dropping
U = quadcross(*pX, *pX)
means = mean(X)
U = quadcrossdev(X, means, X, means)
if (hascons) U = U[|.,. \ cols(U)-1,cols(U)-1|]
diag = sqrt(diagonal(U))
U = (U :/ diag) :/ diag' // normalize
_edittozero(U = diagonal(invsym(U)), 10000)
keep = J(1, cols(X) - sum(!U), cols(X)) // if hascons=1 then last entry will default to cols(X), meaning keep constant, the last term
for (i = 1; i
1) X2[(1::N-1) :* T :+ 1, .] = J(N-1, cols(X), MissingFillValue)
if (MissingFillValue != .) _editmissing(X2, MissingFillValue)
return (X2)
real matrix _Orthog(real matrix X, real scalar N, real scalar T, real scalar NT, real colvector Complete, real scalar forward, | real scalar MissingFillValue) {
pragma unused N
real matrix X2; real scalar i, j, it, _it, NLeadingV real rowvector SumLeadingVals
X2 = J(NT, j=cols(X), MissingFillValue)
for (i = it = NT; i = i - T) {
NLeadingVals = 0; SumLeadingVals = J(1, j, 0)
for (; it > i - T; it--) {
_it = forward? it : NT + 1 - it
if (NLeadingVals)
X2[_it + forward,] = sqrt(1-1/(NLeadingVals+1)) * (X[_it, .] - SumLeadingVals / NLeadingVals)
if (Complete[_it]) {
NLeadingVals++
SumLeadingVals = SumLeadingVals + X[_it,]
return (X2)
real matrix _Explode(real matrix Base, real scalar N, real scalar T, real scalar NT) {
real matrix BaseWide, pr, pc, prStep, pcS real scalar NumBaseVars, TNumBaseVars
BaseWide = J(NT, TNumBaseVars = T * (NumBaseVars=cols(Base)), 0)
pc = TNumBaseVars-NumBaseVars+1 .. TNumBaseVars
pcStep = J(1, NumBaseVars, NumBaseVars)
prStep = J(N, 1, 1)
for (pr = (1::N) :* T; pr[1,1]; pr = pr - prStep) {
BaseWide[pr, pc] = Base[pr, .]
pc = pc - pcStep
return(BaseWide)
void __lag(real matrix X, | real scalar lag) {
if (lag > 0) {
if (lag == .) lag = 1
X[|1+lag,. \ .,.|] = X[|.,. \ rows(X)-lag,.|]
X[|.,. \ lag,.|] = J(lag, cols(X), 0)
} else if (lag 0? "L" : "F") + strofreal(abs(lag)) + ".": "")
pointer (transmorphic matrix) scalar xtabond2_clone(transmorphic matrix X) {
transmorphic matrix Y
return(&(Y = X))
// return matrix whose rows are all the subsets of a row of strings. Null is at top.
string matrix strCombs(string rowvector symbols) {
string matrix t
if (cols(symbols)==1)
return ("" \ symbols)
t = strCombs(symbols[|2\.|])
return ((J(rows(t),1,""), t) \ (J(rows(t),1,symbols[1]), t))
mata mlib create lxtabond2, dir(PLUS) replace
mata mlib add lxtabond2 *(), dir(PLUS)
mata mlib index

我要回帖

更多关于 hansen过度识别检验 的文章

 

随机推荐