📄 glm.ado
字号:
tempname ss
scalar `ss' = (`nobs'-`p')/`nobs'
local vce "jackknife1"
local vcetype "1-step JKnife"
mat `V' = `ss'*`V'
}
if "`jknife'" != "" {
noi JKnife `b' `V' `y' "`xvars'" "`tvar'" /*
*/ "`offopt'" "`constant'" /*
*/ "`argfam'" "irls" "`arglink'" /*
*/ "[`weight'`exp']" `touse' /*
*/ "`cluster'" `dots' "`weight'" "`wt'"
local mm = `nobs'
local vcetype "Jackknife"
if "`cluster'" != "" { local mm = $SGLM_nc }
else local mm=1
local p = colsof(`b')
local nnn = `nobs'-$SGLM_bm
tempname ss
scalar `ss' = (`nnn'-`p')/(`nnn'*`mm')
mat `V' = `ss'*`V'
}
if "`bstrap'" != "" {
tempname b V
mat `b' = e(b)
mat `V' = e(V)
noi Bstrap `b' `V' `y' "`xvars'" "`offopt'" /*
*/ "`constant'" "`argfam'" "irls" /*
*/ "`arglink'" "[`weight'`exp']" /*
*/ `touse' `brep' /*
*/ "`clopt'" `dots'
local vcetype "Bootstrap"
if "`cluster'" != "" { local mm = $SGLM_nc }
else local mm=1
local p = colsof(`b')
tempname ss
scalar `ss' = (`nobs'-`p')/(`nobs'*(`brep'-$SGLM_bm))
mat `V' = `ss'*`V'
}
/* get rid of `Wscale' scaling */
if "`opg'`hacnam'`robust'`jknife'`jknife1'`bstrap'" == "" {
scalar `Wscale' = 1/`Wscale'
mat `V' = `Wscale'*`V'
}
if `"`zapse'"'=="yes" {
local i 1
while `i'<=rowsof(`V') {
mat `V'[`i',`i'] = 0
local i=`i'+1
}
}
if `vfactor' != 1 {
mat `V' = `vfactor'*`V'
}
tempvar mysamp
local k = colsof(`b')
qui gen byte `mysamp' = `touse'
est post `b' `V', depname(`y') obs(`nobs') /*
*/ `dofopt' esample(`mysamp')
est local depvar "`y'"
est local wtype "`weight'"
est local wexp "`exp'"
est scalar rc = `rc'
est scalar disp = `disp'
est scalar k = `k'
est local crittype "deviance"
}
}
mac drop SGLM_running SGLM_nonstan
if "`score'" != "" {
label var `scvar' "Score index from glm"
rename `scvar' `score'
est local scorevars `score'
}
local dfm : word count `xvars'
local p = `dfm' + (`"`constant'"'=="")
local df = `nobs'-`p'
est local link "$SGLM_L"
est local varfunc "$SGLM_V"
est local m "$SGLM_m"
est local a "$SGLM_a"
est local msg "`cd'"
est local cons "`constant'"
est local oim "`oim'"
est local clustvar "`cluster'"
capture confirm number $SGLM_p
if !_rc {
est scalar power = $SGLM_p
}
else {
est scalar power = 0
}
if "$SGLM_V" == "glim_v2" & "$SGLM_L" == "glim_l01" & "`eform'" == "" {
est local msg2 "Coefficients are the risk differences"
}
if "`irls'" != "" {
est local opt irls
if "`oim'" == "" {
est local opt1 "MQL Fisher scoring"
est local opt2 "(IRLS EIM)"
}
else {
est local opt1 "MQL Newton-Raphson"
est local opt2 "(IRLS OIM)"
}
}
else {
est local opt = "ml"
est local opt1 "ML"
est local opt2
if "`vce'" == "eim" & `disp' != 1 {
matrix `V' = `disp'*e(V)
est repost V = `V'
}
}
if "$SGLM_bm" != "" {
if $SGLM_bm > 0 { est scalar Nf = $SGLM_bm }
}
est scalar df_m = `dfm'
est scalar df = `df'
if "$SGLM_nc" != "" {
est scalar N_clust = $SGLM_nc
}
if `brep' > 0 {
est scalar N_brep = `brep'
}
est scalar vf = `vfactor'
est scalar phi = `disp'
est scalar deviance = abs(`newdev')
est scalar dispers = e(deviance)/e(df)
est scalar deviance_s = e(deviance)/e(phi)
est scalar dispers_s = e(deviance_s)/e(df)
est scalar deviance_p = abs(`chi2')
est scalar dispers_p = e(deviance_p)/e(df)
est scalar deviance_ps = e(deviance_p)/e(phi)
est scalar dispers_ps = e(deviance_ps)/e(df)
capture drop `v'
qui gen double `v' = sum((`y'-`mu')^2) if `touse'
if ( "$SGLM_V" == "glim_v2" & "$SGLM_m" != "1" ) {
tempvar mv
gen `mv'=$SGLM_m
summarize `mv', mean
local nobs=r(sum)
}
est scalar bic = e(deviance) - `df'*log(`nobs')
est local varfuncf "$SGLM_vf"
est local varfunct "$SGLM_vt"
est local linkf "$SGLM_lf"
est local linkt "$SGLM_lt"
est local vce "`vce'"
est local vcetype "`vcetype'"
est local setype "`setype'"
est local hac_kernel "`hac_kern'"
est local hac_lag "`haclag'"
est local offset "`offvar'"
est local offset1 /* erase; set by -ml- */
est local predict "glim_p"
est local cmd "glm"
capture mac drop SGLM_bm SGLM_nc
if "$SGLM_V" == "glim_v2" {
if "$SGLM_L" == "glim_l01" | "$SGLM_L" == "glim_l03" {
CheckAdmiss
}
}
if "`display'" == "" {
Display, `eform' level(`level') `header' `table'
}
error `e(rc)'
end
program GetVCE
version 9
syntax [, ///
IRLS ///
BSTRAP /// old vcetype specification
BREP(integer -1) ///
JKNIFE ///
JKNIFE1 ///
NWEST(string) ///
OIM ///
OPG BHHH ///
UNBiased ///
VCE(string) /// new vcetype specification
VCE1(string) ///
* ///
]
// -bhhh- is a synonym for -opg-
if ("`bhhh'" != "") local opg opg
local oldvce `bstrap' `jknife' `jknife1' `opg' `unbiased'
if ("`nwest'" != "") local oldvce `"`oldvce' nwest()"'
local noldvce : word count `olvce'
opts_exclusive "`oldvce'"
// verify -vce(vcetype)-
if `"`vce'`vce1'"' != "" {
if `"`vce1'"' != "" {
// check for conflicting -vce()-
if !inlist("oim",`"`vce'"',`"`vce1'"') {
opts_exclusive "vce(`vce') vce(`vce1')"
}
local oim oim
if `"`vce'"' == "oim" {
local vce `"`vce1'"'
}
local vce1 // clear
}
else if `"`vce'"' == "oim" {
local oim oim
local vce
}
// check for a conflict between old and new vce() options
if !inlist("`oldvce'", "", "`vce'") & "`vce'" != "" {
opts_exclusive `"vce(`vce') `oldvce'"'
}
MapVCE "`irls'" `vce'
if `"`s(vce)'"' != "" {
local vce `s(vce)'
local `vce' `s(vcespec)'
}
if `"`s(vceopt)'"' != "" {
local options `"`options' `s(vceopt)'"'
}
}
else {
if "`nwest'" != "" {
local hac `"`nwest'"'
}
}
c_local bstrap `"`bstrap'"'
c_local brep `"`brep'"'
c_local jknife `"`jknife'"'
c_local jknife1 `"`jknife1'"'
c_local hac `"`hac'"'
c_local oim `"`oim'"'
c_local opg `"`opg'"'
c_local unbiased `"`unbiased'"'
c_local vce `"`vce'"'
c_local options `"`options'"'
end
program MapVCE, sclass
// "<irls>" <vcetype> [,] [<vcerest>]
gettoken irls 0 : 0
local 0 : list retok 0
gettoken vce vcerest: 0, parse(", ")
local vcerest : list retok vcerest
local len : length local vce
if inlist(`"`vce'"', "jknife1", "jackknife1") {
local norest norest
local vce jknife1
local vcespec jknife1
}
else if `"`vce'"' == "hac" {
if `"`vcerest'"' == "" {
di as err "option vce(hac) misspecified"
exit 198
}
local vcespec `"`vcerest'"'
local vcerest
}
else if `"`vce'"' == substr("unbiased",1,max(3,`len')) {
local norest norest
local vce unbiased
local vcespec unbiased
}
else if `"`vce'"' == "eim" {
local norest norest
local vce eim
local vcespec eim
}
else {
if `"`vce'"' == "oim" {
local norest norest
local vcespec oim
}
else if `"`vce'"' == "opg" {
local norest norest
local vcespec opg
}
else if `"`0'"' != "" {
if "`irls'" != "" {
di as err ///
"option vce(`0') is not allowed with irls"
exit 198
}
else {
local vceopt `"vce(`0')"'
}
}
}
if "`norest'" != "" & `"`vcerest'"' != "" {
di as err "option vce(`0') invalid"
exit 198
}
sreturn local vce `"`vce'"'
sreturn local vcespec `"`vcespec'"'
sreturn local vceopt `"`vceopt'"'
end
program CheckAdmiss, eclass
tempvar eta
qui predict double `eta', eta
if "`e(link)'" == "glim_l01" {
cap assert `eta' >= 0 & `eta' <=1
local rc = _rc
}
else {
cap assert `eta' <= 0
local rc = _rc
}
if `rc' {
est local adm_warn warn
}
exit
end
program Display
di
syntax [, noHEADer noTABLE Level(cilevel) eform]
if "`eform'" != "" {
Eform
local eform "`r(eform)'"
}
if "`eform'" != "" {
local eopt "eform(`eform')"
}
if "`header'" == "" { Head }
if "`table'" != "" { exit }
_coef_table , level(`level') `eopt' first
if "`e(msg)'" != "" {
noi di as txt "(Standard errors scaled using `e(msg)')"
}
if "`e(msg2)'" != "" {
noi di as txt "`e(msg2)'"
}
if "`e(adm_warn)'" != "" {
noi di "{help j_glmadmiss##|_new:Warning: parameter estimates produce }" _c
noi di "{help j_glmadmiss##|_new:inadmissible mean estimates in one or }"
noi di "{help j_glmadmiss##|_new: more observations.}"
}
if "`e(Nf)'" != "" {
if `e(Nf)' > 1 { local s "s" }
noi di as txt "(" as res `e(Nf)' as txt /*
*/ " model`s' failed to converge)"
}
_prefix_footnote
end
program Head
/*
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+---
Generalized linear models No. of obs = #########
Optimization : ########################## Residual df = #########
########################## Scale parameter = #########
Deviance = ######### (1/df) Deviance = #########
Pearson = ######### (1/df) Pearson = #########
Variance function: V(u) = ################### ###########################
Link function : g(u) = ################### ###########################
Standard errors : ##########################
HAC kernel (lags): Newey-West (72)
AIC = #########
Log likelihood = ############ BIC = #########
-or-
Generalized linear models No. of obs = #########
Optimization : ########################## Residual df = #########
########################## Scale parameter = #########
Deviance = ######### (1/df) Deviance = #########
Pearson = ######### (1/df) Pearson = #########
Variance function: V(u) = ################### ###########################
Link function : g(u) = ################### ###########################
Standard errors : ##########################
HAC kernel (lags): Newey-West (72)
Quasi-likelihood model with dispersion: ##### BIC = #########
*/
di as txt "Generalized linear models" /*
*/ _col(52) "No. of obs" _col(68) "=" /*
*/ _col(70) as res %9.0g e(N)
di as txt "Optimization : " as res "`e(opt1)'" /*
*/ as txt _col(52) "Residual df" _col(68) "=" /*
*/ _col(70) as res %9.0g e(df)
di as res _col(20) "`e(opt2)'" as txt _col(52) "Scale parameter" /*
*/ _col(68) "=" _col(70) as res %9.0g e(phi)
di as txt "Deviance" _col(18) "=" as res _col(20) %12.0g e(deviance) /*
*/ as txt _col(52) "(1/df) Deviance" /*
*/ _col(68) "=" as res _col(70) %9.0g e(dispers)
di as txt "Pearson" _col(18) "=" as res _col(20) %12.0g e(deviance_p) /*
*/ as txt _col(52) "(1/df) Pearson" /*
*/ _col(68) "=" as res _col(70) %9.0g e(dispers_p)
di
di as txt "Variance function: " as res "V(u) = " /*
*/ as res _col(27) "`e(varfuncf)'" /*
*/ _col(52) as txt "[" as res "`e(varfunct)'" as txt "]"
di as txt "Link function : " as res "g(u) = " /*
*/ as res _col(27) "`e(linkf)'" /*
*/ _col(52) as txt "[" as res "`e(linkt)'" as txt "]"
if "`e(hac_kernel)'" != "" {
di
di as txt "HAC kernel (lags): " ///
e(hac_kernel) " ({yellow:`e(hac_lag)'})" _c
local di di
}
else if "`e(setype)'" != "" {
di
di as txt "Standard errors : " as res "`e(setype)'" _c
local di di
}
else local cr _n
if "`e(ll)'" != "" {
local cr
di
local crtype = upper(substr(`"`e(crittype)'"',1,1)) + ///
substr(`"`e(crittype)'"',2,.)
local crlen = max(18,length(`"`crtype'"') + 2)
di as txt _col(52) "AIC" _col(68) "=" ///
as res _col(70) %9.0g e(aic)
di as txt "`crtype'" _col(`crlen') "= " ///
as res %12.0g e(ll) _c
}
else if "`e(disp)'" != "" & "`e(disp)'" != "1" {
local cr
`di'
di
di as txt "Quasi-likelihood model with dispersion: " /*
*/ as res `e(disp)' _c
}
di as txt `cr' _col(52) "BIC" _col(68) "=" ///
as res _col(70) %9.0g e(bic)
di
end
program MapHAC, rclass /* f [lag] */
args nobs f lag
local f = lower(trim(`"`f'"'))
local l = length(`"`f'"')
local s1
local s2
if `"`f'"'=="" { local s1 "" }
else if `"`f'"'==substr("nwest",1,max(`l',2)) { local s1 "glim_nw1" }
else if `"`f'"'==substr("bartlett",1,max(`l',2)) { local s1 "glim_nw1" }
else if `"`f'"'==substr("gallant",1,max(`l',2)) { local s1 "glim_nw2" }
else if `"`f'"'==substr("parzen",1,max(`l',2)) { local s1 "glim_nw2" }
else if `"`f'"'==substr("anderson",1,max(`l',2)) { local s1 "glim_nw3" }
else { local s1 "`f'" }
if "`lag'" != "" {
confirm integer number `lag'
if `lag' < 0 {
noi di as err "Newey-West lag must be positive"
exit 198
}
}
else local lag = `nobs'-2
if `lag' >= `nobs'-2 {
local lag = `nobs'-2
}
local s2 `lag'
ret local hacnam "`s1'"
ret local haclag "`s2'"
end
program MapFL, rclass /* family link */
args f ulink
MapFam `f' /* map user-specified family */
local fam `"`r(famcode)'"' /* store program in fam */
local mfixed 1
local m 1
local k 1
if `"`fam'"'=="glim_v2" { /* bin takes an optional argument */
tokenize `"`f'"'
if `"`2'"'!="" {
capture confirm number `2'
if _rc {
unabbrev `2'
local m `"`s(varlist)'"'
local mfixed 0
}
else {
if `2'>=. | `2'<1 {
di as err /*
*/ `"`2' in family(binomial `2') invalid"'
exit 198
}
local m `2'
}
}
if `"`3'"'!="" {
di as err "family(`f') invalid"
exit 198
}
}
if `"`fam'"'=="glim_v6" { /* nb takes an optional argument */
tokenize `"`f'"'
if `"`2'"' != "" {
confirm number `2'
if `2'<=0 {
di as err /*
*/ `"`2' in family(nbinomial `2') invalid"'
exit 198
}
local k `2'
}
if `"`3'"'!="" {
di as err "family(`f') invalid"
exit 198
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -