Announcement

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

  • Using scalar or other saved result in graphics

    I'd like to calculate a statistic, store that statistic, and use it in a graphics command. For example, I calculate the mean, store that mean as a scalar, and would like to plot an xline in a density plot at that mean value.
    I see that xline only excepts numlists. Is there a workaround?

    A reproducible example:
    Code:
    sysuse auto.dta, clear
    sum price
    scalar mp=r(mean)
    kdensity price, xline(mp)
    Returns the following error "invalid line argument, mp"

  • #2
    David,

    This should work...

    Code:
    sysuse auto.dta, clear
    sum price
    kdensity price, xline(`=r(mean)')
    Best,
    Lance

    Comment


    • #3
      Code:
      local mp =r(mean)
      Or

      Code:
      xline(`=mp')
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thanks for your replies. Lance's solution works for producing a simple 1 graphic solution
        Code:
        sysuse auto.dta, clear
        sum price
        kdensity price, xline(`=r(mean)')
        But I couldn't get Maarten's solution to work with, let's say, 2 categorical conditional variables.
        Code:
        sum price if foreign==1
        local mpf =r(mean)
        sum price if foreign==0
        local mpnf =r(mean)
        kdensity price, xline(`=mpf') xline(`=mpnf')
        Advice?

        Comment


        • #5
          [code]
          kdensity price, xline(`mpf') xline(`mpnf')
          [/code

          Comment


          • #6
            Some confusion here. Note that a local say mpf is evaluated by just using the appropriate quotation marks, say `mpf'

            In contrast a scalar would need to be evaluated on the fly with extra equals sign, but that same evaluation would not work for locals.

            Code:
            . local foo = 42
            
            . di "`= foo'"
            foo not found
            
            
            . di "`=foo'"
            foo not found
            Lance is bang on that there is no need to push a r-class result into a local and pull it out again, unless you need to store it temporarily.

            I see no need for scalars here at all as the minute extra precision they hold is immaterial for your graphical purpose. They would do no harm except that is a little harder work to set them up to appear and disappear.

            Putting this all together, this will be found to work (setting aside the irrelevant detail that the local macro names don't suit their meaning):

            Code:
            sysuse auto, clear
            sum price if foreign==1
            local mpf = r(mean)
            sum price if foreign==0
            kdensity price, xline(`mpf' `r(mean)')

            Comment


            • #7
              Alternatively, you can replace local with scalar in #4

              Edit: Crossed with Nick. See why this is not a good solution.
              Last edited by Andrew Musau; 24 Oct 2016, 18:12.

              Comment


              • #8
                Now resolved, thank you. Your comments and explanation were helfpul.

                Comment


                • #9
                  Hi David, I'd be curious to see how you resolved the issue with two stored statistics, if you still have this code. I am struggling with the same.

                  Comment

                  Working...
                  X