Announcement

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

  • generating scatter plot with a variable and a local macro

    Hello,

    Is there a way to generate a scatter plot with a Stata variable and a local macro?

    I tried below:

    scatter AUC `alphaGrid'

    and I get the following error message:

    'variable alpha_grid not found'

    Thank you,

  • #2
    No.

    In your previous topic at https://www.statalist.org/forums/for...into-a-numbest you showed that you wanted to loop over a numlist, which is not a Stata variable, and I gave you advice on how to produce a numlist.

    In this topic you want a scatter plot, and help scatter tells us that the arguments are variables, not numlists, which is what I'm guessing the contents of the local macro alphaGrid were intended to be.

    The example in post #1 is incomplete; you do not show how the local macro alphaGrid was created. Based on the error message you report, it appears that you did something like
    Code:
    local alphaGrid alphaGrid
    or perhaps
    Code:
    python: Macro.setLocal("alphaGrid","alphaGrid")
    To produce a scatter plot, you need at a minimum a dataset with two variables - AUC and alphaGrid - and for each observation, the value of AUC will be plotted against the value of alphaGrid in that observation. So you should return your two Python lists to Stata as variables.

    If you need to loop over the values of a Stata variable, my response in the previous post suggests a way of doing that.

    Comment


    • #3
      Thank you very much for your help thus so far.
      I saw your reply and tried to import the python variable as a Stata local macro, but when I iterate the local macro in a loop, nothing is showing. and when I do -di "`alphaGrid'"-, the code runs without error but nothing is showing up neither. I don't think the -Macro.setLocal("alphaGrid",alpha_str)- is working for me.

      A part of my code is below:

      python:

      # implementing grid search for Laplace smoothing in MultinomialNB on Python
      # test the model for alpha = 0.1, 0.6, ...., 4.6
      # set the range for alpha
      parameters = {'alpha':np.linspace(0.1,4.6,10)}

      # right now the parameter is a list of dictionary that stores a numpy array
      # the code below extracts the numpy array stored under the name 'alpha' and
      # converts the numpy array into a python list before exporting it on stata
      alpha_grid = parameters['alpha'].tolist()
      alpha_str = ' '.join(str(e) for e in alpha_grid)
      Macro.setLocal("alphaGrid",alpha_str)

      end

      foreach a1 of local alphaGrid {

      display " `a1' "

      } // the output is empty
      Last edited by Dominique Bourget; 18 Oct 2019, 15:38.

      Comment


      • #4
        Post #3 is not relevant to the current topic of producing scatter plots. For a scatter plot you will need two variables returned from Python to Stata.

        I have copied post #3 to https://www.statalist.org/forums/for...into-a-numbest and replied there.

        Comment

        Working...
        X