Computed theoretical power for N=100 and N=200 scenarios

This commit is contained in:
2024-02-19 18:35:26 +01:00
parent ac9189d26a
commit 238852b08b
704 changed files with 261610 additions and 187 deletions

View File

@ -0,0 +1,835 @@
*! version 1.9.0 2012-07-29 fixes for o. in stata 11+
capture program drop fitstat
program define fitstat, rclass
local ecmdversion = c(version) // 190
version 9.0
local cmd `e(cmd)'
if ( "`cmd'"!="regress" & "`cmd'"!="logit" & "`cmd'"!="probit" ///
& "`cmd'"!="logistic" & "`cmd'"!="cloglog" & "`cmd'"!="ologit" ///
& "`cmd'"!="oprobit" & "`cmd'"!="gologit" & "`cmd'"!="mlogit" ///
& "`cmd'"!="clogit" & "`cmd'"!="tobit" & "`cmd'"!="cnreg" ///
& "`cmd'"!="intreg" & "`cmd'"!="poisson" & "`cmd'"!="nbreg" ///
& "`cmd'"!="zip" & "`cmd'"!="zinb" ///
& "`cmd'"!="ztp" & "`cmd'"!="ztnb" & "`cmd'"!="slogit" ///
& "`cmd'"!="mprobit" & "`cmd'"!="rologit" ///
) {
di in r "fitstat does not work with the last model estimated."
exit
}
tempname n_obs n_obs2 n_parm llF llR df x2 r2 b v_ystar v v_y v_x v_e
tempname factor r2mcf r2mcfa r2mz r2ml r2cu r2 r2ef r2adj aic aicn bicx
tempname bicp isbin isord isvare isystar islgt ispbt ssr dev lrx2 lrx2p
tempname depv2 maxtmp uniqgrp n_obs2 depvgrp ifin v_error bicdif fllR
tempname bnocon phat yphat ydev predpr predcat wrong hiprob fbicp fn_obs
tempname totcor totone maxrow cntr2 cntr2a chkobs d d2 prevval prevdf
tempname results erank Vb noomit
syntax [if] [in] ///
[, Saving(string) Using(string) SAVE Diff FORCE Bic NOSTATA ///
Debug(integer 0) ///
]
* 181 mat `b' = get(_b)
* 190: remove o. from beta matrix
mat `b' = get(_b)
mat `Vb' = e(V)
local colfn : colfullnames `b'
local coln : colnames `b'
local coleq : coleq `b'
_ms_omit_info `b' // remove columns of zeros from b
local cols = colsof(`b')
mat `noomit' = J(1,`cols',1) - r(omit)
forvalues i = 1/`cols' { // double clear for 0 std error
if `Vb'[`i',`i'] == 0 & `noomit'[1,`i']==1 {
mat `noomit'[1,`i']==0
}
}
mata: newbeta = ///
select(st_matrix(st_local("b")), (st_matrix(st_local("noomit"))))
mata: st_matrix(st_local("b"),newbeta)
// reassign column names to b
local i = 0
foreach var of local coln {
local ++i
local nmvar : word `i' of `coln'
local nmeq : word `i' of `coleq'
local nmfn : word `i' of `colfn'
_ms_parse_parts `var'
if "`nmvar'"=="`nmfn'" { // no eq nm
local nmeq ""
}
else { // eq nm
local nmeq "[`nmeq']"
}
if !`r(omit)' & `nmeq'_se[`nmvar']!=0 {
local coln2 `coln2' `nmfn'
}
}
version 11: matrix colnames `b' = `coln2'
local depv `e(depvar)'
if "`nostata'"!="nostata" { // stata bic and aic
tempname stataic statabic stataaic
qui estat ic
mat `stataic' = r(S)
scalar `statabic' = `stataic'[1,6]
scalar `stataaic' = `stataic'[1,5]
}
if ("`save'"!="") local saving 0
if ("`diff'"!="") local using 0
*-> get information from last model estimated
local wtis ""
local iswgt "no"
if "`e(wtype)'"!="" {
local iswgt "yes"
local wtis "[`e(wtype)'`e(wexp)']"
}
mat `v_e' = J(1,1,1)
sca `n_obs' = e(N)
sca `llF' = e(ll)
sca `llR' = e(ll_0)
sca `df' = e(df_m)
if "`cmd'"~="regress" & "`cmd'"~="tobit" & "`cmd'"~="cnreg" {
sca `x2' = e(chi2)
}
if "`cmd'"=="tobit" | "`cmd'"=="cnreg" {
sca `x2' = e(F)
}
if "`cmd'"=="regress" {
sca `ssr' = e(rss)
sca `r2' = e(r2)
sca `r2adj' = e(r2_a)
mat `v_e'[1,1] = (e(rmse)*e(rmse)*e(df_r)/e(N))
}
if "`cmd'"=="clogit" {
if "`e(wtype)'"=="iweight" |"`e(wtype)'"=="pweight" ///
| "`e(wtype)'"=="aweight" {
di in r "cannot use fitstat after clogit with `e(wtype)'"
exit
}
qui egen `uniqgrp' = rank(_n) if e(sample)==1, by(`e(group)')
qui replace `uniqgrp' = . if `uniqgrp' ~= 1
qui sum `uniqgrp' `wtis', meanonly
sca `n_obs' = r(sum_w)
}
*-> classify type of model
* binary?
local isbin "no"
if ("`cmd'"=="probit") local isbin "yes"
if ("`cmd'"=="logit") local isbin "yes"
if ("`cmd'"=="logistic") local isbin "yes"
if ("`cmd'"=="cloglog") local isbin "yes"
* ordinal?
local isord "no"
if ("`cmd'"=="oprobit") local isord "yes"
if ("`cmd'"=="ologit") local isord "yes"
* var(e) assumed?
local isvare "no"
if ("`cmd'"=="probit") local isvare "yes"
if ("`cmd'"=="logit") local isvare "yes"
if ("`cmd'"=="logistic") local isvare "yes"
if ("`cmd'"=="oprobit") local isvare "yes"
if ("`cmd'"=="ologit") local isvare "yes"
* is there a ystar?
local isystar "`isvare'"
if ("`cmd'"=="tobit") local isystar "yes"
if ("`cmd'"=="intreg") local isystar "yes"
if ("`cmd'"=="cnreg") local isystar "yes"
* logit model?
local islgt "no"
if ("`cmd'"=="logit") local islgt "yes"
if ("`cmd'"=="ologit") local islgt "yes"
if ("`cmd'"=="logistic") local islgt "yes"
* probit model?
local ispbt "no"
if ("`cmd'"=="probit") local ispbt "yes"
if ("`cmd'"=="oprobit") local ispbt "yes"
* zip/zinb?
local iszi "no"
if ("`cmd'"=="zip") local iszi "yes"
if ("`cmd'"=="zinb") local iszi "yes"
* provide pseudoR2 measures - jf 20Jun2005
local pseudo "yes"
if ("`cmd'"=="regress") local pseudo "no"
if ("`cmd'"=="rologit") local pseudo "no"
if ("`cmd'"=="slogit") local pseudo "no"
if ("`cmd'"=="mprobit") local pseudo "no"
* log-lik intercept only - jf 20Jun2005
local llio "yes"
if ("`cmd'"=="rologit") local llio "no"
if ("`cmd'"=="slogit") local llio "no"
if ("`cmd'"=="mprobit") local llio "no"
*-> compute numbers of variables and parameters
sca `n_parm' = colsof(`b')
local n_cat = e(k_cat)
* version 1.8.1 2007-09-18 - stata 10 changed returns for some commands
if c(stata_version) >= 10 {
local n_cat = e(k_out)
if ("`cmd'"=="ologit" | "`cmd'"=="oprobit") local n_cat = e(k_cat)
}
if "`n_cat'"=="." {
local n_cat = 2
}
local n_rhs = `n_parm' - `n_cat' + 1
if "`cmd'"=="mlogit" {
local n_rhs = (`n_parm'/(`n_cat'-1)) - 1
}
if "`cmd'"=="tobit" | "`cmd'"=="intreg" /// for var(e)
| "`cmd'"=="nbreg" | "`cmd'"=="cnreg" /// for alpha
| "`cmd'"=="zip" /// for 2nd intercept
{
local n_rhs = `n_rhs' - 1
}
if "`cmd'"=="zinb" {
local n_rhs = `n_rhs' - 2
} // 2nd int & alpha
local varlist : colnames(`b')
parse "`varlist'", parse (" ")
local rhsnam ""
if "`isord'"=="yes" {
local i 1
while "``i''"!= "" {
local j = substr("``i''",1,4)
* pre1.7.0 050216 if "`j'"!="_cut" {
* 050216 for Stata 9
if "`j'"!="_con" {
local rhsnam "`rhsnam' ``i''"
}
local i = `i' + 1
}
}
if "`isbin'"=="yes" | "`cmd'"=="mlogit" | "`cmd'"=="regress" /*
*/ | "`cmd'"=="tobit" | "`cmd'"=="intreg" | "`cmd'"=="cnreg" /*
*/ | "`cmd'"=="poisson" | "`cmd'"=="nbreg" {
local i 1
while "``i''"!= "" {
if "``i''"!="_cons" & "``i''"!="_se" {
local rhsnam "`rhsnam' ``i''"
}
local i = `i' + 1
}
}
*-> compute ll0 for zip and zinb (ll0 is for model with two intercepts)
if "`iszi'"=="yes" {
tempname res zill0
tempvar insamp
sca `df' = `n_parm' - 2
if "`cmd'"=="zinb" {
* remove alpha parameter
sca `df' = `n_parm' - 3
}
gen `insamp' = e(sample)
local inftyp "`e(inflate)'"
if "`inftyp'"=="logit" local inftyp ""
local doit "`e(cmd)' `e(depvar)' if `insamp' `wtis',inf(_cons) `inftyp'"
_estimates hold `res'
quietly `doit'
sca `llR' = e(ll) /* sic: not ll_0 */
_estimates unhold `res'
sca `x2' = -2*(`llR'-`llF')
} // iszi model
if "`bic'" == "" {
di _newline in g "Measures of Fit for " in y "`cmd'" in g " of " ///
in y "`depv'" _newline
}
if "`isbin'" == "yes" | "`isord'" == "yes" | "`cmd'" == "mlogit" ///
| "`cmd'" == "mprobit" {
* sum does not allow pweights or iweights
if "`e(wtype)'"=="pweight" | "`e(wtype)'"=="iweight" {
sca `cntr2a' = .
sca `cntr2' = .
if "`isbin'" == "yes" {
sca `r2ef' = .
}
if "`isbin'"=="yes" {
di _n in blu /*
*/ "(Efron's R2, Count R2, and Adj Count R2 not calculated if " /*
*/ in whi "`e(wtype)'" in blu " used)"
}
else {
di _n in blu /*
*/ "(Count R2 and Adj Count R2 not calculated if " /*
*/ in whi "`e(wtype)'" in blu " used)"
}
}
* can compute measures if aweight, fweight or no weight
else quietly {
egen `depvgrp' = group(`depv') if e(sample)==1
sca `maxrow' = 0
gen `hiprob' = 0 if e(sample)==1
gen `predcat' = 0 if e(sample)==1
local counter = 1
* BRM - Efron R2 and Count R2s
if "`isbin'" == "yes" {
* Efron R2
* convert 0/!0 to 0/1 for binary outcomes
gen `depv2' = (`depv'!=0) if e(sample)==1
predict `phat' if e(sample)==1
sum `depv2' `wtis' if e(sample)==1
local mn = r(mean)
gen `yphat' = (`depv2' - `phat')^2 if e(sample)==1
gen `ydev' = (`depv2' - `mn')^2 if e(sample)==1
sum `yphat' `wtis' if e(sample)==1
local num = r(mean)
sum `ydev' `wtis' if e(sample)==1
local den = r(mean)
scalar `r2ef' = 1 - (`num'/`den')
* Count R2 and Adjusted Count R2
replace `predcat' = round(`phat', 1) if e(sample)==1
gen `wrong' = (`predcat'~=`depv2') if e(sample)==1
sum `depv2' `wtis' if `depv2' == 1 & e(sample)==1
sca `totone' = r(N)
if `totone' > (`n_obs'-`totone') {
sca `maxrow' = `totone'
}
else {
sca `maxrow' = `n_obs' - `totone'
}
}
* ORM - Count R2s
if "`isord'" == "yes" {
forvalues counter = 1(1)`n_cat' {
* ctnam is argument for ologitp/oprobitp
tempvar ct`counter'
local ctnam "`ctnam' `ct`counter''"
* find frequency of the modal category (for adj count R2)
sum `depvgrp' if (`depvgrp'==`counter') & (e(sample)==1) `wtis'
if r(N) > `maxrow' {
sca `maxrow' = r(N)
}
}
predict `ctnam' if e(sample) == 1
forvalues counter = 1(1)`n_cat' {
replace `predcat' = `counter' ///
if (`ct`counter''>`hiprob') & (e(sample)==1)
replace `hiprob' = `ct`counter'' ///
if (`ct`counter''>`hiprob') & (e(sample)==1)
}
gen `wrong' = (`predcat'~=`depvgrp') if e(sample)==1
}
* MNLM & and MPROBIT - Count R2s
if "`cmd'" == "mlogit" ///
| "`cmd'" == "mprobit" {
while `counter' <= `n_cat' {
tempvar ct`counter'
sum `depv' `wtis' ///
if (`depvgrp'==`counter') & (e(sample)==1), meanonly
local outcm = r(mean)
predict `ct`counter'', outcome(`outcm')
sum `depv' if (`depv'==`outcm') & (e(sample)==1) `wtis'
if r(N) > `maxrow' {
sca `maxrow' = r(N)
}
replace `predcat' = `outcm' ///
if (`ct`counter''>`hiprob') & (e(sample)==1)
replace `hiprob' = `ct`counter'' ///
if (`ct`counter''>`hiprob') & (e(sample)==1)
local counter = `counter' + 1
}
gen `wrong' = (`predcat'~=`depv') if e(sample)==1
}
* compute count R2s
sum `wrong' if `wrong' == 0 & e(sample) == 1 `wtis'
sca `totcor' = r(N)
sca `cntr2' = `totcor'/`n_obs'
sca `cntr2a' = (`totcor'-`maxrow')/(`n_obs'-`maxrow')
}
} // is binary, ordinal or nominal
* CLOGIT - Count R2s
if "`cmd'"=="clogit" {
quietly {
tempname cntr2 clphat clhi clpick clpred clwrong clright /*
*/ posval motest1 motest2 mptest1
* generate predicted outcomes for each group
predict `clphat' if e(sample) == 1
egen `clhi' = max(`clphat') if e(sample) == 1, by(`e(group)')
gen `clpred' = 1 if `clhi'==`clphat' & e(sample)==1
replace `clpred' = 0 if `clhi'!=`clphat' & e(sample)==1
* check if multiple predicted 1's per group
egen `mptest1' = sum(`clpred') if e(sample), by(`e(group)')
* weight of multiple predicted 1's per group split among each 1
replace `clpred' = `clpred'/`mptest1' if `clpred'==1 & e(sample)==1
* calculate count R2
sum `clpred' `wtis' if `clpred'>0 & `clpred'~=. & `depv'==0 & e(sample)==1
sca `clwrong' = r(sum)
sum `clpred' `wtis' if `clpred'>0 & `clpred'~=. & `depv'!=0 & e(sample)==1
sca `clright' = r(sum)
sca `cntr2' = `clright' / (`clwrong'+`clright')
* warn user if multiple outcomes per group
gen `posval'=1 if `depv'!=0 & `depv'~=. & e(sample)==1
egen `motest1' = sum(`posval') if `posval'==1, by(`e(group)')
sum `motest1'
sca `motest2' = r(max)
if `motest2' > 1 {
noi di in blu "(Multiple outcomes per group for clogit: Count R2 may not be valid)"
}
} // quietly
} // clogit
*-> compute Var(y*) using cov_x
if "`isystar'"=="yes" {
sca `factor' = 1/(`n_obs'-1)
quietly mat accum `v_x' = `depv' `rhsnam' `wtis' if e(sample), ///
deviations noconstant
sca `v_y' = `factor' * `v_x'[1,1] // sum y/(n-1)
mat `v_x' = `factor' * `v_x'[2...,2...] // cov matrix of rhs vars
if "`e(cmd)'"=="intreg" {
mat `v_x' = `v_x'[2...,2...]
}
mat `bnocon' = `b'[1,1..`n_rhs'] // trim off _con
mat `v' = `bnocon' * `v_x' * `bnocon''
if "`ispbt'"=="yes" {
mat `v_e'[1,1] = 1
}
if "`islgt'"=="yes" {
mat `v_e'[1,1] = (_pi*_pi)/3
}
if "`e(cmd)'"=="intreg" {
mat `v_e'[1,1] = e(sigma)*e(sigma)
}
if "`e(cmd)'"=="tobit" | "`e(cmd)'"=="cnreg" {
mat `v_e'[1,1] = `b'[1,"sigma:_cons"]*`b'[1,"sigma:_cons"]
}
mat `v' = `v' + `v_e'
sca `v_ystar' = `v'[1,1]
}
*-> information measures
sca `aicn' = ((-2*`llF') + (2*`n_parm'))
sca `aic' = `aicn'/`n_obs'
sca `dev' = -2*`llF'
local devdf = `n_obs'-`n_parm'
sca `lrx2' = 2*(`llF'-`llR')
local lrx2df = e(df_m)
if "`iszi'"=="yes" {
local lrx2df = `df'
}
if "`cmd'"=="mprobit" {
scalar `lrx2' = e(chi2)
}
sca `lrx2p' = chiprob(`lrx2df',`lrx2')
if "`cmd'"=="slogit" {
scalar `lrx2' = e(chi2)
sca `lrx2p' = e(p)
}
sca `bicx' = `dev' - (`devdf'*ln(`n_obs'))
sca `bicp' = -`lrx2' + (e(df_m)*ln(`n_obs'))
capture return scalar dev_df = `devdf'
capture return scalar lrx2_df = `lrx2df'
if "`iszi'"=="yes" {
* df = # rhs in both equations
sca `bicp' = -`lrx2' + (`df'*ln(e(N)))
}
* these equal usual R2 in regress
if "`pseudo'"=="yes" {
sca `r2mcf' = 1 - (`llF'/`llR')
sca `r2mcfa' = 1 - ((`llF'-`n_parm')/`llR')
sca `n_obs2' = 2/`n_obs'
sca `r2ml' = 1 - exp(2*(`llR'-`llF')/`n_obs')
sca `r2cu' = (`r2ml')/(1-exp(2*`llR'/`n_obs'))
}
if "`isystar'"=="yes" {
sca `r2mz' = (`v_ystar' - `v_e'[1,1])/`v_ystar'
sca `v_error' = `v_e'[1,1]
}
*-> output
* define output macros with format where / is parse character:
* output_text/tempname_of_scalar/abbrev_for_r()
* /df?/width/half(1, 2 or ? for either)
local stat2 "Log-Lik Intercept Only/llR/ll_0/no/11/1"
local stat3 "Log-Lik Full Model/llF/ll/no/11/2"
local stat4 "D/dev/dev/yes/11/1"
local stat6 "LR/lrx2/lrx2/yes/11/2"
local stat8 "Prob > LR/lrx2p/lrx2_p/no/9/2"
local stat9 "McFadden's R2/r2mcf/r2_mf/no/9/1"
local stat10 "McFadden's Adj R2/r2mcfa/r2_mfadj/no/9/2"
local stat11 "ML (Cox-Snell) R2/r2ml/r2_ml/no/9/1"
local stat12 "Cragg-Uhler(Nagelkerke) R2/r2cu/r2_cu/no/7/2"
local stat13 "McKelvey & Zavoina's R2/r2mz/r2_mz/no/9/?"
local stat14 "Efron's R2/r2ef/r2_ef/no/9/?"
local stat15 "Variance of y*/v_ystar/v_ystar/no/11/1"
local stat16 "Variance of error/v_error/v_error/no/11/2"
local stat17 "Count R2/cntr2/r2_ct/no/9/1"
local stat18 "Adj Count R2/cntr2a/r2_ctadj/no/9/2"
local stat19 "R2/r2/r2/no/9/1"
local stat20 "Adjusted R2/r2adj/r2_adj/no/9/2"
local stat21 "AIC/aic/aic/no/11/1"
local stat22 "AIC*n/aicn/aic_n/no/11/2"
local stat23 "BIC/bicx/bic/no/11/1"
local stat24 "BIC'/bicp/bic_p/no/11/2"
local stat25 "BIC used by Stata/statabic/statabic/no/11/1"
local stat26 "AIC used by Stata/stataaic/stataaic/no/11/2"
if "`llio'"=="no" {
local stat2 ""
local stat3 "Log-Lik Full Model/llF/ll/no/11/1"
local stat4 "D/dev/dev/yes/11/2"
}
if "`cmd'"=="slogit" local stat24 ""
if "`cmd'"=="mprobit" | "`cmd'"=="slogit" {
local stat6 "Wald X2/lrx2/lrx2/yes/11/1"
}
if "`cmd'"=="mprobit" | "`cmd'"=="slogit" {
local stat8 "Prob > X2/lrx2p/lrx2_p/no/9/2"
}
*-> check models for using and saving
if "`saving'" != "" {
if (length("`saving'")>4) {
di in red "saving() name must be < 5 characters long"
exit 198
}
}
if "`using'"!= "" {
if (length("`using'")>4) {
di in red "using() name must be < 5 characters long"
exit 198
}
qui capture mat list fs_`using'
if _rc == 111 {
di in red "Incorrect using() option: no indices saved as `using'"
exit 111
}
* if the using option has been set, prepare comparison macros
sca `fn_obs' = fs_`using'[1,1]
if `fn_obs' == -9999 {
sca `fn_obs' = .
}
* estimate command saved as row name
local fcmd : rownames(fs_`using')
local fcmd : word 1 of `fcmd'
* test if current and using are same type of model
local same "no"
if ("`fcmd'"=="logit"&"`e(cmd)'"=="logistic") | ///
("`fcmd'"=="logistic"&"`e(cmd)'"=="logit") {
local same "yes"
}
if ("`fcmd'"!="`e(cmd)'" & "`same'"=="no") & "`force'"=="" {
di in r "Current model estimated by `e(cmd)'" ///
in r ", but saved model estimated by `fcmd'."
di in r "To make the comparisons anyway, use the " ///
"force option."
exit
}
if ("`fcmd'"!="`e(cmd)'" & "`same'"=="no") {
di in y "Warning: Current model estimated by " /*
*/ "`e(cmd)', but saved model estimated by `fcmd'"
di
}
if `n_obs' != `fn_obs' & "`force'"=="force" {
di in y "Warning: N's do not match."
di
}
di in g _col(30) %9s "Current" _col(48) " Saved" ///
_col(65) "Difference"
di in g "Model:" _col(30) %9s in y "`e(cmd)'" ///
%9s _col(48) in y "`fcmd'"
di in g "N:" _col(30) %9.0f in y `n_obs' ///
%9.0f _col(48) in y `fn_obs' ///
%9.0f _col(66) in y (`n_obs' - `fn_obs')
* test if N's match
if `n_obs' != `fn_obs' & "`force'"=="" {
di _newline in r "N's do not match. To make the comparisons, use the " ///
in y "force" in r " option."
exit
}
* test of ll0 match
sca `fllR' = fs_`using'[1,2]
if `fllR'==-9999 {
sca `fllR' = .
}
if abs(`llR'-`fllR') > .0001 & (`llR'-`fllR') ~= . & "`force'"=="" {
di _newline in r /*
*/ "Log-likelihoods for intercept model differ for current and saved models."
di in r "To make the comparisons, use the " in y "force" in r " option."
exit
}
} /* if "`using'"!="" */
capture return scalar n_parm = `n_parm'
capture return scalar n_rhs = `n_rhs'
capture return scalar N = `n_obs'
*-> create matrices and macros containing results
local nstats = 24 // number of stats reported in output matrix
if "`nostata'"!="stata" local nstats = `nstats' + 2
mat `results' = J(1, `nstats'+2, .)
mat `results'[1,1] = `n_obs'
mat `results'[1,`nstats'+1] = `n_parm'
mat `results'[1,`nstats'+2] = `n_rhs'
local cnames "N" // start bulding column names for output matrix
forvalues i = 2(1)`nstats' {
* read individual stats
tokenize "`stat`i''", parse(/)
* outtxt - name of statistic
local outtxt`i' "`1'"
* scalnm - name of scalar holding value of statistic
local scalnm`i' "`3'"
* abnm - name of statistic for return value
local abnm`i' "`5'"
* df - whether statistic has df
local df`i' "`7'"
* outwid - width of output - TO DO: try to delete this
local outwid`i' "`9'"
* outhalf - need to be in first or second half
local outhalf`i' "`11'"
* cnames - macro of column names for output
local cnames "`cnames' `abnm`i''"
if "`df`i''"=="yes" {
local cnames "`cnames' `abnm`i''_df"
}
* see if scalar exists, if not, no output
capture return scalar `abnm`i'' = ``scalnm`i'''
if _rc != 0 {
if "`outtxt`i''" != "" {
mat `results'[1, `i'] = .x
if "`df`i''"=="yes" {
mat `results'[1,`i'+1] = .x
}
}
}
if _rc == 0 {
* add to results matrix
mat `results'[1, `i'] = ``scalnm`i'''
if "`df`i''"=="yes" {
mat `results'[1,`i'+1] = ``scalnm`i''df'
}
} // if _rc == 0
if "`df`i''"=="yes" {
local i = `i' + 1
}
} // forvalues i
*-> loop through to print results if using option not used
if "`using'"=="" {
* arrange numbers in rows and columns for printing
local trow = 1
local tcol = 1
forvalues i = 2(1)`nstats' {
* figure out if result should be in output
if `results'[1,`i']!=.x & "`outtxt`i''" != "" & ///
("`bic'"=="" | `i' >= 21) {
* figure out if need to advance to next row of table
if "`outhalf`i''" == "1" & `tcol' == 3 {
local ++trow
local tcol = 1
}
if "`outhalf`i''" == "2" & `tcol' == 1 {
local tcol = 3
}
local op`trow'_`tcol' = "`outtxt`i''"
if "`df`i''" == "yes" {
local temp = ``scalnm`i''df'
local op`trow'_`tcol' = "`op`trow'_`tcol''(`temp')"
}
local op`trow'_`tcol' = "`op`trow'_`tcol'':"
local ++tcol
local op`trow'_`tcol' = ``scalnm`i'''
* take care of output width
local fmt`trow'_`tcol' = "%`outwid`i''.3f"
// move to next column
local ++tcol
if `tcol' == 5 { // if needed, move to next row
local tcol = 1
local ++trow
}
}
} // forvalues i
* print rows and columns
local numrows = `trow'
if `tcol' == 1 {
local numrows = `trow' - 1
}
forvalues i = 1(1)`numrows' {
di in g "`op`i'_1'" _col(28) in y %11.3f `op`i'_2' ///
_col(42) in g "`op`i'_3'" _col(69) in y %11.3f `op`i'_4'
}
} // using option not used
*-> loop through to print results if using option has been used
if "`using'"!="" {
local trow = 1
forvalues i = 2(1)`nstats' {
* figure out if result should be in output
if `results'[1,`i']!=.x & ("`bic'"=="" | `i' >= 21) {
* calculate differences
sca `prevval' = fs_`using'[1,`i']
tempname difvals
sca `difvals' = ``scalnm`i'''-`prevval'
* for LR prob give LR prob -- computed during LR loop
if "`outtxt`i''"=="Prob > LR" {
sca `difvals' = `LRchip'
}
* print results
if "`df`i''" == "no" {
di in g "`outtxt`i''" ///
_col(28) in y %11.3f ``scalnm`i''' ///
_col(46) in y %11.3f `prevval' ///
_col(64) in y %11.3f `difvals'
}
if "`df`i''" == "yes" {
local tmp_curdf = ``scalnm`i''df'
sca `prevdf' = fs_`using'[1,(`i'+ 1)]
local tmp_prevdf = `prevdf'
local tmp_difdf = abs(`tmp_curdf' - `tmp_prevdf')
* for D remove the negative sign from the difference
if "`outtxt`i''"=="D" {
sca `difvals' = abs(`difvals')
}
* for LR statistic compute LR test prob
if "`outtxt`i''"=="LR" | "`outtxt`i''"=="Wald LR" {
tempname LRdif LRdf LRchip
sca `difvals' = abs(``scalnm`i''' - `prevval')
sca `LRdf' = abs(``scalnm`i''df'-`prevdf')
sca `LRchip' = chiprob(`LRdf', `difvals')
}
di in g "`outtxt`i''" ///
_col(28) in y %11.3f ``scalnm`i''' in g "(`tmp_curdf')" ///
_col(46) in y %11.3f `prevval' in g "(`tmp_prevdf')" ///
_col(64) in y %11.3f `difvals' in g "(`tmp_difdf')"
}
}
} // forvalues i
}
*-> BIC comparisons
if "`using'" != "" {
if abs(`llR'-`fllR') < .0001 { /* check that LL0s are the same */
sca `fbicp' = fs_`using'[1, 24]
sca `bicdif' = abs(`bicp'-`fbicp')
if `bicdif'~=. {
local sup "very strong"
if `bicdif'<= .0000000001 {
local sup "no"
}
else if `bicdif' <=2 {
local sup "weak"
}
else if `bicdif' <=6 {
local sup "positive"
}
else if `bicdif' <=10 {
local sup "strong"
}
local modpref "current"
if `bicp' > `fbicp' {
local modpref "saved"
}
if `bicdif'< .0000000001 & `bicdif'>-.0000000001 /*
*/ {
local modpref "either"
}
di _newline in g "Difference of " %8.3f in y `bicdif' in g /*
*/ " in BIC' provides " in y "`sup'" /*
*/ in g " support for " in y "`modpref'" in g " model."
}
} /* if `llR' == `fllR' */
} /* if "`using'" != "" */
if "`bic'"=="" & "`using'" != "" {
di _n in g "Note: p-value for difference in LR is only valid if models are nested."
}
local cnames "`cnames' n_parm n_rhs"
mat rownames `results' = `e(cmd)'
mat colnames `results' = `cnames'
if "`saving'" != "" {
capture mat drop fs_`saving'
mat fs_`saving' = `results'
di _n in blu "(Indices saved in matrix fs_`saving')"
}
quietly `cmd'
end
exit
* version 1.8.1 2007-09-18 returns for some commands changed in Stata 10
* version 1.8.0 19Jun2006 df correction in lrtest for zinb
* version 1.7.9 26Oct2005 fix slogit and mprobit output for LR to Wald X2
* version 1.7.8b 20Jun2005 small fix to new output routine
* version 1.7.8 20Jun2005 new output routine
* version 1.7.7 17Jun2005 streamlined/stata9 improvements to code
* version 1.7.6 15Jun2005 added rologit
* version 1.7.5 10Jun2005 clogit, tobit, cnreg, Cragg-Uhler (Nagelkerke) R2 for dif, ztp, ztnb
* version 1.7.4 10Jun2005 align Variance of y*
* version 1.7.2 13Apr2005
* version 1.7.1 28Feb2005 mprobit; fixed bic'; added count R2
* version 1.7.0 16Feb2005 slogit mprobit ztp, ztnb, format on others
* version 1.6.9 12Feb2005 add stata bic and aic
* version 1.6.8 07Feb2005 add ztp and ztnb
* version 1.6.7 22Jan2005 change LRX2

View File

@ -0,0 +1,79 @@
.-
help for ^fitstat^ - 03Nov2005
.-
To document: ztp and ztnb
: option nostata to supress bic aic based on stata formula
Compute fit statistics for single equation regression models.
-------------------------------------------------------------
^fitstat^ [^, s^aving^(name)^ ^u^sing^(name)^ ^b^ic ^force^ ^save^ ^diff^]
Description
-----------
^fitstat^ is a post-estimation command that computes a variety of measures
of fit for many kinds of regression models. It works after the following:
^clogit^, ^cnreg^, ^cloglog^, ^intreg^, ^logistic^, ^logit^, ^mlogit^, ^nbreg^, ^ocratio^,
^ologit^, ^oprobit^, ^poisson^, ^probit^, ^regress^, ^zinb^, and ^zip^. With the ^s^aving^()^
and ^u^sing^()^ options, it can also be used to compare fit measures for two
different models.
For all models, ^fitstat^ reports the log-likelihoods of the full and
intercept-only models, the deviance (D), the likelihood ratio chi-square
(G2), Akaike's Information Criterion (AIC), AIC*N, the Bayesian Information
Criterion (BIC), and BIC'.
For all models except ^regress^, ^fitstat^ reports McFadden's R2, McFadden's
adjusted R2, the maximum likelihood R2, and Cragg & Uhler's R2. These
measures all equal R2 for OLS regression. ^fitstat^ reports R2 and the
adjusted R2 after ^regress^.
fitstat reports the regular and adjusted count R2 for categorical data models.
For ordered or binary logit or probit models, as well as models for censored
data (^tobit^, ^cnreg^, or ^intreg^), it also reports McKelvey and Zavonia's R2.
In addition, it reports Efron's R2 for ^logit^ or ^probit^.
Not all measures are provided for models estimated with ^pweight^s or ^iweight^s.
Options
-------
^saving(^name^)^ saves the computed measures in a matrix for subsequent
comparisons. Name cannot be longer than 4 characters.
^using(^name^)^ compares the fit measures for the current model with those of
the model saved as name. Name cannot be longer than 4 characters.
^bic^ only presents information measures.
^force^ will compare two models even when the number of observations or the
estimation method varies between the two models.
^save^ and ^diff^ are equivalents of saving(0) and using(0) and do not require
the user to provide model names.
Examples
--------
To compute fit statistics for a single model:
. ^use mroz,clear^
. ^logit lfp k5 k618 age wc hc lwg inc^
. ^fitstat^
To compute and save fit measures:
. ^logit lfp k5 k618 age wc hc lwg inc^
. ^fitstat, saving(mod1)^
To compare saved model to current model:
. ^logit lfp k5 k618 age age2 wc hc lwg inc^
. ^fitstat, using(mod1)^
.-
Authors: J. Scott Long and Jeremy Freese
www.indiana.edu/~jslsoc/spost.htm
spostsup@@indiana.edu

View File

@ -0,0 +1,214 @@
capture program drop fitstat_ers
*!version 1.0 22aug 2011
*!christian a. gregory, economic research service
***********************************************************************************************
*this program computes outfit and infit statistics for the conditional maximum likelihood (cml)
*rasch model, using formulas outlined in the documentation. these fit statistics differ from
*those computed by the raschtest. for more information on the fit statistics, see documentation
*or contact mark nord @ marknord@ers.usda.gov, or christian gregory @ cgregory@ers.usda.gov.
************************************************************************************************
*the gammasym module is required to run this program. to get this module, type "findit gammasym"
*in stata. if you wish to keep the fit statistics in an excel file, install xml_tab.ado; if you wish
*to keep them in a LaTex file, install outtable.ado.
************************************************************************************************
program define fitstat_ers, rclass
version 9.0
syntax varlist(min=2) [if] [in], ///
[excel(string) ///
latex(string) ///
replace ]
tempfile fitstat
qui save `fitstat', replace
marksample touse
qui keep if `touse'
qui keep `varlist'
*compute score
tempvar score
genscore `varlist', score(`score')
*prep data to reshape
g id = _n
local r 0
foreach x of local varlist {
local `++r'
rename `x' item`r'
}
local nbitems: word count `varlist'
*total obs before reshape
qui count if `score'!=0 & `score'!=`nbitems'
local totalobs = r(N)
*get size of groups by score
forvalues i=0/`nbitems' {
qui count if `score'==`i' //& item==1
local realscore`i'=r(N)
}
local t = `nbitems'-1
*reshape
qui reshape long item, i(id) j(rep)
forvalues i = 1/`nbitems' {
qui {
g item`i' = rep==`i'
replace item`i' = -item`i'
}
}
*estimate parameters
qui clogit item item1-item`t', group(id)
matrix beta = e(b)
forvalues i=1/`t' {
local beta`i' = beta[1,`i']
}
local beta`nbitems' = 0
tempname Pi
matrix define `Pi'=J(`nbitems',`t',0)
tempname Obs Obs2 Th Th2
matrix define `Obs'=J(`nbitems',`t',0)
matrix define `Th'=J(`nbitems',`t',0)
local listofitemsc
******************************************************************************************************
*estimation of the gamma symetrical functions. i use these to calculate the expected values of the
*model, conditional on the score. hardouin (07) and glas(88) use these forms. the current stata module
*-raschtest- uses them for general cml models and R tests.
*******************************************************************************************************
forvalues j=1/`nbitems' {
local listini`j'
local listofitemsc "`listofitemsc' `beta`j''"
forvalues k=1/`nbitems' {
local listini`j'k`k'
if `k'!=`j' {
local listini`j' "`listini`j'' `beta`k''" //vector w/o current item paramter--diags of vcv
}
forvalues l=1/`nbitems' {
if `l'!=`j'&`l'!=`k' {
local listini`j'k`k' "`listini`j'k`k'' `beta`l''" //vector w/o current item parameter--off diags of vcv
}
}
}
}
qui gammasym `listofitemsc' //gamma function of full vector of difficulty parameters
*estimation of probabilities (pi) and expected (th), as well as observed (obs)
forvalues s=1/`nbitems' {
local denom`s'=r(gamma`s')
}
local m = `nbitems'-1
local p = `m'-1
forvalues j=1/`nbitems' {
forvalues s=1/`m' {
local r=`s'-1
qui count if rep==`j'& item==1 &`score'==`s'
matrix `Obs'[`j',`s']=r(N)
gammasym `listini`j''
local num`j'=r(gamma `r') //gamma function of vector w/o item j, for score s-1
matrix `Pi'[`j',`s']=exp(-`beta`j'')*`num`j''/`denom`s''
matrix `Th'[`j',`s']=`Pi'[`j',`s']*`realscore`s''
}
}
//local totalobs = _N
matrix outstat = J(`nbitems',2,0)
forvalues j = 1/`nbitems' {
local outfit_j = 0
local infit_j = 0
local outfit_num = 0
local outfit_denom = 0
local infit_num = 0
local infit_denom = 0
forvalues s = 1/`m' {
local n_score = `realscore`s''
disp "frac score = " `n_score'/`totalobs'
local obs_js = `Obs'[`j',`s']
local pred_js = `Th'[`j',`s']
local p_obs_js = `obs_js'/`n_score'
local p_exp_js = `pred_js'/`n_score'
local fitnum = `p_obs_js'*(1-`p_exp_js')^2 + (1-`p_obs_js')*`p_exp_js'^2
local fitdenom = `p_exp_js'*(1-`p_exp_js')
local outfit_j = `outfit_j'+ ((`n_score'/`totalobs')*(`fitnum'/`fitdenom'))
local infit_num = `infit_num'+ (`n_score'/`totalobs')*(`fitnum')
local infit_denom = `infit_denom'+ (`n_score'/`totalobs')*(`fitdenom')
}
local infit_j = `infit_j' + (`infit_num'/`infit_denom')
local outfit_j = `outfit_j'
matrix outstat[`j',1] = `outfit_j'
matrix outstat[`j',2] = `infit_j'
}
matrix rownames outstat = `varlist'
matrix colnames outstat = Outfit Infit
if "`excel'"!="" {
local rand = round(1000*runiform(),1)
if "`replace'"!="" {
xml_tab outstat, save(`excel') replace
}
else {
capture confirm new file `excel'
if _rc {
xml_tab outstat, save(outstat`rand')
di in green "`excel' already exists; results saved in outstat`rand'.xml"
}
else {
xml_tab outstat, save(`excel')
}
}
}
if "`latex'"!=""{
local rand = round(1000*runiform(),1)
if "`replace'"!="" {
outtable using `latex', mat(outstat) nobox replace
}
else {
capture confirm new file `latex'.tex
if _rc {
outtable using `latex'`rand', mat(outstat) nobox
di in green "`latex' already exists; results saved in outstat`rand'.tex"
}
else {
outtable using `latex'`rand', mat(outstat) nobox
}
}
}
return matrix outstat = outstat
use `fitstat', clear
end

View File

@ -0,0 +1,27 @@
{smcl}
{* *!version 1.0.0 16dec2011}{...}
{cmd: help fitstat_ers}
{hline}
{title:Title}
{phang}
{bf: fitstat_ers} {hline 2} computes outfit and infit statistics for the conditional maximum likelihood (cml)
Rasch model as outlined by Linacre and Wright (2004). See {browse "http://www.rasch.org/rmt/rmt82a.htm"}.
{title:Syntax}
{phang}
{cmd:fitstat_ers} varlist {ifin} {opt ,} [{opt excel(filename)} {opt latex(name)}]
{p_end}
{title:Description}
{pstd}
{cmd:fitstat_ers} estimates fit statistics for conditional likelihood models, proceeding as if Rasch
model assumptions are met. The current Stata module {cmd:raschtest} calculates infit and outfit statistics transformed
to be normal random variates; this module estimates the un-transformed infit and outfit statistics as outlined
by Linacre and Wright (1994).
{title:Options}
{phang}
{opt excel(filename)} and {opt latex(filename)} output the infit and outfit statistics to tables in their respective
formats. In order to use these options {cmd: xml_tab} and {cmd:outtable} should be installed, respectively.

View File

@ -0,0 +1,31 @@
program def fmtlist, rclass
*! NJC 1.0.0 22 August 2000
version 6.0
gettoken list 0 : 0, parse(",")
if "`list'" == "" | "`list'" == "," {
di in r "nothing in list"
exit 198
}
syntax , Format(str) [ Global(str) Noisily ]
if length("`global'") > 8 {
di in r "global name must be <=8 characters"
exit 198
}
tokenize `list'
while "`1'" != "" {
capture local 1 : di `format' `1'
if _rc {
di in r "inappropriate argument or format?"
exit _rc
}
local newlist "`newlist'`1' "
mac shift
}
if "`noisily'" != "" { di "`newlist'" }
if "`global'" != "" { global `global' "`newlist'" }
return local list `newlist'
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,103 @@
/*
*! funnel dialog version 1.0.0, 13 May 2003, T. J. Steichen, steichen@triad.rr.com
*! for funnel version 1.04, May 2001, M. J. Bradburn, mike.bradburn@cancer.org.uk
Metan-based Funnel Graph
------------------------
Syntax: funnel [varlist] [if exp] [in range] [, ysqrt sample overall(#)
graph_options ]
To install in User Statistics menu via Stata commands:
. window menu append item "stUserStatistics" "Metan-based Funnel Graph (&funnel)" "db funnel"
. window menu refresh
To permanently install, place the commands in your -profile.do- file.
*/
VERSION 8.0
INCLUDE _std_small
INCLUDE header
HELP hlp1, view("help metan")
RESET res1, label("Reset")
DIALOG main, label("funnel 1.04 - Metan-based Funnel Graph") tabtitle("Main")
BEGIN
CHECKBOX cb_effect 10 5 310 ., ///
label("Enter Variables (else use _ES and _seES") ///
onclickon(script vars_on) ///
onclickoff(script vars_off)
TEXT tx_effect 10 25 330 ., ///
label("Vars for Effect Sizes: theta, se(theta), in that order")
VARLIST vl_effect @ _ss @ ., ///
label("Effect Sizes: theta, se(theta)")
/* end vars */
CHECKBOX cb_ysqrt 10 70 270 ., ///
label("Y-axis is Square-root Scaled") ///
option("ysqrt")
CHECKBOX cb_sample 10 90 270 ., ///
label("Y-axis is Sample Size (not SE)") ///
onclickon(main.cb_invert.disable) ///
onclickoff(main.cb_invert.enable) ///
option("sample")
CHECKBOX cb_invert 10 110 270 ., ///
label("Precision (SE) not inverted") ///
option("noinvert")
CHECKBOX cb_overall 10 130 130 ., ///
label("Line at Effect Size:") ///
onclickon(main.ed_overall.enable) ///
onclickoff(main.ed_overall.disable)
EDIT ed_overall 145 130 50 ., ///
label("overall") ///
numonly ///
option("overall")
/* end switches and sets */
GROUPBOX gb_gopts7 10 155 330 _ht1h, ///
label("Allowed Graph7 Options:")
EDIT ed_gopts7 15 175 320 ., ///
label("Graph7 Options")
/* end Graph7 opts */
END
INCLUDE ifin
INCLUDE weights_a
SCRIPT vars_on
BEGIN
main.tx_effect.enable
main.vl_effect.enable
END
SCRIPT vars_off
BEGIN
main.tx_effect.disable
main.vl_effect.disable
END
PROGRAM command
BEGIN
put "funnel "
varlist [main.vl_effect]
INCLUDE _weights_pr
INCLUDE _ifin_pr
beginoptions
option main.cb_ysqrt
option main.cb_sample
option main.cb_invert
optionarg main.ed_overall
put main.ed_gopts7
endoptions
END