Announcement

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

  • plotting trends for treatment and control group

    Hello

    I am trying to create a motivation type pf graph before my main estimation results. I have inflation as my variable and several control groups (dummy = 1,2,3) and 1 treated group (dummy = 0). I want to plot the mean/median over time just to show the trend and i am using the following code.

    Code:
    clear
    append using "\\Client\H$\Desktop\data\motivation_treated.dta" "\\Client\H$\Desktop\data\motivation_monetaryagg.dta" "\\Client\H$\Desktop\data\motivation_exchange.dta" "\\Client\H$\Desktop\data\motivation_other.dta"
    
    gen y_T = year
    replace y_T = 0 
    
    gen T_year=year if y_T == 0
    bys country: egen tr_year=median(T_year)
    gen trr_year=year-tr_year
    
    bys trr_year: egen mean_inflation=median(hcpi_a) if treat == 0
    bys trr_year: egen mean_inflation_monetary=median(hcpi_a) if treat == 1
    bys trr_year: egen mean_inflation_exchange=median(hcpi_a) if treat == 2
    bys trr_year: egen mean_inflation_other=median(hcpi_a) if treat == 3
    
    twoway (line mean_inflation trr_year) (line mean_inflation_monetary trr_year)
    (line mean_inflation_exchange trr_year) (line mean_inflation_other trr_year)
    The issue I am facing is that I want the pre-treatment graph that is values below 0 for all groups to be more like parallel lines or following a similar trend so that after treatment can show how different groups are performing. But I am not able to do that. I generated the following dataset as a reference:

    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3 country float(year hcpi_a treat year_of_treatment)
    "AUS" 2000 10 0 2001
    "AUS" 2001 3 0 2001
    "AUS" 2002 2 0 2001
    "BRA" 2000 5 0 2002
    "BRA" 2001 12 0 2002
    "BRA" 2002 3 0 2002
    "CAN" 2000 4 0 2001
    "CAN" 2001 2 0 2001
    "CAN" 2002 1 0 2001
    "AFG" 2000 11 1 0
    "AFG" 2001 8 1 0
    "AFG" 2002 5 1 0
    "BOL" 2000 9 1 0
    "BOL" 2001 5 1 0
    "BOL" 2002 6 1 0
    "ARG" 2000 4 2 0
    "ARG" 2001 3 2 0
    "ARG" 2002 3 2 0
    "MOZ" 2000 3 2 0
    "MOZ" 2001 2 2 0
    "MOZ" 2002 4 2 0
    "MEX" 2000 2 3 0
    "MEX" 2001 1 3 0
    "MEX" 2002 5 3 0
    "CAM" 2000 6 3 0
    "CAM" 2001 4 3 0
    "CAM" 2002 5 3 0
    end
    [/CODE]

    Can I please get some help?

  • #2
    I'm confused. How can you have several control groups when there's only one treatment?

    Anyways, if this were my problem I would make a new dataframe and just collapse all this down nicely, using collapse. After I have lunch in a few minutes, if you've not solved it or nobody's beaten me to the punch, I'll show you

    Comment


    • #3
      Hi Jared Greathouse

      Thank you for your response.

      I have only 1 control group but there are various subgroups that Have been combined together to form it. But since for my purpose, I want to see the impact of each subgroup, I separated them. Per your suggestion, I did try
      Code:
      collapse (median) hcpi_a, by(trr_year treat)
      but nothing really worked out.

      I would be grateful if you could help me out.

      Comment


      • #4
        I want to see the impact of each subgroup
        I don't understand. Why? Various subgroups? I'm only seeing 9 countries here..... right? I don't get why you'd need various subgroups, why not just use one control group comprised of all the other countries? Are there three different treatments? Anyways
        but nothing really worked out
        What do you mean? What about it didn't work out? I get a graph when I do this
        Code:
        clear
        input str3 country float(year hcpi_a treat year_of_treatment)
        "AUS" 2000 10 0 2001
        "AUS" 2001 3 0 2001
        "AUS" 2002 2 0 2001
        "BRA" 2000 5 0 2002
        "BRA" 2001 12 0 2002
        "BRA" 2002 3 0 2002
        "CAN" 2000 4 0 2001
        "CAN" 2001 2 0 2001
        "CAN" 2002 1 0 2001
        "AFG" 2000 11 1 0
        "AFG" 2001 8 1 0
        "AFG" 2002 5 1 0
        "BOL" 2000 9 1 0
        "BOL" 2001 5 1 0
        "BOL" 2002 6 1 0
        "ARG" 2000 4 2 0
        "ARG" 2001 3 2 0
        "ARG" 2002 3 2 0
        "MOZ" 2000 3 2 0
        "MOZ" 2001 2 2 0
        "MOZ" 2002 4 2 0
        "MEX" 2000 2 3 0
        "MEX" 2001 1 3 0
        "MEX" 2002 5 3 0
        "CAM" 2000 6 3 0
        "CAM" 2001 4 3 0
        "CAM" 2002 5 3 0
        end
        
        cls 
        
        collapse (median) hcpi_a, by(year treat)
        
        
        line hc year, by(treat)
        What about this result is bad? Or put another way: what do you want for Stata to do that it isn't doing with your code? Or alternatively, what is it doing that you don't want for it to do? "It doesn't work" isn't a very useful thing, because it doesn't tell me what's wrong about the current solutions. I should also say that I'm not saying any of this to be mean, I'm just saying that for us to better help you, we need to have a clear idea of what the problem is. So, my first idea was to simply collapse your outcome into their medians over time and graph it like that (we can do the same with the mean). But, that's not what you wanted. So I guess my question is, what do you want for Stata to do?

        Comment


        • #5
          Jared Greathouse I apologise if I wasn't clear with my explanation
          • I made 1 big control group from different subgroups because that's what I use in my main estimation against the treated group. In that estimation, there is no distinction between subgroups. All of them have a treat value dummy = 0.
          • I got a graph as well but what I want is something like this picture below
          Click image for larger version

