*! version 0.5.0 2013-10-06

//  tpr - VanHeuvelen & Long (tvanheuv@indiana.edu)
//  save dataset with characteristics

program define tpr_pop_savedta

    version 13

    char _dta[author] $author
    char _dta[date] $date
    char _dta[program] $program
    char _dta[dataset] $dataset
    char _dta[summary] $summary
    char _dta[design] $design
    char _dta[seed] $seed
    char _dta[pop_nobs] $pop_nobs
    char _dta[error_dist] $error_dist

    local pop_dta tpr-$pop_dta-pop.dta
    local samplesizes $sample_smallest $samplesother
*    local nvars $x_nvars
    local x_nvars = wordcount("$x_names")
    quietly sum ycat
    local y_catmin = r(min)
    local y_catmax = r(max)

    qui ologit $y_name $x_names
    mat b = r(table)
    capture matrix drop best
    forvalues ib = 1/`x_nvars' {
        matrix a = b[1,`ib']
        matrix rowna a = "est_b`ib'"
        matrix best = nullmat(best) \ a
        matrix a = b[2,`ib']
        matrix rowna a = "est_bse`ib'"
        matrix best = nullmat(best) \ a
    }
    local bcutloc = `x_nvars'
    local catsNm1 = $y_catsN - 1
    forvalues icut = 1/`catsNm1' {
        local ++bcutloc
        matrix a = b[1,`bcutloc']
        matrix rowna a = "est_cut`icut'"
        matrix best = nullmat(best) \ a
    }
    char _dta[pop_b0] $pop_b0
    forvalues i = 1/`x_nvars' {
        local gnm "pop_b`i'"
        char _dta[`gnm'] $`gnm'
    }
    local nms : rownames best
    local i = 1
    foreach nm in `nms' {
        local b = best[`i',1]
        char _dta[`nm'] "`b'"
        local ++i
    }

    char _dta[trim_pct] $trim_pct
    char _dta[trim_sd] $trim_sd

    char _dta[y_name] $y_name
    char _dta[y_dist] $y_dist
    forvalues icat = 1/$y_catsN {
        local gnm "y_est`icat'prob"
        char _dta[y_cat`icat'estprob] $`gnm'
    }
    char _dta[y_catsN] $y_catsN
    char _dta[y_catmin] `y_catmin'
    char _dta[y_catmax] `y_catmax'

    char _dta[x_nvars] `x_nvars'
    char _dta[x_names] $x_names

    foreach nm in $x_names {
        char _dta[`nm'_desc] $`nm'_desc
        char _dta[`nm'_pct1] $`nm'_pct1
        char _dta[`nm'_wtx1] $`nm'_wtx1
        char _dta[`nm'_wtx2] $`nm'_wtx2
        char _dta[`nm'_wtnormal] $`nm'_wtnormal
        char _dta[`nm'_wtchi] $`nm'_wtchi
        char _dta[`nm'_wtskew] $`nm'_wtskew
    }

    char _dta[sample_planned_reps] $sample_planned_reps
    char _dta[sample_smallest] $sample_smallest
    char _dta[sample_sizes] $sample_sizes
    char _dta[sample_maxdrawn] $sample_maxdrawn
    char _dta[sample_100factor] $sample_100factor
    char _dta[sample_50factor] $sample_50factor
    char _dta[sample_nonconvergefactor] $sample_nonconvergefactor

//  save population data

    aorder
    order id ycat ystar x* e
    qui compress
    qui datasignature set, reset
    label data "$dataset.dta | $program.do $author $date"
    note _dta: $dataset | $program.do $author $date.
    saveold $dataset, replace
    di "$dataset was saved with these characteristics:"
    di
    char list

//  check data

    clear all
    use $dataset, clear
    datasignature confirm

    sum ystar ycat $x_names xb e
    corr ystar ycat $x_names xb e

    regress ystar $x_names
    listcoef

    ologit $y_name $x_names, nolog
    listcoef, std

    gologit2 $y_name $x_names, npl nolog

end
exit