Announcement

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

  • How to graph two variables and the growth rate of a variable

    Hi,

    I have a problema that I just cant resolve.

    I have a dataset with more than 120.000 obs and 66 variables.

    I want to graph 1 variable called "Id_momento" (which goes from 1 to 6) through time. If I tab this variable with year I have the next table:


    Click image for larger version

Name:	Id momento.png
Views:	3
Size:	18.2 KB
ID:	1499659


    So what I want to do is create 6 graphs (1 for every id code) for the variable Id_momento. I have the next code:

    egen sum=sum(Id_momento) if Id_momento==1, by (Año)
    twoway (line sum Año), by (Id_momento)

    But that only graphs "Preinscritos" and leaves the other graphs empty.

    Also I wish to graph the growth rate of this variable for each identificator (1 to 6) but I dont know how to procced to create the variable growth rate.

    If you could help me or point me in the right direction it would help me a lot.

  • #2
    The following code is untested as you did not provide example data, so I don't guarantee it.

    Code:
    contract id_momento ano, freq(counts)
    by id_momento (ano), sort: gen growth= (counts-counts[_n-1])/counts[_n-1]
    
    graph twoway connect growth ano, by(id_momento) name(growth, replace)
    graph twoway connect counts ano, by(id_momento) name(counts, replace)
    The above code will create a separate panel for each of the 6 levels of id_momento and combine them into a tableau. You will have one tableau for growth rates and one for the totals.

    If you would prefer to have the 6 growth rates in a single panel, and the 6 totals in another panel, the code below will do it that way. But graphs with multiple lines like this can be difficult to read ("spaghetti graphs").

    Code:
    reshape wide growth counts, i(ano) j(id_momento)
    graph twoway connect growth* ano, name(growth, replace)
    graph twoway connect counts* ano, name(counts, replace)
    Last edited by Clyde Schechter; 22 May 2019, 16:31. Reason: Correct error in originally posted code

    Comment


    • #3
      Clyde thank you very much, I would have never thought this. This showed me the comm that i needed to do the same thing with other variables that I need to graph.

      Thank you very much.

      I would attach the result of your code.

      Freq Graph:
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.8 KB
ID:	1499665


      Growth rate Graph

      Click image for larger version

Name:	Graph 2.png
Views:	1
Size:	19.2 KB
ID:	1499666

      Comment

      Working...
      X