Announcement

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

  • Problem exporting matrix from mata ado

    Hi -- I've written a brief ado file to spit back characteristics of parametrized sinusoids (specifically, a 4-term Fourier series). The code is below. The mata code works fine when I run it on it's own --- say with a= .0005769, b= .0422043, c= .0131597, d= -.0168322 --- as an individual mata session. All four outputs (minimum, maximum, amplitude, lowpoint) are calculated, and all four also stored into a stata matrix that I can access after I end mata. However, when I call the ado as in the context at the bottom, it exports all matrices except lowpoint. I think I'm using minindex correctly --- it's underlined in red in the ado file whenever I store the index as anything but the letter i, but it does seem to work despite that --- and again, lowpoint *does* export to a stata matrix in individual mata sessions. Just now when run through the ado file. Any idea what's going on? Thank you!

    ** ADO FILE:
    Code:
    /* User written command by Leah E. M. Bevis */
    /* December 2018 */
    /* Input: coefficients (a,b,c,d) from the terms of this 4-term Fourier series: */
    /* y = k + a*cos(2*pi*t/T1) + b*sin(2*pi*t/T1) + c*cos(2*pi*t/T2) + d*cos(2*pi*t/T2) */
    /* Output: maximum, minimum, amplitude, index of lowest point */
    
    program sinusoid
    version 14.2
        args a b c d
        mata: FourierAmp(`a',`b',`c',`d')
    
    end
    
    version 14.2
    mata: 
    void FourierAmp(a, b, c, d) {
    
        t = (1::365)
        t=t/365
        
        T1=1
        omega1=2*pi()/T1
        T2=1/2
        omega2=2*pi()/T2
    
        y=a*cos(omega1*t) + b*sin(omega1*t) + c*cos(omega2*t) + d*sin(omega2*t)
    
        lowpoint=999
        w=999
        minindex(y,1,lowpoint,w) 
        
        minimum=min(y)
        maximum=max(y)
        amplitude=maximum-minimum
    
        st_matrix("minimum", minimum)
        st_matrix("maximum", maximum)
        st_matrix("amplitude", amplitude)
        st_matrix("lowpoint", lowpoint)
    
    }
    end

    ** CONTEXT FOR USE
    Code:
    reg Y csmnth snmnth csmnth2 snmnth2 
            local a = _b[csmnth]
            local b = _b[snmnth] 
            local c = _b[csmnth2]
            local d = _b[snmnth2] 
            sinusoid `a' `b' `c' `d'           
    mat lis amplitude
    mat lis minimum
    mat lis maximum
    mat lis lowpoint

  • #2
    I think your code works fine. I saved your Stata and Mata code as sinusoid.ado and then run it with your sample values:

    Code:
    . sinusoid .0005769 .0422043 .0131597  -.0168322
    
    . mat list lowpoint
    
    symmetric lowpoint[1,1]
         c1
    r1  256
    So the index of the minimum is 256, which one can easily verify is correct with your input parameters.

    I often find useful to decouple the Stata and Mata code as much as possible. One way is to modify FourierAmp() to return a 1x4 matrix, just change void to matrix and instead of calling st_matrix() four times, simply assign r = (minimum, maximum, amplitude, lowpoint) and then return(r). This way you can call it with various inputs and easily see the output. When you call it from the ado file you can store the result in a Stata matrix and even name the columns appropriately.

    Comment


    • #3
      This is so odd; you're right, the code is working fine now. There must have been some sort of temporary memory thing going on with my copy of Stata? I have no idea what happened; apologies for wasting your time! However, yes, great point -- I'll assign everything into r to return it together, that's a great point. Thank you for suggestion!

      Comment

      Working...
      X