Announcement

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

  • How to loop (/run a regression) for every value of a variable?

    I have a dataset with 27 countries and I wish to run 6 regressions for every country only using data from that country.
    Currently it does not start the loop. The error is dependent on how I try.

    My loopcode below
    (Suggestion on how to save with different file names using the Country variable are also welcome).

    ---------

    foreach country in CountryFromReprRisk {

    xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom EcoIncidentsTotal Leverage i.Year if PrivateCom == 0 & country == CountryFromReprRisk , fe
    estimate store PuEco

    xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom SocIncidentsTotal Leverage i.Year if PrivateCom == 0 & country == CountryFromReprRisk , fe
    estimate store PuSoc

    xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom GovIncidentsTotal Leverage i.Year if PrivateCom == 0 & country == CountryFromReprRisk , fe
    estimate store PuGov

    xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom EcoIncidentsTotal Leverage i.Year if PrivateCom == 1 & country == CountryFromReprRisk , fe
    estimate store PrEco

    xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom SocIncidentsTotal Leverage i.Year if PrivateCom == 1 & country == CountryFromReprRisk , fe
    estimate store PrSoc

    xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom GovIncidentsTotal Leverage i.Year if PrivateCom == 1 & country == CountryFromReprRisk , fe
    estimate store PrGov

    estimates table PuEco PrEco PuSoc PrSoc PuGov PrGov, star(0.1 0.05 0.01) drop(i.Year)
    estimate drop PuEco PuSoc PuGov PrEco PrSoc PrGov


    }

  • #2
    The issue isn't naming of the country variable but rather how you're using the mechanics of the foreach loop. The use of foreach's "in" here is the first culprit -- assuming CountryFromReprRisk is some kind of macro then you'll want to use "of" (or alternatively change CountryFromReprRisk to something like `CountryFromReprRisk' to have it loop over the contents of that macro). Next, in the if statement you'll want to evaluate the regression so that it runs on each country with something like this pseudo code:
    Code:
     variable == value from the loop
    here this appears to be:
    Code:
    country == `country'
    though admittedly that's not the easiest to follow syntax. You cannot set your if statement to equal to CountryFromReprRisk because that supposedly contains a list of all the countries, not just one of them!

    Consider the example below and how it uses -display- to help trace the contents of each run of the loop. This (plus a careful reading of the -help foreach-) will likely help you get closer to your solution.

    Code:
    sysuse auto, clear
    
    local CountryFromReprRisk 1 2 3
    foreach country of local CountryFromReprRisk {
    
        display `"The value being used in the regression is: `country'"'
    
        regress mpg price turn if rep78 == `country'
        }
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      CountryFromReprisk is a column or more specifically a cell that each company it has it's. The Cell is filled with "NL" or "BE".

      Comment


      • #4
        Henk Hollander This doesn't materially change my response. I suppose your new concern is that the cell is filled with a string value? Here's my example looping over string values in the variable 'make' .

        Code:
        sysuse auto, clear
        
        tab make  
        
        local CountryFromReprRisk `""Subaru" "Olds 98" "VW Dasher""'
        
        foreach country of local CountryFromReprRisk {      
        display `"The value being used in the regression is: `country'"'      
        table mpg price turn if make == `"`country'"'    
        }
        If I had to guess on how to fix your loop without having more information, it'd likely be:


        Code:
        local CountryFromReprRisk `""NL" "BE" "etc""'
        
        foreach country of local CountryFromReprRisk {
        
        display `"NOTE: Here is the value of country being inserted into the if statement:  `country' "'
        
        xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom EcoIncidentsTotal Leverage i.Year if PrivateCom == 0 & country == `country' , fe
        estimate store PuEco
        
        xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom SocIncidentsTotal Leverage i.Year if PrivateCom == 0 & country == `country' , fe
        estimate store PuSoc
        
        xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom GovIncidentsTotal Leverage i.Year if PrivateCom == 0 & country == `country' , fe
        estimate store PuGov
        
        xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom EcoIncidentsTotal Leverage i.Year if PrivateCom == 1 & country == `country' , fe
        estimate store PrEco
        
        xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom SocIncidentsTotal Leverage i.Year if PrivateCom == 1 & country == `country' , fe
        estimate store PrSoc
        
        xtreg ln_ROA ln_employees ln_OperatingRevenue PrivateCom GovIncidentsTotal Leverage i.Year if PrivateCom == 1 & country == `country' , fe
        estimate store PrGov
        
        estimates table PuEco PrEco PuSoc PrSoc PuGov PrGov, star(0.1 0.05 0.01) drop(i.Year)
        estimate drop PuEco PuSoc PuGov PrEco PrSoc PrGov
        
        }
        Last edited by eric_a_booth; 29 May 2025, 08:12.
        Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

        Comment


        • #5
          I'll try to simplfy my question.
          I have a cell with in it the headquarter country of a company. So think of it like "BE" or "NL" or "UK"
          Then I wish to run my panel data regression separately for every country.
          So
          Foreach loopingCountryVariable in AllcountriesInTheDataset
          {
          reg y x if HomecountryCompany == loopingCountryVariable
          }
          So how to do this correctly?
          How to do the foreachloop good? (How to generate AllcountriesInTheDataset from cells of HomecountryCompany)?

          Comment


          • #6
            Just read your new reply.
            I'll try.

            Comment


            • #7
              Edit: My previous comment wasnt true.
              Last edited by Henk Hollander; 29 May 2025, 08:20. Reason: Wasn't True

              Comment


              • #8
                Edit: My previous comment wasnt true.
                Last edited by Henk Hollander; 29 May 2025, 08:20. Reason: Wasn't True

                Comment


                • #9
                  See https://www.stata.com/support/faqs/d...-with-foreach/ for some small tricks that should help.

                  Comment

                  Working...
                  X