Announcement

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

  • Help with Estout table showing 3 values vertically, in 3 rows

    Hi,

    I am trying to get my table to look like the following in a .tex file, using estout. This shows the coefficient in the first row, the standard error in the second row, and observations in the third row. I am very close, but the values for observations in the third row always comes up blank (also I would like the observation count in brackets)? I'm sure it is a minor tweak in the "cells" line of the code, but I can't seem to get it, if anyone has any great insight. Many thanks. Desired output and code below. (In the photo here I had to enter in the third row values manually).




    Code:
    forvalues v=1/5 {
    
    preserve
    keep if visit == `v'
    reg lhs1 rank1
    estimates store model`v'
    restore
    
    }
    
        #delimit ; 
            estout model1
                   model2
                   model3
                   model4
                   model5
                   using "hiv_ci_4.tex" ,
                   style(tex) 
                   cells(b(star fmt(3)) se(par fmt(3)) N(par fmt[%9.0gc]))
                   mlabels("(W1)" "(W2)" "(W3)" "(W4)" "(W5)")  
                   collabels(none) 
                   starl(* 0.1 ** 0.05 *** 0.01)   
                   keep(rank1)              
                   order(rank1) 
                   prehead( 
                       \begin{table}[htbp!]\caption{HIV Prevalence Concentration Index by Wave -- Treated\label{tttResultsrobust} }                     
                               \begin{small}
                       \begin{center}
                       \textbf{}
                       \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}} 
                       \hline \hline             
                   )
                   posthead(\hline) 
                   prefoot() 
                   postfoot(
                       \noalign{\smallskip} \hline \hline 
                       \end{tabular}
                       \end{center}
                       \footnotesize 
                       Notes: \( @starlegend \). Standard errors in parentheses. Observation count in brackets. ///
                            \end{small}
                   \end{table}
                   )
                   replace;
            #delimit cr

  • #2
    estout is from SSC (FAQ Advice #12). Your table is not visible. Can you reupload it.

    Comment


    • #3
      Can you see this?

      Click image for larger version

Name:	stata list_v2.png
Views:	1
Size:	10.6 KB
ID:	1616273


      Comment


      • #4
        Yes, thanks. As the number of observations is a statistic in estout, it is not as straightforward as you imagine to display it as an estimate. However, it is doable. The following uses estadd which should come compiled with estout. If not, then

        Code:
        ssc install estadd, replace
        Here is an example:

        Code:
        sysuse auto, clear
        regress mpg weight
        mat obs= e(b)
        forval i=1/`=colsof(obs)'{
            mat obs[1, `i']=e(N)
        }
        estadd matrix obs= obs
        eststo m1
        estout m1,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a))) drop(_cons) mlab("Model 1")
        You need to run the code highlighted in red each time you run a regression to store the matrix with the number of observations in e()

        Res.:

        Code:
         estout m1,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a))) drop(_cons) mlab("Model 1")
        
        ----------------------------
                          Model 1  
                         b/se/obs  
        ----------------------------
        weight             -0.006***
                          (0.001)  
                             [74]  
        ----------------------------

        Comment


        • #5
          Thank you! This is amazing! I'm still struggling to get it to work with the loops in my code, etc. But I'm sure I'll get it. Can I ask one more question if you happen to know it? Say for example I want to run another 5 models (displayed horizontally like in my picture), but for the next community. So we then have an output table that is five models across, two communities (or more) down. Do you have a suggestion how to amend my code to do that?
          Last edited by Christa Hansen; 25 Jun 2021, 09:34.

          Comment


          • #6
            Ben Jann's appendmodels program, attached below, can be used for the coefficients and standard errors. It needs to be modified to include the observations - I will see if I can get time to do this, probably tomorrow. So you estimate each set of models separately and then append the corresponding models. It's a good idea to name the models in such a way that the model numbers match across sets of models, to enable an easy append. Below, I estimate the first set of models for domestic cars in the auto dataset and name these m1, m2,..., m5. The second set is for foreign cars, and I name these k1, k2,..., k5. So my loop appends m1 to k1, m2 to k2, and so on. Make sure that you rename the relevant variable differently across models so that you can define the coefficient labels at the final step to whatever you wish. Run the code in a do-file. Otherwise, you can run the first set of models and use esttab to save them in the tex file. Then run the second set and use the -append- option. This won't give you 2 rows, but two tables, one on top of the other.

            Code:
            sysuse auto, clear
            eststo clear
            *FIRST SET OF 5 MODELS
            local j 1
            local model
            rename weight weight_1
            foreach var in mpg turn length gear disp{
                regress `var' weight if !foreign
                mat obs= e(b)
                    forval i=1/`=colsof(obs)'{
                    mat obs[1, `i']=e(N)
                }
                estadd matrix obs= obs
                eststo m`j'
                local model "`model' `" Model `j' "'"
                local ++j
            }
            
            *SECOND SET OF 5 MODELS
            rename weight weight_2
            local j 1
            foreach var in mpg turn length gear disp{
                regress `var' weight if foreign
                mat obs= e(b)
                    forval i=1/`=colsof(obs)'{
                    mat obs[1, `i']=e(N)
                }
                estadd matrix obs= obs
                eststo k`j'
                local ++j
            }
            
            *PROGRAM TO APPEND MODELS
            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
            
            *APPEND THE MODELS
            forval i=1/`=`j'-1'{
                eststo M`i': appendmodels m`i' k`i'
            }
            
            esttab M*,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a)))  mlab(`model')  ///
            collabels(none) coeflab(weight_1 "Domestic"  weight_2 "Foreign") nonumbers noobs
            Res.:

            Code:
            . esttab M*,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a)))  mlab(`model')  collabels(none) coeflab(weight_1 "Domestic"  wei
            > ght_2 "Foreign") nonumbers noobs
            
            --------------------------------------------------------------------------------------------
                             Model 1         Model 2         Model 3         Model 4         Model 5    
            --------------------------------------------------------------------------------------------
            Domestic           -0.006***        0.005***        0.027***       -0.000***        0.103***
                              (0.000)         (0.000)         (0.002)         (0.000)         (0.009)  
                                                                                                        
            Foreign            -0.010***        0.002***        0.029***       -0.000*          0.055***
                              (0.002)         (0.001)         (0.003)         (0.000)         (0.004)  
                                                                                                        
            --------------------------------------------------------------------------------------------

            Comment


            • #7
              Oh wow, thank you! Yes tomorrow is certainly fine - I appreciate all of the help!

              Comment


              • #8
                It turns out that the modification is simple. I call the modified program "appendmodelsobs".

                Code:
                sysuse auto, clear
                eststo clear
                *FIRST SET OF 5 MODELS
                local j 1
                local model
                rename weight weight_1
                foreach var in mpg turn length gear disp{
                    regress `var' weight if !foreign
                    mat obs= e(b)
                        forval i=1/`=colsof(obs)'{
                        mat obs[1, `i']=e(N)
                    }
                    estadd matrix obs= obs
                    eststo m`j'
                    local model "`model' `" Model `j' "'"
                    local ++j
                }
                
                *SECOND SET OF 5 MODELS
                rename weight weight_2
                local j 1
                foreach var in mpg turn length gear disp{
                    regress `var' weight if foreign
                    mat obs= e(b)
                        forval i=1/`=colsof(obs)'{
                        mat obs[1, `i']=e(N)
                    }
                    estadd matrix obs= obs
                    eststo k`j'
                    local ++j
                }
                
                
                *PROGRAM TO APPEND MODELS
                capt prog drop appendmodelsobs
                *! modification of appendmodels version 1.0.0  14aug2007  Ben Jann
                program appendmodelsobs, eclass
                    // using first equation of model
                    version 8
                    syntax namelist
                    tempname b obs 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(obs)
                        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 `obs' = nullmat(`obs') , `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'
                    mat obs= `obs'
                    quietly estadd matrix obs
                    eret local cmd "whatever"
                end
                
                *APPEND THE MODELS
                forval i=1/`=`j'-1'{
                    eststo M`i': appendmodelsobs m`i' k`i'
                }
                
                esttab M*,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a)))  mlab(`model')  ///
                collabels(none) coeflab(weight_1 "Domestic"  weight_2 "Foreign") nonumbers noobs
                Res.:

                Code:
                . esttab M*,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a)))  mlab(`model')  ///
                > collabels(none) coeflab(weight_1 "Domestic"  weight_2 "Foreign") nonumbers noobs
                
                --------------------------------------------------------------------------------------------
                                 Model 1         Model 2         Model 3         Model 4         Model 5    
                --------------------------------------------------------------------------------------------
                Domestic           -0.006***        0.005***        0.027***       -0.000***        0.103***
                                  (0.000)         (0.000)         (0.002)         (0.000)         (0.009)   
                                     [52]            [52]            [52]            [52]            [52]   
                Foreign            -0.010***        0.002***        0.029***       -0.000*          0.055***
                                  (0.002)         (0.001)         (0.003)         (0.000)         (0.004)   
                                     [22]            [22]            [22]            [22]            [22]   
                --------------------------------------------------------------------------------------------

                Comment


                • #9
                  I am working to amend this to my data now - thank you so much, again!

                  Comment


                  • #10
                    This has been the most helpful advice ever. I can't thank you enough. For some reason I am having problems amending the code so that I can stack more than two models. It seems like this should be easy, but do you have a suggestion of where to amend the code? Many thanks.

                    Comment


                    • #11
                      Hi Christa, it's just a matter of changing the model prefix, making sure that there is a correspondence in the appended models as explained in #6. Now if we assume that we are running regressions by repair record which has 4+ levels instead of foreign which is binary, we can add the following (highlighted):

                      Code:
                      sysuse auto, clear
                      eststo clear
                      *FIRST SET OF 5 MODELS
                      local j 1
                      local model
                      rename weight weight_1
                      foreach var in mpg turn length gear disp{
                          regress `var' weight if rep78==1
                          mat obs= e(b)
                              forval i=1/`=colsof(obs)'{
                              mat obs[1, `i']=e(N)
                          }
                          estadd matrix obs= obs
                          eststo m`j'
                          local model "`model' `" Model `j' "'"
                          local ++j
                      }
                      
                      *SECOND SET OF 5 MODELS
                      rename weight weight_2
                      local j 1
                      foreach var in mpg turn length gear disp{
                          regress `var' weight if rep78==2
                          mat obs= e(b)
                              forval i=1/`=colsof(obs)'{
                              mat obs[1, `i']=e(N)
                          }
                          estadd matrix obs= obs
                          eststo k`j'
                          local ++j
                      }
                      
                      *THIRD SET OF 5 MODELS
                      rename weight weight_3
                      local j 1
                      foreach var in mpg turn length gear disp{
                          regress `var' weight if rep78==3
                          mat obs= e(b)
                              forval i=1/`=colsof(obs)'{
                              mat obs[1, `i']=e(N)
                          }
                          estadd matrix obs= obs
                          eststo a`j'
                          local ++j
                      }
                      
                      *FOURTH SET OF 5 MODELS
                      rename weight weight_4
                      local j 1
                      foreach var in mpg turn length gear disp{
                          regress `var' weight if rep78==4
                          mat obs= e(b)
                              forval i=1/`=colsof(obs)'{
                              mat obs[1, `i']=e(N)
                          }
                          estadd matrix obs= obs
                          eststo b`j'
                          local ++j
                      }
                      
                      *PROGRAM TO APPEND MODELS
                      capt prog drop appendmodelsobs
                      *! modification of appendmodels version 1.0.0  14aug2007  Ben Jann
                      program appendmodelsobs, eclass
                          // using first equation of model
                          version 8
                          syntax namelist
                          tempname b obs 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(obs)
                              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 `obs' = nullmat(`obs') , `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'
                          mat obs= `obs'
                          quietly estadd matrix obs
                          eret local cmd "whatever"
                      end
                      
                      
                      *APPEND THE MODELS
                      forval i=1/`=`j'-1'{
                          eststo M`i': appendmodelsobs m`i' k`i' a`i' b`i'
                      }
                      
                      esttab M*,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a)))  mlab(`model')  collabels(none) ///
                      coeflab(weight_1 "Poor"  weight_2 "OK" weight_3 "Good"  weight_4 "Excellent") nonum noobs
                      It is possible to write one loop for all the models, but I show this in bits for easy comprehension. If you are having a problem adapting the code, post your individual regressions within CODE delimiters.

                      Res.:

                      Code:
                      . esttab M*,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a)))  mlab(`model')  collabels(none) ///
                      > coeflab(weight_1 "Poor"  weight_2 "OK" weight_3 "Good"  weight_4 "Excellent") nonum noobs
                      
                      --------------------------------------------------------------------------------------------
                                       Model 1         Model 2         Model 3         Model 4         Model 5    
                      --------------------------------------------------------------------------------------------
                      Poor               -0.008           0.003           0.024           0.000           0.108   
                                            (.)             (.)             (.)             (.)             (.)   
                                            [2]             [2]             [2]             [2]             [2]   
                      OK                 -0.008***        0.004**         0.028***       -0.000           0.152***
                                        (0.001)         (0.001)         (0.005)         (0.000)         (0.035)   
                                            [8]             [8]             [8]             [8]             [8]   
                      Good               -0.004***        0.004***        0.025***       -0.000***        0.110***
                                        (0.001)         (0.001)         (0.002)         (0.000)         (0.010)   
                                           [30]            [30]            [30]            [30]            [30]   
                      Excellent          -0.005***        0.005***        0.031***       -0.000***        0.102***
                                        (0.001)         (0.000)         (0.001)         (0.000)         (0.007)   
                                           [18]            [18]            [18]            [18]            [18]   
                      --------------------------------------------------------------------------------------------
                      Last edited by Andrew Musau; 14 Aug 2021, 12:51.

                      Comment


                      • #12
                        Andrew, I cannot tell you how much I appreciate your time and responses. I am trying to do a semi-complicated (not really, but in this context) double loop. (I have 21 vertical models, each with 5 horizontal models - so was trying to avoid doing a manual m`i', k`i', a`i', b`i' situation). I will look over your code this afternoon and attempt to amend mine. If I cannot amend it properly by tomorrow morning, I will re-post my code as far as I have made it. I don't want to waste your time until I have given it a proper go. Thank you!!!!

                        Comment


                        • #13
                          (I have 21 vertical models, each with 5 horizontal models - so was trying to avoid doing a manual m`i', k`i', a`i', b`i' situation)
                          You can avoid specifying the models manually by storing the prefixes in a macro. Here is one way to do it (added lines highlighted).

                          Code:
                          sysuse auto, clear
                          eststo clear
                          local prefixes
                          *FIRST SET OF 5 MODELS
                          local j 1
                          local model
                          rename weight weight_1
                          foreach var in mpg turn length gear disp{
                              regress `var' weight if rep78==1
                              mat obs= e(b)
                                  forval i=1/`=colsof(obs)'{
                                  mat obs[1, `i']=e(N)
                              }
                              estadd matrix obs= obs
                              eststo m`j'
                              local prefixes `prefixes' m
                              local model "`model' `" Model `j' "'"
                              local ++j
                          }
                          
                          *SECOND SET OF 5 MODELS
                          rename weight weight_2
                          local j 1
                          foreach var in mpg turn length gear disp{
                              regress `var' weight if rep78==2
                              mat obs= e(b)
                                  forval i=1/`=colsof(obs)'{
                                  mat obs[1, `i']=e(N)
                              }
                              estadd matrix obs= obs
                              eststo k`j'
                              local prefixes `prefixes' k
                              local ++j
                          }
                          
                          *THIRD SET OF 5 MODELS
                          rename weight weight_3
                          local j 1
                          foreach var in mpg turn length gear disp{
                              regress `var' weight if rep78==3
                              mat obs= e(b)
                                  forval i=1/`=colsof(obs)'{
                                  mat obs[1, `i']=e(N)
                              }
                              estadd matrix obs= obs
                              eststo a`j'
                              local prefixes `prefixes' a
                              local ++j
                          }
                          
                          *FOURTH SET OF 5 MODELS
                          rename weight weight_4
                          local j 1
                          foreach var in mpg turn length gear disp{
                              regress `var' weight if rep78==4
                              mat obs= e(b)
                                  forval i=1/`=colsof(obs)'{
                                  mat obs[1, `i']=e(N)
                              }
                              estadd matrix obs= obs
                              eststo b`j'
                              local prefixes `prefixes' b
                              local ++j
                          }
                          
                          *PROGRAM TO APPEND MODELS
                          capt prog drop appendmodelsobs
                          *! modification of appendmodels version 1.0.0  14aug2007  Ben Jann
                          program appendmodelsobs, eclass
                              // using first equation of model
                              version 8
                              syntax namelist
                              tempname b obs 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(obs)
                                  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 `obs' = nullmat(`obs') , `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'
                              mat obs= `obs'
                              quietly estadd matrix obs
                              eret local cmd "whatever"
                          end
                          
                          local prefixes: list uniq prefixes
                          *APPEND THE MODELS
                          forval i=1/`=`j'-1'{
                              local models: subinstr local prefixes " " "`i' ", all
                              eststo M`i': appendmodelsobs `models'`i'
                          }
                          
                          esttab M*,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a)))  mlab(`model')  collabels(none) ///
                          coeflab(weight_1 "Poor"  weight_2 "OK" weight_3 "Good"  weight_4 "Excellent") nonum noobs
                          Last edited by Andrew Musau; 16 Aug 2021, 07:16.

                          Comment


                          • #14
                            I'm getting frustrated so will post. I know I am close. The other problem I am having is the long footnote distorts the column widths in LaTeX. Here is my code right now. There are not a lot of variables. The `v' variable is the loop that is the horizontal five model loop. the `d' is what represents the individual models that will stack vertically. (So each `d' has 5 horizontal models, if that makes sense). But I keep hitting an error as I try to append, plus the footnote issue. Many thanks to anyone willing to take a stab!

                            Code:
                            eststo clear
                            
                            *local z " 5 6 8 9 10 11"
                            local j 1
                            
                            *local z "2 5 6 8 9 10 11"
                            *foreach d of local z {
                            forvalues d=1/21 {
                            forvalues v=1/5 {
                            
                            preserve
                            keep if visit ==  `v'
                            keep if demcom == `d'
                            
                                capture noisily reg lhs1 rank`d'
                                mat obs= e(b)
                                    forval i=1/`=colsof(obs)'{
                                    mat obs[1, `i']=e(N)
                                }
                                estadd matrix obs= obs
                                eststo k`d'`j'`v'
                                local ++j
                                
                            restore
                            
                            }
                            }
                            
                            
                            *PROGRAM TO APPEND MODELS
                            capt prog drop appendmodelsobs
                            *! modification of appendmodels version 1.0.0  14aug2007  Ben Jann
                            program appendmodelsobs, eclass
                                // using first equation of model
                                version 8
                                syntax namelist
                                tempname b obs 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(obs)
                                    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 `obs' = nullmat(`obs') , `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'
                                mat obs= `obs'
                                quietly estadd matrix obs
                                eret local cmd "whatever"
                            end
                            
                            *APPEND THE MODELS
                            forvalues v=1/5 {
                            forval i=1/`=`j'-1'{
                            *local z "2 5 6 8 9 10 11"
                            *foreach d of local z {
                            forvalues d=1/21 {
                                eststo M`d'`i'`v' : appendmodelsobs k`d'`i'`v'
                            }
                            }
                            }
                            
                            esttab M* using D:\Dropbox\2021\health_inequities\hiv_ci_treated.tex ,  cells(b(star fmt(3)) se(par fmt(3)) obs(par([ ]) fmt(a)))  mlabels("(W1)" "(W2)" "(W3)" "(W4)" "(W2-4)")  title("HIV Prevalence Concentration Index by Wave -- Treated (Zambia)")   ///
                            note("Note: *p$<$0.1, **p$<$0.05, ***p$<$0.01. Standard errors in parentheses. Observation count in brackets. Triplet 1 is Communities 1 \&2, Triplet  2 is Communities 5 \&6, and aTriplet 3 is Communities 8 \&9.") ///
                            collabels(none) coeflab(rank1 "Community 1" rank2 "Community 2" rank5 "Community 5" rank5 "Community 5" rank6 "Community 6" rank8 "Community 8" rank9 "Community 9" rank10 "Community 10" rank11 "Community 11") nonumbers noobs replace

                            Comment


                            • #15
                              Do you have variables rank1, rank2, ..., rank21 in your dataset? And how does "demcom" relate to rank as you are doing:

                              keep if demcom == `d'
                              capture noisily reg lhs1 rank`d'

                              Also, do some regressions fail as you are using capture? The reason I ask is because the code maps coefficients across models, so if some do not exist, we need a different strategy.

                              Comment

                              Working...
                              X