Announcement

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

  • Intersection of Kdensity Curves

    Hey Statalist, this is actually the first time I can't find a solution to my problem in the forums, therefore is my first post.

    I need to find the value in the x-axis when two kdensity curves intersect, and I can't find a way for Stata to do it.
    I can visually see the intersection when I use:

    twoway (kdensity var1 if var2==1) (kdensity var1 if var2==0)

    (I need to this analysis for many years, and basically in all times the curves only intersect once.

    I appreciate all the help and thanks =)


  • #2
    You need to generate the densities as variables before you can compare them numerically.

    Comment


    • #3
      Thanks Nick but I can't seem to do it with the if command:

      kdensity var1 if var2==0 , kernel(parzen) gen(den) nograph

      Returns invalid syntax

      Comment


      • #4
        As the help tells you, two variable names are needed for the generate() option.

        Comment


        • #5
          Nick Cox Hi,
          I do have the same problem than Benjamin. I can visually see where the density functions intersect, as seen in the attached picture, but I would need more exact value.

          When I use the equation Benjamin mentioned in the #1''twoway (kdensity var1 if var2==1) (kdensity var1 if var2==0'', I get a graph that does not say anything (attached). I do have the densities as variables as you pointed out in the #2.


          NonTarget-Target density intersection value.gph
          Intersection value.gph

          Comment


          • #6
            .....Click image for larger version

Name:	1.png
Views:	3
Size:	332.9 KB
ID:	1531962 Click image for larger version

Name:	2.png
Views:	1
Size:	291.2 KB
ID:	1531961
            Attached Files

            Comment


            • #7
              You have more problems than you know. Evidently your variable is bounded, but the kernel density estimation routine doesn't know or care. As your graph implies, probability has been smeared below 0 and above 1. The remedy for that depends on the precise character of the data, but an ideal is to estimate the density function on logit scale and transform back.

              There is no example data here to experiment. Nor is it clear whether

              1. Your variables var1 and var2 (it's a good idea to choose evocative variable names) are defined in the same observations.

              2. How you generated new variables for the densities.

              https://www.statalist.org/forums/help#stata is key reading, please, for showing us the exact form of your data and your code in reproducible form. (It also explains that .gph attachments are not as helpful as you hope.)

              Comment

              Working...
              X