Announcement

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

  • Getting blank graph when using if condition with kdensity: Stata version 17.0 and Windows 10

    Hello,

    I am trying to generate a kdensity function with a if condition and specifying bandwidth and getting a blank graph. I am sharing the description and summary stats of the two variables (one continuous and one categorical) as well as the codes I am using for kdensity and graph that I am getting when I use the if condition and when I do not. It will be very helpful if you can let me know what am I doing wrong.

    Description and summary statistics


    Code 1: kdensity function without specifying an if condition
    kdensity prop_idealfamsize_girl, bwidth(5)

    Result of Code 1


    Code 2: kdensity function specifying an if condition
    kdensity prop_idealfamsize_girl if v013==15-19, bwidth(5)


    Result of Code 2






  • #2
    Code:
    kdensity prop_idealfamsize_girl if inrange(v013,15,19), bwidth(5)

    Comment


    • #3
      Originally posted by Øyvind Snilsberg View Post
      Code:
      kdensity prop_idealfamsize_girl if inrange(v013,15,19), bwidth(5)
      Thanks so much! I used your code but get same blank graph. Also, I shared the tab results for v013. It takes values 15-19, 20-24,... and so on. It does not take values 15,16,17.... I imagine the inrange could work if it was the latter.

      Comment


      • #4
        my bad,
        Code:
        kdensity prop_idealfamsize_girl if v013=="15-19", bwidth(5)

        Comment


        • #5
          Originally posted by Øyvind Snilsberg View Post
          my bad,
          Code:
          kdensity prop_idealfamsize_girl if v013=="15-19", bwidth(5)
          Thanks. I am sorry about replying late. v013 is a stored as a byte and not a string. My original code 2 is same as the one you shared and that is what gives me a blank.

          Comment


          • #6
            I found a solution. It is not elegant but gets the work done. When I convert v013 to a string variable using decode, the code (
            kdensity prop_idealfamsize_girl if v013=="15-19", bwidth(5)) works! Still not sure why it gives blank when if condition is on a numeric variable but I get the graphs I want. Thank you.

            Comment


            • #7
              The variable was numeric and 15-19 evaluates to -4. So, the code was legal but you had no such values. Otherwise put, you were confusing values and value labels.

              Comment


              • #8
                Note that there is a syntax for specifying observations you want using value labels. See e.g. https://www.stata-journal.com/articl...article=dm0009

                Here is an example:


                Code:
                . sysuse auto, clear
                (1978 automobile data)
                
                . list make if foreign=="Foreign":origin
                
                     +----------------+
                     | make           |
                     |----------------|
                 53. | Audi 5000      |
                 54. | Audi Fox       |
                 55. | BMW 320i       |
                 56. | Datsun 200     |
                 57. | Datsun 210     |
                     |----------------|
                 58. | Datsun 510     |
                 59. | Datsun 810     |
                 60. | Fiat Strada    |
                 61. | Honda Accord   |
                 62. | Honda Civic    |
                     |----------------|
                 63. | Mazda GLC      |
                 64. | Peugeot 604    |
                 65. | Renault Le Car |
                 66. | Subaru         |
                 67. | Toyota Celica  |
                     |----------------|
                 68. | Toyota Corolla |
                 69. | Toyota Corona  |
                 70. | VW Dasher      |
                 71. | VW Diesel      |
                 72. | VW Rabbit      |
                     |----------------|
                 73. | VW Scirocco    |
                 74. | Volvo 260      |
                     +----------------+
                In short, you don't need to decode. Also, if you looked up the value under the value label, you could use that directly. In your case the categorical variable looks like age in intervals, and
                Code:
                label list
                would remind you of the labels. .

                Giving us a data example as requested in the FAQ Advice would have speeded up this thread.

                Comment

                Working...
                X