Announcement

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

  • Synthetic Control:period of treatment is not not found in timevar

    Hi,

    I am using the Synthetic Control Method to analyze my research and am trying to use the commands "synth" and "synth2". However, both commands return the error "treatment period not found in timevar". I have verified that the time period exists for all items. I would be grateful for any assistance. Thank you.

    My commands are as follows:

    gen mon=0
    replace mon=01 if month=="Jan"
    replace mon=02 if month=="Feb"
    replace mon=03 if month=="Mar"
    replace mon=04 if month=="Apr"
    replace mon=05 if month=="May"
    replace mon=06 if month=="Jun"
    replace mon=07 if month=="Jul"
    replace mon=08 if month=="Aug"
    replace mon=09 if month=="Sep"
    replace mon=10 if month=="Oct"
    replace mon=11 if month=="Nov"
    replace mon=12 if month=="Dec"
    gen mdate=ym(year, mon)
    format mdate %tmCCYYNN
    xtset it mdate
    "
    Panel variable: it (strongly balanced)
    Time variable: mdate, 200501 to 202405
    Delta: 1 month
    "
    synth2 price item, trunit(1) trperiod(202203)
    "invalid trperiod() -- treatment period not found in timelvar
    r(198);
    "
    Last edited by Jane Quan; 12 Jul 2024, 23:31.

  • #2
    Jane:
    how your -trperiod- is coded? Is it in -string- format?
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      Jane:
      how your -trperiod- is coded? Is it in -string- format?
      Hi Carlo,

      Thank you for replying.

      The color of the numbers on the Edit board suggests they are not in string format. However, when I double-click the "mdate" variable (which is the time variable), I can see a letter with the number. For example, double-clicking on 200501 displays 2005m1.

      So, I also tried code like:

      synth2 price item, trunit(1) trperiod(2022m3)
      or
      synth2 price item, trunit(1) trperiod("2022m3")
      or
      synth2 price item, trunit(1) trperiod("202203")
      But all of them show the error "option trperiod() invalid."

      I would be grateful for any assistance. Thank you.

      Comment


      • #4
        Hey Jane, show us, please, the dataex for your data. This way we need not guess on these details. Just show us the treated unit and say 5 donors. That is, show the panel variable, time variable, and the outcome of interest.

        Edit: by the way, the reason you're seeing this problem is because you need to use the number that Stata understands as the date/time. So for example, this month is 773 because it's been 773 months since January of 1960.




        This is kind of why I don't understand why Hainmuller didn't simply force the users to use a "treated" variable to define treatment or controls, which is a dummy, that way users wouldn't run into these issues, but synth isn't my command so I guess I don't get to make those decisions. I think when i reviewed synth2 for SJ I wanted to make similar suggestions, but I didn't end up doing so.
        Last edited by Jared Greathouse; 13 Jul 2024, 04:20.

        Comment


        • #5
          Jane:
          there's a more straightforward approach for date conversion, that I cannot recall at the moment.
          That said, you may want to try:
          Code:
          . split yourstring_2023m1, p(m)
          
          . destring var11, g(year)
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment


          • #6
            Originally posted by Jared Greathouse View Post
            Hey Jane, show us, please, the dataex for your data. This way we need not guess on these details. Just show us the treated unit and say 5 donors. That is, show the panel variable, time variable, and the outcome of interest.

            Edit: by the way, the reason you're seeing this problem is because you need to use the number that Stata understands as the date/time. So for example, this month is 773 because it's been 773 months since January of 1960.

            This is kind of why I don't understand why Hainmuller didn't simply force the users to use a "treated" variable to define treatment or controls, which is a dummy, that way users wouldn't run into these issues, but synth isn't my command so I guess I don't get to make those decisions. I think when i reviewed synth2 for SJ I wanted to make similar suggestions, but I didn't end up doing so.
            Hi Jared,

            Thank you for your reply.

            You are right; when I try to provide a data example, the date variable appears as just a number instead of year and month data, even though it looks like date data in the Edit board (see below). So, I tried some different date conversion code as Carlo suggested. However, when I use the xtset command, other methods show that the time variable has some gaps, which also leads to errors in the synth or synth2 commands.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float mdate long it double price
            540 20 6.423
            540  7   243
            540  1 62.27
            540 29 10.12
            540 31    13
            540  3 14741
            540 11    15
            540 22   1.5
            540 15  6.67
            540 33 46.46
            540 16  5.01
            540 34 10.56
            540 17 22.28
            540 14 28.52
            540  4   439
            540 28 39.14
            540 32   .52
            540 12 7.604
            540 19 15.62
            540 23  2.69
            540 18 24.37
            540 30   113
            540  6    24
            540  5 26.81
            540 21   5.9
            540 25  3.56
            540  9    20
            540  2   1.3
            540 13 25.35
            540 24  54.6
            540 26  8.28
            540  8 29.79
            540 10 5.929
            540 27  25.5
            541 23  2.16
            541 33 47.67
            541 21     4
            541 14 28.52
            541 13    16
            541 27  25.5
            541 34  9.86
            541 11    15
            541  1 59.57
            541  6    24
            541 18 24.37
            541 20 6.568
            541  7   242
            541  4   458
            541 31 11.35
            541 10  5.94
            end
            format %tmCCYYNN mdate
            label values it it
            label def it 1 "Banana, Cavendish", modify
            label def it 2 "Cassava", modify
            label def it 3 "Cattle", modify
            label def it 4 "Coconut", modify
            label def it 5 "Coffee", modify
            label def it 6 "Durain", modify
            label def it 7 "Egg, chicken", modify
            label def it 8 "Fowl", modify
            label def it 9 "Garlic", modify
            label def it 10 "Glutinous paddy", modify
            label def it 11 "Groundnut", modify
            label def it 12 "Hommali paddy", modify
            label def it 13 "Longan", modify
            label def it 14 "Longkong", modify
            label def it 15 "Lychee", modify
            label def it 16 "Maize", modify
            label def it 17 "Mango, Khiaw Sawei", modify
            label def it 18 "Mangosteen", modify
            label def it 19 "Mung bean", modify
            label def it 20 "Non-Glutinous paddy", modify
            label def it 21 "Onion", modify
            label def it 22 "Orchids", modify
            label def it 23 "Palm kernel (weight over 15 kg.)", modify
            label def it 24 "Peppercorn", modify
            label def it 25 "Pineapple", modify
            label def it 26 "Potato", modify
            label def it 27 "Rambutan", modify
            label def it 28 "Rubber, un-smoked sheet 3", modify
            label def it 29 "Shallot", modify
            label def it 30 "Shrimp, Vannamei", modify
            label def it 31 "Soybean", modify
            label def it 32 "Sugarcane", modify
            label def it 33 "Swine (weight over 100 kg.)", modify
            label def it 34 "Tangerine", modify

            Comment


            • #7
              The solution you seek is to do

              Code:
              synth price item, trunit(1) trperiod(ym(2022,3))


              EDIT: by the way, why are you including the "item" variable after price? Jane Quan

              Comment


              • #8
                Originally posted by Jared Greathouse View Post
                The solution you seek is to do

                Code:
                synth price item, trunit(1) trperiod(ym(2022,3))


                EDIT: by the way, why are you including the "item" variable after price? Jane Quan
                Jared Greathouse

                Well, in the case of the univariate, I mean only "price" as the dependent variable. I do not need to include "item" based on the description of the "synth" code using "help synth." However, when I use another dataset, it seems I have to include "item" to make "trunit(1)" work. Otherwise, "trunit" won't recognize the donor pool or treatment...


                By the way, the code you provided is not working. It shows "trperiod() invalid -- invalid number". Anyway, thank you all the same.

                Code:
                use data, clear
                gen mon=0
                replace mon=01 if month=="Jan"
                replace mon=02 if month=="Feb"
                replace mon=03 if month=="Mar"
                replace mon=04 if month=="Apr"
                replace mon=05 if month=="May"
                replace mon=06 if month=="Jun"
                replace mon=07 if month=="Jul"
                replace mon=08 if month=="Aug"
                replace mon=09 if month=="Sep"
                replace mon=10 if month=="Oct"
                replace mon=11 if month=="Nov"
                replace mon=12 if month=="Dec"
                
                encode item, gen(it)
                
                gen mdate=ym(year, mon)
                format mdate %tmCCYYNN
                xtset it mdate
                synth price it, trunit(1) trperiod(ym(2022,3)) or  synth price, trunit(1) trperiod(ym(2022,3))
                Last edited by Jane Quan; 13 Jul 2024, 07:01.

                Comment


                • #9
                  I think you need to include it as a macro, like
                  Code:
                  loc interdate: di ym(2022,3)
                  synth price it, trunit(1) trperiod(`interdate')
                  You do not wish to include the item as a covariate. You are using the values of your panel variable as a covariate, and that's not how SCM works.

                  Comment


                  • #10
                    Originally posted by Jared Greathouse View Post
                    I think you need to include it as a macro, like
                    Code:
                    loc interdate: di ym(2022,3)
                    synth price it, trunit(1) trperiod(`interdate')
                    You do not wish to include the item as a covariate. You are using the values of your panel variable as a covariate, and that's not how SCM works.
                    Hi Jared Greathouse,

                    The good thing is that when I include it as a macro, it reports the result. However, if I include "item," there is a conformability error. If I exclude it, it says, "not a single variable specified. Please supply at least a response variable."

                    Do you have any idea what the response variable should be? I couldn't find an explanation in the "synth" code documentation.

                    Comment


                    • #11
                      Well, you want to include lags of your outcome. Lags of your outcome for the algorithm (the average at that time point) to see which units are most similar to your treated unit, or other additional predictors

                      Comment


                      • #12
                        by the way, I will shamelessly plug myself here: if you want to avoid these issues, you can use the newly developed forward DID command, written by me. You can install the ado and help like
                        Code:
                        copy "https://raw.githubusercontent.com/jgreathouse9/jgreathouse9.github.io/master/stata/fdid/fdid.ado" "C:\ado\plus\f\fdid.ado", replace
                        copy "https://raw.githubusercontent.com/jgreathouse9/jgreathouse9.github.io/master/stata/fdid/fdid.sthlp" "C:\ado\plus\f\fdid.sthlp", replace
                        To use it, all you require is a treatment variable, an outcome, a unit, and a time variable. You can use it like
                        Code:
                        clear *
                        
                        u "http://fmwww.bc.edu/repec/bocode/s/scul_Taxes", clear
                        
                        loc int_time: disp tq(2012q1)
                        
                        cls
                        
                        qui xtset
                        
                        local lbl: value label `r(panelvar)'
                        
                        loc unit ="Kansas":`lbl'
                        
                        g treat = cond(`r(panelvar)'==`unit' & `r(timevar)' >=`int_time',1,0)
                        
                        fdid gdp, tr(treat) gr1opts(scheme(sj) name(kansas, replace))
                        Yeah, your outcomes won't be a weighted average of controls, but at least you won't need to deal with the headache of synth's weird syntax sometimes

                        Comment


                        • #13
                          Just another comment: your response variable is still your outcome. However, scm isn't like traditional regression estimation in the sense where you want to include your unit/time variables in the model. It's a little more complicated. Under the hood, you're using matrix math to predict the preintervention outcomes of your treated unit, using the outcomes of your control units. The covariates in this case are meant to help the algorithm choose the donors which are the closest on both the outcomes and the covariate values.

                          I should note that in principle DID does a very similar thing, except except a different set of restrictions than SCM does. By the way, if you really were interested in my FDID estimator, it's now officially ready for use. You can install it via
                          Code:
                          net install fdid, from("https://raw.githubusercontent.com/jgreathouse9/FDIDTutorial/main") replace
                          It's syntax is a lot simpler than SCM, so it may come in handy if you have only one treated unit. Plus, with DID there's readily available inference theory, whereas with SCM the matter is a little more tricky. So... should you be interested in an SCM alternative, Forward Difference-in-Differences is one of them. Jane Quan

                          Comment


                          • #14
                            Hi, Jared Greathouse

                            Thank you so much for providing an alternative method. I have already tried the code for FDID using the data you provided, and it works just fine. However, when I apply the code to my data (7,689 observations with 10 treatments), it takes a long time to run. It's still running on my computer. I'm not sure if this is normal.

                            So I stopped the process and dropped around 1,000 observations, but when I ran the same code, it said "frame __dfcopy already defined r(110)." I don't know what that means, and I couldn't find a similar issue on the STATALIST forum.

                            I reloaded my data, and it still reports the same error.

                            By the way, as to the r(110):
                            [P] error . . . . . . . . . . . . . . . . . . . . . . . . Return code 110
                            __________ already defined
                            A variable or a value label has already been defined, and you
                            attempted to redefine it. This occurs most often with generate.
                            If you really intend to replace the values, use replace. If you
                            intend to replace a value label, specify the replace option with
                            the label define command. If you are attempting to alter an
                            existing label, specify the add or modify option with the
                            label define command.

                            (end of search)
                            Last edited by Jane Quan; 14 Jul 2024, 21:58.

                            Comment


                            • #15
                              So wait, are you telling me that you have 10 treated units? So, in the help, I explain that FDID at the moment only works for cases where we have one single treated unit. I just modified the code on my githib to throw an error if you have more than one treated unit.


                              Here's the solution though. Say we have 2 treated units, Chicago and Phoenix. We have Miami, Atlanta, LA, and Washington as donors.

                              You can make one frame per treated unit. That is, a frame for Chicago which excludes Phoenix and a frame for Phoenix which excludes Chicago.

                              Then, looping over each frame, you may estimate fdid as is. You may then take the average of the ATTs and CIs to obtain your treatment effects

                              Comment

                              Working...
                              X