Announcement

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

  • regressions on subsets with local macros and loop

    Dear Statalist,

    I am struggling to make the code below shorter: I would like to use a loop to include local macros below within the if condition.
    Furthermore, I would like to include these macros in both the file name when saving regression results and the title of the table showing the regression outcomes.
    Specifically, I aim to run regressions on subsets defined by combining the local macro `abovems' with the local macros `onlys', `onlym', and `onlyeng', respectively. Similarly, I will run the same regressions on subsets defined by combining the local macro `belowms' with the three specified local macros.

    How can I automate all these tasks?

    Thank you for your help!

    Chwen Chwen


    Code

    local abovems "above_medsize==1" // above median size
    local belowms "above_medsize==0" // below median size
    local onlys "onlys==1"
    local onlym "onlym==1"
    local onlye "onlyeng==1"

    est clear
    local m=1
    foreach var in dep1 dep2 dep3 dep4 dep5 dep6 dep7 {
    foreach x in ind1 ind2 ind3 {
    eststo model_`m': regr `var' `x' `ctrlist' if (`onlys' & `belowms'), robust
    local m `++m'
    }

    esttab model_* using "$path\Mdata-`belowms'-`var'-`onlys'.txt", ///
    replace noconstant se ar2 star(* .10 ** .05 *** .01) b(%10.3f) ///
    title("Regr analysis `var' and `onlys' education `belowms'")

    est clear

    }

  • #2
    If I understand correctly what you want:
    Code:
    local abovems "above_medsize==1" // above median size
    local belowms "above_medsize==0" // below median size
    local onlys "onlys==1"
    local onlym "onlym==1"
    local onlye "onlyeng==1"
    
    foreach ms in abovems belowms {
        foreach only in onlys onlym onlye {    
            est clear
            local m=1
            foreach var in dep1 dep2 dep3 dep4 dep5 dep6 dep7 {
                foreach x in ind1 ind2 ind3 {
                    eststo model_`m': regr `var' `x' `ctrlist' if (``only'' & ``ms''), robust
                    local m `++m'
                }
    
                esttab model_* using "$path\Mdata-``ms''-`var'-``only''.txt", ///
                replace noconstant se ar2 star(* .10 ** .05 *** .01) b(%10.3f) ///
                title("Regr analysis `var' and ``only'' education ``ms''")
    
                est clear
    
            }
        }
    }
    Added: As no example data on which your original code could be run was provided, this code is untested. Its correctness relies on my correctly simulating in my imagination what Stata will do, a fallible process. If the code I have provided does not run correctly, when posting back be sure to post example data that reproduces what ever problem(s) you encounter. And be sure to use the -dataex- command to do that. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
    Last edited by Clyde Schechter; 02 Feb 2024, 19:47.

    Comment


    • #3
      This should be similar, if not quite identical.


      Code:
      foreach ms in 0 1 {
          foreach only in onlys onlym onlyeng {    
      
              est clear
              local m=1
              foreach var in dep1 dep2 dep3 dep4 dep5 dep6 dep7 {
                  foreach x in ind1 ind2 ind3 {
                      eststo model_`m': regr `var' `x' `ctrlist' if (`only' == 1 & above_medsize == `ms'), robust
                      local ++m 
                  }
      
              local which = cond(`ms' == 0, "below", "above")     
                  esttab model_* using "$path\Mdata-`which'-`var'-`only'.txt", ///
                  replace noconstant se ar2 star(* .10 ** .05 *** .01) b(%10.3f) ///
                  title("Regr analysis `var' and `only' education `which'")
      
                  est clear
      
              }
          }
      }

      Comment


      • #4
        Hi Clyde, hi Nick,
        Thank you so much for your help! This is exactly what I was looking for.

        Comment


        • #5
          Dear Statalist,

          I am facing two problems when running the code above. I regret that, because of data confidentiality, I cannot provide any example data.
          I will do my best to be as clear as possible.

          Below is the code that I revised as to the names of local macros.

          I would like to ask your help about the following:
          1) I get this error message when I run the code:

          tagempyer not found
          r(111);
          r(111);

          which I find weird because of the following result:

          . count if `tagempyer'
          206


          2) how can I include the local tagboard in the same loop with tagempyer and tagempyee for the regressions?

          Thank you very much in advance.

          Chwen Chwen

          *********************************

          Code

          local abovems "above_medsize_ta == 1"
          local belowms "above_medsize_ta == 0"
          local tagboard "tag_finexptr == 1 & brrnbody == 1" // tag_finexptr is a variable taking value of 1 for the observation of interest, brrnbody is a categorical variable
          local tagempyer "tag_finexptr_empyer == 1" // tag_finexptr_empyer is a variable taking value of 1 for the observation of interest
          local tagempyee "tag_finexptr_empyee == 1" // as above


          foreach ms in 0 1 {
          foreach tag in /*tagboard*/ tagempyer tagempyee {

          est clear
          local m=1
          foreach var in dep1 dep2 dep3 dep4 dep5 dep6 dep7 {
          foreach x in ind1 ind2 ind3 {
          eststo model_`m': regr `var' `x' `ctrlist' if (`tag' == 1 & above_medsize_ta == `ms'), robust
          local m `++m'
          }

          local which = cond(`ms' == 0, "below", "above")
          esttab model_* using "$path\Mdata20-`which'-`var'-`tag'206r.txt", ///
          replace noconstant se ar2 star(* .10 ** .05 *** .01) b(%10.3f) ///
          title("Regr analysis `var' and `tag' fin educ `which'")

          est clear

          }

          }
          }


          Comment


          • #6
            Code:
            foreach ms in 0 1 {
                foreach tag in /*tagboard*/ tagempyer tagempyee {
            
                    est clear
                    local m=1
                    foreach var in dep1 dep2 dep3 dep4 dep5 dep6 dep7 {
                        foreach x in ind1 ind2 ind3 {
                            eststo model_`m': regr `var' `x' `ctrlist' if (``tag''  & above_medsize_ta == `ms'), robust
                            local m `++m'
                        }
            
                        local which = cond(`ms' == 0, "below", "above")
                        esttab model_* using "$path\Mdata20-`which'-`var'-`tag'206r.txt", ///
                        replace noconstant se ar2 star(* .10 ** .05 *** .01) b(%10.3f) ///
                        title("Regr analysis `var' and `tag' fin educ `which'")
            
                        est clear
            
                    }
            
                }
            }
            Explanation: in your -foreach tag in...- loop, your iterator is local macro tag. It iterates over a list that contains tagempyer tagempyee. So `tag', when you refer to it in the loop will be either tagempyer or tagempyee. In the second and third occurrences of `tag', that's fine because you are just specifying some title information, and it doesn't matter what `tag' is. But in the first occurrence of `tag', Stata expects a logical expression, and tagempyer is not a logical expression. Rather, it is the name of a local macro that contains a logical expression. Therefore, you need another set of macro quotes (` ') around that to reach the intended logical expression tag_finexptr_empyer == 1 or tag_finexptr_empyee == 1.

            Comment


            • #7
              Thank you very much for your explanation, Clyde.

              Comment

              Working...
              X