*! version 0.1.1 2013-12-06 | vanheuvelen long

* tpr: save data for given to merge later

program define tpr_sim_dta_tempsave

* timing

    capture timer list
    local itertime = r(t1)
    local iterhours = `itertime'/(60*60)
    local iterhours = string(`iterhours',"%6.1f")
    local iterminutes = `itertime'/(60)
    local iterminutes = string(`iterminutes',"%6.1f")
    char _dta[timer$samplesize]= `itertime'
    char _dta[minutes$samplesize]= `iterminutes'
    char _dta[hours$samplesize]= `iterhours'
    display
    display _new ///
        "n=$samplesize: " _col(10) "$nvalid successful iterations " ///
        "in `iterminutes' minutes." _new
    timer clear

* select cases and variables to retain

    qui drop in 1/L

* misc labels and information

    char _dta[mldifficult$samplesize] $difficult

* create variables with model results

    matrix colnames OLmat = $OLnms
    svmat OLmat, names(col)
    forvalues i = 1/$x_nvars {
        label var OLb`i' "ologit b`i'"
        label var OLb`i'se "ologit se(b`i')"
    }
    forvalues i = 1/$ncuts {
        label var OLcut`i' "ologit cut`i'"
        label var OLcut`i'se "ologit se(cut`i')"
    }
    label var OLstatus "ologit completion status"
    label var OLtried "ologit attempted?"
    label var OLaic "AIC value for Ologit model"
    label var OLbic "BIC value for Ologit model"
    label var OLconverged "ologit converged?"
    label var OLn "ologit e(N)"
    label var OLrc "ologit rc"

    matrix colnames GLmat = $GLnms
    svmat GLmat, names(col)
    forvalues icons = 1/$ncuts {
        local icat = `icons' - 1
        forvalues irhs = 1/$x_nvars {
            label var GLb`irhs'`icat' "gologit2 b`irhs'`icat'"
            label var GLb`irhs'`icat'se "gologit2 se(b`irhs'`icat')"
        }
        label var GLcut`icat' "gologit2 cut`icat'"
        label var GLcut`icat'se "gologit2 se(cut`icat')"
    }
    label var GLstatus "gologit2 completion status"
    label var GLtried "gologit2 attempted?"
    label var GLaic "AIC value for Gologit2 model"
    label var GLbic "BIC value for Gologit2 model"
    label var GLconverged "gologit2 converged?"
    label var GLn "gologit2 e(N)"
    label var GLrc "gologit2 rc"

