Announcement

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

  • Difference in Difference

    Apologies I recently changed my project and I am trying to run a difference in difference regression:

    I am attempting to run the following regression:

    Pricemarket i -Pricemarket j= Mobile + Paved Roads + Population + Fuel Price + Rainfall + Price type + Commodity type

    Mobile is a dummy variable. I want the variable to take 1 if both the markets i and j have 1 for the mobile observation.

    Paved roads, population, fuel price and rainfall are observed explanatory variables (control variables)

    I include dummy variables for price type and commodity type to see if the price differencials are impacted by the type of commodity (perishable or non-perishable) and type of price (retail or trader)

    Code:
    set more off
    
    *Setting up qdate
    gen qdate = quarterly(string(quarter)+"q"+string(year), "QY")
    format qdate %tq
    
    *Setting up panel variable
    egen panel_price = group(commodityid pricetype)
    
    *reshape from long to wide for regress
    reshape wide price pavedroadskm population fuelprice rainfall, i(commodity country pricetype mobile qdate) j(marketid)
    
    *market differences
    foreach v in price {
    gen delta_`v' = `v'2 - `v'1
    }
    
    gen mtreatment=0
    foreach w in treatment {
    replace mtreatment=1 if 'w'1=1 = 'w'2=1
    }
    
    
    global ylist delta_price
    global xlist pavedroadskm population fuelprice rainfall
    reg $ylist $mtreatment $xlist
    The error I keep receiving is:

    Code:
    'w'1 invalid name
    r(198);
    I have tried many variations but am unable to fix this.

    I attach a copy of my data (using 'dataex')

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 country byte(countryid marketid quarter) int year str2 unit str3 currency str7 commodity byte commodityid str8 pricetype float(price rainfall pavedroadskm population fuelprice) byte mobile
    "Benin" 1 1 1 2000 "KG" "XOF" "Millet" 1 "Producer"    110  .8320871 24.66  82.87595  364.8 0
    "Benin" 1 1 2 2000 "KG" "XOF" "Millet" 1 "Producer"    130  9.266488 24.66  83.04096 363.38 0
    "Benin" 1 1 3 2000 "KG" "XOF" "Millet" 1 "Producer"    115 17.017979 24.66   83.2063 364.59 0
    "Benin" 1 1 4 2000 "KG" "XOF" "Millet" 1 "Producer"    110 2.7170014 24.66  83.37196 360.17 0
    "Benin" 1 1 1 2001 "KG" "XOF" "Millet" 1 "Producer" 131.81  .4435913 27.12  85.38624 361.77 0
    "Benin" 1 1 2 2001 "KG" "XOF" "Millet" 1 "Producer"    135 8.8764305 27.12  85.53993 358.57 0
    "Benin" 1 1 3 2001 "KG" "XOF" "Millet" 1 "Producer"    170 15.918653 27.12   85.6939 356.98 0
    "Benin" 1 1 4 2001 "KG" "XOF" "Millet" 1 "Producer" 172.22 1.1938106 27.12  85.84815 363.98 0
    "Benin" 1 1 1 2002 "KG" "XOF" "Millet" 1 "Producer"    175 1.2990117 33.87  88.00756 353.83 0
    "Benin" 1 1 2 2002 "KG" "XOF" "Millet" 1 "Producer"    175   9.72025 33.87  89.09885 356.81 0
    "Benin" 1 1 3 2002 "KG" "XOF" "Millet" 1 "Producer"    110 17.741055 33.87  90.20368 366.09 0
    "Benin" 1 1 4 2002 "KG" "XOF" "Millet" 1 "Producer"    115  3.802082 33.87   91.3222 372.07 0
    "Benin" 1 1 1 2003 "KG" "XOF" "Millet" 1 "Producer"    115 1.2800548 37.36  90.72687 376.51 1
    "Benin" 1 1 2 2003 "KG" "XOF" "Millet" 1 "Producer" 111.11  12.08556 37.36  91.16417 370.82 1
    "Benin" 1 1 3 2003 "KG" "XOF" "Millet" 1 "Producer"    135  21.25432 37.36  91.60358 380.46 1
    "Benin" 1 1 4 2003 "KG" "XOF" "Millet" 1 "Producer" 131.22 3.5291195 37.36  92.04511 394.77 1
    "Benin" 1 1 1 2004 "KG" "XOF" "Millet" 1 "Producer" 142.19   .776113 37.49  93.52702 402.65 1
    "Benin" 1 1 2 2004 "KG" "XOF" "Millet" 1 "Producer"    175 11.494287 37.49  94.02841 400.95 1
    "Benin" 1 1 3 2004 "KG" "XOF" "Millet" 1 "Producer" 152.22    17.888 37.49  94.53249 405.85 1
    "Benin" 1 1 4 2004 "KG" "XOF" "Millet" 1 "Producer" 142.22  2.598993 37.49  95.03928 412.34 1
    "Benin" 1 1 1 2005 "KG" "XOF" "Millet" 1 "Producer"    135 1.9511178 49.82  96.38049 408.16 1
    "Benin" 1 1 2 2005 "KG" "XOF" "Millet" 1 "Producer"    115  9.160417 49.82  96.83106 412.34 1
    "Benin" 1 1 3 2005 "KG" "XOF" "Millet" 1 "Producer" 141.11 16.668852 49.82  97.28374 418.93 1
    "Benin" 1 1 4 2005 "KG" "XOF" "Millet" 1 "Producer" 111.11 2.5000436 49.82  97.73855 419.24 1
    "Benin" 1 1 1 2006 "KG" "XOF" "Millet" 1 "Producer" 138.11  1.093254 49.93  99.26842 441.15 1
    "Benin" 1 1 2 2006 "KG" "XOF" "Millet" 1 "Producer" 211.22  8.828572 49.93  99.96013 447.75 1
    "Benin" 1 1 3 2006 "KG" "XOF" "Millet" 1 "Producer" 152.22  17.47267 49.93 100.65665 473.89 1
    "Benin" 1 1 4 2006 "KG" "XOF" "Millet" 1 "Producer"    150  3.420969 49.93 101.35802 462.73 1
    "Benin" 1 1 1 2007 "KG" "XOF" "Millet" 1 "Producer"    155  .8597299 50.04 102.18684 478.21 1
    "Benin" 1 1 2 2007 "KG" "XOF" "Millet" 1 "Producer" 111.11 12.250764 50.04 102.92442 494.21 1
    "Benin" 1 1 3 2007 "KG" "XOF" "Millet" 1 "Producer"    145 19.497534 50.04 103.66733 527.63 1
    "Benin" 1 1 4 2007 "KG" "XOF" "Millet" 1 "Producer" 115.89  2.830138 50.04  104.4156 546.86 1
    "Benin" 1 1 1 2008 "KG" "XOF" "Millet" 1 "Producer" 111.11  1.162299  50.2 105.16927  566.8 1
    "Benin" 1 1 2 2008 "KG" "XOF" "Millet" 1 "Producer" 141.11 11.977502  50.2 105.92838 556.57 1
    "Benin" 1 1 3 2008 "KG" "XOF" "Millet" 1 "Producer"    175  22.16582  50.2 106.69298 546.53 1
    "Benin" 1 1 4 2008 "KG" "XOF" "Millet" 1 "Producer" 132.22  3.137085  50.2  107.4631 526.01 1
    "Benin" 1 1 1 2009 "KG" "XOF" "Millet" 1 "Producer"    175  1.074003 49.94 108.23875 517.47 1
    "Benin" 1 1 2 2009 "KG" "XOF" "Millet" 1 "Producer" 141.11  10.02601 49.94 109.02002 507.23 1
    "Benin" 1 1 3 2009 "KG" "XOF" "Millet" 1 "Producer" 138.11 18.241352 49.94 109.80693 489.96 1
    "Benin" 1 1 4 2009 "KG" "XOF" "Millet" 1 "Producer" 188.11  4.314962 49.94 110.59952 512.07 1
    "Benin" 1 1 1 2010 "KG" "XOF" "Millet" 1 "Producer" 111.11 1.2736073 49.96 111.39782 529.63 1
    "Benin" 1 1 2 2010 "KG" "XOF" "Millet" 1 "Producer" 142.22  8.645645 49.96  112.2019 547.79 1
    "Benin" 1 1 3 2010 "KG" "XOF" "Millet" 1 "Producer" 111.22 17.182041 49.96 113.01176 560.93 1
    "Benin" 1 1 4 2010 "KG" "XOF" "Millet" 1 "Producer"    175  3.729227 49.96 113.82748 586.01 1
    "Benin" 1 1 1 2011 "KG" "XOF" "Millet" 1 "Producer" 110.28 1.3433656 50.13  114.6491 600.18 1
    "Benin" 1 1 2 2011 "KG" "XOF" "Millet" 1 "Producer"    155  6.857734 50.13 115.37757  626.9 1
    "Benin" 1 1 3 2011 "KG" "XOF" "Millet" 1 "Producer" 118.11 13.282522 50.13 116.11068 642.18 1
    "Benin" 1 1 4 2011 "KG" "XOF" "Millet" 1 "Producer"    150 3.2788765 50.13 116.84845  648.4 1
    "Benin" 1 1 1 2012 "KG" "XOF" "Millet" 1 "Producer" 113.42   .695286 56.75  117.5909 628.81 1
    "Benin" 1 1 2 2012 "KG" "XOF" "Millet" 1 "Producer"    180  8.839492 56.75 118.33807  635.1 1
    "Benin" 1 1 3 2012 "KG" "XOF" "Millet" 1 "Producer" 152.22 12.117025 56.75    119.09 609.33 1
    "Benin" 1 1 4 2012 "KG" "XOF" "Millet" 1 "Producer"    175 2.5099454 56.75  119.8467 602.89 1
    "Benin" 1 1 1 2013 "KG" "XOF" "Millet" 1 "Producer" 108.11 1.4844406 58.59  120.6082 590.34 1
    "Benin" 1 1 2 2013 "KG" "XOF" "Millet" 1 "Producer" 131.22 11.528778 58.59 121.37454 578.05 1
    "Benin" 1 1 3 2013 "KG" "XOF" "Millet" 1 "Producer" 131.22 13.377524 58.59 122.14576 572.61 1
    "Benin" 1 1 4 2013 "KG" "XOF" "Millet" 1 "Producer"    180   3.35759 58.59 122.92187 560.87 1
    "Benin" 1 1 1 2014 "KG" "XOF" "Millet" 1 "Producer" 188.11 1.2139716 58.62 123.70292 549.37 1
    "Benin" 1 1 2 2014 "KG" "XOF" "Millet" 1 "Producer" 111.11 10.257064 58.62  124.7477 530.71 1
    "Benin" 1 1 3 2014 "KG" "XOF" "Millet" 1 "Producer" 132.22 18.031187 58.62 125.80133 515.76 1
    "Benin" 1 1 4 2014 "KG" "XOF" "Millet" 1 "Producer" 201.11   3.15808 58.62 126.86385 507.54 1
    "Benin" 1 1 1 2015 "KG" "XOF" "Millet" 1 "Producer" 131.22  2.468094 63.91 127.93534 484.21 1
    "Benin" 1 1 2 2015 "KG" "XOF" "Millet" 1 "Producer" 171.22  6.944751 63.91 129.01588 485.38 1
    "Benin" 1 1 3 2015 "KG" "XOF" "Millet" 1 "Producer" 131.11 15.586768 63.91 129.57907 440.46 1
    "Benin" 1 1 4 2015 "KG" "XOF" "Millet" 1 "Producer" 151.11  3.730871 63.91 130.14473 453.95 1
    "Benin" 1 1 1 2016 "KG" "XOF" "Millet" 1 "Producer" 132.22    3.4156 64.51 130.71284 443.93 1
    "Benin" 1 1 2 2016 "KG" "XOF" "Millet" 1 "Producer" 171.22    8.3423 64.51 131.28345 454.59 1
    "Benin" 1 1 3 2016 "KG" "XOF" "Millet" 1 "Producer" 208.08   18.4363 64.51 131.85654 434.13 1
    "Benin" 1 1 4 2016 "KG" "XOF" "Millet" 1 "Producer"    205    3.9235 64.51 132.43213 424.55 1
    "Benin" 1 1 1 2000 "KG" "XOF" "Millet" 1 "Retail"   271.22  .8320871 24.66  82.87595  364.8 0
    "Benin" 1 1 2 2000 "KG" "XOF" "Millet" 1 "Retail"      285  9.266488 24.66  83.04096 363.38 0
    "Benin" 1 1 3 2000 "KG" "XOF" "Millet" 1 "Retail"   248.11 17.017979 24.66   83.2063 364.59 0
    "Benin" 1 1 4 2000 "KG" "XOF" "Millet" 1 "Retail"   232.22 2.7170014 24.66  83.37196 360.17 0
    "Benin" 1 1 1 2001 "KG" "XOF" "Millet" 1 "Retail"   252.22  .4435913 27.12  85.38624 361.77 0
    "Benin" 1 1 2 2001 "KG" "XOF" "Millet" 1 "Retail"   251.22 8.8764305 27.12  85.53993 358.57 0
    "Benin" 1 1 3 2001 "KG" "XOF" "Millet" 1 "Retail"      285 15.918653 27.12   85.6939 356.98 0
    "Benin" 1 1 4 2001 "KG" "XOF" "Millet" 1 "Retail"      285 1.1938106 27.12  85.84815 363.98 0
    "Benin" 1 1 1 2002 "KG" "XOF" "Millet" 1 "Retail"      285 1.2990117 33.87  88.00756 353.83 0
    "Benin" 1 1 2 2002 "KG" "XOF" "Millet" 1 "Retail"      285   9.72025 33.87  89.09885 356.81 0
    "Benin" 1 1 3 2002 "KG" "XOF" "Millet" 1 "Retail"      214 17.741055 33.87  90.20368 366.09 0
    "Benin" 1 1 4 2002 "KG" "XOF" "Millet" 1 "Retail"      210  3.802082 33.87   91.3222 372.07 0
    "Benin" 1 1 1 2003 "KG" "XOF" "Millet" 1 "Retail"   208.11 1.2800548 37.36  90.72687 376.51 1
    "Benin" 1 1 2 2003 "KG" "XOF" "Millet" 1 "Retail"      200  12.08556 37.36  91.16417 370.82 1
    "Benin" 1 1 3 2003 "KG" "XOF" "Millet" 1 "Retail"   222.22  21.25432 37.36  91.60358 380.46 1
    "Benin" 1 1 4 2003 "KG" "XOF" "Millet" 1 "Retail"      215 3.5291195 37.36  92.04511 394.77 1
    "Benin" 1 1 1 2004 "KG" "XOF" "Millet" 1 "Retail"   221.22   .776113 37.49  93.52702 402.65 1
    "Benin" 1 1 2 2004 "KG" "XOF" "Millet" 1 "Retail"   251.22 11.494287 37.49  94.02841 400.95 1
    "Benin" 1 1 3 2004 "KG" "XOF" "Millet" 1 "Retail"   228.11    17.888 37.49  94.53249 405.85 1
    "Benin" 1 1 4 2004 "KG" "XOF" "Millet" 1 "Retail"   218.11  2.598993 37.49  95.03928 412.34 1
    "Benin" 1 1 1 2005 "KG" "XOF" "Millet" 1 "Retail"      205 1.9511178 49.82  96.38049 408.16 1
    "Benin" 1 1 2 2005 "KG" "XOF" "Millet" 1 "Retail"      185  9.160417 49.82  96.83106 412.34 1
    "Benin" 1 1 3 2005 "KG" "XOF" "Millet" 1 "Retail"      210 16.668852 49.82  97.28374 418.93 1
    "Benin" 1 1 4 2005 "KG" "XOF" "Millet" 1 "Retail"   178.11 2.5000436 49.82  97.73855 419.24 1
    "Benin" 1 1 1 2006 "KG" "XOF" "Millet" 1 "Retail"      205  1.093254 49.93  99.26842 441.15 1
    "Benin" 1 1 2 2006 "KG" "XOF" "Millet" 1 "Retail"   277.11  8.828572 49.93  99.96013 447.75 1
    "Benin" 1 1 3 2006 "KG" "XOF" "Millet" 1 "Retail"      215  17.47267 49.93 100.65665 473.89 1
    "Benin" 1 1 4 2006 "KG" "XOF" "Millet" 1 "Retail"      210  3.420969 49.93 101.35802 462.73 1
    "Benin" 1 1 1 2007 "KG" "XOF" "Millet" 1 "Retail"      215  .8597299 50.04 102.18684 478.21 1
    "Benin" 1 1 2 2007 "KG" "XOF" "Millet" 1 "Retail"      170 12.250764 50.04 102.92442 494.21 1
    "Benin" 1 1 3 2007 "KG" "XOF" "Millet" 1 "Retail"   201.11 19.497534 50.04 103.66733 527.63 1
    "Benin" 1 1 4 2007 "KG" "XOF" "Millet" 1 "Retail"      170  2.830138 50.04  104.4156 546.86 1
    end
    Further please note that the above data example is before I ran the regression. I get an error (input statement exceeds linesize limit) if i use dataex after running my code

    Many thanks in advance
    Last edited by Mohammed Islam; 26 Mar 2018, 18:32.

  • #2
    There are a few problems I identify here. The first is your attempt to try and loop over variables but to only feed in one (hence making the loop redundant).

    The second is where you write 'w' instead of `w'. Note the small but crucial difference in the slanted apostrophe ( ` ) versus a more normal apostrophe ( ' ). When you call locals you should start with a slanted apostrophe and close with a normal one (just as you have done when you wrote the foreach loop that makes delta_`v').

    Third, there is no variable in your data called treatment or treatment1 or treatment2 (which is what you're telling Stata to look for in that loop). Hence you cannot loop over treatment (also, you wouldn't want to because it's just one variable), and you cannot evaluate the condition treatment1=1 (or treatment2=1).

    Fourth, when setting up logical conditions in Stata you should use two equals signs not one (so assuming there was a variable called treatment and you did want to loop over a single variable, that line should be treatment1==1 and treatment2==1).

    Finally, you can't write treatment1==1 = treatment2==1. It doesn't really make sense. What you likely mean is treatment1==1 & treatment2==1.

    FYI, i tried running your code with the data you shared and it breaks on this line:

    Code:
    . *first differences
    . foreach v in price {
      2. gen delta_`v' = `v'2 - `v'1
      3. }
    price2 not found
    If you can share data that has both the price variable and the treatment variable you indicate exists somewhere so I can replicate your problem, I can likely give you more constructive suggestions rather than just identifying issues in your code.








    Comment


    • #3
      I have tried many variations but am unable to fix this.
      Well, I suppose one variation you haven't tried is this:

      [code]
      gen mtreatment=0
      foreach w in treatment {
      replace mtreatment=1 if `w'1=1 = `w'2=1
      }
      The character that is used to start a reference to a local macro (including the parameter of a loop) is not a regular quote character ('), it is the "left quote" character (`) that slopes down to the right. On a US keyboard it is located to the left of the 1! key.

      After you fix that, you will then encounter a syntax error because -`w1' = 1- is illegal syntax: equality of two expressions in Stata is represented by the == operator, and it cannot be abbreviated to =. The single = operator is used only for assignment operations (gen, replace, egen). (There are a few old commands that allow = for equality, such as ttest, but these are exceptions to the general rule, and I know of no modern Stata commands that permit it.)

      It is also wrong in another way. The condition you stated in words is that you want it to be 1 if treatment1 and treatment2 are both 1. But the code, if you fixed the syntax, would set it to 1 if treatment1 and treatment2 were either both 1 or both not 1.

      But, in any case, this is unnecessarily complicated. There is no point in looping over a list containing only 1 item. Moreover, you don't need to first set mtreatment to 0 and then conditionally overwrite it with 1. The whole thing can be done with just:

      Code:
      gen mtreatment = (treatment1 == 1) & (treatment2 == 1)
      (If you really meant to set mtreatment to 1 to indicate that treatment1 and treatment2 are either both 1 or both not 1, then replace the & in this command by ==.

      Added: Crossed with #2 which makes the same points and some others as well.

      Comment


      • #4
        Thank you to you both:

        Apologies to Chris, the treatment variable is labelled in my dataset as mobile. It takes a value of 0 when no coverage is present and 1 once coverage is introduced

        I am unsure if there is any other way of sharing my data other than dataex? (I'm not particularly familiar with stata as i've only been using it for 2.5 weeks and this module is untaught)

        If I change my code to either

        Code:
        gen mtreatment=0
        foreach w in mobile {
        replace mtreatment=1 if `w'1=1 = `w'2=1
        }
        or

        Code:
        gen mtreatment = (mobile1 == 1) & (mobile2 == 1)
        I get the same error

        Code:
        mobile1 not found
        r(111);
        If I switch my code to include mobile (below)

        Code:
        set more off
        
        *Setting up qdate
        gen qdate = quarterly(string(quarter)+"q"+string(year), "QY")
        format qdate %tq
        
        *Setting up panel variable
        egen panel_price = group(commodityid pricetype)
        
        *reshape from long to wide for regress
        reshape wide price pavedroadskm population fuelprice rainfall mobile i(commodity country pricetype mobile qdate) j(marketid)
        
        *market price differences
        foreach v in price {
        gen delta_`v' = `v'2 - `v'1
        }
        I get

        Code:
        option mobile not allowed
        r(198);
        If I drop mobile I get
        Code:
        option i() required
        
                 long                                wide
                +---------------+                   +------------------+
                | i   j   a   b |                   | i   a1 a2  b1 b2 |
                |---------------| <--- reshape ---> |------------------|
                | 1   1   1   2 |                   | 1   1   3   2  4 |
                | 1   2   3   4 |                   | 2   5   7   6  8 |
                | 2   1   5   6 |                   +------------------+
                | 2   2   7   8 |
                +---------------+
        
                long to wide: reshape wide a b, i(i) j(j)    (j existing variable)
                wide to long: reshape long a b, i(i) j(j)    (j    new   variable)
        r(198);
        My current code is

        Code:
        set more off
        
        *Setting up qdate
        gen qdate = quarterly(string(quarter)+"q"+string(year), "QY")
        format qdate %tq
        
        *Setting up panel variable
        egen panel_price = group(commodityid pricetype)
        
        *reshape from long to wide for regress
        reshape wide price pavedroadskm population fuelprice rainfall i(commodity country pricetype mobile qdate) j(marketid)
        
        *market price differences
        foreach v in price {
        gen delta_`v' = `v'2 - `v'1
        }
        
        gen mtreatment=0
        foreach w in mobile {
        replace mtreatment=1 if `w'1=1 = `w'2=1
        }
        
        *gen mtreatment = (mobile1 == 1) & (mobile2 == 1)
        
        global ylist delta_price
        global xlist pavedroadskm population fuelprice rainfall
        reg $ylist $mtreatment $xlist
        Last edited by Mohammed Islam; 26 Mar 2018, 19:34.

        Comment


        • #5
          Ok, there's still a few problems with what you're attempting (namely that mobile1 and mobile2 are not variables in your data).

          Let's step back though. It's unlikely you want your data in wide format, so I doubt you need to use the reshape command.

          For a diff-in-diff you want:
          • data structured in a way as to represent time; this would be qdate,
          • an outcome (which as you show in your model in #1 is price),
          • a treatment indicator (which as you explain is mobile),
          • and an indicator for the different groups; i'm not clear on what the different groups are in your data. You say that you're looking at differences between markets but I only see one value for market (market==1). I'm going to assume this is because you've only shared 100 observations of your data and that in your full dataset there are multiple values for market.
          I can see that your treatment begins at 2003q1. Assuming you're working in a larger dataset with multiple markets (or, if i'm wrong about what your group variable is replace market for whatever it is), and assuming that mobile takes the value of 0 for all other markets, you then want to generate a new variable that interacts your treatment variable with the market variable. E.g.

          Code:
          generate interaction = mobile*market
          You then just need to run a simple regression that takes the form:

          Code:
          regress price market mobile interaction [covariates], r
          Does this make sense to you? And does it help?

          Comment


          • #6
            Well, it's hard to help you out at this point. Your example data has marketid = 1 in every observation, so the -reshape- doesn't actually accomplish anything other than renaming most of your variables, adding a suffix 1 to them. And any reference in your code to mobile2 (or anything2) is of course going to break because there are no such variables.

            If your real data also has only marketid = 1, then there is something completely misguided about your research aims. You can't study differences between markets if the data set only has one market. But you know that. So I'm guessing that your full data actually has some observations with marketid = 2.

            What I suggest is that you create a new, small data set, containing, say 25 or 50 observations with marketid = 1, and then the corresponding observations having marketid = 2 (corresponding in the sense of referring to the same country, year, qdate, commodity, and pricetype. Also, -drop- from it any variables that are not relevant to your current problem. So, for example, variables like currency play no role here. And since countryid corresponds 1:1 with country, you can drop country. You can drop unit. And probably you can eliminate some of the variables among rainfall, pavedroadskm, population, and fuelprice as not necessary for figuring out the data management question at hand. Then use -dataex- to post that.

            The -dataex- command is very simple to use. Have you read the -help dataex- file? The problem you are encountering is just because your data set contains too many variables to fit in a dataex output. But if you create the new, smaller data set I have proposed in the preceding paragraph, you should be able to get it to work.

            Now, a few comments about some of the error messages you are getting. The -option i() required- message refers to your reshape command. There is a general principle of Stata syntax that applies to nearly every command: before you specify any options, you need to start with a comma (,). You didn't put a comma between rainfall and i(... It needs to be there. You do not need additional commas to separate options (and, in fact, they would be illegal), but you must have one comma to separate the variable list from the options.

            The mobile1 not found error refers to the evident fact that at that point in the code, your data set does not have a variable called mobile1. You had that command in there before the reshape, and at that point there is a variable called mobile, but none called mobile1 or mobile2. So that command can only be used after you -reshape-.

            Another point about the -reshape- command, you cannot have the variable mobile mentioned both in the varlist of the command and in the i() option. In this case, it clearly needs to be in the varlist, and you must remove it from the i() option.

            Concerning your current code, then I would make the following changes to it:

            Code:
            *Setting up qdate
            gen qdate = quarterly(string(quarter)+"q"+string(year), "QY")
            format qdate %tq
             *Setting up panel variable egen panel_price = group(commodityid pricetype)  
            *reshape from long to wide for regress
            reshape wide price pavedroadskm population fuelprice mobile rainfall i(commodity country pricetype qdate) j(marketid)
            // NOTE REMOVAL OF mobile FROM i() OPTION  
            *market price differences
            foreach v in price {
                gen delta_`v' = `v'2 - `v'1
            }  
            
            gen mtreatment = (mobile1 == 1) & (mobile2 == 1)
            
            
            local ylist delta_price
            local xlist pavedroadskm population fuelprice rainfall
            reg `ylist' mtreatment `xlist'
            Let me comment on the changes I have made in the last three lines, as these are different issues from what has been discussed so far. I have changed your globals to locals, and, correspondingly, all references of $whatever to `whatever'. This is a general principle. Global macros are an inherently unsafe coding practice and should never be used except in the very rare situations where no alternative will serve the purpose. There are very few circumstances where a local can't be used instead, and locals are safer. You are already struggling with Stata, so I don't want to drag you through the technicalities of why globals are dangerous. For now, I just suggest that you try to forget you ever learned about global macros. I have been using Stata since 1994 on an everyday basis, and so far I have only twice encountered a need to use global macros. I am willing to bet that in your entire career, you will never encounter such a situation. So just use locals for all macros. In the regression command, you also had a reference $mtreatment, but you never defined any global macro with the name mtreatment anyway, so this would have just been evaluated as an empty string. Finally, I understand that you are new to Stata, as we all were at one time. I have the clear sense that at this point you are struggling with a problem that is beyond your level of Stata preparation. You will get a lot of help from this Forum along the way, but you will find it a very frustrating experience. If you do not have a tight deadline for this project, I recommend that you step away from it temporarily and invest some time in learning the fundamentals of Stata. From the Help menu in Stata, select PDF Documentation and then use the links there to read the Getting Started [GS] and User's Guide [U] volumes. It is a lot to read, but it will explain Stata's approach to data management and analysis, the general principles of Stata syntax that apply to the vast majority of commands, and it will introduce you to the common commands that every one needs to know to use Stata efffectively. You won't remember everything, but when you're done you will have been exposed to the basics, and in most situations you will be able to guess which commands you are likely to need to use. If you have forgotten the details of how those commands work, you can then refer to the help files or the PDF documentation again. This investment of time will amply repay itself, and I think you will find Stata is much less difficult to work with than it appears to you at this point.

            Added: Crossed with #5. I actually disagree with Chris Larkin's advice to create an interaction variable. I am willing to bet that Mohammed will not know how to interpret the output of the regression and we will next be teaching him the confusing science of using -lincom- to unwind his results. I think he would be much better off relying on factor-variable notation and not creating the interaction term himself, and then using -margins- to more easily interpret the output:

            Code:
            regress price i.market##i.mobile [covariates], r
            margins market
            margins market, dydx(mobile)
            margins mobilel, dydx(market)
            That said, I don't have a clear enough picture of just what the goal of this project is to know if this is even an appropriate analysis. At the very least, using -regress- seems to be ignoring the longitudinal component of the data and the nesting of observations within some combination of country, commodity and perhaps some other variables. (It is, however consistent with his own approach.)

            Last edited by Clyde Schechter; 26 Mar 2018, 20:45.

            Comment


            • #7
              For a brief minute I thought i'd written a 'comprehensive' reply What great advice you give Clyde!

              You're absolutely right that factor notation + the liberal use of margins will likely be easier for Mohammed. I usually turn my group variable into a binary for received treatment / didn't receive treatment and that makes interpreting a simple interaction variable quite easy. I missed out that step in my previous post though.

              Another thing which occurs to me regarding Mohammed's research approach is that it's not immediately clear whether the non-interference assumption holds. I wasn't thinking too carefully about your group variable before, but if they are markets interconnected in some way then a change in one is likely to affect a change in others. If this is true then one of the key assumptions of a diff-in-diff identification strategy fails.

              Comment


              • #8
                Thank you both for your comments:

                regarding #5:
                - My outcome variable of interest is price arbitrage, so the price differentials between markets i and j.

                My full datasource contains 112 markets though I have been practising on the first few in order to understand how to use stata

                regarding #6

                I drop unit, currency, countryid and commodity (and the code I use now contains commodityid shown below).

                I also create smaller datasets specific to a commodity type, and price type (my dataex below is specific to millet producer prices).

                [ Note: I am not sure if I am still required to include commodityid and pricetype in the code since the dataset is specific for each ]

                Running the following code:

                Code:
                *Setting up qdate
                gen qdate = quarterly(string(quarter)+"q"+string(year), "QY")
                format qdate %tq
                
                *reshape from long to wide for regress
                reshape wide price pavedroadskm population fuelprice mobile rainfall, i(commodityid country pricetype qdate) j(marketid)
                
                *commodity variable removed
                 
                *market price differences
                foreach v in price {
                    gen delta_`v' = `v'5 - `v'6
                } 
                
                * When I used "`v'2 - `v'1" I recieved the error:
                * price2 not found
                * r(111);
                
                
                gen mtreatment = (mobile5 == 1) & (mobile6 == 1)
                
                *When i used mobile1 and mobile2 I received the error:
                *mobile1 ambiguous abbreviation
                *r(111);
                
                
                local ylist delta_price
                local xlist pavedroadskm population fuelprice rainfall
                reg `ylist' mtreatment `xlist'
                
                regress price i.market##i.mobile [covariates], r
                margins market
                margins market, dydx(mobile)
                margins mobilel, dydx(market)
                I get the error:
                Code:
                pavedroadskm ambiguous abbreviation
                r(111);
                However I cannot substitute in pavedroads specific to 5 and 6

                Noting your advice I have begun to read the Getting Started PDF but I am time-constrainted as my dissertation is due in 6 days (Next monday)

                Regarding #7
                I understand, and there are certainly spillover and network effects, however the literature largely ignores this issue and continues to use DinD. I comment on this in my dissertation

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input str12 country byte(marketid quarter) int year byte commodityid str8 pricetype float(price rainfall pavedroadskm population fuelprice) byte mobile
                "Burkina Faso" 5 1 2000 1 "Producer"  53.53   8.363715     72.87  51.23164  599.8935 0
                "Burkina Faso" 5 2 2000 1 "Producer"  51.33   2.533733     72.87  51.33364  600.8296 0
                "Burkina Faso" 5 3 2000 1 "Producer"  37.19  .09660327     72.87  51.43585 599.55756 0
                "Burkina Faso" 5 4 2000 1 "Producer"  51.11   8.282632     72.87  51.53826  602.0313 0
                "Burkina Faso" 5 1 2001 1 "Producer"     51   15.00022     80.15  52.70847  601.4302 0
                "Burkina Faso" 5 2 2001 1 "Producer"     47  17.457169     80.15  52.80335 602.63306 0
                "Burkina Faso" 5 3 2001 1 "Producer"  51.22  .08843722     80.15  52.89839  603.2355 0
                "Burkina Faso" 5 4 2001 1 "Producer"     57  .06649566     80.15  52.99361 598.55084 0
                "Burkina Faso" 5 1 2002 1 "Producer"  55.22   3.499156     100.1  54.23708 604.44196 0
                "Burkina Faso" 5 2 2002 1 "Producer"  52.25   19.71138     100.1  54.90962  632.6803 0
                "Burkina Faso" 5 3 2002 1 "Producer"     51    21.5307     100.1   55.5905  649.1343 0
                "Burkina Faso" 5 4 2002 1 "Producer"  53.11  2.5906565     100.1  56.27983  639.5771 0
                "Burkina Faso" 5 1 2003 1 "Producer"  93.04    .643503    110.43  55.81958  647.1945 0
                "Burkina Faso" 5 2 2003 1 "Producer"  37.57  2.4769015    110.43  56.08863  657.5212 0
                "Burkina Faso" 5 3 2003 1 "Producer"  42.29  18.251131    110.43  56.35898  674.6213 0
                "Burkina Faso" 5 4 2003 1 "Producer"  43.25  21.610495    110.43  56.63063   678.582 0
                "Burkina Faso" 5 1 2004 1 "Producer"  85.74   9.688042    110.79  57.46115  693.9456 0
                "Burkina Faso" 5 2 2004 1 "Producer"     52  .22795478    110.79   57.7692  707.1395 0
                "Burkina Faso" 5 3 2004 1 "Producer"  52.11    5.24999    110.79   58.0789  699.4703 0
                "Burkina Faso" 5 4 2004 1 "Producer"  45.82   8.848248    110.79  58.39026   710.652 0
                "Burkina Faso" 5 1 2005 1 "Producer"  144.4   23.10369    147.25  59.16823  719.8701 0
                "Burkina Faso" 5 2 2005 1 "Producer"  42.11  1.4593186    147.25  59.44484   710.652 0
                "Burkina Faso" 5 3 2005 1 "Producer" 101.48   .3229206    147.25  59.72274   726.321 0
                "Burkina Faso" 5 4 2005 1 "Producer" 142.27  1.7062407    147.25  60.00195  722.0125 0
                "Burkina Faso" 5 1 2006 1 "Producer" 141.23  17.021362    147.56  60.94529  713.5607 0
                "Burkina Faso" 5 2 2006 1 "Producer" 111.11   22.61802    147.56  61.36996  735.2964 0
                "Burkina Faso" 5 3 2006 1 "Producer"  118.7   9.304757    147.56  61.79758  766.5306 0
                "Burkina Faso" 5 4 2006 1 "Producer"  48.22  .05846529    147.56  62.22819  759.8938 0
                "Burkina Faso" 5 1 2007 1 "Producer"  41.11   .2623838    147.89  62.79449   785.314 0
                "Burkina Faso" 5 2 2007 1 "Producer" 111.11   9.029676    147.89  63.24774  811.5845 0
                "Burkina Faso" 5 3 2007 1 "Producer" 118.22  23.917044    147.89  63.70426  853.4489 0
                "Burkina Faso" 5 4 2007 1 "Producer" 111.52  20.021976    147.89  64.16408   884.559 0
                "Burkina Faso" 5 1 2008 1 "Producer" 115.11 .017173614    148.35  64.62721  825.8417 0
                "Burkina Faso" 5 2 2008 1 "Producer"  47.22  3.1956995    148.35   65.0937  820.8262 0
                "Burkina Faso" 5 3 2008 1 "Producer" 103.42   5.499363    148.35  65.56354   806.015 0
                "Burkina Faso" 5 4 2008 1 "Producer"  30.54  26.256794    148.35  66.03678   806.068 0
                "Burkina Faso" 5 1 2009 1 "Producer" 132.11   7.286169     147.6 66.513435  763.1654 1
                "Burkina Faso" 5 2 2009 1 "Producer"  44.32   1.368114     147.6  66.99352  777.2916 1
                "Burkina Faso" 5 3 2009 1 "Producer" 117.08   .3800685     147.6  67.47708  791.7923 1
                "Burkina Faso" 5 4 2009 1 "Producer" 101.25   8.798138     147.6 67.964134  818.9481 1
                "Burkina Faso" 5 1 2010 1 "Producer"  42.22  18.005587    147.66   68.4547  847.0352 1
                "Burkina Faso" 5 2 2010 1 "Producer" 111.22  19.642023    147.66  68.94881  876.0856 1
                "Burkina Faso" 5 3 2010 1 "Producer" 107.11 .019910967    147.66  69.44648  906.4828 1
                "Burkina Faso" 5 4 2010 1 "Producer"     42  .04939732    147.66  69.94775  937.2096 1
                "Burkina Faso" 5 1 2011 1 "Producer" 145.11   3.928859    148.17  71.37046  969.9152 1
                "Burkina Faso" 5 2 2011 1 "Producer"  43.11  23.885586    148.17 71.823944 1002.5982 1
                "Burkina Faso" 5 3 2011 1 "Producer"   70.5  26.828135    148.17  72.28031  1003.277 1
                "Burkina Faso" 5 4 2011 1 "Producer" 117.05  3.3787925    148.17 72.739586  969.3527 1
                "Burkina Faso" 5 1 2012 1 "Producer"    151  .09015556    167.73 73.201775  835.5552 1
                "Burkina Faso" 5 2 2012 1 "Producer"  24.11  1.6211625    167.73  73.66689  835.8281 1
                "Burkina Faso" 5 3 2012 1 "Producer"  25.21  13.934354    167.73  74.13497  801.9039 1
                "Burkina Faso" 5 4 2012 1 "Producer"  34.42  22.957415    167.73 74.606026   801.119 1
                "Burkina Faso" 5 1 2013 1 "Producer"   88.7  12.017582    173.17  75.08008  784.4368 1
                "Burkina Faso" 5 2 2013 1 "Producer"    115  .09703401    173.17  75.55714  768.1021 1
                "Burkina Faso" 5 3 2013 1 "Producer" 101.32   4.018069    173.17 76.037224   753.582 1
                "Burkina Faso" 5 4 2013 1 "Producer" 103.23   7.878382    173.17  76.52036  738.1306 1
                "Burkina Faso" 5 1 2014 1 "Producer"  58.82   23.43509    173.24  78.01511  823.4366 1
                "Burkina Faso" 5 2 2014 1 "Producer"    172  1.9878854    173.24  78.67403   831.085 1
                "Burkina Faso" 5 3 2014 1 "Producer" 102.85 .026229504    173.24  79.33852   797.852 1
                "Burkina Faso" 5 4 2014 1 "Producer"   30.2   1.937119    173.24  80.00861  794.8048 1
                "Burkina Faso" 5 1 2015 1 "Producer"  87.11  17.158663    188.89  80.68436  749.0428 1
                "Burkina Faso" 5 2 2015 1 "Producer" 148.22   26.03307    188.89  82.92992  760.1083 1
                "Burkina Faso" 5 3 2015 1 "Producer" 147.11   9.684564    188.89  83.29194  681.3703 1
                "Burkina Faso" 5 4 2015 1 "Producer" 111.27  .05490687    188.89  83.65553  710.8828 1
                "Burkina Faso" 5 1 2016 1 "Producer"  41.22   .4535165    190.67  84.02071  695.1932 1
                "Burkina Faso" 5 2 2016 1 "Producer"     43   9.054109    190.67  84.38749  703.2197 1
                "Burkina Faso" 5 3 2016 1 "Producer" 142.25  24.659674    190.67  84.75587  679.8499 1
                "Burkina Faso" 5 4 2016 1 "Producer" 101.22   18.47649    190.67  85.12585  664.8452 1
                "Burkina Faso" 6 1 2000 1 "Producer"  24.11   3.419884 14.193193  158.8937  521.0363 0
                "Burkina Faso" 6 2 2000 1 "Producer"  33.88  1.0360315 14.193193 159.21005 521.84937 0
                "Burkina Faso" 6 3 2000 1 "Producer"  41.24  .03950062 14.193193 159.52704 520.74457 0
                "Burkina Faso" 6 4 2000 1 "Producer"  42.83   3.386729 14.193193 159.84467  522.8931 0
                "Burkina Faso" 6 1 2001 1 "Producer"  42.13   6.133519 15.610233 163.47404   522.371 0
                "Burkina Faso" 6 2 2001 1 "Producer"  41.22   7.138154 15.610233  163.7683  523.4158 0
                "Burkina Faso" 6 3 2001 1 "Producer"  40.11  .03616156 15.610233  164.0631 523.93896 0
                "Burkina Faso" 6 4 2001 1 "Producer"     44  .02718976 15.610233  164.3584  519.8702 0
                "Burkina Faso" 6 1 2002 1 "Producer"  44.22  1.4307884 19.495995   168.215 524.98694 0
                "Burkina Faso" 6 2 2002 1 "Producer"  44.02   8.059889 19.495995 170.30086 549.51324 0
                "Burkina Faso" 6 3 2002 1 "Producer"  45.11   8.803802 19.495995  172.4126  563.8044 0
                "Burkina Faso" 6 4 2002 1 "Producer"  42.31  1.0593072 19.495995  174.5505  555.5035 0
                "Burkina Faso" 6 1 2003 1 "Producer"  42.22   .2631253  21.50882  173.1231 562.11957 1
                "Burkina Faso" 6 2 2003 1 "Producer"     24  1.0127933  21.50882 173.95753 571.08875 1
                "Burkina Faso" 6 3 2003 1 "Producer"  47.11   7.462801  21.50882   174.796 585.94104 1
                "Burkina Faso" 6 4 2003 1 "Producer"     47   8.836429  21.50882 175.63853 589.38116 1
                "Burkina Faso" 6 1 2004 1 "Producer"  44.23  3.9613945  21.57943 178.21437  602.7251 1
                "Burkina Faso" 6 2 2004 1 "Producer"  41.22  .09320963  21.57943 179.16977  614.1846 1
                "Burkina Faso" 6 3 2004 1 "Producer"  44.76   2.146696  21.57943  180.1303  607.5236 1
                "Burkina Faso" 6 4 2004 1 "Producer"  45.11   3.618007  21.57943   181.096  617.2355 1
                "Burkina Faso" 6 1 2005 1 "Producer"     41    9.44699  28.67997  183.5088  625.2418 1
                "Burkina Faso" 6 2 2005 1 "Producer"  41.38   .5967085  28.67997 184.36673  617.2355 1
                "Burkina Faso" 6 3 2005 1 "Producer"  43.53   .1320407  28.67997 185.22864  630.8447 1
                "Burkina Faso" 6 4 2005 1 "Producer"  44.11   .6976737  28.67997  186.0946  627.1026 1
                "Burkina Faso" 6 1 2006 1 "Producer"     48   6.959955   28.7411 189.02034  619.7617 1
                "Burkina Faso" 6 2 2006 1 "Producer"  47.48   9.248402   28.7411 190.33743  638.6403 1
                "Burkina Faso" 6 3 2006 1 "Producer"  44.22   3.804671   28.7411  191.6637  665.7687 1
                "Burkina Faso" 6 4 2006 1 "Producer"  41.34  .02390618   28.7411  192.9992  660.0043 1
                "Burkina Faso" 6 1 2007 1 "Producer" 104.22   .1072875 28.805315  194.7556  682.0829 1
                "Burkina Faso" 6 2 2007 1 "Producer"  44.14   3.692192 28.805315 196.16133  704.9001 1
                "Burkina Faso" 6 3 2007 1 "Producer"     48   9.779566 28.805315  197.5772  741.2614 1
                "Burkina Faso" 6 4 2007 1 "Producer"  71.89   8.186891 28.805315 199.00333   768.282 1
                end
                Note this dataex is before I run my code, as the transformation of my dataset leads to the error of exceeding the linesize limit

                Comment


                • #9
                  So, you're making good progress here. Your example data set has two marketid's, 5 and 6. That is why you don't get variables like price1 or price2 or mobile1 or mobile2. As you've discovered, you get price5 and price 6 and mobile5 and mobile6, and you successfully modified your data management code accordingly.

                  Now that we're over that obstacle, the next step is to figure out what kind of regression you want to do. You have been clear that you want the price difference, price6 - price5 to be the outcome variable. It is less clear to me what you want as the predictors. In some sense I get that you want the predictors to be paved roads, population, fuel price, mobile and rainfall. But In what sense do you want that. You now have separate values of those variables for each market in your 5-6 pair. Do you mean to use the values from both markets? Or do you want the predictors to be the differences between the two markets on these variables? I'm not an econometrician, and I have no idea which of these (if either) makes sense.

                  Another thing that is unclear to me here is the role of the variable mtreatment. For any given pair of markets, the variable mtreatment is going to be a constant, so it cannot be included as a predictor in the analysis. I'm not sure what you have in mind conceptually, but whatever it is, this approach will not get you there.

                  FInally there is something weird in your example data. For each of the variables rainfall, pavedroadskm, population, and fuelprice, there is a perfect 1.0 correlation between the values in market 5 and in market 6. I discovered this because I was trying to get some sensible regression outputs from them, but the *6 variables always drop due to colinearity.

                  Anyway, here's where things appear to stand so far:

                  Code:
                  *Setting up qdate
                  gen qdate = quarterly(string(quarter)+"q"+string(year), "QY")
                  format qdate %tq
                  
                  *reshape from long to wide for regress
                  reshape wide price pavedroadskm population fuelprice mobile rainfall, i(commodityid country pricetype qdate) j(marketid)
                  
                  *commodity variable removed
                   
                  *market price differences
                  foreach v in price pavedroadskm population fuelprice mobile rainfall{
                      gen delta_`v' = `v'5 - `v'6
                  } 
                  
                  
                  //    THIS COMMAND IS OK, BUT THE VARIABLE IT CREATES IS A CONSTANT
                  gen mtreatment = (mobile5 == 1) & (mobile6 == 1)
                  
                  local ylist delta_price
                  local xlist pavedroadskm* population* fuelprice* rainfall*
                  
                  // OR MAYBE YOU MEAN 
                  // local xlist delta_pavedroadskm delta_population delta_fuelprice delta_rainfall
                  
                  reg `ylist' `xlist'
                  FInally, I don't understand how you plan to expand your approach to include 112 markets. For 112 markets there are 112*111/2 = 6,216 market pairs. If you try to generalize this approach to all those pairs you will have far more variables than you can manage (and perhaps more than a data set even allows, depending on your Stata flavor), and even if you somehow coax Stata into doing the analysis, I can't envision how you could possibly go about interpreting the results. So while I endorse the idea of starting out with a simple case of just two markets, once you have that down, you will need to give some very serious thought to how you plan to scale things up to 112 markets. All pairwise comparisons is just not feasible.


                  Comment


                  • #10
                    Thank you for your comments:

                    I want the outcome variable to be price6-price5, but I include the control variables.

                    In DinD I believe my regression would be like this:

                    [Regression for Price at Market 6 when treatment=1 (and so all the control variables are included) - Regression for Price at Market 6 when treatment=0 (all control variables are included) ] - [Regression for Price at Market 5 when treatment=1 (and so all the control variables are included) - Regression for Price at Market 5 when treatment=0 (again all the controls are included ]

                    I have 112 markets but I would like a single value for each country (112 markets in 11 countries)

                    I want the equation to essentially be:

                    Regression for Price at Market i when treatment=1 (and so all the control variables are included) - Regression for Price at Market i when treatment=0 (all control variables are included) ] - [Regression for Price at Market j when treatment=1 (and so all the control variables are included) - Regression for Price at Market j when treatment=0 (again all the controls are included ]

                    This generality would result in the average of every market pair in a country (e.g. an average price dispertion for each country)

                    Apologies the data for rainfall, fuelprice and population is not mine and I've asked my friend about this.

                    I am switching back to my older datasource and have included a dataex for this if this helps

                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input str12 country byte(marketid quarter) int year byte commodityid str8 pricetype float(price rainfall pavedroadskm population fuelprice) byte mobile
                    "Burkina Faso" 7 1 2000 1 "Producer"  50.22  6.67  69.23  80.58 554.71 0
                    "Burkina Faso" 7 2 2000 1 "Producer"  51.08  2.06  69.23   82.4 579.47 0
                    "Burkina Faso" 7 3 2000 1 "Producer"  54.48   .08  69.23   83.4 578.25 0
                    "Burkina Faso" 7 4 2000 1 "Producer"  53.55   6.4  69.23  78.55 580.63 0
                    "Burkina Faso" 7 1 2001 1 "Producer"  51.01 11.71  76.14  81.19 597.99 0
                    "Burkina Faso" 7 2 2001 1 "Producer"  60.11 14.06  76.14   83.9 581.21 0
                    "Burkina Faso" 7 3 2001 1 "Producer"  44.18   .07  76.14  82.34  569.8 0
                    "Burkina Faso" 7 4 2001 1 "Producer"  58.32   .05  76.14  84.21 589.18 0
                    "Burkina Faso" 7 1 2002 1 "Producer"  50.57  2.73  102.1  83.54 558.92 0
                    "Burkina Faso" 7 2 2002 1 "Producer"  62.11 15.56  102.1  85.47  603.9 0
                    "Burkina Faso" 7 3 2002 1 "Producer"  62.34 17.88  102.1  91.04 638.97 0
                    "Burkina Faso" 7 4 2002 1 "Producer"  64.14     2  102.1  85.78 629.56 0
                    "Burkina Faso" 7 1 2003 1 "Producer"  64.48   .52  103.8   88.7 630.62 0
                    "Burkina Faso" 7 2 2003 1 "Producer"  64.26     2  103.8  89.13 621.07 0
                    "Burkina Faso" 7 3 2003 1 "Producer"  60.23  14.7  103.8  89.55 650.64 0
                    "Burkina Faso" 7 4 2003 1 "Producer"  52.15 16.52  103.8   85.4  674.7 0
                    "Burkina Faso" 7 1 2004 1 "Producer"  64.11  7.65 106.36  89.44 655.48 0
                    "Burkina Faso" 7 2 2004 1 "Producer"  62.85   .18 106.36  90.86 660.91 0
                    "Burkina Faso" 7 3 2004 1 "Producer"  62.11  4.32 106.36  94.17 646.79 0
                    "Burkina Faso" 7 4 2004 1 "Producer"  61.27  6.84 106.36     89 657.13 0
                    "Burkina Faso" 7 1 2005 1 "Producer"  60.22 17.66    134  89.22 701.44 0
                    "Burkina Faso" 7 2 2005 1 "Producer"  61.74  1.16    134  93.49 664.19 0
                    "Burkina Faso" 7 3 2005 1 "Producer"  62.22   .27    134   97.8 671.62 0
                    "Burkina Faso" 7 4 2005 1 "Producer"  63.11  1.39    134  96.32 689.17 0
                    "Burkina Faso" 7 1 2006 1 "Producer"  82.07 13.57 147.56  95.85 716.58 0
                    "Burkina Faso" 7 2 2006 1 "Producer"  65.11 17.85 147.56  95.53 679.92 0
                    "Burkina Faso" 7 3 2006 1 "Producer"   80.2  7.11 147.56  93.19  746.9 0
                    "Burkina Faso" 7 4 2006 1 "Producer"  80.25   .05 147.56 101.91 740.44 0
                    "Burkina Faso" 7 1 2007 1 "Producer"  51.83   .21  140.5  97.74 765.21 1
                    "Burkina Faso" 7 2 2007 1 "Producer"  60.17  7.27  140.5  100.5 782.74 1
                    "Burkina Faso" 7 3 2007 1 "Producer"  82.71 19.27  140.5 101.23 840.08 1
                    "Burkina Faso" 7 4 2007 1 "Producer"  61.25 16.13  140.5 101.96  879.5 1
                    "Burkina Faso" 7 1 2008 1 "Producer"  64.11   .01 142.42 102.69 821.12 1
                    "Burkina Faso" 7 2 2008 1 "Producer"  44.42   2.6 142.42 104.49 824.29 1
                    "Burkina Faso" 7 3 2008 1 "Producer"  50.05  4.25 142.42  99.93 753.32 1
                    "Burkina Faso" 7 4 2008 1 "Producer" 102.11 20.29 142.42 100.65 801.46 1
                    "Burkina Faso" 7 1 2009 1 "Producer"  62.34  5.93  153.5 106.77  758.8 1
                    "Burkina Faso" 7 2 2009 1 "Producer"  68.02  1.14  153.5 109.71 741.93 1
                    "Burkina Faso" 7 3 2009 1 "Producer"  52.07   .29  153.5 101.75 763.65 1
                    "Burkina Faso" 7 4 2009 1 "Producer"  55.25  6.94  153.5 105.79 806.12 1
                    "Burkina Faso" 7 1 2010 1 "Producer"  52.55 13.77 150.61 103.23 800.08 1
                    "Burkina Faso" 7 2 2010 1 "Producer"  54.22 15.34 150.61 106.21 862.37 1
                    "Burkina Faso" 7 3 2010 1 "Producer"  54.11   .02 150.61 110.35 910.31 1
                    "Burkina Faso" 7 4 2010 1 "Producer"  53.54   .04 150.61 110.01 885.26 1
                    "Burkina Faso" 7 1 2011 1 "Producer" 107.85   3.1 151.13 111.09 906.51 1
                    "Burkina Faso" 7 2 2011 1 "Producer"  54.72 18.85 151.13  111.8 996.87 1
                    "Burkina Faso" 7 3 2011 1 "Producer"  51.74 22.27 151.13 118.37 967.61 1
                    "Burkina Faso" 7 4 2011 1 "Producer"  55.51  2.72 151.13 115.58 954.17 1
                    "Burkina Faso" 7 1 2012 1 "Producer"  55.14   .07 161.02 111.57 780.93 1
                    "Burkina Faso" 7 2 2012 1 "Producer"  62.22  1.32 161.02 118.25 822.74 1
                    "Burkina Faso" 7 3 2012 1 "Producer"  61.51 11.11 161.02  116.6 781.37 1
                    "Burkina Faso" 7 4 2012 1 "Producer"  62.11 18.31 161.02 117.34 788.57 1
                    "Burkina Faso" 7 1 2013 1 "Producer"  68.31  9.68 169.71  119.3 779.95 1
                    "Burkina Faso" 7 2 2013 1 "Producer" 113.22   .08 169.71 117.61 710.25 1
                    "Burkina Faso" 7 3 2013 1 "Producer"  61.41  3.34 169.71 124.52 734.29 1
                    "Burkina Faso" 7 4 2013 1 "Producer"  60.21  6.28 169.71 120.35 711.89 1
                    "Burkina Faso" 7 1 2014 1 "Producer" 111.03 17.92 168.04 117.64  769.6 1
                    "Burkina Faso" 7 2 2014 1 "Producer"  64.11  1.57 168.04 122.46 785.02 1
                    "Burkina Faso" 7 3 2014 1 "Producer"  102.2   .02 168.04 128.64 777.42 1
                    "Burkina Faso" 7 4 2014 1 "Producer" 111.71  1.51 168.04 123.24 750.75 1
                    "Burkina Faso" 7 1 2015 1 "Producer" 111.88 13.68 200.23  126.9 737.31 1
                    "Burkina Faso" 7 2 2015 1 "Producer"  62.22 20.54 200.23 129.09 733.09 1
                    "Burkina Faso" 7 3 2015 1 "Producer"  62.53  7.48 200.23 126.95 630.05 1
                    "Burkina Faso" 7 4 2015 1 "Producer"     61   .04 200.23 132.93 685.61 1
                    "Burkina Faso" 7 1 2016 1 "Producer"  61.22   .36 213.55 132.15 656.66 1
                    "Burkina Faso" 7 2 2016 1 "Producer"   61.7  6.92 213.55 127.25 650.26 1
                    "Burkina Faso" 7 3 2016 1 "Producer"  60.74 19.87 213.55 134.68 628.65 1
                    "Burkina Faso" 7 4 2016 1 "Producer" 117.42 14.28 213.55 129.74 627.99 1
                    "Burkina Faso" 8 1 2000 1 "Producer"  65.11   2.3  26.94  84.33 537.13 0
                    "Burkina Faso" 8 2 2000 1 "Producer"  67.45   .73  26.94  88.05 560.62 0
                    "Burkina Faso" 8 3 2000 1 "Producer"     67   .03  26.94  89.12 553.79 0
                    "Burkina Faso" 8 4 2000 1 "Producer"  153.3  2.42  26.94  90.19 561.75 0
                    "Burkina Faso" 8 1 2001 1 "Producer" 115.11   4.3  28.41  90.41 555.52 0
                    "Burkina Faso" 8 2 2001 1 "Producer"  47.11  5.01  28.41  90.57 562.31 0
                    "Burkina Faso" 8 3 2001 1 "Producer" 172.22   .02  28.41  85.24 534.44 0
                    "Burkina Faso" 8 4 2001 1 "Producer" 175.22   .02  28.41  87.23 524.65 0
                    "Burkina Faso" 8 1 2002 1 "Producer" 140.25   .94  34.72   87.4 541.21 0
                    "Burkina Faso" 8 2 2002 1 "Producer"    113  5.54  34.72  92.29 578.42 0
                    "Burkina Faso" 8 3 2002 1 "Producer"    152   6.3  34.72  97.28 575.11 0
                    "Burkina Faso" 8 4 2002 1 "Producer" 111.11   .75  34.72  97.51 596.78 0
                    "Burkina Faso" 8 1 2003 1 "Producer" 117.11   .17  42.94  89.95 616.09 0
                    "Burkina Faso" 8 2 2003 1 "Producer" 157.22   .73  42.94  98.15 601.13 0
                    "Burkina Faso" 8 3 2003 1 "Producer"     68  4.97  42.94  91.79 591.33 0
                    "Burkina Faso" 8 4 2003 1 "Producer"    117  5.83  42.94  91.25 645.97 0
                    "Burkina Faso" 8 1 2004 1 "Producer" 143.25  2.72  43.08  96.57 621.35 0
                    "Burkina Faso" 8 2 2004 1 "Producer" 111.14   .07  43.08  99.09 659.82 0
                    "Burkina Faso" 8 3 2004 1 "Producer"  65.21  1.51  43.08  99.62 626.29 0
                    "Burkina Faso" 8 4 2004 1 "Producer" 142.23  2.51  43.08  99.15 629.61 0
                    "Burkina Faso" 8 1 2005 1 "Producer" 111.23  6.76  55.57 103.54 678.48 0
                    "Burkina Faso" 8 2 2005 1 "Producer" 147.22    .4  55.57  96.82 622.91 0
                    "Burkina Faso" 8 3 2005 1 "Producer"   82.2   .09  55.57   98.3 643.49 0
                    "Burkina Faso" 8 4 2005 1 "Producer" 152.11   .46  55.57  96.69 660.09 0
                    "Burkina Faso" 8 1 2006 1 "Producer"  67.42  4.84  56.25 103.49 652.36 0
                    "Burkina Faso" 8 2 2006 1 "Producer" 154.11  6.62  56.25  107.4 693.02 0
                    "Burkina Faso" 8 3 2006 1 "Producer" 110.15  2.59  56.25 102.79 679.11 0
                    "Burkina Faso" 8 4 2006 1 "Producer" 151.22   .02  56.25 105.66  680.4 0
                    "Burkina Faso" 8 1 2007 1 "Producer"    172   .07  54.12 105.54 703.16 1
                    "Burkina Faso" 8 2 2007 1 "Producer" 153.22  2.51  54.12  105.2 749.63 1
                    "Burkina Faso" 8 3 2007 1 "Producer"  72.12  6.45  54.12 102.65 796.34 1
                    "Burkina Faso" 8 4 2007 1 "Producer" 135.11   5.4  54.12 103.39 800.36 1
                    end
                    Apologies as my previous post had a mistake with the dataex output
                    Last edited by Mohammed Islam; 27 Mar 2018, 17:50.

                    Comment


                    • #11
                      Well, with all 112 markets anything you do will be complicated, and if you want to pair every market with every other one, it's going to be huge. But why don't you first finish up what your doing with just markets 5 and 6 (or some other single pair) and get that nailed down. For example, you say you want to include covariates, but you don't say what those covariates are supposed to be. Why don't you just finish perfecting what you want for a single pair of markets and then after that we can try to puzzle a way to expand it to include more markets.

                      Comment


                      • #12
                        Thank you for your comments

                        I agree with your comments:

                        The standard DinD regression is:

                        Y= β0 + β1*[Time] + β2*[Intervention] + β3*[Time*Intervention] + β4*[Covariates]+ε

                        Applying this my regression should be:

                        Pricemarket i -Pricemarket j= β0 + β1*[qdate] + β2*[mobile] + β3*[qdate*mobile] + β4*[Paved Roads] + β5*[Population] + β6*[Fuel Price] + β4*[Rainfall]

                        I ran the following code:

                        Code:
                        set more off
                        
                        *Setting up qdate
                        gen qdate = quarterly(string(quarter)+"q"+string(year), "QY")
                        format qdate %tq
                        
                        *Setting up panel variable
                        egen panel_price = group(commodityid pricetype)
                        
                        *reshape from long to wide for regress
                        reshape wide price pavedroadskm population fuelprice rainfall, i(commodity country pricetype mobile qdate) j(marketid)
                        
                        *market price differences
                        foreach v in price {
                        gen delta_`v' = `v'2 - `v'1
                        }
                        
                        gen mtreatment1=0
                        gen mtreatment2=0
                        replace mtreatment1=1 if rainfall1==1.28 & rainfall2==1.64
                        replace mtreatment2=1 if rainfall3==2.03 & rainfall4==1.23
                        
                        
                        local ylist delta_price
                        local xlist pavedroadskm* population* fuelprice* rainfall*
                        reg $ylist $mtreatment1 $xlist
                        reg $ylist $mtreatment2 $xlist
                        I have attached a copy of the dataex output but I do not think the above code corresponds to my regression aims

                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input str5 country byte(marketid quarter) int year byte commodityid str8 pricetype float(price rainfall pavedroadskm population fuelprice) byte mobile
                        "Benin" 1 1 2000 1 "Producer"    110   .83 24.57  82.88  350.2 0
                        "Benin" 1 2 2000 1 "Producer"    130  9.27 24.57  83.04 356.11 0
                        "Benin" 1 3 2000 1 "Producer"    115 17.02 24.57  83.21 339.07 0
                        "Benin" 1 4 2000 1 "Producer"    110  2.72 24.57  83.37 345.76 0
                        "Benin" 1 1 2001 1 "Producer" 131.81   .44 25.67  85.39 365.39 0
                        "Benin" 1 2 2001 1 "Producer"    135  8.88 25.67  85.54 344.23 0
                        "Benin" 1 3 2001 1 "Producer"    170 15.92 25.67  85.69 349.84 0
                        "Benin" 1 4 2001 1 "Producer" 172.22  1.19 25.67  85.85 367.62 0
                        "Benin" 1 1 2002 1 "Producer"    175   1.3 33.75  88.01 350.29 0
                        "Benin" 1 2 2002 1 "Producer"    175  9.72 33.75   89.1 367.51 0
                        "Benin" 1 3 2002 1 "Producer"    110 17.74 33.75   90.2 333.14 0
                        "Benin" 1 4 2002 1 "Producer"    115   3.8 33.75  91.32 338.59 0
                        "Benin" 1 1 2003 1 "Producer"    115  1.28 34.62  90.73 353.92 1
                        "Benin" 1 2 2003 1 "Producer" 111.11 12.09 34.62  91.16 344.86 1
                        "Benin" 1 3 2003 1 "Producer"    135 21.25 34.62   91.6 357.63 1
                        "Benin" 1 4 2003 1 "Producer" 131.22  3.53 34.62  92.05 390.82 1
                        "Benin" 1 1 2004 1 "Producer" 142.19   .78 37.73  93.53  394.6 1
                        "Benin" 1 2 2004 1 "Producer"    175 11.49 37.73  94.03 412.97 1
                        "Benin" 1 3 2004 1 "Producer" 152.22 17.89 37.73  94.53 405.85 1
                        "Benin" 1 4 2004 1 "Producer" 142.22   2.6 37.73  95.04 420.59 1
                        "Benin" 1 1 2005 1 "Producer"    135  1.95 49.15  96.38 387.76 1
                        "Benin" 1 2 2005 1 "Producer"    115  9.16 49.15  96.83 379.35 1
                        "Benin" 1 3 2005 1 "Producer" 141.11 16.67 49.15  97.28 423.12 1
                        "Benin" 1 4 2005 1 "Producer" 111.11   2.5 49.15  97.74 423.43 1
                        "Benin" 1 1 2006 1 "Producer" 138.11  1.09 48.26  99.27 445.56 1
                        "Benin" 1 2 2006 1 "Producer" 211.22  8.83 48.26  99.96 434.32 1
                        "Benin" 1 3 2006 1 "Producer" 152.22 17.47 48.26 100.66 445.46 1
                        "Benin" 1 4 2006 1 "Producer"    150  3.42 48.26 101.36 467.36 1
                        "Benin" 1 1 2007 1 "Producer"    155   .86 50.36 102.19 473.43 1
                        "Benin" 1 2 2007 1 "Producer" 111.11 12.25 50.36 102.92 474.44 1
                        "Benin" 1 3 2007 1 "Producer"    145  19.5 50.36 103.67 495.97 1
                        "Benin" 1 4 2007 1 "Producer" 115.89  2.83 50.36 104.42 524.99 1
                        "Benin" 1 1 2008 1 "Producer" 111.11  1.16 47.02 105.17  583.8 1
                        "Benin" 1 2 2008 1 "Producer" 141.11 11.98 47.02 105.93 523.17 1
                        "Benin" 1 3 2008 1 "Producer"    175 22.17 47.02 106.69 551.99 1
                        "Benin" 1 4 2008 1 "Producer" 132.22  3.14 47.02 107.46 536.53 1
                        "Benin" 1 1 2009 1 "Producer"    175  1.07 49.76 108.24 527.82 1
                        "Benin" 1 2 2009 1 "Producer" 141.11 10.03 49.76 109.02 486.94 1
                        "Benin" 1 3 2009 1 "Producer" 138.11 18.24 49.76 109.81 475.26 1
                        "Benin" 1 4 2009 1 "Producer" 188.11  4.31 49.76  110.6 486.46 1
                        "Benin" 1 1 2010 1 "Producer" 111.11  1.27 50.28  111.4 529.63 1
                        "Benin" 1 2 2010 1 "Producer" 142.22  8.65 50.28  112.2 547.79 1
                        "Benin" 1 3 2010 1 "Producer" 111.22 17.18 50.28 113.01 532.89 1
                        "Benin" 1 4 2010 1 "Producer"    175  3.73 50.28 113.83 533.27 1
                        "Benin" 1 1 2011 1 "Producer" 110.28  1.34 46.96 114.65 582.18 1
                        "Benin" 1 2 2011 1 "Producer"    155  6.86 46.96 115.38 564.21 1
                        "Benin" 1 3 2011 1 "Producer" 118.11 13.28 46.96 116.11 635.76 1
                        "Benin" 1 4 2011 1 "Producer"    150  3.28 46.96 116.85 628.95 1
                        "Benin" 1 1 2012 1 "Producer" 113.42    .7 55.98 117.59 578.51 1
                        "Benin" 1 2 2012 1 "Producer"    180  8.84 55.98 118.34  635.1 1
                        "Benin" 1 3 2012 1 "Producer" 152.22 12.12 55.98 119.09 548.39 1
                        "Benin" 1 4 2012 1 "Producer"    175  2.51 55.98 119.85 542.61 1
                        "Benin" 1 1 2013 1 "Producer" 108.11  1.48 58.97 120.61 602.15 1
                        "Benin" 1 2 2013 1 "Producer" 131.22 11.53 58.97 121.37 566.49 1
                        "Benin" 1 3 2013 1 "Producer" 131.22 13.38 58.97 122.15 555.43 1
                        "Benin" 1 4 2013 1 "Producer"    180  3.36 58.97 122.92 572.08 1
                        "Benin" 1 1 2014 1 "Producer" 188.11  1.21 57.82  123.7 543.87 1
                        "Benin" 1 2 2014 1 "Producer" 111.11 10.26 57.82 124.75 482.94 1
                        "Benin" 1 3 2014 1 "Producer" 132.22 18.03 57.82  125.8 464.18 1
                        "Benin" 1 4 2014 1 "Producer" 201.11  3.16 57.82 126.86 461.86 1
                        "Benin" 1 1 2015 1 "Producer" 131.22  2.47 59.23 127.94 498.73 1
                        "Benin" 1 2 2015 1 "Producer" 171.22  6.94 59.23 129.02 436.84 1
                        "Benin" 1 3 2015 1 "Producer" 131.11 15.59 59.23 129.58 409.63 1
                        "Benin" 1 4 2015 1 "Producer" 151.11  3.73 59.23 130.14 458.49 1
                        "Benin" 1 1 2016 1 "Producer" 132.22  3.42 61.07 130.71 457.25 1
                        "Benin" 1 2 2016 1 "Producer" 171.22  8.34 61.07 131.28 454.59 1
                        "Benin" 1 3 2016 1 "Producer" 208.08 18.44 61.07 131.86 442.81 1
                        "Benin" 1 4 2016 1 "Producer"    205  3.92 61.07 132.43 390.59 1
                        "Benin" 2 1 2000 1 "Producer" 141.22  1.78 43.85  54.87 401.41 0
                        "Benin" 2 2 2000 1 "Producer" 142.22 15.56 43.85  47.41 423.61 0
                        "Benin" 2 3 2000 1 "Producer"    143 28.93 43.85  44.47 357.49 0
                        "Benin" 2 4 2000 1 "Producer" 201.22   4.9 43.85  41.52 388.47 0
                        "Benin" 2 1 2001 1 "Producer"    145   .79 49.27  47.71 335.02 0
                        "Benin" 2 2 2001 1 "Producer" 155.54  18.4 49.27  51.95 421.91 0
                        "Benin" 2 3 2001 1 "Producer"    171 35.64 49.27  54.65 357.81 0
                        "Benin" 2 4 2001 1 "Producer" 172.22  2.38 49.27  47.45 360.86 0
                        "Benin" 2 1 2002 1 "Producer"    130  2.37 64.12  49.71 393.19 0
                        "Benin" 2 2 2002 1 "Producer" 131.22 18.94 64.12     46 380.96 0
                        "Benin" 2 3 2002 1 "Producer"    155 38.62 64.12  59.17 418.79 0
                        "Benin" 2 4 2002 1 "Producer" 142.22  7.09 64.12  57.68 376.99 0
                        "Benin" 2 1 2003 1 "Producer" 145.82  2.84 67.17  52.35  426.6 0
                        "Benin" 2 2 2003 1 "Producer"    170 27.56 67.17  60.91 391.88 0
                        "Benin" 2 3 2003 1 "Producer"    210 39.22 67.17  53.97 339.89 0
                        "Benin" 2 4 2003 1 "Producer" 208.11  5.85 67.17  48.08 356.97 0
                        "Benin" 2 1 2004 1 "Producer" 141.22  1.58 71.69  51.12 355.33 0
                        "Benin" 2 2 2004 1 "Producer" 152.22 23.12 71.69  57.68 414.98 0
                        "Benin" 2 3 2004 1 "Producer"    155 40.42 71.69  45.93 380.26 0
                        "Benin" 2 4 2004 1 "Producer"    215  5.33 71.69  51.37 390.84 0
                        "Benin" 2 1 2005 1 "Producer"    170  3.68 85.75  49.17    418 0
                        "Benin" 2 2 2005 1 "Producer" 152.22 18.04 85.75  57.64 462.71 0
                        "Benin" 2 3 2005 1 "Producer"    145 28.68 85.75   58.5 502.06 0
                        "Benin" 2 4 2005 1 "Producer"    145  5.39 85.75  63.52  406.5 0
                        "Benin" 2 1 2006 1 "Producer" 231.11  2.29 93.57  57.88 384.49 0
                        "Benin" 2 2 2006 1 "Producer"    175  19.4 93.57  61.93 531.72 0
                        "Benin" 2 3 2006 1 "Producer"    205 30.43 93.57  50.74  516.3 0
                        "Benin" 2 4 2006 1 "Producer" 131.22  7.23 93.57  49.86 423.47 0
                        "Benin" 2 1 2007 1 "Producer" 142.22  1.64 87.08  65.79 552.26 1
                        "Benin" 2 2 2007 1 "Producer" 241.22 23.62 87.08  64.39 516.89 1
                        "Benin" 2 3 2007 1 "Producer"    180 34.36 87.08  54.78 505.86 1
                        "Benin" 2 4 2007 1 "Producer" 201.22  5.16 87.08  54.54 625.58 1
                        end
                        Last edited by Mohammed Islam; 28 Mar 2018, 08:41.

                        Comment


                        • #13
                          Well, your -reg- statements are syntactically incorrect. You have followed my advice to declare ylist and xlist as local macros rather than globals. But you failed to change the references to them in the -regression- commands to correspond to that. Instead of $ylist, it has to be `ylist', etc.

                          The code also does not run with the example data because the example data has only 2 markets, but in your variable mtreatment2, you refer to rainfall3 and rainfall4, which never get created. If you have four actual markets in your data then the code will run.

                          Also your definitions of mtreatment will fail. I can almost guarantee you that mtreatment1 and mtreatment2 will always be zero. Conditioning on exact equalty of floating point numbers is a recipe for failure. There is no number in finite precision binary notation that is equal to 1.28. When you give Stata 1.28 as an input in a data set, Stata finds the closest approximation it can. This just like there is no finite decimal number equal to 1/3. Any computer or calculator will, when asked to create 1/3 will create 0.3333... up to some point, and then leave it there. If you then try to test that number for equality with 1/3, depending on how the rounding/truncation is done, it will be found unequal. So whatever it is you are trying to capture with these conditions, you have to find some other way to do it. I'm not going to advise you on how to proceed because I really don't grasp what the intent behind the conditions is.

                          That said, I think you have not clarified in your own head how you want to handle the covariates pavaedroadskm, population fuelprice and rainfall. Based on what you have written, I'm guessing that your planning to use all their values in every market, but that has two problems. First, you will be loading up your data set with hundreds of variables in your regression, and second, it doesn't make sense as far as I can see. Do you have reason to believe that rainfall in market 100 will influence the price differences between markets 1 and 2? Since your outcome variable (at least for now) is just the difference between price in markets 1 and 2, do you really want to include covariates that refer to other markets? Why? Similarly, your variable mtreatment2, referring as it does to rainfall in markets 3 and 4, strikes me as irrelevant to a regression of the difference in prices between markets 1 and 2.



                          Comment


                          • #14
                            Thank you for your comments:

                            My intent with mtreatment is that the mtreatment dummy should equal 1 when both mobile1 and mobile2=1 otherwise 0. Further mtreatment2=1 when mobile3 and mobile4=1 otherwise 0 but I kept running into problems with the coding.
                            Ideally I would like the dummy to take the value one when mobilex=1 and mobilex+1=1 but my code when I tried to use w for generality did not work

                            My data source is formatted to each market is paired with its closed neighbouring market. As such, market 1 and market 2 are a market pair and market 3 and market 4 are a market pair. I will seperate the markets by country and so market 100 should not have an impact on market 1 (if that makes sense). I used rainfall to code the mobile1 and mobile2 as a workaround as I could not get the code to work when mobilex=1 and mobilex+1=1, but I realise I run into the same issue of a lack of generality (and as you have pointed out the lack of specificity)

                            Comment


                            • #15
                              I think I am beginning to grasp what you want to do here. So your 112 markets are actually 66 market pairs. Each pair consists of a market with an odd number like 1, 3, 5,... and it is paired with the marked numbered 1 higher than that. What you really want to do is calculate the difference in price between each market and its pair-mate as an outcome variable and regress that on the values of the covariates rainfall, etc. in those two markets. And your "treatment" variable is an indicator for whether or not mobile is 1 in both markets of the pair. If that's correct, then I think you want to do something like this:

                              Code:
                              gen quarterly_date = yq(year, quarter)
                              format quarterly_date %tq
                              
                              by country commodityid pricetype quarterly_date, sort: gen int marketpair = ceil(marketid/2)
                              gen byte marketindex = mod(marketid, 2)
                              
                              reshape wide marketid price rainfall pavedroadskm population fuelprice mobile, ///
                                  i(country marketpair commodityid pricetype quarterly_date) j(marketindex)
                              
                              foreach v in price rainfall pavedroadskm population fuelprice {
                                  gen delta_`v' = `v'1 - `v'0
                              }
                              gen byte treatment =(mobile1 == 1 & mobile0 == 1)
                              
                              regress delta_price i.treatment rainfall* pavedroadskm* population* fuelprice*
                              This is not fully tested in your example data because it offers only 2 markets, and it also does not have complete data for all quarters in both markets. But it is syntax-error free and I think it has the concept right.

                              The logic of it is to identify each pair of markets with a marketpair variable. Then within each market pair we identify the odd and even numbered market with a variable that, for lack of a better name, I called marketindex, 0 for the even numbered one, and 1 for the odd. Now we -reshape- the data wide with marketindex as the j() variable so that each observation now consists of the data relevant to that pair of markets only (in the same country, same quarter, same commodity, and same pricetype).

                              Comment

                              Working...
                              X