Announcement

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

  • Paneldata with fe

    Dear Statalists,

    I'm using a panel dataset on U.S. imports to the EU including tariff changes for some products on a monthly basis. My goal is to estimate the impact of the imposition of the imposed import tariffs. When running a simple OLS I get estimates that is in line with what to expect: the trade has somewhat declined. But as I use fe and clustered standard errors on product id or for that matter robust standard errors the estimates come out positive. Looking at my data, is there anything that seems to be wrong, or should I change my code in any way? Or is everything in order and the estimates are just a surprise?

    Code:
    drop if product=="TOTAL"
    
    gen int year = 2000 + real(substr(y_m, 1, 2))
    gen int month = real(substr(y_m, 3, 2))
    gen mdate = ym(year, month)
    format mdate %tm
    
    egen product_id=group(product)
    
    gen unit_price = value_in_euros / quantity_in_100kg
    gen ln_unitp = ln(unit_price)
    
    duplicates tag product_ mdate, gen(flag)
    order product_ mdate, first
    *brows if flag
    duplicates drop if flag==1
    
    duplicates tag product_id, gen (flag_i)
    *brows if flag
    duplicates drop if flag_i==74
    
    
    sort product_id mdate
    xtset product_id mdate
    
    replace tariff=0 if tariff==1
    replace tariff=0.25 if tariff==1.25
    
    gen rel_tariff = (1+tariff)/(1+L12.tariff)
    gen ln_tariff = ln(rel_tariff)
    
    gen rel_unitp = unit_price/L12.unit_price
    gen ln_rel_unitp = ln(rel_unitp)
    
    gen rel_q_change=quantity_in_100kg/L12.quantity_in_100kg
    gen ln_q_change=ln(rel_q_change)
    
    gen hype_q=asinh(ln_q_change)
    
    gen rel_imp_change=value_in_euros/L12.value_in_euros
    gen ln_rel_imp = ln(rel_imp_change)
    
    gen hype_imp=asinh(ln_rel_imp)
    and the regressions I'm running are following

    Code:
    xtreg ln_q_change ln_tariff
    
    xtreg ln_q_change ln_tariff, fe vce(cluster product_id)
    
    xtreg hype_q ln_tariff
    
    xtreg ln_q_change ln_tariff, fe vce(cluster product_id)
    
    xtreg ln_rel_imp ln_tariff
    
    xtreg ln_rel_imp ln_tariff, fe vce(cluster product_id)

    My data looks like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(product_id mdate) str8 product long quantity_in_100kg double value_in_euros str4 y_m float(dummy tariff) int(year month) float(unit_price ln_unitp) byte(flag flag_i)
    1246 689 "07104000"     653   110001 "1706" 0   0 2017  6  168.4548  5.126667 0 24
    1246 690 "07104000"       .        . "1707" 0   0 2017  7         .         . 0 24
    1246 691 "07104000"       .        . "1708" 0   0 2017  8         .         . 0 24
    1246 692 "07104000"       .        . "1709" 0   0 2017  9         .         . 0 24
    1246 693 "07104000"      89    10774 "1710" 0   0 2017 10 121.05618  4.796255 0 24
    1246 694 "07104000"       .        . "1711" 0   0 2017 11         .         . 0 24
    1246 695 "07104000"       0       26 "1712" 0   0 2017 12         .         . 0 24
    1246 696 "07104000"       .        . "1801" 0   0 2018  1         .         . 0 24
    1246 697 "07104000"      76    12480 "1802" 0   0 2018  2 164.21053  5.101149 0 24
    1246 698 "07104000"       .        . "1803" 0   0 2018  3         .         . 0 24
    1246 699 "07104000"       .        . "1804" 0   0 2018  4         .         . 0 24
    1246 700 "07104000"     102    13772 "1805" 0   0 2018  5  135.0196   4.90542 0 24
    1246 701 "07104000"       .        . "1806" 1 .25 2018  6         .         . 0 24
    1246 702 "07104000"       .        . "1807" 1 .25 2018  7         .         . 0 24
    1246 703 "07104000"       1      146 "1808" 1 .25 2018  8       146  4.983607 0 24
    1246 704 "07104000"       .        . "1809" 1 .25 2018  9         .         . 0 24
    1246 705 "07104000"      82    11888 "1810" 1 .25 2018 10 144.97562  4.976565 0 24
    1246 706 "07104000"       0       60 "1811" 1 .25 2018 11         .         . 0 24
    1246 707 "07104000"       .        . "1812" 1 .25 2018 12         .         . 0 24
    1246 708 "07104000"       .        . "1901" 1 .25 2019  1         .         . 0 24
    1246 709 "07104000"      10     1498 "1902" 1 .25 2019  2     149.8  5.009301 0 24
    1246 710 "07104000"       .        . "1903" 1 .25 2019  3         .         . 0 24
    1246 711 "07104000"       .        . "1904" 1 .25 2019  4         .         . 0 24
    1246 712 "07104000"       .        . "1905" 1 .25 2019  5         .         . 0 24
    1246 713 "07104000"       .        . "1906" 1 .25 2019  6         .         . 0 24
    1267 689 "07119030"       .        . "1706" 0   0 2017  6         .         . 0 24
    1267 690 "07119030"       .        . "1707" 0   0 2017  7         .         . 0 24
    1267 691 "07119030"       .        . "1708" 0   0 2017  8         .         . 0 24
    1267 692 "07119030"       .        . "1709" 0   0 2017  9         .         . 0 24
    1267 693 "07119030"       .        . "1710" 0   0 2017 10         .         . 0 24
    1267 694 "07119030"       .        . "1711" 0   0 2017 11         .         . 0 24
    1267 695 "07119030"       .        . "1712" 0   0 2017 12         .         . 0 24
    1267 696 "07119030"       .        . "1801" 0   0 2018  1         .         . 0 24
    1267 697 "07119030"       .        . "1802" 0   0 2018  2         .         . 0 24
    1267 698 "07119030"       .        . "1803" 0   0 2018  3         .         . 0 24
    1267 699 "07119030"       .        . "1804" 0   0 2018  4         .         . 0 24
    1267 700 "07119030"       .        . "1805" 0   0 2018  5         .         . 0 24
    1267 701 "07119030"       .        . "1806" 1 .25 2018  6         .         . 0 24
    1267 702 "07119030"       .        . "1807" 1 .25 2018  7         .         . 0 24
    1267 703 "07119030"       .        . "1808" 1 .25 2018  8         .         . 0 24
    1267 704 "07119030"       .        . "1809" 1 .25 2018  9         .         . 0 24
    1267 705 "07119030"       .        . "1810" 1 .25 2018 10         .         . 0 24
    1267 706 "07119030"       0      130 "1811" 1 .25 2018 11         .         . 0 24
    1267 707 "07119030"       .        . "1812" 1 .25 2018 12         .         . 0 24
    1267 708 "07119030"       .        . "1901" 1 .25 2019  1         .         . 0 24
    1267 709 "07119030"       .        . "1902" 1 .25 2019  2         .         . 0 24
    1267 710 "07119030"       .        . "1903" 1 .25 2019  3         .         . 0 24
    1267 711 "07119030"       .        . "1904" 1 .25 2019  4         .         . 0 24
    1267 712 "07119030"       .        . "1905" 1 .25 2019  5         .         . 0 24
    1267 713 "07119030"       .        . "1906" 1 .25 2019  6         .         . 0 24
    1301 689 "07133390"   40939  3812573 "1706" 0   0 2017  6  93.12814 4.5339766 0 24
    1301 690 "07133390"   30486  2858774 "1707" 0   0 2017  7  93.77334 4.5408807 0 24
    1301 691 "07133390"   27367  2457222 "1708" 0   0 2017  8  89.78777  4.497449 0 24
    1301 692 "07133390"   48725  4444359 "1709" 0   0 2017  9  91.21311  4.513199 0 24
    1301 693 "07133390"   54353  4588546 "1710" 0   0 2017 10  84.42121 4.4358187 0 24
    1301 694 "07133390"  105210  6786271 "1711" 0   0 2017 11  64.50215 4.1666985 0 24
    1301 695 "07133390"   52642  4749053 "1712" 0   0 2017 12  90.21414 4.5021863 0 24
    1301 696 "07133390"   72234  6609886 "1801" 0   0 2018  1  91.50658  4.516411 0 24
    1301 697 "07133390"   63467  5347929 "1802" 0   0 2018  2  84.26315 4.4339447 0 24
    1301 698 "07133390"   64492  5306220 "1803" 0   0 2018  3  82.27718  4.410094 0 24
    1301 699 "07133390"   67982  5936656 "1804" 0   0 2018  4  87.32688 4.4696584 0 24
    1301 700 "07133390"   61969  5365171 "1805" 0   0 2018  5  86.57831  4.461049 0 24
    1301 701 "07133390"   62162  5458596 "1806" 1 .25 2018  6  87.81242  4.475203 0 24
    1301 702 "07133390"   45170  4123540 "1807" 1 .25 2018  7  91.28935 4.5140343 0 24
    1301 703 "07133390"   23866  1812959 "1808" 1 .25 2018  8  75.96409 4.3302608 0 24
    1301 704 "07133390"   24994  2038452 "1809" 1 .25 2018  9  81.55766   4.40131 0 24
    1301 705 "07133390"   62791  4976372 "1810" 1 .25 2018 10  79.25295  4.372645 0 24
    1301 706 "07133390"   90471  7199502 "1811" 1 .25 2018 11  79.57801 4.3767376 0 24
    1301 707 "07133390"   54453  4053705 "1812" 1 .25 2018 12  74.44411 4.3100486 0 24
    1301 708 "07133390"   82713  6412751 "1901" 1 .25 2019  1  77.53014  4.350667 0 24
    1301 709 "07133390"   77943  6772119 "1902" 1 .25 2019  2  86.88553 4.4645915 0 24
    1301 710 "07133390"   53230  4653710 "1903" 1 .25 2019  3  87.42645  4.470798 0 24
    1301 711 "07133390"   83379  6794817 "1904" 1 .25 2019  4  81.49315  4.400519 0 24
    1301 712 "07133390"  109569  8874859 "1905" 1 .25 2019  5   80.9979 4.3944235 0 24
    1301 713 "07133390"   48854  4321658 "1906" 1 .25 2019  6  88.46068 4.4825583 0 24
    1843 689 "10059000" 2266165 19499452 "1706" 0   0 2017  6  8.604604 2.1522975 0 24
    1843 690 "10059000" 2579244 41402934 "1707" 0   0 2017  7 16.052353 2.7758555 0 24
    1843 691 "10059000"  901749 15340544 "1708" 0   0 2017  8  17.01199  2.833918 0 24
    1843 692 "10059000"  135142  3023051 "1709" 0   0 2017  9  22.36944  3.107696 0 24
    1843 693 "10059000"   21738  1331833 "1710" 0   0 2017 10  61.26751 4.1152496 0 24
    1843 694 "10059000"   24329  1477711 "1711" 0   0 2017 11  60.73867 4.1065807 0 24
    1843 695 "10059000"  166379  3099280 "1712" 0   0 2017 12 18.627832  2.924657 0 24
    1843 696 "10059000"   19670  2477103 "1801" 0   0 2018  1 125.93304  4.835751 0 24
    1843 697 "10059000"   16520   936627 "1802" 0   0 2018  2  56.69655 4.0377135 0 24
    1843 698 "10059000" 1765248 27855530 "1803" 0   0 2018  3 15.779953 2.7587404 0 24
    1843 699 "10059000" 4865547 75914840 "1804" 0   0 2018  4  15.60253  2.747433 0 24
    1843 700 "10059000" 4203415 67323920 "1805" 0   0 2018  5 16.016481  2.773618 0 24
    1843 701 "10059000" 4374235 71173872 "1806" 1 .25 2018  6 16.271158  2.789394 0 24
    1843 702 "10059000" 2609495 42242205 "1807" 1 .25 2018  7 16.187885  2.784263 0 24
    1843 703 "10059000"   55603  1371415 "1808" 1 .25 2018  8 24.664406  3.205361 0 24
    1843 704 "10059000"   13878   822450 "1809" 1 .25 2018  9  59.26286  4.081983 0 24
    1843 705 "10059000"   15445   943664 "1810" 1 .25 2018 10  61.09835  4.112485 0 24
    1843 706 "10059000"   12396   794560 "1811" 1 .25 2018 11   64.0981 4.1604147 0 24
    1843 707 "10059000"   14674   909117 "1812" 1 .25 2018 12  61.95427 4.1263967 0 24
    1843 708 "10059000"   15801  1019784 "1901" 1 .25 2019  1  64.53921  4.167273 0 24
    1843 709 "10059000"   11717   668788 "1902" 1 .25 2019  2  57.07843 4.0444264 0 24
    1843 710 "10059000"   18480  1068539 "1903" 1 .25 2019  3  57.82137 4.0573583 0 24
    1843 711 "10059000"   14865   882244 "1904" 1 .25 2019  4  59.35042 4.0834594 0 24
    1843 712 "10059000"   19472  1158815 "1905" 1 .25 2019  5  59.51186  4.086176 0 24
    1843 713 "10059000"   10602   651008 "1906" 1 .25 2019  6  61.40426 4.1174793 0 24
    end
    format %tm mdate

    I'm aware that this dataex is not giving negative coefficients in the OLS, but I would appreciate if anyone can find any concerns about my data and the regressions.

    Many thanks in advance

    /Sofie

  • #2
    I haven't looked over the code you show in depth, but it appears correct at a quick reading.

    In your description you refer to OLS regresions, but you don't show any in your code. When you use -xtreg- without the -fe- option, you are running random effects regressions, not OLS.

    As for the results being a surprise, surprise means deviating from expectations. I can't say what expectations are sensible from an economics perspective--way out of my domain. But from a statistical perspective, there is no reason to expect that the sign of a coefficient in a fixed effects regression will match the sign of the coefficient in a corresponding random effects (or OLS) regression. Fixed-effects regressions estimate within-panel effects. Random-effects and OLS regressions are based on the assumption that the within- and between-panel effects are the same and provide a single estimate of that common parameter--which is usually dominated by between-panel effects. The within and between panel effects can be quite different from each other. The following code creates a very simple, toy data set and shows the fixed- and random-effects analyses resulting in opposite signs of the x coefficient. The graph makes it obvious what is going on.

    Code:
    clear
    set obs 5
    gen panel_id = _n
    expand 2
    
    set seed 1234
    by panel_id , sort: gen y = 4*panel_id - _n + 3 + rnormal(0, 0.5)
    by panel_id: gen x = panel_id + _n
    
    xtset panel_id 
    
    xtreg y x, fe
    regress y x
    
    //    GRAPH THE DATA TO SHOW WHAT'S HAPPENING
    separate y, by(panel_id)
    
    graph twoway connect y? x || lfit y x

    Comment

    Working...
    X