Announcement

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

  • Problem when programing with matrix - mata and spmat: codes run with data but not on programing

    Hi everyone,
    I am trying to create a STATA command for econometric analysis, in which there is a command that involves creating data columns that are eigenvectors derived from a spatial connection matrix.
    When I test with real data, my commands work. But when I try to program them into a command, it gives an error.
    The code and results are attached at the end of the post.
    I have tried to find the logic error and read the documentation on matrices and programming in STATA but have not been able to fix the error. I hope to receive help from experts in STATA and mata.
    Thanks in advance.

    When I test with real data, my commands work:
    Code:
    *    Generate data for W matrix
    clear all
    qui {    // Open qui
    input v1 v2 v3 v4
    0    0.1  0.3  0.2
    0.1  0    0.2  0.3
    0.3  0.2  0    0.2
    0.2  0.3  0.2  0
    end
    spmat dta W v1-v4, replace
    
    *     Generate eigenvectors from spmat W matrix
    spmat getmatrix W w_m
    mata:
        E_values = .
        E_vectors = .
        symeigensystem(w_m, E_vectors, E_values)
        st_matrix("E_m", E_vectors)
    end
    svmat E_m, names(E)
    }        // Close qui
    
    sum E*
    clear all
    Code:
    . *       Generate data for W matrix
    . clear all
    
    . qui {   // Open qui
    
    .
    . sum E*
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
              E1 |          4   -.4993792    .0287599  -.5242861  -.4744724
              E2 |          4           0    .5773502  -.6015009   .6015009
              E3 |          4           0    .5773502  -.6015009   .6015009
              E4 |          4    .0249068    .5766335  -.4744724   .5242861
    
    . clear all
    When I try to program them into a command, it gives an error:
    Code:
    * Programing    
    cap program drop eigvecvars    
    program define eigvecvars
        version 11
        syntax varlist, Wmatrix(name)
        
    *    Check spmat
        qui cap spmat summarize `wmatrix'
        if _rc > 0 {
            di as err "spmat object `wmatrix' not found"
            exit 498
        }
        
        spmat getmatrix `wmatrix' w_m
        mata:
            E_values = .
            E_vectors = .
            symeigensystem(w_m, E_vectors, E_values)
            st_matrix("E_m", E_vectors)
        end
        svmat E_m, names(E)
    end
    Code:
    . do "C:\Users\lnd9492\AppData\Local\Temp\STD21b4_00001a.tmp"
    
    . cap program drop eigvecvars    
    
    . program define eigvecvars
      1.         version 11
      2.         syntax varlist, Wmatrix(name)
      3.        
    . *       Check spmat
    .         qui cap spmat summarize `wmatrix'
      4.         if _rc > 0 {
      5.                 di as err "spmat object `wmatrix' not found"
      6.                 exit 498
      7.         }
      8.        
    .         spmat getmatrix `wmatrix' w_m
      9.         mata:
     10.                 E_values = .
     11.                 E_vectors = .
     12.                 symeigensystem(w_m, E_vectors, E_values)
     13.                 st_matrix("E_m", E_vectors)
     14.         end
    
    .         svmat E_m, names(E)
    matrix E_m not found
    r(111);
    Last edited by Manh Hoang Ba; Yesterday, 11:49.

  • #2
    The end in the line numbered as 14 ends the program define part. You can't use end here to end Mata.

    A solution is to define a macro named end_mata with the content "end" and use the macro to end Mata within the program:
    Code:
    cap program drop eigvecvars    
    program define eigvecvars
        version 11
        syntax varlist, Wmatrix(name)
        
    *    Check spmat
        qui cap spmat summarize `wmatrix'
        if _rc > 0 {
            di as err "spmat object `wmatrix' not found"
            exit 498
        }
        
        spmat getmatrix `wmatrix' w_m
        local end_mata end
        mata
            E_values = .
            E_vectors = .
            symeigensystem(w_m, E_vectors, E_values)
            st_matrix("E_m", E_vectors)
        `end_mata'
        svmat E_m, names(E)
    end
    -
    By the way: The program name is no acronym, see FAQ #18
    Last edited by Dirk Enzmann; Today, 08:37. Reason: Code correction

    Comment

    Working...
    X