Announcement

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

  • Panel data: Plot line graph for 2 periods before and after state change

    Hi everyone,

    I have a panel data set which looks approximately like this:
    id time crime income
    1 1 0 2000
    1 2 0 1800
    1 3 1 1000
    1 4 0 1200
    1 5 0 1400
    2 1 0 1500
    2 2 1 1100
    2 3 1 1400
    2 4 0 1700
    2 5 0 2000
    For every person, I have data on 100 periods, with a variable saying whether they committed a crime in that period, and what their income was in that period.

    I would like to plot a graph where I show for the average income of all offenders on t-2, t-1, t=0 (when crime = 1), t+1 and t+2. So basically, I want to show the average income of offenders two periods before and two periods after committing the crime.

    It should look something like this:

    Click image for larger version

Name:	Screenshot 2019-07-11 at 13.43.41.png
Views:	1
Size:	11.4 KB
ID:	1507146

    where the line is at t=0 (where crime = 1), in this case one can see average income from t-8 until t+8.

    I have no idea how to get to such a graph, so it would be great if someone could help me out!

  • #2
    Your example is helpful but immediately raises the question of what you want done if there are crimes at two or more times for each individual. I am going to assume that you want to use the first time.

    Here is some technique,

    Code:
    clear 
    input id    time    crime    income
    1    1    0    2000
    1    2    0    1800
    1    3    1    1000
    1    4    0    1200
    1    5    0    1400
    2    1    0    1500
    2    2    1    1100
    2    3    1    1400
    2    4    0    1700
    2    5    0    2000
    end 
    
    egen origin = min(cond(crime == 1, time, .)), by(id)
    gen T = time - origin 
    egen mean = mean(income), by(T)
    The only tricky bit is getting as new time origin the reference data for each individual. For the technique here see https://www.stata-journal.com/articl...article=dm0055 especially Section 9. But there are many ways to do it, as an earlier FAQ underlines: https://www.stata.com/support/faqs/d...ing-last-date/

    Comment


    • #3
      Thank you for your answer, Nick. I am mainly interested in the first offence, so happy to only look at that one for now. I have tried your technique, but still not 100% sure how to get to the plot. If I understand it correctly, origin should give me the time period t in which the first offence was committed, T a difference between current period and that period and mean would give me the mean income for every difference in time period. How would I then in best case get to the plot?

      Comment


      • #4
        It's just a line plot of mean against T. You showed in #1 that you knew how to get such plots.

        Comment


        • #5
          Great, thank you! I think it worked

          Comment

          Working...
          X