*! version 10.0.0  02Dec2015
program define myregress10, eclass sortpreserve
    version 14

    syntax varlist(numeric ts fv) [if] [in] [, vce(string) noCONStant ]
    marksample touse

    gettoken depvar indeps : varlist
    _fv_check_depvar `depvar'

    tempname zpz xpx xpy xpxi b V
    tempvar  xbhat res res2

    if `"`vce'"' != "" {
        my_vce_parse , vce(`vce')
        local vcetype     "robust"
        local clustervar  "`r(clustervar)'"
        if "`clustervar'" != "" {
            markout `touse' `clustervar'
            sort `clustervar'
        }
    }

    quietly matrix accum `zpz' = `varlist' if `touse' , `constant'
    local N                    = r(N)
    local p                    = colsof(`zpz')
    matrix `xpx'               = `zpz'[2..`p', 2..`p']
    matrix `xpy'               = `zpz'[2..`p', 1]
    matrix `xpxi'              = syminv(`xpx')
    matrix `b'                 = (`xpxi'*`xpy')'
    local k                    = `p' - diag0cnt(`xpxi') - 1
    quietly matrix score double `xbhat' = `b' if `touse'
    quietly generate double `res'       = (`depvar' - `xbhat') if `touse'
    quietly generate double `res2'      = (`res')^2 if `touse'

    if "`vcetype'" == "robust" {
        if "`clustervar'" == "" {
            tempname M
            quietly matrix accum `M' = `indeps'         ///
                [iweight=`res2'] if `touse' , `constant'
            local fac                = (`N'/(`N'-`k'))
            local df_r               = (`N'-`k')
        }
        else  {
            tempvar idvar
            tempname M
            quietly egen `idvar' = group(`clustervar') if `touse'
            quietly summarize `idvar' if `touse', meanonly
            local Nc   = r(max)
            local fac  = ((`N'-1)/(`N'-`k')*(`Nc'/(`Nc'-1)))
            local df_r = (`Nc'-1)
            matrix opaccum `M' = `indeps' if `touse'     ///
                , group(`clustervar') opvar(`res')
        }
        matrix `V' = (`fac')*`xpxi'*`M'*`xpxi'
        local vce                   "robust"
        local vcetype               "Robust"
    }
    else {                            // IID Case
        quietly summarize `res2' if `touse' , meanonly
        local sum           = r(sum)
        local s2            = `sum'/(`N'-`k')
        local df_r          = (`N'-`k')
        matrix `V'          = `s2'*`xpxi'
    }

    ereturn post `b' `V', esample(`touse') buildfvinfo
    ereturn scalar N       = `N'
    ereturn scalar rank    = `k'
    ereturn scalar df_r    = `df_r'
    ereturn local  vce     "`vce'"
    ereturn local  vcetype "`vcetype'"
    ereturn local clustvar "`clustvar'"
    ereturn local  cmd     "myregress10"
    ereturn display
end

program define my_vce_parse, rclass
    syntax  [, vce(string) ]

    local case : word count `vce'

    if `case' > 2 {
        my_vce_error , typed(`vce')
    }

    local 0 `", `vce'"'
    syntax  [, Robust CLuster * ]

    if `case' == 2 {
        if "`robust'" == "robust" | "`cluster'" == "" {
            my_vce_error , typed(`vce')
        }

        capture confirm numeric variable `options'
        if _rc {
            my_vce_error , typed(`vce')
        }

        local clustervar "`options'"
    }
    else {    // case = 1
        if "`robust'" == "" {
            my_vce_error , typed(`vce')
        }

    }

    return clear
    return local clustervar "`clustervar'"
end

program define my_vce_error
    syntax , typed(string)

    display `"{red}{bf:vce(`typed')} invalid"'
    error 498
end