1 Star 0 Fork 20

xinjunlyu/gtfpch

forked from kerrydu/gtfpch 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
teddf3.ado 54.66 KB
一键复制 编辑 原始数据 按行查看 历史
kerrydu 提交于 2021-04-23 09:50 . revised20210423
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
*! version 3.0, 20 Apr 2021
* use frame to store results
*! version 3.0, 6 Apr 2021
* add subsample bootstrap
*! version 2.1, 7 Aug 2020
*! version 2.0
* By Kerry Du, Daoping Wang, Ning Zhang, 30 Jul 2020
**
*
capture program drop teddf3
program define teddf3, rclass
version 16
gettoken word 0 : 0, parse(" =:,")
while `"`word'"' != ":" & `"`word'"' != "=" {
if `"`word'"' == "," | `"`word'"'=="" {
error 198
}
local invars `invars' `word'
gettoken word 0 : 0, parse("=:,")
}
unab invars : `invars'
gettoken word 0 : 0, parse(" =:,")
while `"`word'"' != ":" & `"`word'"' != "=" {
if `"`word'"' == "," | `"`word'"'=="" {
error 198
}
local gopvars `gopvars' `word'
gettoken word 0 : 0, parse(" =:,")
}
unab gopvars : `gopvars'
syntax varlist [if] [in], Dmu(varname) [rf(varname) Time(varname) gx(varlist) gy(varlist) gb(varlist) ///
BIennial SEQuential GLObal VRS NONRadial brep(integer 0) alpha(real 0.7) ///
Wmat(string) SAVing(string) WINdow(numlist integer max=1 >=1) level(real 95) ///
maxiter(numlist integer >0 max=1) tol(numlist max=1) NODOTS SAVing(string) noPRINT]
local bopvars `varlist'
if "`nodots'"!=""{
local nodots = 1
}
else{
local nodots = 0
}
if (`brep'<100 & `brep'>0) {
di "{p 1 1 7}{error}# of bootstrap replications must be at least 100."
exit 198
}
if (`brep'<200 & `brep'>0) {
di as red "Warning: Statistical inference may be unreliable for small number of bootstrap replications."
}
if (`level' < 10 | `level' > 99.99) {
di "{p 1 1 7}{error}level() must be between 10 and 99.99 inclusive{p_end}"
exit 198
}
if (`alpha' < 0.5 | `alpha' > 1) {
di "{p 1 1 7}{error}alpha() must be between 0.5 and 1{p_end}"
exit 198
}
// copy data to ddfResults frame & saved resulst in ddfResults
qui pwf
local currentframe = r(currentframe)
frame copy `currentframe' ddfResults
cwf ddfResults
marksample touse
markout `touse' `invars' `gopvars' `gx' `gy' `gb'
local invars: list uniq invars
local gopvars: list uniq gopvars
local bopvars: list uniq bopvars
local ninp: word count `invars'
local ngo: word count `gopvars'
local nbo: word count `bopvars'
local nvar=`ninp'+`ngo'+`nbo'
local band = 0
confirm numeric var `invars' `gopvars' `bopvars' `rf'
local comvars: list invars & gopvars
if !(`"`comvars'"'==""){
disp as error "`comvars' should not be specified as input and desriable output simultaneously."
error 498
}
local comvars: list invars & bopvars
if !(`"`comvars'"'==""){
disp as error "`comvars' should not be specified as input and undesriable output simultaneously."
error 498
}
local comvars: list gopvars & bopvars
if !(`"`comvars'"'==""){
disp as error "`comvars' should not be specified as desriable and undesriable outputs simultaneously."
error 498
}
if "`nonradial'"==""{
if `"`wmat'"'!=""{
disp as error "wmat() should be only used when nonradial is specified."
error 498
}
}
else{
tempname weightvec
if `"`wmat'"'!=""{
confirm matrix `wmat'
local ncol=colsof(`wmat')
if `ncol'!=`nvar'{
dis as error `"# of column of `wmat' != # of input-output variables"'
exit 498
}
forv i=1/`ncol'{
local wival=`wmat'[1,`i']
if `wival'<0{
di as error `"The element of matrix `wmat' should not be less than 0."'
exit 498
}
}
mat `weightvec'=`wmat'
}
else{
mat `weightvec'=J(1,`nvar',1)
}
forval i = 1/`=colsof(`weightvec')' {
local wvalues `wvalues' `=`weightvec'[1,`i']'
}
di
disp " The weight vector is (`wvalues')"
//mat list `weightvec',noblank noheader
}
local techtype "contemporaneous production technology"
local ttype contemporaneous
//local techtype "contemporaneous"
if `"`time'"'==""& "`sequential'`biennial'`window'"==""{
local techtype "global production technology"
local global global
}
if "`global'"=="" {
if "`time'"==""{
disp as error "For biennial/window/sequential/ model, time() should be specified."
error 498
}
}
if "`maxiter'"==""{
local maxiter=-1
}
if "`tol'"==""{
local tol=-1
}
if "`global'"!=""{
if "`sequential'"!=""{
disp as error "global and sequential cannot be specified together."
error 498
}
if "`window'"!=""{
disp as error "global and window() cannot be specified together."
error 498
}
if "`biennial'"!=""{
disp as error "global and biennial cannot be specified together."
error 498
}
local techtype "global production technology"
local ttype global
}
if "`sequential'"!=""{
if "`window'"!=""{
disp as error "sequential and window() cannot be specified together."
error 498
}
if "`biennial'"!=""{
disp as error "sequential and biennial cannot be specified together."
error 498
}
local techtype "sequential production technology"
local ttype sequential
}
if "`window'"!=""{
if "`biennial'"!=""{
disp as error "biennial and window() cannot be specified together."
error 498
}
local band = `window'
local techtype "window(`window') production technology"
local ttype window
}
if "`biennial'"!=""{
local techtype "biennial production technology"
local ttype biennial
}
*preserve
if "`gx'"!=""{
local ngx: word count `gx'
if `ngx'!=`ninp'{
disp as error "# of input variables != # of variables specified in gx()."
error 498
}
local gmat `gmat' `gx'
local gmatname `gmatname' `gx'
}
else{
local invarscopy `invars'
forv k=1/`ninp'{
gettoken word invarscopy:invarscopy
//di "`word'"
tempvar gx_`k'
qui gen `gx_`k''=-`word'
local gmat `gmat' `gx_`k''
local gmatname `gmatname' -`word'
}
}
if "`gy'"!=""{
local ngy: word count `gy'
if `ngy'!=`ngo'{
disp as error "# of desriable output variables != # of variables specified in gy()."
error 498
}
local gmat `gmat' `gy'
local gmatname `gmatname' `gy'
}
else{
local gopvarscopy `gopvars'
forv k=1/`ngo'{
gettoken word gopvarscopy:gopvarscopy
tempvar gy_`k'
qui gen `gy_`k''=`word'
local gmat `gmat' `gy_`k''
local gmatname `gmatname' `word'
}
}
if "`gb'"!=""{
local ngb: word count `gb'
if `ngb'!=`nbo'{
disp as error "# of undesriable output variables != # of variables specified in gb()."
error 498
}
local gmat `gmat' `gb'
local gmatname `gmatname' `gb'
}
else{
local bopvarscopy `bopvars'
forv k=1/`nbo'{
gettoken word bopvarscopy:bopvarscopy
tempvar gb_`k'
qui gen `gb_`k''=-`word'
local gmat `gmat' `gb_`k''
local gmatname `gmatname' -`word'
}
}
di
di " The diectional vector is (`gmatname')"
local rstype=0
if "`vrs'"!=""{
local rstype=1
}
qui keep `invars' `gopvars' `bopvars' `dmu' `time' `gmat' `touse' `rf'
qui gen Row=_n
label var Row "Row # in the original data"
tempvar tvar dmu2
qui egen `dmu2'=group(`dmu')
if `"`time'"'!="" {
qui egen `tvar'=group(`time')
}
else{
qui gen `tvar'=1
}
sort `dmu2' `tvar' // important for mm_sample() in the context of panel data
tempvar touse2
qui gen byte `touse2'=1
if "`rf'"!=""{
qui replace `touse2'=(`rf'!=0) if !missing(`rf')
}
markout `touse2' `invars' `opvars' `badvars' `rf'
*count if `touse2'
qui mata mata mlib index
local data `invars' `gopvars' `bopvars'
tempname data0 dataref gmat0
qui keep if `touse'
qui putmata `data0' = (`dmu2' `tvar' `data') if `touse', replace
qui putmata `gmat0' = (`gmat') if `touse', replace
qui putmata `dataref' = (`dmu2' `tvar' `data') if `touse2', replace
qui gen double Dv=.
label var Dv "value of distance function"
local outputvar Dv
if `brep'>0{
qui gen double Dv_bc=.
label var Dv_bc "the bias-corrected Dv"
qui gen double Dv_lower=.
label var Dv_lower "the lower-bound estimate of Dv"
qui gen double Dv_upper=.
label var Dv_upper "the upper-bound estimate of Dv"
qui gen double bootvar=.
label var bootvar "the bootstrap variance estimate"
}
if "`nonradial'"!=""{
local Dvbeta Dv
foreach v in `invars' `gopvars' `bopvars'{
qui gen double B_`v'=.
label var B_`v' `"beta:`v'"'
//local adjvars `adjvars' B_`v'
local Dvbeta `Dvbeta' B_`v'
}
tempname wmat0
mata: `wmat0'=st_matrix("`weightvec'")
if `brep'==0{
tempname DV
mata: `DV'=nddf`ttype'(`data0',`dataref',`ninp',`ngo',`wmat0',`gmat0',`band',`rstype',`maxiter',`tol')
qui getmata (`Dvbeta')= `DV', replace
local outputvar `Dvbeta'
}
else{
mata: nddf`ttype'bt(`data0',`dataref',`ninp',`ngo',`wmat0',`gmat0',`band',`alpha',`level',`brep',`rstype',`maxiter',`tol',`nodots',"`Dvbeta'")
local outputvar `Dvbeta' Dv_bc Dv_lower Dv_upper bootvar
}
}
else{
local outputvar Dv
if `brep'==0{
tempname DV
mata: `DV'=ddf`ttype'(`data0',`dataref',`ninp',`ngo',`gmat0',`band',`rstype',`maxiter',`tol')
qui getmata Dv = `DV', replace
}
else{
mata: ddf`ttype'bt(`data0',`dataref',`ninp',`ngo',`gmat0',`band',`alpha',`level',`brep',`rstype',`maxiter',`tol',`nodots')
local outputvar Dv Dv_bc Dv_lower Dv_upper bootvar
}
}
local NDDF=cond("`nonradial'"!="","Non-raidal","")
format `outputvar' %9.4f
order Row `dmu' `time' `outputvar'
keep Row `dmu' `time' `outputvar'
//di "`print'"
if "`print'" != "noprint" {
disp _n(2) " `NDDF' Directional Distance Function Results:"
disp " (Row: Row # in the original data; Dv: Estimated value of `NDDF' DDF.)"
list, sep(0)
di "Note: Missing value indicates infeasible problem."
}
if `"`saving'"'!=""{
save `saving'
gettoken filenames saving:saving, parse(",")
local filenames `filenames'.dta
disp _n `"Estimated Results are saved in `filenames'."'
*disp _n `"Estimated Results are also saved in ddfResults frame temporarily."'
cwf `currentframe'
frame drop ddfResults
}
else{
disp _n `"Estimated Results are temporarily saved in ddfResults frame."'
cwf `currentframe'
}
return local file `filenames'
*restore
end
///////////////////////////////////////////////////////
version 16
cap mata mata drop _ddf()
cap mata mata drop _nddf()
cap mata mata drop _ddf2()
cap mata mata drop _nddf2()
cap mata mata drop _tenddf()
cap mata mata drop _tenddf2()
cap mata mata drop ddfcontemporaneous()
cap mata mata drop ddfcontemporaneousbt()
cap mata mata drop ddfbiennial()
cap mata mata drop ddfbiennialbt()
cap mata mata drop ddfsequential()
cap mata mata drop ddfsequentialbt()
cap mata mata drop ddfwindow()
cap mata mata drop ddfwindowbt()
cap mata mata drop ddfglobal()
cap mata mata drop ddfglobalbt()
cap mata mata drop nddfcontemporaneous()
cap mata mata drop nddfcontemporaneousbt()
cap mata mata drop nddfbiennial()
cap mata mata drop nddfbiennialbt()
cap mata mata drop nddfsequential()
cap mata mata drop nddfsequentialbt()
cap mata mata drop nddfwindow()
cap mata mata drop nddfwindowbt()
cap mata mata drop nddfglobal()
cap mata mata drop nddfglobalbt()
cap mata mata drop biasandciddf()
cap mata mata drop displaydots()
cap mata mata drop calddf()
cap mata mata drop calnddf()
cap mata mata drop bootdea()
mata:
mata clear
void function displaydots(real scalar k)
{
if(mod(k,50)==0){
printf("..........%2.0f\n",k)
}
//else{
// printf(".")
//}
}
// define structure
struct bootdea {
real colvector tebc
real colvector LL
real colvector UU
real colvector vari
real colvector BovV
real colvector bias
}
real matrix function calddf(real matrix data,
real matrix dataref,
real scalar M,
real scalar N,
real matrix gmat,
real scalar rstype,
real scalar maxiter,
real scalar tol )
{
///nb=rows(data)-M-N
X=data[1::M,.]
Y=data[(M+1)::(M+N),.]
B=data[(M+N+1)::rows(data),.]
Xref=dataref[1::M,.]
Yref=dataref[(M+1)::(M+N),.]
Bref=dataref[(M+N+1)::rows(data),.]
gX=gmat[1::M,.]
gY=gmat[(M+1)::(M+N),.]
gB=gmat[(M+N+1)::rows(data),.]
class LinearProgram scalar q
q = LinearProgram()
c=(1,J(1,cols(dataref),0))
lowerbd=.,J(1,length(c)-1,0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
beta=J(cols(data),1,.)
for(i=1;i<=cols(data);i++){
Aie1=-gX[.,i],Xref
bie1=X[.,i]
Aie2=gY[.,i],-Yref
bie2=-Y[.,i]
Aec=-gB[.,i],Bref
bec=B[.,i]
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=0,J(1,N,1)
q.setEquality(Aec \ lsum, bec \ 1)
}
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
beta[i,1]=q.optimize()
}
return(beta)
}
real matrix function calnddf(real matrix data,
real matrix dataref,
real scalar M,
real scalar N,
real rowvector wmat,
real matrix gmat,
real scalar rstype,
real scalar maxiter,
real scalar tol )
{
///nb=rows(data)-M-N
X=data[1::M,.]
Y=data[(M+1)::(M+N),.]
B=data[(M+N+1)::rows(data),.]
Xref=dataref[1::M,.]
Yref=dataref[(M+1)::(M+N),.]
Bref=dataref[(M+N+1)::rows(data),.]
gX=gmat[1::M,.]
gY=gmat[(M+1)::(M+N),.]
gB=gmat[(M+N+1)::rows(data),.]
class LinearProgram scalar q
q = LinearProgram()
beta=J(cols(data),rows(data)+1,.)
c=(wmat,J(1,cols(dataref),0))
lowerbd=J(1,length(c),0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
for(i=1;i<=cols(data);i++){
Aie1=diag(-gX[.,i]),J(M,rows(data)-M,0),Xref
bie1=X[.,i]
Aie2=J(N,M,0),diag(gY[.,i]),J(N,rows(data)-M-N,0),-Yref
bie2=-Y[.,i]
Aec=J(rows(data)-M-N,M+N,0),diag(-gB[.,i]),Bref
bec=B[.,i]
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=J(1,length(wmat),0),J(1,cols(dataref),1)
q.setEquality(Aec \ lsum, bec \ 1)
}
b0=q.optimize()
if (q.converged()==1){
bslk=q.parameters()
beta[i,]=b0,bslk[1..rows(data)]
}
else{
beta[i,]=J(1,1+rows(data),.)
}
}
return(beta)
}
real matrix function nddfcontemporaneous( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),Mc-1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:==t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
return(D0)
}
void function nddfcontemporaneousbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:==t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1) // require sorting data by id time
datastar=dataref[idx,.]
Dbooti=nddfcontemporaneous(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:==t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub)
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
//st_view(g1=.,.,"Dv")
//g1[.,.]=D0
//st_view(g2=.,.,"Dv_bc")
//g2[.,.]=Dbc
//st_view(g3=.,.,"Dv_lower")
//g3[.,.]=LL
//st_view(g4=.,.,"Dv_upper")
//g4[.,.]=UU
//st_view(g5=.,.,"bootvar")
//g5[.,.]=vari
}
real matrix function nddfbiennial( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),Mc-1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:>=t0uniq[j]):*(t1:<=t0uniq[j]+1))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function nddfbiennialbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:>=t0uniq[j]):* (t1:<=t0uniq[j]+1))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dbooti=nddfbiennial(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],(t1:>=t0uniq[j]):* (t1:<=t0uniq[j]+1)))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub)
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function nddfsequential( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),Mc-1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function nddfsequentialbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
mata:Dbooti=nddfsequential(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:<=t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub*sum(t1:<=t0uniq[j]))
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function nddfglobal( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
XYB = data0[.,3..cols(data0)]
XYBref = dataref[.,3..cols(dataref)]
D0 = calnddf(XYB',XYBref',k1,k2,wmat,gmat',rstype,maxiter,tol)
//D0
return(D0)
}
real matrix function nddfglobalbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
//t1 = dataref[.,2]
//Mc=cols(data0)
t0uniq=uniqrows(t0)
Kr=rows(dataref)
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
XYB = data0[.,3..cols(data0)]
XYBref = dataref[.,3..cols(dataref)]
D0[.,.] = calnddf(XYB',XYBref',k1,k2,wmat,gmat',rstype,maxiter,tol)
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
msubmat=J(1,brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
//rows(datastar)
msubmat[1,i]=rows(datastar)
Dbooti=nddfglobal(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msubmat[1,j])
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
// bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function nddfwindow( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),Mc-1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=(t0uniq[j]+band):*(t1:>=t0uniq[j]-band))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function nddfwindowbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real scalar wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:<=t0uniq[j]+band):*(t1:>=t0uniq[j]-band))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dbooti=nddfwindow(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],(t1:<=t0uniq[j]+band):*(t1:>=t0uniq[j]-band)))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub*sum((t1:<=t0uniq[j]+band):* (t1:>=t0uniq[j]-band)))
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
//////////////////////////////////////
void function _ddf( string scalar d, ///
real scalar k1, ///
real scalar k2, ///
string scalar touse1, ///
string scalar touse2, ///
string scalar gname, ///
string scalar bname, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
data=st_data(.,d,touse1)
data=data'
M=rows(data)
g=st_data(.,gname,touse1)
g=g'
dataref=st_data(.,d,touse2)
dataref=dataref'
Xref=dataref[1..k1,.]
Yref=dataref[k1+1..(k1+k2),.]
Bref=dataref[(k1+k2+1)..M,.]
X=data[1..k1,.]
Y=data[k1+1..(k1+k2),.]
B=data[(k1+k2+1)..M,.]
theta=J(cols(data),1,.)
for(j=1;j<=cols(X);j++){
theta[j]=_ddf2(X[.,j],Y[.,j],B[.,j],Xref,Yref,Bref,g[.,j],rstype,maxiter,tol)
}
st_view(gen=.,.,bname,touse1)
gen[.,.]=theta
}
real matrix function ddfcontemporaneous( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:==t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
return(D0)
}
void function ddfcontemporaneousbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:==t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dboot[.,i]=ddfcontemporaneous(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:==t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub)
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
//st_view(g1=.,.,"Dv")
//g1[.,.]=D0
//st_view(g2=.,.,"Dv_bc")
//g2[.,.]=Dbc
//st_view(g3=.,.,"Dv_lower")
//g3[.,.]=LL
//st_view(g4=.,.,"Dv_upper")
//g4[.,.]=UU
//st_view(g5=.,.,"bootvar")
//g5[.,.]=vari
}
real matrix function ddfbiennial( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:>=t0uniq[j]):*(t1:<=t0uniq[j]+1))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function ddfbiennialbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:>=t0uniq[j]):* (t1:<=t0uniq[j]+1))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dboot[.,i]=ddfbiennial(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],(t1:>=t0uniq[j]):* (t1:<=t0uniq[j]+1)))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub)
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function ddfsequential( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function ddfsequentialbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dboot[.,i]=ddfsequential(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:<=t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub*sum(t1:<=t0uniq[j]))
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function ddfglobal( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
XYB = data0[.,3..cols(data0)]
XYBref = dataref[.,3..cols(dataref)]
D0 = calddf(XYB',XYBref',k1,k2,gmat',rstype,maxiter,tol)
//D0
return(D0)
}
real matrix function ddfglobalbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
//t1 = dataref[.,2]
//Mc=cols(data0)
t0uniq=uniqrows(t0)
Kr=rows(dataref)
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
XYB = data0[.,3..cols(data0)]
XYBref = dataref[.,3..cols(dataref)]
D0[.,1] = calddf(XYB',XYBref',k1,k2,gmat',rstype,maxiter,tol)
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
msubmat=J(1,brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
//rows(datastar)
msubmat[1,i]=rows(datastar)
Dboot[.,i]=ddfglobal(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msubmat[1,j])
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
// bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function ddfwindow( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=(t0uniq[j]+band):*(t1:>=t0uniq[j]-band))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function ddfwindowbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:<=t0uniq[j]+band) + (t1:>=t0uniq[j]-band))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dboot[.,i]=ddfwindow(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:<=t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub*sum((t1:<=t0uniq[j]+band):* (t1:>=t0uniq[j]-band)))
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
function biasandciddf( real colvector te,real matrix teboot, ///
real scalar M, real scalar N, real scalar K, real scalar Kr, ///
real scalar level,real scalar smoothed, real scalar msub)
{
if(smoothed){
con1=1
con2=1
}
else{
con1=msub^(2/(M+N+1))
con2=Kr^(-2/(M+N+1))
}
con3=con1*con2
bias=J(K,1,.)
vari=J(K,1,.)
BovV=J(K,1,.)
tebc=J(K,1,.)
UU=J(K,1,.)
LL=J(K,1,.)
for(i=1;i<=K;i++){
TEi=teboot[i,.]
TEi=select(TEi,TEi:!=.)
if(length(TEi)<100){
bias[i]=.
vari[i]=.
BovV[i]=.
tebc[i]=.
LL[i]=.
UU[i]=.
}
else{
bias[i]=con3*(mean(TEi')-te[i])
vari[i]=variance(TEi')
BovV[i]=(bias[i])^2 / vari[i] * 3
tebc[i] = te[i] - bias[i]
teOverTEhat = (TEi':- te[i] ) * con1
quans =mm_quantile(teOverTEhat,1, ((0.5-level/200)\(0.5+level/200) ))
//quans* con2
LL[i] = te[i] - quans[2] * con2
UU[i] = te[i] - quans[1] * con2
//quans =mm_quantile(TEi',1, ((0.5-level/200)\(0.5+level/200) ))
//LL[i] = quans[1]
//UU[i] = quans[2]
}
}
struct bootdea scalar res
res.tebc = tebc
res.vari = vari
res.BovV = BovV
res.bias = bias
res.LL = LL
res.UU = UU
return(res)
}
real scalar function _ddf2( real colvector X, ///
real colvector Y, ///
real colvector B, ///
real matrix Xref, ///
real matrix Yref, ///
real matrix Bref, ///
real colvector g, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
k1=length(X)
k2=length(Y)
M=length(B)+k1+k2
gX=g[1..k1,1]
gY=g[k1+1..(k1+k2),1]
gB=g[(k1+k2+1)..M,1]
N=cols(Xref)
class LinearProgram scalar q
q = LinearProgram()
c=(1,J(1,N,0))
lowerbd=.,J(1,length(c)-1,0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
Aie1=-gX,Xref
bie1=X
Aie2=gY,-Yref
bie2=-Y
Aec=-gB,Bref
bec=B
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=0,J(1,N,1)
q.setEquality(Aec \ lsum, bec \ 1)
}
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
beta=q.optimize()
return(beta)
}
void function _nddf( string scalar d, ///
real scalar nx, ///
real scalar ny, ///
string scalar touse1, ///
string scalar touse2, ///
string scalar gname, ///
string scalar wname, ///
string scalar bname, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
wmat=st_matrix(wname)
data=st_data(.,d,touse1)
data=data'
M=rows(data)
g=st_data(.,gname,touse1)
g=g'
dataref=st_data(.,d,touse2)
dataref=dataref'
Xref=dataref[1..nx,.]
Yref=dataref[nx+1..(nx+ny),.]
Bref=dataref[(nx+ny+1)..M,.]
X=data[1..nx,.]
Y=data[nx+1..(nx+ny),.]
B=data[(nx+ny+1)..M,.]
theta=J(cols(data),1,.)
for(j=1;j<=cols(data);j++){
theta[j]=_nddf2(X[.,j],Y[.,j],B[.,j],Xref,Yref,Bref,g[.,j],wmat,rstype,maxiter,tol)
}
st_view(gen=.,.,bname,touse1)
gen[.,.]=theta
}
real function _nddf2( real colvector X, ///
real colvector Y, ///
real colvector B, ///
real matrix Xref, ///
real matrix Yref, ///
real matrix Bref, ///
real colvector g, ////
real rowvector wmat, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
nx=length(X)
ny=length(Y)
nb=length(B)
M=nb+nx+ny
gX=g[1..nx,1]
gY=g[nx+1..(nx+ny),1]
gB=g[(nx+ny+1)..M,1]
N=cols(Xref)
class LinearProgram scalar q
q = LinearProgram()
c=(wmat,J(1,N,0))
lowerbd=J(1,length(c),0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
Aie1=diag(-gX),J(nx,ny+nb,0),Xref
bie1=X
Aie2=J(ny,nx,0),diag(gY),J(ny,nb,0),-Yref
bie2=-Y
Aec=J(nb,nx+ny,0),diag(-gB),Bref
bec=B
lsum=J(1,length(wmat),0),J(1,N,1)
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=J(1,M,0),J(1,N,1)
q.setEquality(Aec \ lsum, bec \ 1)
}
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
beta=q.optimize()
//q.getInequality()
return(beta)
}
void function _tenddf( string scalar d, ///
real scalar nx, ///
real scalar ny, ///
string scalar touse1, ///
string scalar touse2, ///
string scalar gname, ///
string scalar wname, ///
string scalar bname, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
wmat=st_matrix(wname)
data=st_data(.,d,touse1)
data=data'
M=rows(data)
g=st_data(.,gname,touse1)
g=g'
dataref=st_data(.,d,touse2)
dataref=dataref'
Xref=dataref[1..nx,.]
Yref=dataref[nx+1..(nx+ny),.]
Bref=dataref[(nx+ny+1)..M,.]
X=data[1..nx,.]
Y=data[nx+1..(nx+ny),.]
B=data[(nx+ny+1)..M,.]
theta=J(cols(data),rows(data)+1,.)
for(j=1;j<=cols(data);j++){
theta[j,.]=_tenddf2(X[.,j],Y[.,j],B[.,j],Xref,Yref,Bref,g[.,j],wmat,rstype,maxiter,tol)
//q.getInequality()
}
st_view(gen=.,.,bname,touse1)
gen[.,.]=theta
}
real function _tenddf2( real colvector X, ///
real colvector Y, ///
real colvector B, ///
real matrix Xref, ///
real matrix Yref, ///
real matrix Bref, ///
real colvector g, ////
real rowvector wmat, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
nx=length(X)
ny=length(Y)
nb=length(B)
M=nb+nx+ny
gX=g[1..nx,1]
gY=g[nx+1..(nx+ny),1]
gB=g[(nx+ny+1)..M,1]
N=cols(Xref)
class LinearProgram scalar q
q = LinearProgram()
c=(wmat,J(1,N,0))
lowerbd=J(1,length(c),0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
Aie1=diag(-gX),J(nx,ny+nb,0),Xref
bie1=X
Aie2=J(ny,nx,0),diag(gY),J(ny,nb,0),-Yref
bie2=-Y
Aec=J(nb,nx+ny,0),diag(-gB),Bref
bec=B
lsum=J(1,length(wmat),0),J(1,N,1)
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=J(1,M,0),J(1,N,1)
q.setEquality(Aec \ lsum, bec \ 1)
}
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
dval=q.optimize()
if (q.converged()==1){
bslk=q.parameters()
beta=dval,bslk[1..M]
}
else{
beta=J(1,1+M,.)
}
//q.getInequality()
return(beta)
}
end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xinjunlyu/gtfpch.git
git@gitee.com:xinjunlyu/gtfpch.git
xinjunlyu
gtfpch
gtfpch
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385