Announcement

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

  • trade cost calculation

    I am trying to calculate trade costs following the methodology outlined by Novy (2012). The code and example data used are provided below. While the results are fine, the structure of the code seems rather rudimentary. suggestions to enhance the sophistication of the do-file would be greatly appreciated.

    Code:
    clear all
    input str3 exporter str3 importer year trade
    "IND" "UAE" 1990 500
    "UAE" "IND" 1990 600
    "IND" "IND" 1990 1000
    "UAE" "UAE" 1990 1200
    "IND" "UAE" 1991 550
    "UAE" "IND" 1991 650
    "IND" "IND" 1991 1100
    "UAE" "UAE" 1991 1250
    end
    save trade_cost.dta, replace
    preserve
    gen intratrade= trade if exporter==importer
    keep exporter year intratrade
    drop if intratrade==.
    save intratrade1.dta, replace
    
    restore
    merge m:1 exporter year using intratrade1
    drop _merge
    rename intratrade intratrade_exp
    save trade_cost.dta, replace
    preserve
    
    use intratrade1.dta
    rename exporter importer
    save intratrade1.dta, replace
    
    restore
    merge m:1 importer year using intratrade1
    rename intratrade intratrade_imp
    drop _merge
    drop if exporter==importer
    save trade_cost.dta, replace
    
    preserve
    rename importer importer1
    rename exporter importer
    rename importer1 exporter
    rename trade trade_imp
    keep importer exporter year trade
    save Inter_trade.dta, replace
    
    restore
    merge 1:1 exporter importer year using Inter_trade.dta
    drop _merge
    rename trade trade_exp
    
    * Set theta parameter
    scalar theta = 8
    
    * Compute trade costs with robust error handling
    gen tau = .
    replace tau = ((intratrade_exp*intratrade_imp)/(trade_exp*trade_imp))^(1/(2*(theta-1))) - 1

  • #2
    The code is long and complicated, and, to be honest, I don't grasp what you are actually trying to do.

    I have the general sense that what you are doing here involves repeatedly merging the original data set with a subset ("intratrade1.dta"), but using different linkage variables for the purpose at different points in the code. If I have that right the code could be simplified (and sped up, because so many disk operations would become unnecessary) by putting the information from intratrade1.dta into a frame instead of saving it to disk. Then the varying linkages used at different times could be accomplished with the -frlink- command instead of all of the -rename-ing and -merge-ing. But as I don't understand what is really going on here, I can't be more specific than that.

    By the way, references like Novy (2012) are not helpful. In your circle, it may be folklore, but for those of us who are not in your field, it is just a mystery. At the very least, a full reference is needed so that somebody who wanted to check that article to figure out what you're doing could find it. Even that, though, is not as helpful as one might hope, because these articles are typically behind a paywall. Even to those whose institutions enable them to get through the paywall, it is usually more effort to find and read an article than most volunteer helpers want to undertake. So you are better off providing an extended quote from the article, or, a clear, concise paraphrase.

    Comment


    • #3
      Clyde Schechter 's point about the very limited usefulness of bare name (date) references is not only one I would endorse strongly, it's also part of the advice we ask all posters to read before posting:


      https://www.statalist.org/forums/help#references

      Comment


      • #4
        I am sorry for not following general guidelines for posting. The working paper is available here: https://cep.lse.ac.uk/pubs/download/dp1114.pdf. Specifically, I am trying to calculate bilateral trade cost by using the formula given by equation number 5 on page 6. Yes Clyde, I just can't figure out how to use frame and frlink link to reduce disk operations.

        Comment


        • #5
          Thanks for sending the article. Unfortunately, as I am not an economist, I do not understand much of the terminology it uses, so I don't know which variables in the article's equations correspond to which variables in your data.

          What I've done below is attempt to simply "translate" your code into frame-based code. I think I have it right, although I would be more confident if I really understood what is being done here. Anyway, see if this works for you. Your original code is interspersed, and commented out in blocks set off by /* */ and greyed out. Each "paragraph" of my proposed code is my proposed "translation" of the immediately preceding block of your code. The lines of code set in italics is your original code, unmodified by me and retained as active.
          Code:
          clear all
          input str3 exporter str3 importer year trade
          "IND" "UAE" 1990 500
          "UAE" "IND" 1990 600
          "IND" "IND" 1990 1000
          "UAE" "UAE" 1990 1200
          "IND" "UAE" 1991 550
          "UAE" "IND" 1991 650
          "IND" "IND" 1991 1100
          "UAE" "UAE" 1991 1250
          end
          /*
          save trade_cost.dta, replace
          preserve
          gen intratrade= trade if exporter==importer
          keep exporter year intratrade
          drop if intratrade==.
          save intratrade1.dta, replace
          */
          
          frame copy default intratrade1
          frame intratrade1 {
              keep if exporter == importer
              rename trade intratrade
              drop exporter
              rename importer trader
          }
          
          /*
          restore
          merge m:1 exporter year using intratrade1
          drop _merge
          rename intratrade intratrade_exp
          save trade_cost.dta, replace
          preserve
          */
          
          frlink m:1 year exporter, frame(intratrade1 year trader)
          frget intratrade_exp = intratrade, from(intratrade1)
          
          /*
          use intratrade1.dta
          rename exporter importer
          save intratrade1.dta, replace
          */
          
          /*
          restore
          merge m:1 importer year using intratrade1
          rename intratrade intratrade_imp
          drop _merge
          drop if exporter==importer
          save trade_cost.dta, replace
          */
          
          drop intratrade1
          frlink m:1 year importer, frame(intratrade1 year trader)
          frget intratrade_imp = intratrade, from(intratrade1)
          drop if importer == exporter
          
          
          /*
          preserve
          rename importer importer1
          rename exporter importer
          rename importer1 exporter
          rename trade trade_imp
          keep importer exporter year trade
          save Inter_trade.dta, replace
          */
          
          drop intratrade1
          frame drop intratrade1
          
          /*
          restore
          merge 1:1 exporter importer year using Inter_trade.dta
          drop _merge
          rename trade trade_exp
          */
          
          frame put exporter importer year trade, into(reverse_trade1)
          rename trade trade_imp
          frlink 1:1 year exporter importer, frame(reverse_trade1 year importer exporter)
          frget trade_exp = trade, from(reverse_trade1)
          drop reverse_trade1
          frame drop reverse_trade1
          
          
          * Set theta parameter
          scalar theta = 8
          
          * Compute trade costs with robust error handling
          gen tau = .
          replace tau = ((intratrade_exp*intratrade_imp)/(trade_exp*trade_imp))^(1/(2*(theta-1))) - 1

          Comment


          • #6
            Thanks, dear Clyde, this is exactly what I wanted.

            Comment

            Working...
            X