Announcement

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

  • plot stepwise functions using tw function?

    Dear list members,

    is there a way to plot stepwise functions using twoway function? I tried using cond(), but it doesn't seem to be working. I know I could use pci or similar, but I was still wondering. To be clear, I don't have variables, I just want to plot immediate arguments.
    I'm using StataNow/MP 18.5

  • #2
    What you call stepwise may be what I would call segmented. This works for me:


    Code:
     twoway function cond(x < 1, x, 2 * x - 1), ra(0 2)
    If that doesn't answer your question, we may need more detail and to see your code. Similarly I don't see a difference between immediate arguments and specifying functions concretely.

    Comment


    • #3
      Yes, stepwise/piecewise/segmented. I'm trying to nest various cond(). I can't see the mistake - though I'm pretty sure it's there.

      Code:
      tw function cond(21.11<=x<26.62,0.992,cond(26.62<=x<32.17,0.925,0.925)), ra(20 40)
      I can't have
      Code:
      j<x<k
      expressions? It accepts with inrange(), but there's a problem at the discontinuities.
      I'm using StataNow/MP 18.5

      Comment


      • #4
        Your syntax is legal but Stata implements each binary operation before it looks at the next. 21.11 <= x <= 26.62 will always evaluate as 1. You need & and some extra parentheses in there. Stata does treat that kind of expression as you would in school mathematics.

        Comment


        • #5
          Ahh, true. But it doesn't seem to be able to accept the convention that the plot of the discontinuity requires a perpendicular segment (with the empty or filled points not being present here, ok, but still...).

          Code:
          tw function cond((x>0)&(x<=1),1,2), lw(vthin) ra(0 2) || pci 1 1 2 1, lw(vthin) legend(off)
          With pci, I get bad corners.
          I'm using StataNow/MP 18.5

          Comment


          • #6
            twoway function does not analyze the function you give it, and see if there are any discontinuities, maxima, minima, saddle points, ... and adjust the graph accordingly. twoway function just creates a set of, by default 300, x-values regularly spaced between the two numbers in the range() option, evaluates the function at each of these x-values, and draws a straight line between them. So, if the value at which the function is evaluated is not exactly at the point of the discontinuity, you get a point slightly to the left of the discontinuity and a point slightly to the right of the discontinuity connected by a straight line, i.e. it is slightly slanted and not perpendicular. You can decrease the slant by increasing the number of x-values, i.e. decrease the distance between those x-values. You do that with the n() option, e.g.

            Code:
            tw function cond((x>0)&(x<=1),1,2), lw(vthin) ra(0 2) n(5000) || pci 1 1 2 1, lw(vthin) legend(off)
            looks fine on my laptop, but if that is not good enough for your application nothing stops you from going n(10000). Well, there is a trade-off: It takes longer to calculate, and it increases the amount of memory that is needed to store that graph in many formats.
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment


            • #7
              twoway function does not analyze the function you give it, and see if there are any discontinuities, maxima, minima, saddle points, ... and adjust the graph accordingly. twoway function just creates a set of, by default 300, x-values regularly spaced between the two numbers in the range() option, evaluates the function at each of these x-values, and draws a straight line between them. So, if the value at which the function is evaluated is not exactly at the point of the discontinuity, you get a point slightly to the left of the discontinuity and a point slightly to the right of the discontinuity connected by a straight line, i.e. it is slightly slanted and not perpendicular. You can decrease the slant by increasing the number of x-values, i.e. decrease the distance between those x-values. You do that with the n() option, e.g.

              Code:
              tw function cond((x>0)&(x<=1),1,2), lw(vthin) ra(0 2) n(5000) || pci 1 1 2 1, lw(vthin) legend(off)
              looks fine on my laptop, but if that is not good enough for your application nothing stops you from going n(10000). Well, there is a trade-off: It takes longer to calculate, and it increases the amount of memory that is needed to store that graph in many formats.
              ---------------------------------
              Maarten L. Buis
              University of Konstanz
              Department of history and sociology
              box 40
              78457 Konstanz
              Germany
              http://www.maartenbuis.nl
              ---------------------------------

              Comment

              Working...
              X