Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Approximating values to 3 decimal places using matrices

    Hi everyone,

    I am using STATA version 18 and I am interested in presenting in a table the standard erros and p-values from several OLS regressions in the following way: the rows would show the name of the dependent variable of the several regressions whereas there are two columns showing the standard errors and p-values of one main explanatory variable by each regression run and, therefore, each row.

    However, I would need to approximate the values to 3 decimal places, which I have not been able to do as I use matrixes to capture the standard errors and p-values of each regression. Is there any way to accomplish this?

    I use esttab from Stata Journal authored by Ben Jann and appendmodels also authored by Ben Jann. For the sake of simplicity, I am using as an example the auto data and three regressions: 1) weight on price; 2) weight on mpg; and 3) weight on rep78. Please see the code I am using below with the final output given by esttab as the last line.

    Any feedback will be highly appreciated.

    Best regards,
    Daniel

    Code:
    sysuse auto, clear
    
    local x "price mpg rep78"
    
    *GETTING STD ERRORS*
    eststo clear
    
    foreach name of local x {
     
    eststo `name': qui: reg `name' weight, vce(robust)
    }
    
    esttab, wide noconstant se scalar(p) nostar
    matrix C = r(coefs)
    
    eststo clear
    local rnames2 : rownames C
    local rnames
    foreach name of local rnames2 {
        local rnames `rnames' `=strtoname("`name'")'
    }
    cap mat rownames C= "`rnames'"
    local models : coleq C
    local models : list uniq models
    local i 0
    foreach name of local rnames {
        local ++i
        local j 0
        capture matrix drop b
        capture matrix drop se
        foreach model of local models {
            local ++j
            matrix tmp = C[`i', 2*`j'-1]
            if tmp[1,1]<. {
                matrix colnames tmp = `model'
                matrix b = nullmat(b), tmp
                matrix tmp[1,1] = C[`i', 2*`j']
                matrix se = nullmat(se), tmp
            }
        }
        ereturn post se
        eststo `name'
    }
    
    esttab, wide mtitle noobs nostar compress nonumb
    est save res1, replace
    
    *GETTING P-VALUES*
    foreach name of local x {
     
    eststo `name': qui: reg `name' weight, vce(robust)
    
    local depvar "`e(depvar)'"
    rename `depvar' holding
    gen `depvar'= (2 * ttail(e(df_r), abs(_b[weight]/_se[weight])))
    eststo P_`name': mean `depvar'
    drop `depvar'
    rename holding `depvar'
    }
    
    *PROGRAM TO APPEND COEFFICIENTS -> NÂș OBSERVATIONS AND P-VALUES*
    capt prog drop appendonlyb
    *Modification of appendmodels ! version 1.0.0  14aug2007  Ben Jann
    program appendonlyb, eclass
        // using first equation of model
        version 8
        syntax namelist
        tempname b V tmp
        foreach name of local namelist {
            qui est restore `name'
            mat `tmp' = e(b)
            local eq1: coleq `tmp'
            gettoken eq1 : eq1
            mat `tmp' = `tmp'[1,"`eq1':"]
            local cons = colnumb(`tmp',"_cons")
            if `cons'<. & `cons'>1 {
                mat `tmp' = `tmp'[1,1..`cons'-1]
            }
            mat `b' = nullmat(`b') , `tmp'
            mat `tmp' = e(V)
            mat `tmp' = `tmp'["`eq1':","`eq1':"]
            if `cons'<. & `cons'>1 {
                mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
            }
            capt confirm matrix `V'
            if _rc {
                mat `V' = `tmp'
            }
            else {
                mat `V' = ///
                ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
                ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
            }
        }
        local names: colfullnames `b'
        mat coln `V' = `names'
        mat rown `V' = `names'
        eret post `b' 
        eret local cmd "whatever"
    end
    
    eststo P: appendonlyb P_price P_mpg P_rep78
    
    estimates use res1
    eststo res1
    
    esttab res1 P, mtitles("std error" "p-value") keep(`x') plain nogaps nonumb noobs nostar label

  • #2
    Code:
    esttab res1 P, cells(b(fmt(3))) mtitles("std error" "p-value") ...

    Comment


    • #3
      Thank you very much, Andrew! Oh, using cells option, right... amazingly, it had eluded me so far.

      It totally worked and I really appreciate your time!

      Comment

      Working...
      X