Announcement

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

  • Charting frequency of counts

    I have a variable which describes the number of months between two incidents (start of incarceration and self-harm). It looks like this (where for observation 1 the person self harmed 43 months after the start of incarceration):

    . list diff_months in 1/10

    +----------+
    | diff_m~s |
    |----------|
    1. | 43 |
    2. | 24 |
    3. | 10 |
    4. | 2 |
    5. | 11 |
    |----------|
    6. | 10 |
    7. | 2 |
    8. | 19 |
    9. | 6 |
    10. | 24 |
    +----------+


    The variable is summarized as follows:

    . summarize diff_months

    Variable | Obs Mean Std. dev. Min Max
    -------------+---------------------------------------------------------
    diff_months | 880 25.40114 35.31061 0 276



    And here is an excerpt of its tabulation:

    . tab diff_months

    Number of |
    months |
    between |
    sentence |
    start & |
    incident | Freq. Percent Cum.
    ------------+-----------------------------------
    0 | 72 8.18 8.18
    1 | 55 6.25 14.43
    2 | 43 4.89 19.32
    3 | 48 5.45 24.77
    4 | 40 4.55 29.32
    5 | 44 5.00 34.32



    I want to create a frequency chart such as a line graph or a histogram that depicts how many individuals self-harmed after x months. So the x-axis should depict the variable from 0 to 276 and the y-axis should depict the frequency, say in % (so for 0 it would be 8.18%, for 1 it would be 6.25%, etc.).

    I have tried the command below but it produces strange frequencies as you can see in the attachment (the graph depicts 45% for 0 months when it should be 8% for example)

    . hist diff_months, percent
    (bin=29, start=0, width=9.5172414)


    What am I doing wrong? Thank you.


    Attached Files

  • #2
    You are misreading the graph. Pay attention to the explanatory output that Stata gave you immediately after your -histogram- command:
    Code:
    (bin=29, start=0, width=9.5172414)
    

    That tells you that there are 29 bins (vertical bars) in the graph, and that the first one starts at 0. But don't ignore width = 9.5172414, which tells you that each bar covers a range of values that is 9.5172414 wide. Otherwise put, the first bar gives the percentage of incidents occurring between 0 and 9.5172414 months. The next bar gives the percentage of incidents between 9.5172414 and 19.034483 months, etc.

    Now, that said, this may well not be what you want. If you want the first bar to be 0 only, and the next to be 1 only, etc. you can add the -width(1)- option to your -histogram- command. Then the first bar will cover only 0 and will show a percentage of 8.18. BUT, you pay a price for that. There will be 276 bars, and in order for them to fit across the graph, they will be very, very narrow, about one-tenth as wide as the bars you currently have. It might prove altogether unreadable.

    You might want to consider other kinds of graphical displays if you cannot get a -histogram- you are satisfied with. A simple line plot of percentage vs months might be a better approach for you.

    Comment


    • #3
      Thank you so much! This makes sense.
      I adjusted the bin width, and the frequency looks correct now. Here is the command I used and attached the generated histogram.

      . histogram diff_months2 , width(1)
      (bin=276, start=0, width=1)


      That said, I would much prefer a line plot as you suggest but am also struggling with that command. To do that, don't I need to plot 2 variables? How can I get a simple line plot of percentage vs my variable (diff_months2)? Would I need to create a new variable for frequency, and if so how? I have tried looking through STATA help and elsewhere but am unable to find a response. Thank you so much!
      Attached Files

      Comment


      • #4
        Code:
        contract diff_months2, percent(percentage)
        graph twoway line percentage diff_months2, sort
        See -help contract- for details about this command.

        Added: Just a reminder that attachments are discouraged here, especially Office documents, and especially Word documents. Some Forum members won't download attachments from people they do not know. This is especially the case for Word documents as they can contain active malware. Inserting the graph as a picture in your post is a safer, as well as more convenient, way to show it in this Forum.
        Last edited by Clyde Schechter; 24 Sep 2024, 15:50.

        Comment


        • #5
          This is so helpful. Thank you very much! The graph is exactly what I wanted now. And thanks also for the tip re attachments!

          Comment

          Working...
          X