Announcement

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

  • Problem with macro in forvalues

    Hi! I'm trying to construct the variables for exporter-time and importer-time fixed effects for our gravity model but I only get the error 'invalid syntax' r(198). I simply copied the Stata commands from the book by Yotov et al. (2016) but there seems to be a problem that I can't resolve. Thank you all for your help in advance. Your feedback will be a big help to our research. Here are the codes I used:

    describe IMPORTER_TIME_FE*
    global N = r(r)
    global N_1 = $N - 1
    forvalues i = 1 (1) $N_1 {
    replace EXPORTER_TIME_FE`i' = EXPORTER_TIME_FE`i' * exp(_b[EXPORTER_TIME_FE`i'])
    replace IMPORTER_TIME_FE`i' = IMPORTER_TIME_FE`i' * exp(_b[IMPORTER_TIME_FE`i'])
    }
    replace EXPORTER_TIME_FE$N = EXPORTER_TIME_FE$N * exp(_b[EXPORTER_TIME_FE$N ])
    replace IMPORTER_TIME_FE$N = IMPORTER_TIME_FE$N * exp(0)

    egen exp_pi_BLN = rowtotal(EXPORTER_TIME_FE1-EXPORTER_TIME_FE$N)
    egen exp_chi_BLN = rowtotal(IMPORTER_TIME_FE1-IMPORTER_TIME_FE$N)


    The error appears when I run the forvalues command:

    . forvalues i = 1 (1) $N_1 {
    2. replace EXPORTER_TIME_FE`i' = EXPORTER_TIME_FE`i' * exp(_b[EXPORTER_TIME_FE`i'])
    3. replace IMPORTER_TIME_FE`i' = IMPORTER_TIME_FE`i' * exp(_b[IMPORTER_TIME_FE`i'])
    4. }
    invalid syntax
    r(198);

  • #2
    Where does r(r) come from? It's not a saved result of describe.

    My guess. r(r) was never defined by your code or a value from a previous command was overwritten by the results of describe.

    It is not an error to refer to an r-class result that is not defined, but the result is missing.

    Therefore your global N is missing and so is N_1, which didn't bite until the loop failed.

    Detail: Name (date) references are deprecated: see https://www.statalist.org/forums/help#references


    Comment


    • #3
      Sir Nick Cox, thank you so much for your help and my sincerest apologies for not giving a link to my reference. I've now attached a link to it at the bottom of this post. Upon rechecking my codes I realized you were right, r(r) was never defined. I tried to look for where they define it in the Stata codes from Yotov et al. (2016)'s gravity model estimation handbook but there seems to be no information available. I thought it might be equivalent to the number of observations since the objective of the command is to create variables for the importer and exporter fixed effects, so I tried using r(N) instead. However, when I did that these were the results:

      . global N = r(N)

      . global N_1 = $N - 1

      .
      . forvalues i = 1 (1) $N_1 {
      2. replace EXPORTER_FE`i' = EXPORTER_FE`i' * exp(_b[EXPORTER_FE`i'])
      3. replace IMPORTER_FE`i' = IMPORTER_FE`i' * exp(_b[IMPORTER_FE`i'])
      4. }
      [EXPORTER_FE1] not found
      r(111);

      I tried searching for EXPORTER_FE1 in the variables tab and it's showing there so I'm not sure what the error message means. Would you happen to know if I might have misinterpreted what r(r) is for? Here is the guide I am using as reference and the codes are in pages 104-105: https://www.wto.org/english/res_e/bo...ctad2016_e.pdf

      Thank you so much again!
      Last edited by Marianne Sasing; 05 Dec 2021, 12:56.

      Comment


      • #4
        I can't check against the original code, still referenced by a minimal authors and year reference. But the loop is clearly expecting a bundle of variables

        Code:
        EXPORTER_TIME_FE*
        and an associated coefficient for each, so that the number you need is the number of such predictors.

        You may have worse problems if the code can't even find the first such variable in your dataset.

        I fear that the worthy or unworthy Yotov and friends wrote highly specific code geared to their dataset, and hence that you need to replicate what they did very closely.

        Comment


        • #5
          Sorry. the reference expanding Yotov et al. was implied in your link. So you did what we asked and provided a reference. Thanks! But sorry again, I am not going to wade through a lengthy 144 page report to try to work out what they did.

          Comment


          • #6
            Hi Sir Nick Cox , it's okay and sorry for the confusion. I was hoping to just refer to one page of the book but I have now consulted with the author and found that the global is used to define the last country on the exporter or on the importer side (if the panel is balanced), e.g., if I have 60 countries and I type

            Code:
            quietly tabulate exporter
            global N = r(r)
            then N = r(r) = 60. Then I can use this in loops elsewhere to refer to the last country, e.g., replace exporter_fe$N=....

            Unfortunately, I am still facing the r(111) error code where [IMPORTER_TIME_FE1] is not found. I am quite confused since I am able to find the variable in my list so I don't know why it's generating this error code. Here are my revised codes for reference and thank you so much again for your help!

            Code:
            //Generate the fixed effects//
            //country-specific fixed effects//
            egen exp_time=group(exporter year)
            quietly tabulate exp_time, generate(EXPORTER_TIME_FE)
            egen imp_time=group(importer year)
            quietly tabulate imp_time, generate(IMPORTER_TIME_FE)
            
            //country-pair fixed effects; NOTE: the pair_id variable is already generated//
            quietly tabulate pair_id, generate(PAIR_FE)
            
            describe IMPORTER_TIME_FE*
            describe EXPORTER_TIME_FE*
            describe PAIR_FE*
            global N = 52
            global N_1 =  $N - 1
            forvalues i = 1 (1) $N_1 {
                replace IMPORTER_TIME_FE`i' = IMPORTER_TIME_FE`i' * exp(_b[IMPORTER_TIME_FE`i'])
                replace EXPORTER_TIME_FE`i' = EXPORTER_TIME_FE`i' * exp(_b[EXPORTER_TIME_FE`i'])
                replace PAIR_FE`i' = PAIR_FE`i' * exp(_b[PAIR_FE`i'])
            }
                replace EXPORTER_TIME_FE$N = EXPORTER_TIME_FE$N * exp(_b[EXPORTER_TIME_FE$N ])
                replace IMPORTER_FE$N = IMPORTER_TIME_FE$N * exp(_b[EXPORTER_TIME_FE$N ])
                replace PAIR_FE$N = PAIR_FE$N * exp(_b[PAIR_FE$N ])
            
            egen exp_pi = rowtotal(EXPORTER_FE1-EXPORTER_FE$N )
            egen exp_chi = rowtotal(IMPORTER_FE1-IMPORTER_FE$N )
            egen exp_gamma = rowtotal(PAIR_FE1-PAIR_FE$N )
            
            ppmlhdfe exp_pi ln_exporter_nomGDP ln_exporter_infra vce(cluster pair_id)
            ppmlhdfe exp_chi ln_importer_nomGDP ln_importer_infra vce(cluster pair_id)
            ppmlhdfe exp_gamma EXPORTER_TIME_FE* IMPORTER_TIME_FE* ln_distcap contig com_lang_ethno col_dep_ever vce(cluster pair_id)
            Last edited by Marianne Sasing; 06 Dec 2021, 00:36.

            Comment


            • #7
              A guess: The coefficients won't exist before you've fitted the corresponding model.

              Comment


              • #8
                Hi Sir Nick Cox , you were right. It's working perfectly now. Thank you so much for all your help!!

                Comment

                Working...
                X