Announcement

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

  • Write ado command with function defined on Mata

    I am writing an ado file and I use structures like:
    program define XX, eclass
    myfunction()
    ereturn matrix V = `V'
    end


    mata:
    myfunction(
    calculate V
    st_matrix(`V', v)
    )
    end

    I use trace and it tells me that ereturn matrix V = __000003 invalid syntax
    I use -ereturn post `V' before, but it gives back matrix __000003 not found. (but matrix V existed if I use matrix list V instead)
    I think I already changed the calculated V into matrix in st_matrix. Do I need mat `V' = e (V) somewhere?

    Thanks!
    Last edited by Xiduo Chen; 09 Nov 2021, 02:08.

  • #2
    You need to declare the variables you are returning correctly. The code should look like:

    Code:
    program define XX, eclass
    tempname V
    mata myfunction("`V'")
    ereturn matrix `V'
    end
    
    mata:
    function myfunction(string scalar VName)
    real matrix v_int
    v_int = rnormal(10,10,0,1)
    st_matrix(VName,v_int)
    end
    In the Stata program you create a temporary name V, which is then passed to the mata program. Within the mata program you create/do calculations with the real matrix v_int, which is then passed to Stata using st_matrix().
    Last edited by JanDitzen; 09 Nov 2021, 03:45.

    Comment


    • #3
      Thanks a lot! That partially solved my problem.
      The trace told me that I have conformability error when I use ereturn post V. (I am sure V is really a matrix by listing it and I also tried to transpose it into column vectors)

      Comment


      • #4
        It seems I need to change the name and use column vector

        Comment

        Working...
        X