📄 xtgee.ado
字号:
global S_X_D2 = r(sum)/$S_E_dof
cap drop `dj'
gen double `dj' = (`y'-`mu')/sqrt((`mu')^2)
replace `dj' = (`dj')^2
summ `dj' `wtexp', meanonly
global S_X_P1 = r(sum)
global S_X_P2 = r(sum)/$S_E_dof
}
else if "$S_X_mvar" == "binom" {
cap drop `dj'
gen double `dj' = 2*`y'*log(`y'/`mu')+ /*
*/ 2*(`m'-`y')*log((`m'-`y')/(`m'-`mu'))
replace `dj' = 2*`m'*log(`m'/(`m'-`mu')) if `y'==0
replace `dj' = 2*`y'*log(`y'/`mu') if `y'==`m'
summ `dj' `wtexp', meanonly
global S_X_D1 = r(sum)
global S_X_D2 = r(sum)/$S_E_dof
cap drop `dj'
gen double `dj' = (`y'-`mu')/sqrt(`mu'*(1-`mu'/`m'))
replace `dj' = (`dj')^2
summ `dj' `wtexp', meanonly
global S_X_P1 = r(sum)
global S_X_P2 = r(sum)/$S_E_dof
}
else if "$S_X_mvar" == "nbinom" {
local k = $S_X_nba
cap drop `dj'
gen double `dj' = 2*ln(1+`k'*`mu')/`k'
replace `dj' = 2*(`y'*ln(`y'/`mu')-(1+`k'*`y')/`k'* /*
*/ ln((1+`k'*`y')/(1+`k'*`mu'))) if `y'
summ `dj' `wtexp', meanonly
global S_X_D1 = r(sum)
global S_X_D2 = r(sum)/$S_E_dof
cap drop `dj'
gen double `dj' = (`y'-`mu')/sqrt(`mu'+`k'*`mu'^2)
replace `dj' = (`dj')^2
summ `dj' `wtexp', meanonly
global S_X_P1 = r(sum)
global S_X_P2 = r(sum)/$S_E_dof
}
end
program define ChkWt /* ivar wv */, rclass
args ivar v
cap by `ivar': assert `v'==`v'[_n-1] if _n>1
if _rc {
noi di in red "weight must be constant within `ivar'"
exit 199
}
tempname cc
tempvar aa bb
qui by `ivar': gen double `aa' = `v' if _n==_N
qui by `ivar': gen double `bb' = _N*`v' if _n==_N
qui capture qui summ `aa'
qui capture scalar `cc' = r(sum)
qui capture qui summ `bb'
if _rc {
noi di in red "insufficient observations"
exit 2000
}
ret scalar mean = r(sum)/`cc'
ret scalar sum = `cc'
end
program define ChkSyn
args tol iter hascons cons
if `tol' <= 0 {
noi di in red "tolerance() must be positive"
exit 198
}
if `iter' < 1 {
noi di in red "iterate() must be positive"
exit 198
}
if "$S_X_scp" != "" {
cap confirm number $S_X_scp
if _rc {
if "$S_X_scp" != "dev" & "$S_X_scp" != "x2" /*
*/ & "$S_X_scp" != "phi" {
noi di in red /*
*/ "unknown scale() option"
exit 198
}
}
}
if "`hascons'" != "" & "`cons'" != "" {
noi di in red /*
*/ "options hascons and nocons may not be specified together"
exit 198
}
if "`hascons'" != "" | "`cons'" != "" {
global S_X_con 0
}
else global S_X_con 1
end
program define fixwords, eclass
if "$S_E_fam" == "gauss" { global S_E_fam "Gaussian" }
if "$S_E_fam" == "binom" { global S_E_fam "binomial" }
if "$S_E_fam" == "poiss" { global S_E_fam "Poisson" }
if "$S_E_fam" == "gamma" { global S_E_fam "gamma" }
if "$S_E_fam" == "nbinom"{
local val = round($S_X_nba,.0001)
global S_E_fam "negative binomial(k=`val')"
}
if "$S_E_fam" == "igauss"{
global S_E_fam "inverse Gaussian"
}
if "$S_E_lnk" == "ident" { global S_E_lnk "identity" }
if "$S_E_lnk" == "log" { global S_E_lnk "log" }
if "$S_E_lnk" == "logit" { global S_E_lnk "logit" }
if "$S_E_lnk" == "cloglog"{ global S_E_lnk "cloglog" }
if "$S_E_lnk" == "recip" { global S_E_lnk "reciprocal" }
if "$S_E_lnk" == "probit" { global S_E_lnk "probit" }
if "$S_E_lnk" == "power" {
local val = round($S_X_pow,.0001)
global S_E_lnk "power(`val')"
}
if "$S_E_lnk" == "opower" {
local val = round($S_X_pow,.0001)
global S_E_lnk "odds power(`val')"
}
if "$S_E_lnk" == "nbinom"{
global S_E_lnk "negative binomial"
}
if "$S_E_cor" == "indep" { global S_E_cor "independent" }
if "$S_E_cor" == "exchan" { global S_E_cor "exchangeable" }
if "$S_E_cor" == "statm" { global S_E_cor "stationary($S_E_bw)" }
if "$S_E_cor" == "arm" { global S_E_cor "AR($S_E_bw)" }
if "$S_E_cor" == "nonstat" { global S_E_cor "nonstationary($S_E_bw)" }
if "$S_E_cor" == "unstr" { global S_E_cor "unstructured" }
if "$S_E_cor" == "fixed" { global S_E_cor "fixed (specified)" }
est local family "$S_E_fam"
est local corr "$S_E_cor"
est local link "$S_E_lnk"
end
program define KeepMode /* ivar tvar [force] */
args ivar tvar force
if "`force'"!="" { exit }
tempvar bb
qui {
by `ivar': gen float `bb' = `tvar'-`tvar'[_n-1]
by `ivar': replace `bb' = `bb'[2] if _n==1
qui summ `bb'
cap assert `bb' == r(min) | `bb'>=.
if _rc==0 { exit }
tempname mesh
tempvar freq bad
sort `bb'
by `bb': gen `freq'=_N if _n==_N
summ `freq'
summ `bb' if `freq'==r(max)
scalar `mesh' = r(min)
sort `ivar' `tvar'
drop `freq'
by `ivar': gen `bad' = cond(_n==1,0,`bb'!=`mesh')
by `ivar': replace `bad' = cond(_n==_N,sum(`bad')!=0,0)
summ `bad', mean
}
di in gr _n /*
*/ "note: observations not equally spaced" _n /*
*/ _col(8) "modal spacing is delta `tvar' = " `mesh' _n /*
*/ _col(8) r(sum) " groups omitted from estimation"
qui by `ivar': drop if `bad'[_N]
if _N==0 {
di in red "no observations"
exit 2000
}
end
program define TooFewMg /* band */
di in gr "note: some groups have fewer than " /*
*/ `1'+1 " observations" _n _col(8) /*
*/ "not possible to estimate correlations for those groups" _n /*
*/ _col(8) r(sum) " groups omitted from estimation" _n
end
program define GetLag, sclass /* will return s(timevar) */
args corr t
if "`corr'"=="statm" | "`corr'"=="nonst" | "`corr'"=="arm" /*
*/ | "`corr'"=="unstr" | "`corr'"=="fixed" {
xt_tis `t' /* returns s(timevar) */
}
else { sret local timevar /* reset s(timevar) */ }
end
program define SaveMac, eclass
est scalar df_pear = `1'
est scalar N = `2'
est scalar N_g = `3'
est scalar g_max = `4'
est scalar g_min = `5'
est scalar g_avg = `6'
est scalar tol = `7'
est scalar dif = `8'
est scalar phi = `9'
est local family = trim("`10'")
est local link = trim("`11'")
est local corr = trim("`12'")
est local tvar = trim("`13'")
est local depvar = trim("$S_X_depv")
est local vcetype "$S_X_vce"
est local crittype "tolerance"
global X_level = `15'
if "$S_X_9" != "" {
est scalar disp = $S_X_9
}
est local scale "$S_X_A"
est scalar deviance = $S_X_D1
est scalar dispers = $S_X_D2
est scalar chi2_dev = $S_X_P1
est scalar chi2_dis = $S_X_P2
est local power $S_X_pow
/* Double saves below here */
global S_E_rdf = e(df_pear)
global S_E_nobs = e(N)
global S_E_ncl = e(N_g)
global S_E_ntmx = e(g_max)
global S_E_ntmn = e(g_min)
global S_E_ntav = e(g_avg)
global S_E_tol = e(tol)
global S_E_dif = e(dif)
global S_E_phi = e(phi)
global S_E_fam = trim("`e(family)'")
global S_E_lnk = trim("`e(link)'")
global S_E_cor = trim("`e(corr)'")
global S_E_tvar = trim("`e(tvar)'")
global S_E_depv = trim("`e(depvar)'")
global S_E_vl "$S_X_depv $S_X_idep"
global S_X_ivar
global S_E_bw "`14'"
global X_level = `15'
global S_E_dis "$S_X_9"
global S_E_scp "$S_X_A"
global S_E_D1 "$S_X_D1"
global S_E_D2 "$S_X_D2"
global S_E_P1 "$S_X_P1"
global S_E_P2 "$S_X_P2"
global S_E_vce "$S_X_vce"
if "$S_X_nba" != "" {
global S_E_nba = 1/$S_X_nba
est local nbalpha $S_E_nba
}
global S_E_pow $S_X_pow
end
program define BigEnuf, rclass /* ivar [tord] */
args ivar tord
tempvar bb
tempname chkme
quietly {
by `ivar': gen float `bb' = _N if _n==_N
capture summ `bb'
if _rc {
noi di in red "insufficient observations"
exit 2000
}
ret scalar min = r(min)
ret scalar max = r(max)
ret scalar mean = r(mean)
ret scalar xmax = r(max)
ret scalar N = r(N)
if "`tord'"!="" {
drop `bb'
by `ivar': gen float `bb' = `tord' if _n==_N
summ `bb'
ret scalar xmax = r(max)
}
cap mat `chkme' = I(`return(xmax)')
if _rc == 908 {
noi di in red /*
*/ "matsize too small -- must be able to set matsize to at least " /*
*/ return(xmax)+1
exit 908
}
mat drop `chkme'
}
end
program define KeepMin, rclass /* corr ivar band */
args corr ivar band
if ("`corr'"!="statm" & "`corr'"!="arm" & "`corr'"!="nonst" ) {
ret local band `band'
exit
}
if `band' < 0 {
local band 1
}
tempvar bb c
quietly {
by `ivar': gen int `c' = _N
summ `c'
if `band'<r(min) {
ret local band `band'
exit
}
by `ivar' : gen byte `bb' = 1 if `c'<=`band' & _n==1
summ `bb'
noi TooFewMg `band'
drop `bb'
keep if `c' > `band'
}
ret local band `band'
end
program define GetIval /* hascons nocons wexp xarg oarg -> b1 */
args hascons nocons wtex xarg oarg ARROW b1 offarg
if "`offarg'" != "" {
local options "OFFset(string)"
parse ",`offarg'"
local moff "-`offset'"
}
*local xtra 1
if "$S_X_nba" != "" {
local xtra = 1/$S_X_nba
}
local link "$S_X_link"
local family "$S_X_mvar"
local xarg "`xarg' `xtra'"
local cap "capture"
if "`hascons'" == "" & "`nocons'" == "" {
local glink = trim("`link' $S_X_pow")
if "`family'" == "gauss" & "`link'" == "ident" {
tempvar jwh
gen double `jwh' = $S_X_depv `moff'
`cap' reg `jwh' $S_X_idep `wtex'
}
else if "`family'" == "binom" & "`link'" == "probit" /*
*/ & "`oarg'" == "" {
`cap' probit $S_X_depv $S_X_idep `wtex', /*
*/ nolog nocoef `offarg' asis
}
else if "`family'" == "binom" & "`link'" == "logit" /*
*/ & "`oarg'" == "" {
`cap' logit $S_X_depv $S_X_idep `wtex', `offarg'/*
*/ asis
}
else if "`family'" == "binom" & "`link'" == "cloglog" /*
*/ & "`oarg'" == "" {
`cap' cloglog $S_X_depv $S_X_idep `wtex', `offarg'
}
else {
if "`glink'" == "recip" {
local glink = "power -1"
}
*noi di in red "`cap' glm $S_X_depv $S_X_idep `wtex', fam(`family' `xarg') link(`glink') `offarg'"
`cap' glm $S_X_depv $S_X_idep `wtex', /*
*/ fam(`family' `xarg') link(`glink') `offarg'
}
if _rc & _rc!=430 {
if _rc==1 { exit 1 }
tempvar jwh
gen double `jwh' = $S_X_depv `moff'
`cap' reg `jwh' $S_X_idep
}
LoadMat `b1'
exit
}
if "`hascons'" != "" & "`cons'" == "" {
local glink = trim("`link' $S_X_pow")
if "`glink'" == "recip" {
local glink = "power -1"
}
local cf 0
`cap' glm $S_X_depv $S_X_idep `wtex', /*
*/ fam(`family' `xarg') link(`glink') nocons `offarg'
if _rc & _rc!=430 {
if _rc==1 { exit 1 }
tempvar jwh
gen double `jwh' = $S_X_depv `moff'
`cap' reg `jwh' $S_X_idep, nocons
local cf 1
}
LoadMat `b1'
local nc = colsof(`b1') - `cf'
mat `b1' = `b1'[1,1..`nc']
exit
}
local glink = trim("`link' $S_X_pow")
if "`glink'" == "recip" {
local glink = "power -1"
}
`cap' glm $S_X_depv $S_X_idep `wtex', /*
*/ fam(`family' `xarg') link(`glink') nocons `offarg'
if _rc & _rc!=430 {
if _rc==1 { exit 1 }
tempvar jwh
gen double `jwh' = $S_X_depv `moff'
`cap' reg `jwh' $S_X_idep, nocons
}
LoadMat `b1'
end
program define LoadMat /* matname */
capture mat `1' = get(_b)
if _rc==0 { exit }
if _rc != 301 { error _rc }
di in red "family/link inappropriate for this data"
exit 2001
end
program define Checkt
args ivar tvar rtvar
qui {
tempvar bb
gen float `bb' = .
by `ivar': replace `bb' = `tvar'-`tvar'[_n-1]
by `ivar': replace `bb' = `bb'[2] if _n==1
summ `bb', meanonly
local rmin "`r(min)'"
if "`rmin'" != "" {
if `rmin' == 0 {
noi di in red /*
*/ "`rtvar' has duplicate values within panel"
exit 198
}
}
}
end
exit
note: observations not equally spaced
modal spacing is delta timevar = xxx
14 groups omitted from estimation
note: some groups have fewer than xx observations
not possible to estimate correlations for those groups
15 groups omitted from estimation
Took this out for logit and probit links:
noi di in red "estimates diverging for logistic model"
exit 198
comment A
/* Changed dmd 02/16/00 this code was used to map pweights and
iweights robust to aweights. The purpose being that aweights
have the average n per panel and the number of panels calculated
differently. This is not correct. pweights and iweights robust
do affect the number of panels, just like they affect `the number of
obs. in calculating a mean.
if "`weight'" == "pweight" {
local weight = "aweight"
local robust "robust"
}
else if "`weight'" == "iweight" & "`robust'" != "" {
local weight "aweight"
}
*/
comment B
/* changed 0 -> 1 02/16/00 dmd
We wanted to allow aweights, but
that created a need for two different definitions of the sum of
the weights. Since this proved difficult and aweights in this
particular context have dubious use, we removed aweights. So,
the fact that we "and 0" reinforces the fact that we do NOT allow
aweights above (at the initial parse).
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -