Announcement

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

  • Graph by group of states

    Hi!

    I am using stata13/MP from a Mac computer.

    I am new to stata and I am having trouble creating graphs. That would be great if someone could give me some advice.

    In my data, I have a "state" variable, which contains the 50 U.S. states. I also have a variable called "mandate_year" which gives the year in which a given state passed a mandate.
    I want to create a graph showing the evolution of healthcare coverage over time. The tricky part is that I want one graph for each group of states that passed a mandate in the same year.
    For instance, if CA and CO passed a mandate in 2008, but MN and MA passed a law in 2009, I will want:
    1) One graph showing the evolution of coverage for the states CA and CO only
    2) One graph showing the evolution of coverage for the states of MN and MA only

    I thought about using a local macro:
    local states2008 "CO CA"
    local states2009 "MN MA"

    foreach group of local states2008 local states2009 {
    [insert graph code]
    }

    Unfortunately, that doesn't seem to work. Could you give me some help?

    Thanks in advance!
    Best,

    Ana

  • #2
    All of the graph commands I can think of accept the -if- qualifier, so something like the following will work:

    [CODE]
    levelsof mandate_year, local(yearlist)
    foreach y of local yearlist {
    Some Graph Command if (mandate_year == `y')
    graph rename GraphFor`y'
    }

    This would leave a collection of graphs accessible to you under the names GraphFor`y'.

    Now, depending on how your data is structured, you might need to create some kind of summary measure of coverage levels of the states sharing a given mandate year, which would require other manipulations within the loop.
    -Mike

    Comment


    • #3
      Ana: please clarify what are the relevant variables in this analysis. I am unfamiliar with the US context. In particular, how does "coverage" differ from "having passed a mandate"? Does coverage only get measured for each state in year after mandate-passing? Or is it measured before and after? (As I assume below)
      I ask because if matters for the types of graph that might be drawn. Are you looking to see if there is a step change in coverage around the mandate-passing year? In which case, I would use something like Mike Lacy's code to wrap things, and the graph command might be something like:

      Code:
      tw connect coverage year if mandate_year == `y'
      This is a first step -- I am unsure that I would pool states with a common mandate-passing year, though. I suspect I'd want to see results, state by state.

      Comment


      • #4
        Thank you very much for your answers!

        Stephen: Coverage is measured before and after the mandate year. I want to see if people changed their health insurance coverage after the mandates were passed.

        I actually am encountering a new issue. Here are some more details:
        I am working with survey data. For each respondent, I have a dummy variable for each type of coverage. For instance, for person1, I have "HMO == 1" if that person is covered by an HMO plan, "emp_ins == 1" if he is covered by an employer-sponsored plan etc.
        In my graph, I want to see the evolution of coverage over time for each type of plan. That is, how many respondents chose each type of coverage in each year. So in my graph I want "year" on the x axis, and "number of people covered" in the "y" axis. Then I want a line for "HMO", one for "employer-provided insurance" etc.

        Originally, what I had in mind was a code like: foreach type of emp_ins HMO {
        egen `type'_tot = total(`type')
        }
        twoway (scatter total_HMO year) ///
        (scatter total_emp_ins year), qfit ///
        (qfit total_HMO year) ///
        (qfit total_emp_ins year), ///
        legend(label(3 "Quadratic Fit") label(4 "Quadratic Fit")) ///
        legend(order(1 3 2 4))

        I am now really confused about how to use this code with the "if" option. Is that possible? Or would you have any advice on how to get a line for each type of coverage differently?
        Note: I have around 7 coverage types so it would be nice if I could work in a loop.

        Sorry for all of these questions, and thanks again for you help!

        Ana



        Comment


        • #5
          Is it as simple as this? or is there any complication? Perhaps you want to plot the switchers only? Are two vars mutually exclusive?
          Best, Sergiy Radyakin

          Code:
          version 12
          use "http://www.stata-press.com/data/r12/nlswork.dta", clear
          // south is your HMO, union is your emp_ins
          collapse south union, by(year)
          twoway line south union year
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	36.1 KB
ID:	150020

          Comment


          • #6
            Hi Sergiy,

            This is exactly what I want! Thank you so much!
            So, just to be sure, I don't need the "egen total_HMO" step? The "collapse" command already computes the sum by year?

            Comment


            • #7
              Hey again!

              Actually, I have another question:
              The collapse (sum) option would give me the graph I want, but there is a problem. I want to use the variable mandate_year (different from the "interview year" we have on the x axis) to generate a graph for each group of states that passed a mandate in the same year.
              As suggested by Mike and Stephen, I was thinking about doing it with a code such as
              "
              [CODE]
              levelsof mandate_year, local(yearlist)
              foreach y of local yearlist {
              Some Graph Command if (mandate_year == `y')
              graph rename GraphFor`y'
              }
              "
              The collapse option takes away the "mandate_year" variable. Do you know how I can work my way around this?

              Thanks a lot in advance,

              Ana

              Comment


              • #8
                I'm not sure what you mean by saying "The collapse option takes away the "mandate_year" variable," since
                Code:
                collapse ..., by(mandate_year)
                would leave mandate_year as a variable in the file. Can you explain?

                Also, to clarify, it sounds to me like you want a) a separate line graph for each mandate_year; and b) a separate line graph for each type of plan. I wouldn't put all those lines on one graph. Am I correct that you want one graph for HMO by year, with all the separate mandate_years, one graph for employer insurance by year with all the mandate_years, etc.

                - Mike

                Comment


                • #9
                  Collapse averages variables by default. If you want coverage rate, you can leave it at default and take care of weights. If you want coverage as number of people, then use the (sum) prefix. I am not sure about the mandate year variable. To me it seems like a state-characteristic, which shouldn't be part of the beneficiaries file, and that you should merge in later, once you collapse the file. Collapsing the file will leave as many observations as many groups you have in the by() option. So if you want to do analysis separately by state later, you need to do by-both year and yearofmandate.

                  Code:
                  version 12
                  use "http://www.stata-press.com/data/r12/nlswork.dta", clear
                  // south is your HMO, union is your emp_ins, grade is your state
                  // we generate year of mandate, and it is constant by state, but may differ between states
                  rename grade state
                  generate int yearm=2000+floor(state/3)
                  collapse south union , by(year yearm)
                  label variable south "HMO"
                  label variable union "Employee insurance"
                  twoway line south union year, by(yearm, row(3) title("Dynamics of coverage by year of mandate"))  ///
                       xsize(12) ysize(8)
                  Click image for larger version

