Announcement

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

  • Labelling units in a numlist of a loop

    Dear all,

    For a number of countries in PISA, I am studying the expectation of college graduation among adolescents interviewed in PISA. This is my dependent variable (expect_ISCED5A). I am considering both individual-level and school-level variables. Thus, my model is a multilevel logistic regression. My key independent variables at individual level are adolescent's gender (female) and father's education (fisced4).

    My objective is to run a multilevel logistic regression for each country; extract the marginal effect of gender for the highest and lowest category of father's education (fisced4==1 / fisced4==4) and then compile the contrast between these marginal effects for each country in a single Excel file.

    Instead of asking Stata to run a multilevel logistic regression for each country separately, and in order to simplify my do-file, I have decided to run the following loop. Some countries in the numlist (the list countries to run the loop over) are omitted because the corresponding national samples lack some important control variables.

    Code:
    foreach i of numlist 1/3 5/14 17 19/21 25/30 {
           xtmelogit expect_ISCED5A i.female i.fisced4 female#fisced4 if country3==`i' || schoolid:, variance
           margins, dydx(female) over(r.fisced4) predict(mu fixedonly) vsquish level (90) post
           estimates store Model`i'
           }
    After running the loop, I try to export the results to a csv file as follows:
    Code:
    esttab * using "G:\Mi unidad\Word\Datos........ \ME_high_low_fath.csv", replace
    At opening the Excel file (after converting it from csv format), it is hard for me to identify to which country correspond the different contrast of margins that have been stored. For this reason, I would very much like to get the name of the country somehow in the Excel file.

    Could anyone of you give me some guidance as to how to introduce the country label either in the loop or at the moment of exporting the results to a csv file?

    Many thanks for your attention

    And kind regards

    Luis Ortiz



  • #2
    The extended macro functions (-help extended_fcn-) have facilities for manipulating value labels.

    Working from there, the command
    Code:
    local CountryName: label (country3) `i'
    placed at the top of your loop will capture the country's value label for a particular value of country3. I'm not a user of -esttab-, but I'm guessing you can put your -estttab- command inside the loop, and then put the CountryName local into the title of each -esttab- table as you -append- it to your output file

    Comment


    • #3
      Many thanks, Mike...¡

      It sounds really useful, but I do not arrive to understand where the line should be placed exactly. Does "at the top of your loop" means inside the loop? I have tried (see next) but to no avail:

      Code:
      foreach i of numlist 1/2  {
             local CountryName: label (country3) `i'
             xtmelogit expect_ISCED5A immig3 famstruc3 Above_mode Below_mode PV1MATH PV1READ positive_att vocational media_educ media_socio i.female i.fisced4 female#fisced4 if fisced4!=5 & country3==`i' || schoolid:, variance
             margins, dydx(female) over(r.fisced4) predict(mu fixedonly) vsquish level (90) post
             outreg2 using [myfile], excel append ctitle(CountryName)
             }

      I have also tried to introduce outreg2 (insted of esttab) in the loop, as a way of exporting my results to a single Excel file. Outreg2 may be useful because of the ctitle option of outreg. Yet, I believe I am not making a proper use of the macro to insert the label of each country in the variable 'country3'.

      I believe I am almost there (thanks to your guidance) but I do not completely succeed in getting what I want.

      Many thanks for your attention, at any rate.

      All the best

      Luis Ortiz

      Comment


      • #4
        Yes, by "top" I meant inside the loop. You clearly want CountryName to take on different values depending on `i', and that can only happen *inside* the loop, where `i' varies.

        And yes, you are not using the macro CountryName correctly. As with all local macros in Stata, one accesses its contents by "dereferencing' its name with a backquote and a quote, as you correctly do with `i' to obtain the value of i.

        I don't think it's necessary to use -outreg2-, as I believe that -esttab- also has a title option. However, in the code you show, you should be able to get what you want with:
        Code:
        outreg2 using myfile, excel append ctitle(`CountryName')
        (Note also that putting the file name "myfile" in "[ ]" is also probably wrong (check examples in the documentation for -outreg2-).

        And, the next time you report a problem, it would help if you say something more descriptive than "to no avail." This, or its more common variant "didn't work," is not well-liked on StataList because it doesn't give enough information to help someone help you. You need to tell/show us more specifically how something didn't work.


        I'm not

        Comment


        • #5
          It works perfectly now, Mike....¡

          Many thanks for your help. And my apologies for the lack of precision in my report of the initial attempt.

          All the best

          Luis Ortiz

          Comment

          Working...
          X