Announcement

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

  • Coding permutations in STATA

    Hi everyone,

    I have a simple coding question. I have a firm panel data (earnings, assets, labor) and I know where each firm is located (country). I would like to run all possible combinations of countries in my data set. The regression is the following:

    1) Earnings of firm= a+b total assets +b labor+u

    Let's assume that I have a variable called country and there are 3 countries in it (a,b,c)-some firms are located in country a, some in b and some in c. I would like to run this regression for all combinations of this three countries. :
    a
    b
    c
    ab
    ac
    bc

    How can I code this in Stata (note I have 20 countries in the real dataset)? I looked at permute, but it is not doing what I was hoping. Any ideas?
    Thank you

    Chiara

  • #2
    These are combinations, not permutations, since ab would be the same as ba in your notation.

    More importantly, if with your 20 countries you mean to run not only ab and ac and ad but also abc and abd and abcd, then overall you will have 220-1 regressions to run, a very large number.

    Perhaps I misunderstood what you seek.

    Comment


    • #3
      Sorry, you very much correct. That's exactly what I am trying to do. Is there a way that I code this in Stata (maybe with loops). So far all my attempts failed.

      Comment


      • #4
        Code:
        ssc describe tuples
        http://www.statalist.org/forums/help#spelling

        Comment


        • #5
          Thank you Dr. Cox. I apologize for the misspelling of Stata.

          I looked at tuples already and even if it does generates all possible combinations of a list it is very difficult to use that for my regression.

          xtset firm year
          xtreg earnings assets labor if country=="XXXX"|country=="YYY"......, fe

          the XXXX and YYYY should be the countries I am looking at that obviously change in every regression (they will more than just 2 at some point). How can I overcome this issue?

          Thank you
          Chiara

          Comment


          • #6
            First, encode country to a numeric variable to make life easy for yourself. It's much easier to loop over 1, 1 & 2, 1 & 3, ... than a list of string values.

            Second, this hints at some technique:

            Code:
            tuples 1 2 3 4 5 
            
            forval j = 1/31 { 
                local this : subinstr local tuple`j' " " ",", all
                di inlist(3, `this') 
            }
            i.e. you can convert each tuple to a comma-separated list each time you go round a loop.

            Comment


            • #7
              Thank you for your help. Still it is not clear how I can integrate that into the regression to have all possible combinations.


              Comment


              • #8
                I am showing how to program your if condition. In #5 you showed that you realise that each regression needs such a condition.

                I don't have your data and don't feel inclined to invent a surrogate dataset, but this works

                Code:
                sysuse auto, clear 
                
                numlist "1/5" 
                tuples `r(numlist)' 
                
                forval j = 1/31 { 
                    local this : subinstr local tuple`j' " " ",", all
                    su mpg if inlist(rep78, `this'), meanonly 
                    di "`this'{col 14}"  %2.1f r(mean) 
                }
                Naturally with regress there is more output than this minimal example with summarize, but I don't yet see any details on what you want to see or save. In your case you could start with an encoded variable and supply a numlist from 1 to its maximum.

                Comment


                • #9
                  Thank you for your help. Using your data, lets assume that you would like to regress weight on mpg for each group of rep78 (that you identified with tuples) and export that in excel or world (with outreg2).
                  So run a regression (reg mpg weight) for each combination of rep78 ( rep78=1, rep78=2.... rep78=1,2,3,4,5.) followed by outreg2.
                  How would you code that?

                  Thank you again

                  Comment


                  • #10
                    Sorry, but I don't ever export regressions to Excel or ever use outreg2 (SSC) and so can't advise further. Someone else may be able to willing to help, but I don't think you have a well specified problem. You would need to say what you wanted to export.

                    Comment


                    • #11
                      Thank you Dr. Cox.

                      I am trying to export the regression results from each regression.

                      Comment

                      Working...
                      X