Announcement

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

  • Error " <istmt>: 3499 mm_root() not found" when finding the root using mm_root command

    Dear Statalisters,


    I have an unbalanced panel data, where among many other variables, I have: p(current price), eps1, dps, eps2 and x(implied cost of capital). All variables, except x, are known and carry a value. An excerpt of first 20 observations is shown below.
    p hvz1_eps dps hvz2_eps
    1. . . . .
    2. . .1247489 . .
    3. . . . .
    4. . . . .
    5. 1.91 . 0 .
    6. 3.72 . 0 .1975022
    7. 4.7 .2124444 0 .2172921
    8. 5.37 .2103094 .19 .2131406
    9. 6.72 .2066778 .23 .2100024
    10. 6.57 .2006544 .22 .
    11. 7.01 . .22 .
    12. . . . .
    13. . . . .2131791
    14. . .208495 . .
    15. . . . .
    16. 5.3 .2281662 .12 .2314164
    17. 5.39 .2250118 .27 .2278917
    18. 6.1 .2258492 .4 .2257041
    19. 6.71 .2245243 .32 .2224477
    20. 6.91 .2251943 .4
    My purpose is to find x using PEG model (by Easton (2004) by following formula:

    Click image for larger version

Name:	Capture.JPG
Views:	2
Size:	9.6 KB
ID:	1429963

    I've searched previous posts and found this back in 2010: https://www.stata.com/statalist/arch.../msg01113.html

    I made an effort to change the code to meet my setting:
    HTML Code:
         gen x=.
    mata
    v=J(1,1,.)
    st_view(v,.,"x p hvz1_eps dps hvz2_eps")
    function y(x,a,b,c,d) {
    return((.2131406+(x*0.19)-.2103094)/x^2)
    }
    for (i=1;i<=rows(v);i++) {
    r=mm_root(x=.,&y(),0,1,1e-9,1000,v[i,2],
    v[i,3],v[i,4],v[i,5])
    v[i,1]=x
    }
    end
    To follow previous post, my intuition was that I should use the first non-missing observation, so I used observation number 8 in the code (in bold). The code is supposed to find the value of x between lower and upper bound of 0 and 1. Moremata is installed from SSC. But the code ends up with error:
    Code:
    Code:
     <istmt>:  3499  mm_root() not found
    I am unfamiliar with Mata and it is kind of complicated for me. So I will be grateful if you guide me through this.

    Thanks in advance for your kind attention.

    Best,
    Mohsen

  • #2
    First step is to determine if mm_root() is installed on your system, type

    Code:
    mata: mata which mm_root
    If it is indeed installed, you should see the output like:

    Code:
    . mata: mata which mm_root
      mm_root():  lmoremata
    If otherwise, ssc install moremata again. Please report back if mm_rrot() is available and error persists.

    Comment


    • #3
      Dear Hua,

      Although I had installed moremata at the first place, it seems that it is only after restarting application that Stata can access mm_root. I did so and
      Code:
      <istmt>: 3499 mm_root() not found
      disappeared.

      But the original code only generates missing value for x variable when I run if for the first time. Output is shown below:

      Code:
      . gen x=.
      (6,448 missing values generated)
      
      . mata
      ------------------------------------------------- mata (type end to exit) ------------------------------
      : v=J(1,1,.)
      
      : st_view(v,.,"x p hvz1_eps dps hvz2_eps")
      
      : function y(x,a,b,c,d) {
      > return((.2131406+(x*0.19)-.2103094)/x^2)
      > }
      note: argument a unused
      note: argument b unused
      note: argument c unused
      note: argument d unused
      
      : for (i=1;i<=rows(v);i++) {
      > r=mm_root(x=.,&y(),0,1,1e-9,1000,v[i,2],
      > v[i,3],v[i,4],v[i,5])
      > v[i,1]=x
      > }
      
      : end
      --------------------------------------------------------------------------------------------------------


      Running the same code again brought following errors:

      Code:
      . gen x=.
      (6,448 missing values generated)
      
      . mata
      ------------------------------------------------- mata (type end to exit) ------------------------------
      : v=J(1,1,.)
      
      : st_view(v,.,"x p hvz1_eps dps hvz2_eps")
      
      : function y(x,a,b,c,d) {
      y() already exists
      r(3000);
      
      : return((.2131406+(x*0.19)-.2103094)/x^2)
      'return' found where almost anything else expected
      r(3000);
      
      : }
      expression invalid
      r(3000);
      
      : for (i=1;i<=rows(v);i++) {
      > r=mm_root(x=.,&y(),0,1,1e-9,1000,v[i,2],
      > v[i,3],v[i,4],v[i,5])
      > v[i,1]=x
      > }
      
      : end
      --------------------------------------------------------------------------------------------------------
      I really appreciate advises on this matter.

      Best Regards,
      Mohsen

      Comment


      • #4
        You made three "mistakes":
        1. You typed mata instead of mata: (note the colon!), so the code kept running even after the first mistake.
        2. You created the function -y-, which will keep existing until you either type i) clear mata, ii) mata: mata drop y() , or iii) restart stata. For good practice, you can start your do file with "clear all" or "clear mata"
        3. " the original code only generates missing value for x variable when I run if for the first time". That's because the second time you ran the code the variables (r, v, etc.) already existed. Unless you wrap that code inside a function, all the variables created will be global (you can see which variables currently exist with "mata: mata describe". Note that typing "clear mata" above will also solve this problem.

        Best,
        S

        Comment


        • #5
          Hi Sergio,

          Thank you for truly helpful comments. I followed your guideline and now the code runs smoothly.

          I share the code for future reference of those who need (usual disclaimer applies).

          Code:
          clear mata
          gen xx=.
          mata:
          v=J(1,1,.)
          st_view(v,.,"x p eps1 dps eps2")
          function y(x,a,b,c,d) {
          return((-(x^2)+((d+(x*c)-b)/a)))
          }
          for (i=1;i<=rows(v);i++) {
          r=mm_root(x=.,&y(),0,1,1e-9,1000,v[i,2],
          v[i,3],v[i,4],v[i,5])
          v[i,1]=x
          }
          end
          Best,
          Mohsen
          Last edited by Mohsen Jafarian; 19 Feb 2018, 01:13.

          Comment

          Working...
          X