Announcement

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

  • How to loop regressions with and without a variable?

    Without loop function, I can run these two regressions separately: one with popgrowth and the other without it.

    Code:
    webuse lifeexp
    reg lexp gnppc popgrowth
    reg lexp gnppc
    I want to use a foreach loop to achieve the same goal: use popgrowth in the first iteration and an empty string in the second iteration:

    Code:
    webuse lifeexp
    local p12 "popgrowth" ""
    
    foreach p in `p12'{
    
        reg lexp gnppc `p'
    
    }
    Stata complains "invalid something: quotes do not match" about the declaration of p12 macro. Is there an alternative to achieve this goal?
    Last edited by Luck Good; 11 Jul 2022, 12:22.

  • #2
    Code:
    webuse lifeexp
    local p12 `""popgrowth" " ""'
    
    foreach p in `p12'{
    
        reg lexp gnppc `p'
    
    }
    Welcome to Statalist. Please see our strong preference for full real names: https://www.statalist.org/forums/help#realnames. Click on “Contact us” located at the bottom right-hand corner of the page and ask that your name be changed.
    Last edited by Andrew Musau; 11 Jul 2022, 12:37.

    Comment


    • #3
      Code:
      webuse lifeexp, clear
      
      
      cls
      
      local p12 popgrowth ""
      
      foreach p of loc p12 {
      
          reg lexp gnppc `p'
      
      }
      
      . esttab
      
      --------------------------------------------
                            (1)             (2)   
                           lexp            lexp   
      --------------------------------------------
      gnppc            0.000293***     0.000323***
                         (6.99)          (8.06)   
      
      popgrowth          -0.983*                  
                        (-2.03)                   
      
      _cons               70.67***        69.45***
                        (87.56)        (126.73)   
      --------------------------------------------
      N                      63              63   
      --------------------------------------------
      t statistics in parentheses
      * p<0.05, ** p<0.01, *** p<0.001
      i used esttab from ssc, but either way this does the job
      Last edited by Jared Greathouse; 11 Jul 2022, 12:33.

      Comment


      • #4
        Thank you. Both solutions work!

        Comment


        • #5
          When defining a local or global macro, if Stata encounters a double quote as the first character, it will expect a matching double quote as the final character, and will remove both. So you must wrap your macro definition in compound double quotes, as shown in the example below. Note that I ran the regression quietly because who needs to see the regression results, and then displayed the command line that had been run from the stored estimates in e().
          Code:
          . webuse lifeexp
          (Life expectancy, 1998)
          
          . // demonstrate the problem
          . local p12 "popgrowth" " "
          
          . macro list _p12
          _p12:           popgrowth" "
          
          . // fix the problem
          . local p12 `" "popgrowth" " " "'
          
          . macro list _p12
          _p12:            "popgrowth" " "
          
          . 
          . foreach p in `p12' {
            2. quietly reg lexp gnppc `p'
            3. display `" `e(cmdline)' "'
            4. }
           regress lexp gnppc popgrowth 
           regress lexp gnppc   
          
          .

          Comment


          • #6
            Thank you for the solution and explanations. Is there a way to display this empty string in outreg2? Since it is not exactly the same question as this one, I asked another question in the forum. The link is here.

            Comment

            Working...
            X