Announcement

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

  • Parsing option arguments into mata

    Hi all,

    I am writting an ado file where I would like to use mata as well. I am having some difficulty parsing the options from my program into mata. Here is a simplified version of my problem

    Code:
    program define addition
    syntax varlist(max=1), deriv(real) x(real)
    mata : m_add("`varlist'", "`deriv'","`x'","`b'")
    display "b is"
    mmatrix list `b'
    end
    
    mata: 
    void m_add(string scalar vname, string scalar deriv, string scalar   x, string scalar mname)
    {
    
    real vector x 
    real scalar w height de
    
    x = st_data(.,vname)
    height = st_local(.,h)
    de = st_local(.,deriv)
    
    w = mean(data) + height + de
    st_matrix(mname,w)
    }
    end
    
    sysuse auto, clear 
    addition mpg, deriv(1) x(1)
    Ideally, I would like to parse deriv and x into mata, but I am having some trouble with them. Can someone help me out here? The correct solution should be 21.2973 + 1 + 1.

  • #2
    I see four problems with your code.
    1. You never define the local "b". So you need another option with the name "b".
    2. You have a typo. Instead of "mmatrix" it is "matrix".
    3. You define "x" two times. Once as a string scalar and second as a real vector.
    4. You use "st_local()" incorrectly. Currently, it puts into a local named "." the content of "h" or "deriv".
    I have no clue what you want to achieve with the locals, so you should post more detailed information about what exactly does not work.

    Comment


    • #3
      Hi,

      Thanks for the feedback. The problem I have right now is I want to take the options (deriv and x) in my program and use them in mata. I have a separate function written in mata that uses deriv and x for some calculations. I was wondering how I can parse the option values in to mata.

      Thanks.

      Best,
      Han

      Comment


      • #4
        What do you mean with "parsing the option values into Mata"? Your example code is already passing the contents of the options to Mata. You need to show exactly what you have done so far and what you want to achieve. Otherwise, I still have troubles understanding where your problem is.

        Comment


        • #5
          Code:
          clear all 
          set more off
          program varsum 
              syntax varname [if] [in], x(real)
              marksample touse
              mata: calcsum("`varlist'","`touse'","`x'")
              display as txt "sum = " as res r(sum)
          end
          
          mata
          void calcsum(string scalar varname, string scalar touse, string scalar x)
          {
          real colvector z 
          st_view(z,.,varname,touse)
          sum = colsum(z)
          st_numscalar("r(sum)", sum)
          }
          
          end
          
          
          sysuse auto
          varsum mpg, x(1)
          Sorry for the confusion, I think this is a simplified version of my problem. The above code works. However, when I introduce the options of my program into mata, the code fails. In particular I want to change this part of my code:

          sum = colsum(z). ========>. sum = colsum(z) + x

          where x is the "option" of varsum. So I was wondering is there something like a st_view, that allows you to parse specific values from Stata into mata? I really apologise for being very loose with my terminologies. Thanks.

          Comment


          • #6
            Try this.
            Code:
            clear all
            set more off
            program varsum
                syntax varname [if] [in], x(real)
                marksample touse
                mata: calcsum("`varlist'","`touse'",`x')
                display as txt "sum = " as res r(sum)
            end
            
            mata
            void calcsum(string scalar varname, string scalar touse, real scalar x)
            {
            real colvector z
            st_view(z,.,varname,touse)
            sum = colsum(z) + x
            st_numscalar("r(sum)", sum)
            }
            
            end
            
            sysuse auto
            varsum mpg, x(0)
            varsum mpg, x(3.1415)
            Code:
            . sysuse auto
            (1978 Automobile Data)
            
            . varsum mpg, x(0)
            sum = 1576
            
            . varsum mpg, x(3.1415)
            sum = 1579.1415

            Comment


            • #7
              Hi William,

              Thanks for the help. This solves my problem. On a side note, I was wondering if it is possible to directly parse the arguments of my program into mata without defining a function? For example

              Code:
              program define varsum
              syntax varlist [if] [in], x(real)
              marksample touse
              
              mata
              x = st_data(.,touse)
              h = st_local("x")
              "...some calculations..."
              end
              end
              If this is possible, can you point me to the correct place to read up on the syntax? I have been trying to find the documentation, but I haven't been successful. Thanks.

              Comment


              • #8
                I recently discussed this problem at

                https://www.statalist.org/forums/for...16#post1554216

                Comment


                • #9
                  Thanks, will look into it.

                  Comment

                  Working...
                  X