Announcement

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

  • Loop with a synthetic control method

    Hi everyone,

    I have a dataset at the state level with 39 states and I want to do a synthetic control regression (as shown) only for the states 4,5,21,34 by using the command levelsof ..., local() in a loop . Note that at the end of the regression I have to save the results in dataset "ps" that contains the number of the state. In this example if I run the regression for the state 4 I will save it in the dataset ps4 but I have to save also ps5, ps21 and ps34 in a loop.

    Many thanks for your help!

    Code:
    tsset state year
    synth cigsale beer  lnincome(1980&1985) age15to24  retprice  cigsale(1988) cigsale(1980) cigsale(1975), trunit(3) trperiod(1989) fig keep(ps4, replace)


    Code:
    state year cigsale lnincome beer retprice age15to24
    29 1970 123.9        . . 39.3  .1831579
    32 1970  99.8        . . 39.9  .1780438
    10 1970 134.6        . . 30.6 .17651588
    21 1970 189.5        . . 38.9  .1615542
    14 1970 115.9        . . 34.3  .1851852
    27 1970 108.4        . . 38.4 .17545916
    22 1970 265.7        . . 31.4  .1707317
    25 1970  93.8        . . 37.3   .184466
     2 1970 100.3        . . 36.7 .16900676
    36 1970 124.3        . . 28.8 .18942162
     9 1970 124.8        . . 41.4  .1669667
    31 1970  92.7        . . 38.5 .17867868
    34 1970  65.5        . . 34.6 .20207743
     7 1970 109.9        . . 34.3  .1874455
    17 1970  93.4        . . 36.2 .18313035
     4 1970 124.8        . . 29.4 .19095023
    16 1970 104.3        . . 39.1  .1747241
     3 1970   123        . . 38.8 .17815833
    33 1970 106.4        . . 40.4 .18314135
    13 1970 155.8        . . 28.3 .18131015
    15 1970 128.5        . .   38  .1690141
    24 1970 172.4        . . 27.3  .1935484
    19 1970 111.2        . .   34  .1757925
    35 1970 122.6        . . 37.7  .1797753
    11 1970 108.5        . . 37.7 .16884956
     5 1970   120        . . 42.2 .16292876
    12 1970   114        . . 34.2 .18052468
     6 1970   155        . .   39 .17335767
    38 1970 106.4        . . 38.5   .174287
     8 1970 102.4        . . 33.8  .1781206
    23 1970    90        . . 39.7 .18485743
    37 1970 114.5        . . 33.7 .17259175
    28 1970 107.3        . . 38.4   .163376
    30 1970 103.6        . . 32.5 .20030876
    26 1970 121.6        . . 36.6  .1732195
    20 1970 108.1        . . 33.9 .17373738
    18 1970 121.3        . .   36   .167593
     1 1970  89.8        . . 39.6  .1788618
    39 1970 132.2        . . 34.1  .1746988
     9 1971 125.6        . . 41.4  .1689976
    22 1971   278        . . 34.1  .1723339
    31 1971  96.7        . . 38.5 .18049243
    13 1971 163.5        . . 30.1  .1822996
    39 1971 131.7        . . 34.4 .17722893
    19 1971 115.6        . . 34.7  .1771459
    17 1971 105.4        . . 37.5 .18437305
    12 1971 102.8        . . 38.9 .18155004
     6 1971 161.1        . . 41.3  .1758872
    34 1971  67.7        . . 36.6 .20206134
    27 1971 115.4        . . 39.8  .1765248
    15 1971 133.2        . . 38.8  .1703349
     7 1971 115.7        . . 35.8 .18786626
    36 1971 128.4        . . 30.2  .1898735
    18 1971 127.6        . . 36.8 .16925956
     1 1971  95.4        . . 42.7 .17992784
    25 1971  98.5        . . 38.9 .18638696
    24 1971 187.6        . . 29.4  .1936767
     8 1971 108.5        . . 33.6 .17609245
    23 1971  92.6        . . 41.7  .1860954
    28 1971 106.3        . . 44.7  .1650846
     2 1971 104.1        . . 38.8 .16995385
    32 1971 106.3        . . 41.6 .17881927
    20 1971 108.6        . . 34.7 .17521714
    33 1971 108.9        . .   42 .18430856
    26 1971 124.6        . . 38.1  .1745399
    38 1971 105.4        . . 40.2 .17634407
    11 1971 108.4        . . 38.5   .170839
    37 1971 111.5        . . 41.6 .17312744
     3 1971   121        . . 39.7 .17929636
     4 1971 125.5        . . 31.1  .1916476
    30 1971   115        . . 34.3  .2004893
    21 1971 190.5        . .   44 .16377378
    10 1971 139.3        . . 32.2 .17797175
    16 1971 116.4        . . 40.1  .1767316
    14 1971 119.8        . . 39.3  .1867808
    29 1971 123.2        . . 40.2  .1838495
    35 1971 124.4        . . 39.5  .1813672
     5 1971 117.6        . . 45.5  .1646539
     7 1972   117  9.63889 . 40.9   .188287
    34 1972  71.3 9.601122 . 37.2 .20204525
    13 1972 179.4 9.547482 . 30.6 .18328904
    31 1972   103 9.630849 . 39.1  .1823062
    15 1972 136.5  9.59714 . 41.5  .1716557
    26 1972 124.4 9.779579 . 38.4 .17586027
    32 1972 111.5 9.569716 . 41.6  .1795947
    20 1972 104.9 9.746475 . 41.1  .1766969
    29 1972 134.4 9.770211 . 41.6  .1845411
    33 1972 108.6 9.675209 . 46.9 .18547577
    22 1972 296.2 9.736376 . 36.1 .17393607
    27 1972 121.7 9.625027 . 39.8 .17759047
    28 1972   109 9.784274 . 44.7 .16679317
     8 1972 126.1 9.651839 . 33.7  .1740643
    11 1972 109.4 9.730265 . 41.9 .17282845
    19 1972 122.2 9.694641 . 40.1  .1784993
     4 1972 134.3 9.805548 . 31.2 .19234496
    35 1972   138 9.673069 .   40 .18295917
     3 1972 123.5 9.930814 . 39.9  .1804344
    30 1972 118.7 9.509309 . 34.1  .2006698
    21 1972 198.6 9.944233 . 40.6 .16599335
    12 1972   111 9.768118 . 38.8  .1825754
    end
    label values state state
    label def state 1 "Alabama", modify
    label def state 2 "Arkansas", modify
    label def state 3 "California", modify
    label def state 4 "Colorado", modify
    label def state 5 "Connecticut", modify
    label def state 6 "Delaware", modify
    label def state 7 "Georgia", modify
    label def state 8 "Idaho", modify
    label def state 9 "Illinois", modify
    label def state 10 "Indiana", modify
    label def state 11 "Iowa", modify
    label def state 12 "Kansas", modify
    label def state 13 "Kentucky", modify
    label def state 14 "Louisiana", modify
    label def state 15 "Maine", modify
    label def state 16 "Minnesota", modify
    label def state 17 "Mississippi", modify
    label def state 18 "Missouri", modify
    label def state 19 "Montana", modify
    label def state 20 "Nebraska", modify
    label def state 21 "Nevada", modify
    label def state 22 "New Hampshire", modify
    label def state 23 "New Mexico", modify
    label def state 24 "North Carolina", modify
    label def state 25 "North Dakota", modify
    label def state 26 "Ohio", modify
    label def state 27 "Oklahoma", modify
    label def state 28 "Pennsylvania", modify
    label def state 29 "Rhode Island", modify
    label def state 30 "South Carolina", modify
    label def state 31 "South Dakota", modify
    label def state 32 "Tennessee", modify
    label def state 33 "Texas", modify
    label def state 34 "Utah", modify
    label def state 35 "Vermont", modify
    label def state 36 "Virginia", modify
    label def state 37 "West Virginia", modify
    label def state 38 "Wisconsin", modify
    label def state 39 "Wyoming", modify

  • #2
    You don't say how you intend to use levelsof, but no loops needed. Note I use allsynth from ssc.
    Code:
    u "http://fmwww.bc.edu/repec/bocode/s/synth_smoking.dta", replace
    
    qui xtset state year, y
    cls
    
    loc int_time = 1989
    
    tempfile cali
    
    loc others (1980(1)1988)
    
    loc beer (1984(1)1988)
    cls
    allsynth cigsale beer`beer' ///
        lnincome`others' retprice`others' ///
        age15to24`others' ///
        cigsale(1988) ///
        cigsale(1980) ///
        cigsale(1975), ///
        fig trunit(4) ////
        trperiod(`int_time') ///
        gapfig(classic placebos lineback) pval keep(smokingresults) rep
        
        
    u smokingresults, clear
    
    keep if inlist(state,4,5,21,34)
    This estimates all your palcebos as you want, and then retains the states of interest. I recognize these as the donors for California. Why do you want to only estimate an SC for these?

    Comment


    • #3
      Hi, thanks for your answer. I want to run this command using levelsof but it does not work. It says "4,state invalid name r(198)". Do you have any idea to solve the problem?

      Many thanks.

      Code:
      levelsof state if (state==4,state==5, state==21,state==34),local(levels)
      
      foreach i of local levels {
       qui synth cigsale retprice cigsale(1988) cigsale(1980) cigsale(1975), ///
      xperiod(1980(1)1988) trunit(`i') trperiod(1989) keep(ps`i', replace)
      }

      Comment


      • #4
        I just said you don't need levelsof. allsynth takes care of this, no loops or levelsof needed. You could loop over a numlist for this issue, but as I said, this is wholly unnecessary and can be accomplished without any looping at all. You also didn't answer my second question- why exactly do you want to do this? These are the selected donors for California from the Abadie 2010 paper. Why do you want these placebos, specifically? I'm not even saying it's wrong, I'm just curious on why you want these specific placebo estimates.

        EDIT:
        If you really want to use a loop, which again, I will say is not needed, you can just do
        Code:
        foreach i of num 4 5 21 34 {
        
        qui synth cigsale retprice cigsale(1988) cigsale(1980) cigsale(1975), ///
        xperiod(1980(1)1988) trunit(`i') trperiod(1989) keep(ps`i', replace)
        }
        This does as you wish, but again, as someone who writes SCM methods in Stata, I'm telling you there's a better way to accomplish what you desire.
        Last edited by Jared Greathouse; 28 Nov 2022, 07:15.

        Comment

        Working...
        X