Name:	Research-Design-This-figure-depicts-our-difference-i-n-differences-DiD-research.png
Views:	1
Size:	28.0 KB
ID:	1709727



          in this picture as you can see, pre treatment trend is same parallel for both control and treatment group, but after is when you can see it working well. This is something I want stata to do which I am not able to figure out.

          With my original data, I ran the following code and got the image below:

          Code:
          gen y_T = year
          replace y_T = 0
          
          gen T_year=year if y_T == 0
          bys country: egen tr_year=median(T_year)
          gen trr_year=year-tr_year
          
          bys trr_year: egen mean_inflation=median(hcpi_a) if treat == 1
          bys trr_year: egen mean_inflation_monetary=median(hcpi_a) if treat == 0
          
          twoway (line mean_inflation trr_year, lw(medthick)) (line mean_inflation_monetary trr_year)
          Click image for larger version

Name:	new 1.jpg
Views:	1
Size:	37.1 KB
ID:	1709729

          Comment


          • #6
            Oh I see, you want to give an example graph looking like the first one, right?

            Comment


            • #7
              Yes the first one!

              Comment


              • #8
                digression - I can't let the statement, "How can you have several control groups when" in #2 stand as is - there are lots of reasons to have more than one control group especially in observational studies. Paul Rosenbaum has written lots about this; e.g., Rosenbaum, PR (1987), "The role of a second control group in an observation study", Statistical Science, 2: 292-316, with discussion - he has also discussed this in some of his boots including Design of Observational Studies, second edition ​​​​​​​ (2020), Springer

                Comment


                • #9
                  there are lots of reasons to have more than one control group especially in observational studies
                  Yeah there are. My work in econometrics is in part based on control group selection and how thorny it can be (especially with the synthetic control method).

                  Sharma Swati the answer is less exciting than at first glance. You can actually just generate linear functions and do this pretty well. I'm not at my computer, but I would just generate two linear functions, maybe
                  Code:
                  g treat = 2*x
                  g control = 3*x
                  Then, you can just after the hypothetical intervention point, add in a treatment effect (say, treat = t - 2 if time> intervention_time.

                  You can even make a variable if only the real post-intervention outcomes, and overlay it as a dotted line. Again I'm not at my computer, but I'll do this in a few.

                  Comment


                  • #10
                    Jared Greathouse

                    Hi Jared,

                    I did find a fix to my problem but in a different manner. I had to create 2 separate median columns each for both the treated and control group and then combine them into one single column to plot them. This gave me a result like the one in the picture. Thank you so much for your input!

                    Comment


                    • #11
                      Could you post your solution Sharma Swati

                      Comment


                      • #12
                        Jared Greathouse :
                        I used my own dataset and not the sample I generate for reference for you earlier, but I tried on that sample too and it worked. Here's my code.

                        Code:
                        *append datasets
                        
                        gen y_T = year
                        replace y_T = 0
                        gen T_year=year if y_T == 0
                        bys country: egen tr_year=median(T_year)
                        gen trr_year=year-tr_year
                        
                        egen a = median(hcpi_a) if treat == 0 & trr_year<0
                        egen b = median(hcpi_a) if treat==1 & trr_year<0
                        bys trr_year : egen c=median(hcpi_a) if treat == 0 & trr_year>=0
                        bys trr_year: egen d=median(hcpi_a) if treat == 1 & trr_year>=0
                        
                        replace a = 0 if a ==.
                        replace b = 0 if b ==.
                        replace c = 0 if c ==.
                        replace d = 0 if d ==.
                        
                        gen IT_countries = a + c
                        replace IT_countries = . if IT_countries == 0
                        gen Monetary_Agg_Target = b + d
                        replace Monetary_Agg_Target =. if Monetary_Agg_Target == 0
                        
                        twoway (line IT_countries trr_year) (line Monetary_Agg_Target trr_year), tlabel(-25(5)25) tline(0, lc(black) lp(dash))
                        Last edited by Sharma Swati; 15 Apr 2023, 20:49.

                        Comment


                        • #13
                          I have a similar problem where, I have two variables Violent and Property, two Treatment groups (Treatment_Group == 1 | Treatment_Group == 2), and one control group (Treatment_Group == 0). I need to plot the averages of the variables Violent and Property against the Time_to_Treatment (t2tminor; t2tmajor) variable for both treatment and control, in one graph.

                          clear
                          input double fips float year double(Violent Property) float(t2tminor t2tmajor Minor_Treated Major_Treated) str50 Treatment_Status
                          1101 1990 240 2524 -5 . 1 0 ""
                          1101 1991 1490 3008 -4 . 1 0 ""
                          1101 1992 2601 1812 -3 . 1 0 ""
                          1101 1993 1863 2062 -2 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 1994 2295 2446 -1 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 1995 2290 1936 0 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 1996 2535 1754 1 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 1997 1387 1246 2 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 1998 377 884 3 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 1999 275 821 4 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2000 293 883 5 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2001 78 234 6 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2002 93 288 7 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2003 77 244 8 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2004 86 209 9 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2005 45 141 10 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2006 101 216 11 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2007 87 262 12 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2008 80 228 13 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2009 63 263 14 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2010 0 0 15 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2011 0 0 16 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2012 0 0 17 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2013 0 0 18 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2014 5 19 19 . 1 0 "Treated by 1 Minor Hurricane and 0 Major Hurricane"
                          1101 2015 124 271 20 . 1 0 ""
                          1101 2016 210 326 21 . 1 0 ""
                          1101 2017 148 278 22 . 1 0 ""
                          1101 2018 185 353 23 . 1 0 ""
                          1101 2019 144 175 24 . 1 0 ""
                          1103 1990 54 35 . . 0 0 ""
                          1103 1991 16 24 . . 0 0 ""
                          1103 1992 5 44 . . 0 0 ""
                          1103 1993 9 46 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 1994 10 49 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 1995 9 67 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 1996 8 78 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 1997 11 67 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 1998 4 86 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 1999 10 103 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2000 4 12 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2001 4 80 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2002 6 81 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2003 10 109 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2004 7 97 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2005 10 72 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2006 6 55 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2007 8 98 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2008 10 94 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2009 8 147 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2010 6 181 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2011 0 0 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2012 0 0 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2013 0 0 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2014 12 133 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1103 2015 13 131 . . 0 0 ""
                          1103 2016 14 74 . . 0 0 ""
                          1103 2017 8 88 . . 0 0 ""
                          1103 2018 17 110 . . 0 0 ""
                          1103 2019 12 115 . . 0 0 ""
                          1105 1990 54 39 . . 0 0 ""
                          1105 1991 28 57 . . 0 0 ""
                          1105 1992 58 32 . . 0 0 ""
                          1105 1993 26 26 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 1994 40 48 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 1995 40 53 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 1996 30 19 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 1997 30 22 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 1998 15 6 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 1999 23 13 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2000 27 7 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2001 10 8 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2002 22 3 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2003 18 20 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2004 34 15 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2005 21 15 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2006 9 9 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2007 20 20 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2008 29 24 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2009 10 21 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2010 6 2 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2011 0 0 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2012 0 0 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2013 0 0 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2014 1 0 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1105 2015 10 4 . . 0 0 ""
                          1105 2016 0 0 . . 0 0 ""
                          1105 2017 2 0 . . 0 0 ""
                          1105 2018 1 1 . . 0 0 ""
                          1105 2019 0 2 . . 0 0 ""
                          1107 1990 38 75 . . 0 0 ""
                          1107 1991 23 74 . . 0 0 ""
                          1107 1992 30 65 . . 0 0 ""
                          1107 1993 43 72 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1107 1994 37 125 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1107 1995 26 93 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1107 1996 43 71 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1107 1997 33 79 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1107 1998 16 63 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          1107 1999 21 62 . . 0 0 "Treated by 0 Minor Hurricane and 0 Major Hurricane"
                          end

                          Comment


                          • #14
                            Duplicate post. See https://www.statalist.org/forums/for...-time-variable if interested.

                            Comment

                            Working...
                            X