Announcement

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

  • Using Macros in a regression

    Hello all!

    I'm trying to run a series of regressions that involve utilizing the same variables over and over again. The do file is getting unwieldy and when I have to make changes, it's a pain to make them to each individual iteration of the model

    Now, I thought using the local command would provide some relief but it's creating more issues for me.

    I thought I could run a regression after defining a local

    Code:
    . local base_variables ( var1 var2 var3 var4)
    
    . regress dependent_var `base_variables' var5
    The regression runs but doesn't actually report coefficients for any of the variables defined by the local "base_variables"

    Can anyone outline for me what I'm doing wrong? Very much appreciated!




  • #2
    Possibly running the commands separately, e.g. line by line from a do-file editor window.

    Comment


    • #3
      I'm not sure I follow. Those were separate commands in a do file. Can you clarify what you mean?

      Comment


      • #4
        Your problem indicates that the regression command can't see the definition of the local.

        That can happens when lines are executed separately. For example, some people copy and paste one line at a time into the Command window and then run each. Or some people execute one line from a Command window and one line from a do-file editor window. I don't why they do either, but it often happens, as the problem you report is widespread.

        Alternatively, the code you typed looks schematic. Do you really call your dependent variable dependent_var rather than something evocative? Showing us schematic code is often a bad idea as you may accidentally fix the problem in the real code. It certainly doesn't help us not to see a reproducible problem.

        Comment


        • #5
          Here's the actual code I'm running as separate lines out of a do file:

          Code:
          local base (income_1000 upb_1000 cltv_spline* BO_AGE fico_spline* dti_spline* ficodif coborrower Unemp_threeyear HPI_threeyear AMIdummy*)
          
          regress hfa_flag `base' state_d* year* if cem_matched==1 [aweight = cem_weights1], vce(robust)
          This code spits out coefficients for all of the various state-d and year variables (my fixed effects dummies) but doesn't estimate any of the variables defined in the code for local

          Alternatively, I have tried to run it this way:

          Code:
          global base (income_1000 upb_1000 cltv_spline* BO_AGE fico_spline* dti_spline* ficodif coborrower Unemp_threeyear HPI_threeyear AMIdummy*)
          
          regress hfa_flag $base state_d* y* if cem_matched==1 [aweight = cem_weights1], vce(robust)
          And the same issue occurs. I get coefficients for the state_d variables and the various year variables but nothing else.

          Comment


          • #6
            Try omitting the parentheses in the macro definition.

            Comment


            • #7
              Thank you for both the suggestion and the quick reply, that did indeed work as long as I ran the commands together.

              However, this sets me back to square one. The entire purpose of using macro was to define a set of variables that I could plug into different configurations of models that I'm running. If I have to run the macro simultaneously with the various iterations of the code, it doesn't help very much.

              Is there a way for me to define groups of variables at the beginning of a do file without having to run the commands at the same time?

              Put another way: let's say I need to, for clarity, run my code in chunks (rather than all at once), is there anyway for me the macro I define to be held in memory so that I can refer back to it as I change and run different parts of my code?
              Last edited by Matthew Record; 06 Jun 2017, 19:15.

              Comment


              • #8
                Code:
                help include

                Comment

                Working...
                X