Announcement

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

  • Using local macro to create 'blocks' of variables to be used in multivariable regression analyses

    Dear Statalisters,
    There have been several parallel threads on using local macros to create lists of variables when running repetitive regressions, for instance changing the outcome variable but keeping the same model variables (or vice-versa). I found these useful in learning about the foreach command.

    I would like to be able to use local to create a block of variables and then loop over these blocks so that the first model would be a minimally adjusted (containing two or three variables) and then a fully adjusted model containing all the variables I wish to control for.

    At the moment I have developed the following command strategy:

    local predictors "x1 x2 x3 x4 x5 x6 x7"
    local outcome "y1 y2"
    foreach x of local predictors {
    local regressors `regressors' `x'
    foreach y of local outcome {
    local model `y' `regressors' `x'
    reg `y' `regressors'
    }
    }

    What this does is run consecutive regressions for outcome y1 first adding in x1 to the model, then y2 adding in x1 to the model. In the next round, y1 is the outcome and then x1 and x2 are included in the model, then y2 as the outcome with x1 and x2 in the model and so forth.

    Order of operations:
    reg y1 x1
    reg y2 x1
    reg y1 x1 x2
    reg y2 x1 x2
    reg y1 x1 x2 x3
    reg y2 x1 x2 x3 .....

    My question is - can a local be defined such that I can save variables x1 x2 x3 as the 'minimally' adjusted model [i.e as block1] and then in the next round of the loop x4 x5 x6 x7 are all added in at the same time for the 'fully' adjusted model [i.e. block1 + block2].

    In other words, I would like the order of operations to be:
    reg y1 x1 x2 x3
    reg y2 x1 x2 x3
    reg y1 x1 x2 x3 x4 x5 x6 x7
    reg y2 x1 x2 x3 x4 x5 x6 x7

    I am using Stata v14.2

    Many thanks,
    Alexander
    Many thanks,
    Alexander
    (Stata v14.2 IC for Mac)

  • #2
    Code:
    local block1 x1 x2 x3
    local block2 x4 x5 x6 x7
    local outcome "y1 y2" 
    forvalues i = 1/2 { 
        local regressors `regressors' `block`i'' 
        foreach y of local outcome { 
            local model `y' `regressors' `x' 
            reg `y' `regressors' 
        } 
    }
    In the future, when posting Stata code or results, please use code delimiters. They make the code align better, and preserve indentation, so it is much easier to read. If you are not familiar with code delimiters, please read Forum FAQ #12 for instructions and explanation.

    Comment


    • #3
      EDITED TO ADD: This crossed with Clyde's post, who of course already incorporates it into Alexander's loop.

      Sure - I do this all the time. I name the control variables (that I include in all my regressions) `rhsvars' . Then I can add the main (hypothesized) variables as needed. In fact, having the control variables in a local macro is really nice, because if I decide to change control variables, I just change them in the local macro, and it automatically carries through to each of my regressions. The only downside is that you can't run individual regression models without declaring the local macro again.

      Code:
      local block1 "x1 x2 x3"
      local block2 "x4 x5 x6 x7"
      * If you did local block2 "`block1' x4 x5 x6 x7" , block2 would then contain "x1 x2 x3 x4 x5 x6 x7"
      
      
      reg y1 `block1'
      reg y2 `block1'
      reg y1 `block1' `block2'
      reg y2 `block1' `block2'
      * Or you could use them in your foreach loop to loop over y1 and y2
      Last edited by David Benson; 04 Dec 2018, 22:56.

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Code:
        local block1 x1 x2 x3
        local block2 x4 x5 x6 x7
        local outcome "y1 y2"
        forvalues i = 1/2 {
        local regressors `regressors' `block`i''
        foreach y of local outcome {
        local model `y' `regressors' `x'
        reg `y' `regressors'
        }
        }
        In the future, when posting Stata code or results, please use code delimiters. They make the code align better, and preserve indentation, so it is much easier to read. If you are not familiar with code delimiters, please read Forum FAQ #12 for instructions and explanation.
        Dear Clyde,

        Your solution worked perfectly! I am now having my own fun manipulating elements of the code and learning more about the forvalues command. Thank you again and thank you also for the kind reminder FAQ#12. In my concentration to make sure I asked an explicit question, I forgot to go back and format the code.

        Many thanks, Alexander
        Many thanks,
        Alexander
        (Stata v14.2 IC for Mac)

        Comment


        • #5
          Originally posted by David Benson View Post
          EDITED TO ADD: This crossed with Clyde's post, who of course already incorporates it into Alexander's loop.

          Sure - I do this all the time. I name the control variables (that I include in all my regressions) `rhsvars' . Then I can add the main (hypothesized) variables as needed. In fact, having the control variables in a local macro is really nice, because if I decide to change control variables, I just change them in the local macro, and it automatically carries through to each of my regressions. The only downside is that you can't run individual regression models without declaring the local macro again.

          Code:
          local block1 "x1 x2 x3"
          local block2 "x4 x5 x6 x7"
          * If you did local block2 "`block1' x4 x5 x6 x7" , block2 would then contain "x1 x2 x3 x4 x5 x6 x7"
          
          
          reg y1 `block1'
          reg y2 `block1'
          reg y1 `block1' `block2'
          reg y2 `block1' `block2'
          * Or you could use them in your foreach loop to loop over y1 and y2
          Dear David,

          Thank you for your input too, I see now that I can include a local macro within another local macro as you have done in the first part of your response.

          Cheers, Alexander
          Many thanks,
          Alexander
          (Stata v14.2 IC for Mac)

          Comment

          Working...
          X