* create variables with test results

    matrix colnames BRmat = $BRnms
    svmat BRmat, names(col)
    label var BRstatus "Brant olm completion status"
    label var BRtried "Brant olm attempted?"
    label var BRchi "Brant olm chi2"
    label var BRdf "Brant olm df"
    label var BRprob "Brant olm pvalue"
    label var BRrc "Brant olm rc"

    matrix colnames WAmat = $WAnms
    svmat WAmat, names(col)
    label var WAstatus "Wald golm completion status"
    label var WAtried "Wald golm attempted?"
    label var WAchi "Wald golm chi2"
    label var WAdf "Wald golm df"
    label var WAprob "Wald golm pvalue"
    label var WArc "Wald golm rc"

    foreach nm of varlist $x_names {
        local gnm `"WAtestvar_`nm'"'
        matrix colnames WA`nm'mat = $`gnm'
        svmat WA`nm'mat, names(col)
        local gnm `"BRtestvar_`nm'"'
        matrix colnames BR`nm'mat = $`gnm'
        svmat BR`nm'mat, names(col)
    }

    matrix colnames LRmat = $LRnms
    svmat LRmat, names(col)
    label var LRstatus "LR golm completion status"
    label var LRtried "LR golm attempted?"
    label var LRchi "LR golm chi2"
    label var LRdf "LR golm df"
    label var LRprob "LR golm pvalue"
    label var LRrc "LR golm rc"

    matrix colnames PRmat = $PRnms
    svmat PRmat, names(col)
    forvalues i = $y_catmin/$y_catmax {
        * raw differences of probabilities
        label var PRdif`i'c0 "PR cat`i' OL-GL 0 centile"
        label var PRdif`i'c2 "PR cat`i' OL-GL 2 centile"
        label var PRdif`i'c5 "PR cat`i' OL-GL 5 centile"
        label var PRdif`i'c25 "PR cat`i' OL-GL 25 centile"
        label var PRdif`i'c50 "PR cat`i' OL-GL 50 centile"
        label var PRdif`i'c75 "PR cat`i' OL-GL 75 centile"
        label var PRdif`i'c95 "PR cat`i' OL-GL 95 centile"
        label var PRdif`i'c98 "PR cat`i' OL-GL 98 centile"
        label var PRdif`i'c100 "PR cat`i' OL-GL 100 centile"
        * absolute differences
        label var PRabsdif`i'c0 "PR cat`i' abs(OL-GL) 0 centile"
        label var PRabsdif`i'c2 "PR cat`i' abs(OL-GL) 2 centile"
        label var PRabsdif`i'c5 "PR cat`i' abs(OL-GL) 5 centile"
        label var PRabsdif`i'c25 "PR cat`i' abs(OL-GL) 25 centile"
        label var PRabsdif`i'c50 "PR cat`i' abs(OL-GL) 50 centile"
        label var PRabsdif`i'c75 "PR cat`i' abs(OL-GL) 75 centile"
        label var PRabsdif`i'c95 "PR cat`i' abs(OL-GL) 95 centile"
        label var PRabsdif`i'c98 "PR cat`i' abs(OL-GL) 98 centile"
        label var PRabsdif`i'c100 "PR cat`i' abs(OL-GL) 100 centile"
/*
        * squared differences
        label var PRsqdif`i'c0    "PR cat`i' (OL-GL)^2 0 centile"
        label var PRsqdif`i'c2    "PR cat`i' (OL-GL)^2 2 centile"
        label var PRsqdif`i'c5    "PR cat`i' (OL-GL)^2 5 centile"
        label var PRsqdif`i'c25   "PR cat`i' (OL-GL)^2 25 centile"
        label var PRsqdif`i'c50   "PR cat`i' (OL-GL)^2 50 centile"
        label var PRsqdif`i'c75   "PR cat`i' (OL-GL)^2 75 centile"
        label var PRsqdif`i'c95   "PR cat`i' (OL-GL)^2 95 centile"
        label var PRsqdif`i'c98   "PR cat`i' (OL-GL)^2 98 centile"
        label var PRsqdif`i'c100  "PR cat`i' (OL-GL)^2 100 centile"
*/
    }

    matrix colnames OPmat = $OPnms
    svmat OPmat, names(col)
	label var SCchi  "Score test chi2"
    label var SCprob "Score test pvalue"
    label var SCdf   "Score Test degrees of freedom"
    label var WGchi  "Wolfe-Gould test chi2"
    label var WGprob "Wolfe-Gould test chi2"
    label var WGdf   "Wolfe-Gould degrees of freedom"

    label var AICdif "OLaic - GLaic, aic value difference"
    label var BICdif "OLbic - GLbic, bic value difference"

* value labels

    label def Lstatus 1 "1=no errors" ///
        .a "a=OL missing cat" .b "b=OL non convrg" .c "c=OL est error" ///
        .d "d=BR binary failed" .e "e=BR neg chi2" ///
        .f "f=GL rc error" .g "g=GL non convrg" ///
        .h "h=WA rc error" .i "i=LR rc error" ///
        .j "j=BR omnibus failed" .k "=BR rc 2000" ///
        .l "l=BRx neg chi2" .m "m=l BRx failed" .n "n=GL neg prob"

    label val BRstatus Lstatus
    label val GLstatus Lstatus
    label val LRstatus Lstatus
    label val OLstatus Lstatus
    label val WAstatus Lstatus

    label def Lconverge 0 "0Not" 1 "1Converged"
    label val GLconverged Lconverge
    label val OLconverged Lconverge

* value and var labels for individual level vars

    foreach nm of varlist $x_names {
        label val BR`nm'status Lstatus
        label val WA`nm'status Lstatus
        label var BR`nm'chi "Brant olm `nm' chi2"
        label var BR`nm'df "Brant olm `nm' df"
        label var BR`nm'prob "Brant olm `nm' pvalue"
        label var BR`nm'rc "Brant olm `nm' rc"
        label var BR`nm'status "Brant olm `nm' completion status"
        label var WA`nm'chi "Wald golm `nm' chi2"
        label var WA`nm'df "Wald golm `nm' df"
        label var WA`nm'prob "Wald golm `nm' pvalue"
        label var WA`nm'rc "Wald golm `nm' rc"
        label var WA`nm'status "Wald golm `nm' completion status"
    }

* save temp dataset

    label var samplesize "Sample size"
    label var simiter "Iteration number"

    keep $LRnms $WAnms $BRnms $OLnms $GLnms $WAtestallnms ///
        $BRtestallnms $PRnms $OPnms
    qui compress
    qui save temp-$simdta-$samplesize, replace

end
exit