Announcement

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

  • Reading in a stata matrix into Mata

    I am making a serious attempt to learn and use Mata, but am getting stuck at the lack of definitive examples. I am trying to read a STATA matrix into Mata so I can add a row to the bottom of the MATA matrix and then perform a statistical analysis. When I use the following command I get the error message "command CSFTOT is unrecognized". Here is the command: CSFTOT = std_data(.,tokens(pid date_assessment_code csf_abeta42 csf_ptau181 csf_ttau csf_ab42_pt181_ratio csf_ab42_ttau_ratio csf_pt181_ttau_ratio r_margin))
    What am I doing wrong?
    thanks very much

  • #2
    Originally posted by William Shankle View Post
    I am trying to read a STATA matrix into Mata . . . When I use the following command I get the error message "command CSFTOT is unrecognized". What am I doing wrong?
    1. If you want to execute Mata functions, then you need to be in the Mata environment.

    2. There is no such Mata function as std_data(). Check your spelling.

    3. tokens() takes a string scalar argument. You need quotation marks around your variable list.

    A workable example of what I'm guessing that you're trying to do is below.
    Code:
    version 19
    
    clear *
    
    quietly set obs 1
    
    foreach var in pid date_assessment_code csf_abeta42 csf_ptau181 csf_ttau ///
            csf_ab42_pt181_ratio csf_ab42_ttau_ratio csf_pt181_ttau_ratio r_margin {
        generate byte `var' = 0
    }
    
    *
    * Begin here
    *
    mata:
    
    CSFTOT = st_data(., tokens("pid date_assessment_code csf_abeta42 csf_ptau181" + 
        " csf_ttau csf_ab42_pt181_ratio csf_ab42_ttau_ratio csf_pt181_ttau_ratio" +
        " r_margin"))
    
    CSFTOT
    
    end
    
    exit

    Comment


    • #3
      The advice of Joseph Coveney in #2 is eminently sensible and is essentially how I would proceed.

      That said, people beginning to learn Mata might be well advised to learn the capabilities of Stata's putmata and getmata commands for moving Stata data into Mata matrixes and vectors (putmata) and moving Mata matrixes and vectors into Stata datasets (getmata).

      While I generally use st_data(...) and related Mata functions for such purposes others whose Mata programming styles may be less entrenched might find putmata/getmata more to their liking.

      Comment

      Working...
      X