Name:	Graph2.png
Views:	1
Size:	21.7 KB
ID:	150403


                  See also: ADePT Social Protection program for analysis of social protection systems, and ADePT Results-Based Financing (RBF) for analysis of administrative data at facilities level.
                  Both modules available from http://www.worldbank.org/adept

                  Best, Sergiy Radyakin

                  PS: this is relationship between state and yearm, when nonzeroes are in the same column, those observations will be contributing to the same graph (we do not retain state/grade, we collapse by year and yearm only).

                  Code:
                     current |
                       grade |                                    yearm
                   completed |      2000       2001       2002       2003       2004       2005       2006 |     Total
                  -----------+-----------------------------------------------------------------------------+----------
                           0 |        21          0          0          0          0          0          0 |        21
                           1 |         6          0          0          0          0          0          0 |         6
                           2 |         4          0          0          0          0          0          0 |         4
                           3 |         0          2          0          0          0          0          0 |         2
                           4 |         0         36          0          0          0          0          0 |        36
                           5 |         0         41          0          0          0          0          0 |        41
                           6 |         0          0        161          0          0          0          0 |       161
                           7 |         0          0        262          0          0          0          0 |       262
                           8 |         0          0        671          0          0          0          0 |       671
                           9 |         0          0          0        889          0          0          0 |       889
                          10 |         0          0          0      1,518          0          0          0 |     1,518
                          11 |         0          0          0      1,781          0          0          0 |     1,781
                          12 |         0          0          0          0     14,252          0          0 |    14,252
                          13 |         0          0          0          0      1,734          0          0 |     1,734
                          14 |         0          0          0          0      1,751          0          0 |     1,751
                          15 |         0          0          0          0          0        950          0 |       950
                          16 |         0          0          0          0          0      2,681          0 |     2,681
                          17 |         0          0          0          0          0        851          0 |       851
                          18 |         0          0          0          0          0          0        921 |       921
                  -----------+-----------------------------------------------------------------------------+----------
                       Total |        31         79      1,094      4,188     17,737      4,482        921 |    28,532

                  Comment


                  • #10
                    Hi Mike,

                    This is a bit confusing -- let me try to clarify.
                    I am collapsing by interview year, and not by mandate year. I wanna see how coverage evolves across interview years. I want one graph per group of states that passed a mandate in the same mandate year.
                    If I get back to my previous example:
                    CA and CO passed a law in 2008. MA and MN passed a law in 2009. I want one graph with CA and CO only that shows the total number of people enrolled in each type of plan (HMO etc) across interview years. I want to do the same for MA and MN, but in a separate graph.
                    Since I use interview year for the collapse command, I actually "lose" the mandate year variable in the process.

                    Using the variables from Sergiy's example, I was actually thinking about doing the following:
                    bysort year: egen t_south = total(south)
                    bysort year: egen t_union = total(union)
                    levelsof birth_yr, local(yearlist)
                    foreach y of local yearlist {
                    twoway connected t_south t_union year if birth_yr == `y'
                    }
                    Note: Here "year" is my interview year, "birth_yr" is my "mandate year", "south" is "HMO" and "union" is "employer-insurance".
                    I guess this would work, right?

                    Now, I have another issue: my types of plans are actually divided into two groups : (i) HMO, employer-insurance . for people living with their parents and (ii) HMO, employer-insurance for people living without their parents.
                    Could I have the lines for all types in one graph, but have a color code to highlight the fact that there are two groups?
                    For instance: HMO w/ parents in blue versus HMO w/o parents in red?
                    I am also having trouble changing the legend below the graph. I have lots of variables, and keeping the variable names looks messy, unfortunately. Any guidance would be very much appreciated!

                    Thank you very much for your help. I am really grateful!


                    Comment


                    • #11
                      Code:
                      version 12
                      use "http://www.stata-press.com/data/r12/nlswork.dta", clear
                      // south is your HMO, union is your emp_ins, grade is your state
                      // we generate year of mandate, and it is constant by state, but may differ between states
                      // c_city is parents or not attribute of HMO
                      rename grade state
                      generate int yearm=2000+floor(state/3)
                      tabulate state yearm
                      separate south, by(c_city)
                      collapse south0 south1 union , by(year yearm)
                      label variable south0 "HMO not parents"
                      label variable south1 "HMO parents"
                      
                      label variable union "Employee insurance"
                      twoway line south0 south1 union year, ///
                         by(yearm, row(3) title("Dynamics of coverage by year of mandate")) ///
                         lcolor(red pink blue) xsize(12) ysize(8)
                      Click image for larger version

Name:	Graph3.png
Views:	1
Size:	37.7 KB
ID:	150526

                      Comment


                      • #12
                        I find myself further confused here, with your introduction of new questions and the like. Perhaps others can discern your intent better than I, so I'll leave it to them. My suggestion would be for you to show an illustration of your raw data, as might come from a -list- command, so as to make clear what your variables are, the nature of their measurement, and the unit of analysis. Others may be able to guess that, but I find that information is almost always helpful in getting a good answer. You could find examples of that in other posts in this forum.

                        Regards, Mike

                        Comment


                        • #13
                          Thank you so much, Sergyi. This is incredibly helpful!
                          Just one more question: do you know a way to get all of the lines for the "with parents types" (HMO with parents, employer-insurance with parents) in one color, and all of the "without parents" types in another color?
                          Again, thank you very much.

                          Mike: No worries, I agree that this is very confusing. Thank you very much for your help. I learned a lot from the code you sent me this morning!

                          Comment


                          • #14
                            Originally posted by Ana Carla View Post
                            do you know a way to get all of the lines for the "with parents types" (HMO with parents, employer-insurance with parents) in one color...?
                            How are you going to determine which one is which? You can certainly do this, but the graph becomes unusable then. The option in question is:
                            lcolor(red pink blue)
                            which lists the colors in the same sequence as the variables to plot. I have chosen two similar, but still different colors (red and pink) for the lines I wanted to show belonging to one group. The specifics depend on the future intended use of the graph: are you going to put in online? show on a screen via projector? publish in a journal? Each media may have restrictions on number and accuracy of colors. There are other dimensions beyond color, which are line thickness (width) and line pattern (various dash-dot patterns). Their use is advisable, e.g. for cases when you expect your graphs may be printed on a black-and-white printer.

                            To resume: do not use SAME color for different lines on your graph. There are only few exceptions from this rule. One is shown on slide 70 (just think of it, slide SEVENTY!) here.

                            Best, Sergiy

                            Comment


                            • #15
                              Hi Sergiy,

                              Thank you very much for your advice. This is good to know!
                              These graphs may not be for publication -- for now I want to get a grasp of the coverage dynamics. In case I do keep them for the paper, I will stick to different colors then.

                              Thank you again. You really helped me a lot with this!

                              Ana

                              Comment

                              Working...
                              X