Announcement

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

  • Modifying Ben Jann's "appendmodels" program for different commands.

    Hello,

    I am using Ben Jann's appendmodels program which can be found here. The program works perfectly for many commands, however when using
    Code:
    pwmean `var', over(treated)
    the variable name is in the column header instead of the rows, and the row names become "1vs0.treated". I would like the variable names on the rows, and "1vs0.treated" on the column. How can I modify this program to let me do this? Thank you.

    PS: Here is the appendmodels program by Ben Jann.

    Code:
     
     capt prog drop appendmodels *! version 1.0.0  14aug2007  Ben Jann program appendmodels, 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' `V'     eret local cmd "whatever" end sysuse auto eststo b1: quietly regress price weight eststo b2: quietly regress price mpg eststo b3: quietly regress price foreign eststo bivar: appendmodels b1 b2 b3 eststo multi: quietly regress price weight mpg foreign esttab b1 b2 b3 bivar, mtitles esttab multi bivar, mtitles eststo clear

  • #2
    We ask for reproducible examples, otherwise you risk not having your question answered in a timely manner or at all (FAQ Advice #12). appendmodels modifies estimates from esttab (the Stata Journal). From the following, we can see that the issue arises from the names of coefficients of factor variables, which are not legal Stata names, i.e., they begin with numbers and contain periods.

    Code:
    webuse yield, clear
    qui pwmean yield, over(fertilizer)
    esttab

    Res.:

    Code:
    . esttab
    
    ----------------------------
                          (1)  
                        yield  
    ----------------------------
    1.fertilizer        41.36***
                      (36.79)  
    
    2.fertilizer        44.99***
                      (40.01)  
    
    3.fertilizer        41.85***
                      (37.23)  
    
    4.fertilizer        46.29***
                      (41.17)  
    
    5.fertilizer        40.12***
                      (35.69)  
    ----------------------------
    N                          
    ----------------------------
    t statistics in parentheses
    * p<0.05, ** p<0.01, *** p<0.001
    You can address this in a number of ways, one of which is to use the -strtoname()- function, as I do below.

    Code:
    webuse yield, clear
    eststo yield: pwmean yield, over(fertilizer) cimeans
    esttab yield
    matrix C = r(coefs)
    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
    eststo clear
    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 b
        quietly estadd matrix se
        eststo `name'
    }
    esttab, se mtitle noobs compress nonumb
    Res.:

    Code:
    . esttab, se mtitle noobs compress nonumb
    
    ---------------------------------------------------------------------------
               _1bn_fe~r    _2_fert~r    _3_fert~r    _4_fert~r    _5_fert~r  
    ---------------------------------------------------------------------------
    yield          41.36        44.99        41.85        46.29        40.12  
                 (36.79)      (40.01)      (37.23)      (41.17)      (35.69)  
    ---------------------------------------------------------------------------
    Standard errors in parentheses
    * p<0.05, ** p<0.01, *** p<0.001
    Last edited by Andrew Musau; 18 Jan 2021, 04:49.

    Comment


    • #3
      Dear Andrew,

      Thank you very much for your response, I now understand the source of the problem and I will give your solution a try! I apologize for not adequately providing a reproducible example, I will make sure to pay more attention in the future. Much appreciated.

      Comment

      Working...
      X