Announcement

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

  • drawing vertical line on a scatter plot

    I am trying to run the following Stata command:

    scatter AUC Alpha xline(bestAlpha, lcolor(blue))

    , where bestAlpha is a global macro storing the value of 0.0.

    When I just execute the command:

    scatter AUC Alpha

    everything works fine, but when I try to add a vertical line by adding 'xline(bestAlpha, lcolor(blue))' the program would result in error, and I am not sure why.

    Just to provide more detail, when I do:

    list bestAlpha

    Stata gives me the list of length 124 with a value (=0) assigned at index 1, and the rest of the entries are just missing values

    when I do:

    display bestAlpha

    however, Stata gives out its correct value 0.

    What should I do to fix my error? The error message is:

    Invalid varlist

    Thank you,

  • #2
    Well, if what you want is a vertical line at 0, why go through the rigmarole of creating a "variable" that in fact contains only the single value 0 in a single observation? Why not just, in the -scatter- command specify the option as -xline(0, lcolor(blue))-.

    More generally, if it isn't always going to be 0 but might be some other value, you can indeed store that in a macro--still no need for a variable. In that case, you have to refer to the macro correctly. If you store it in a global macro bestAlpha, then the option to scatter would be -xline($bestAlpha, lcolor(blue))-. But let me also point out that a better programming practice is to use a local macro, because it is safe from interference by other programs and contexts. So I would store it in a local macro, and then -xline(`bestAlpha', lcolor(blue))-.

    In any case, you cannot specify -xline(bestAlpha, lcolor(blue))- because bestAlpha, without either $ or `', is a variable name, and the first argument of -xline- cannot be a variable: it must be a number.

    While we're here, let's clear up some confusion you have regarding the -list- and -display- commands.

    -list- is used to show (all or parts of) the data set. When you type -list bestAlpha- you are asking Stata to show you all of the values in the data set variable bestAlpha. So if variable bestAlpha contains 0 in the first observation and is missing in all others, you will get exactly what you saw. By contrast, -display- is not intended for showing the values of variables in the data set. It is intended to show messages and macros. So if you had 0 stored in local macro bestAlpha, -display `bestAlpha'- would cause Stata to print 0 on the next line. When you write -display bestAlpha-, without anything referencing a macro, Stata then sees that you also have a variable named bestAlpha. -display- is not able to list all of the values of a variable. So, by convention, it interprets this as a request to display the value of the variable in the first observation. In other words, -display varname- is equivalent to -display varname[1]-. And in your situation, it leads Stata to print 0.

    Comment


    • #3
      Let me add to Clyde's answer that it is apparent from this post and your previous posts that you are unclear on the difference between a local Stata macro, a global Stata macro, and a Stata variable. In this topic, and in your previous topic, you have referred to Stata variables as Stata global macros. In that earlier topic, we see the code
      Code:
      // transfer the python variables Y_mnb_score and test_compliance as global STATA variables
      python: Data.setObsTotal(len(Y_mnb_score))
      python: Data.addVarFloat('mnbScore')
      python: Data.store(var = 'mnbScore', obs = None, val = Y_mnb_score)
      In it you use the Python Data class, and the the Stata Function Interface description as part of the python command documentation in the Stata Programming Reference Manual PDF tells us

      This class provides access to the Stata dataset in memory.
      Thus mnbScore is a Stata variable in the Stata dataset in memory.

      That same documentation also tells us for the Macro class

      This class provides access to Stata macros.
      If you return data from Python to Stata using the Data class, the data is stored in a Stata variable, while if you use the Macro class, the result will be a local or global Stata macro.

      While the Stata variables may seem to you to be "global" in the sense that both Stata and Python have access to them, in point of fact the syntax of the Stata language has a longstanding, particular, technical meaning for the word "global" that applies to Stata macros and does not apply to Stata variables, and global macros (as well as local macros) are very different from variables included in a Stata dataset.

      It would help you to review section 18.3 of the Stata User's Guide PDF to improve your understanding of Stata macros. Note that all Stata documentation PDFs are included in your Stata installation and accessible from Stata's Help menu.

      Comment


      • #4
        Thank you both! much appreciated.
        However, even when I try to place a vertical line by writing -scatter AUC Alpha xline(0, lcolor(blue)-, Stata still throws an error message of 'invalid varlist'....
        I also tried setting the -bestAlpha- as local or global macro instead of a variable, but in those cases I also got the error message of 'invalid varlist'.
        Yet when I just do -scatter AUC Alpha- , the scatter plot is generated successfully.

        How should I go about this? Thanks again,
        Last edited by Dominique Bourget; 15 Oct 2019, 04:09.

        Comment


        • #5
          Hello,
          I just noticed that I am missing a comma (,) in my original scatter statement:

          use:

          -scatter AUC Alpha, xline(bestAlpha, lcolor(blue))-

          instead of

          -scatter AUC Alpha xline(bestAlpha, lcolor(blue))- (notice that I initially placed no comma between Alpha and xline

          after inserting the comma after 'Alpha', I am getting an error message that says "invalid line argument, bestAlpha", although 'bestAlpha' is appropriately defined as a variable.

          How can I resolve this issue? Thank you,

          Comment


          • #6
            As Clyde already explained in #2 you can't use a variable name, as first argument to xline() even if the variable happens to be constant. It's best just to use a number directly.

            If there were programming reasons to use a variable name, you need to ensure evaluation first. This should work out if and only if the desired value is constant across observations.

            Code:
            scatter AUC Alpha, xline(`=bestAlpha[1]', lcolor(blue))-
            but judging from your posts that is a bit tricky at your stage. Something like this is much simpler and should work.


            Code:
            scatter AUC Alpha, xline(0, lcolor(blue))

            Comment


            • #7
              This worked! Thank you so much

              Comment

              Working...
              X