Announcement

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

  • Loop of regressions over local macros.

    Hello everyone,

    I'm trying to run a number of regressions over local macros. I won't display my data as it's not complex but just too big and I'd rather describe it.
    I have multiple variables named x and y that are numbered 1-10 and 1-5 as identifiers. With these 50 pairs of variables, I want to run 50 regressions. I guess the answer lies somewhere between macros and loops, but I can't get it right.
    Here's an excerpt of my data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(x_1_1 y_1_1 x_2_1 y_2_1 x_1_2 y_1_2 x_2_2 y_2_2)
    
    
    end
    I tried this, but it doesn't regress the individual pairs:
    Code:
    local indepvar y*
    foreach depvar of varlist x* {
        eststo: quietly reg `depvar' `indepvar', noheader
    }
    To make it even clearer, this would be the long version for what I need:
    Code:
    eststo: quietly reg x_1_1 y_1_1
    eststo: quietly regx_2_1 y_2_1
    ...
    eststo: quietly reg x_10_1 y_10_1
    ...
    eststo: quietly reg x_1_2 y_1_2
    eststo: quietly regx_2_2 y_2_2
    ...
    eststo: quietly reg x_10_2 y_10_2
    ...
    eststo: quietly reg x_1_5 y_1_5
    eststo: quietly regx_2_5 y_2_5
    ...
    eststo: quietly reg x_10_5 y_10_5
    Any ideas?

    Thank you
    Moritz

  • #2
    In the code you wrote, local macro indepvar contained "y*", which, in the regress command, expands to -regress x y*-, which in turn expands to regress x y_1_1 y_1_2 y_1_3 ... y_10_5, a multivarible regression, not to a series of bivariate regressions. You need nested loops:
    Code:
    foreach dv of varlist x* {
        foreach iv of varlist y* {
            regress `dv' `iv'
        }
    }

    Comment


    • #3
      Thank you Clyde, this does give me the bivariate regressions but unfortunately it also regresses each individual variable, e.g. x_1_1 with y_2_1, x_1_1 with y_3_1 and so on.
      Is there a way to exclusively regress the paired variables?

      Plus if I'd want to extract the coefficients for all 50 regressions, I'd probably use a macro, right?
      Like this?

      Code:
      foreach dv of varlist log_ccdf_* {
          foreach iv of varlist log_nw_* {
              reg `dv' `iv'
              local coef _b[`iv']
          }
      }
      display `coef1'

      Comment


      • #4
        I read this as your wanting parallel loops, perhaps

        Code:
        foreach x of var x* { 
              local y : subinstr local x "x" "y", all 
              regress `x' `y' 
              di _b[`y'] 
        }
        Conventionally x is notation for an independent (predictor, explanatory) variable and y for a dependent (outcome, response) variable and with this notation you would regress y on x and thus mention the y variable first.

        However, I am confused about your variable names, as in #3 you're using quite different names.


        Much more on parallel loops at https://journals.sagepub.com/doi/pdf...6867X211063415

        Comment


        • #5
          Thank you, Nick, you're right, in #1 I used x and y a bit unintuitively, sorry for that. For this thread I will keep the notations as x for dependent and y for independent var, to not make it even more confusion.
          Unfortunately, your code produced regressions where every variable was regressed with itself.

          However, I did come up with a solution.

          Code:
          forval i = 1/5 {
              forval j = 1/10 {
                 qui regress x_`j'_`i' y_`j'_`i'
                 di _[`x_`j'_`i']
              }
          }

          Comment


          • #6
            Unfortunately, your code produced regressions where every variable was regressed with itself.
            Not so. My guess is that you copied the code and introduced an error.

            Here is proof of concept. The regressions would be of no use or interest, but I think the loop is correct in principle.

            Code:
            clear
            set obs 10
            foreach v in x_1_1 y_1_1 x_2_1 y_2_1 x_1_2 y_1_2 x_2_2 y_2_2 {
                gen `v' = rnormal(0, 1)
            }
            
            foreach x of var x* {
                  local y : subinstr local x "x" "y", all
                  di "`x' `y'"
                  * regress `x' `y'
                  * di _b[`y']
            }
            Output

            Code:
            x_1_1 y_1_1
            x_2_1 y_2_1
            x_1_2 y_1_2
            x_2_2 y_2_2
            Meanwhile, your latest solution is of every x variable with every y variable.

            Your requests seem to flip back and forth between asking for that and asking for regressions only for particular pairs of variables, so the advice varies accordingly.

            That is. Clyde Schechter gave you a nested loop solution, but then you said it gave you too many regressions.

            If you now have what you want, that's good.
            Last edited by Nick Cox; 12 Mar 2023, 10:03.

            Comment

            Working...
            X