Announcement

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

  • Mata matrix to variables using st_store

    I've created a matrix of Halton Draws in Mata, and I'd like to use each column of the matrix as a variable in Stata. Here's some code that works and shows what I'm trying to do:

    Code:
    clear
    set obs 100
    local nsimulations 100
    local N = _N
    mata
    hdrawsburn = J(`N'+10,100,.)
    hdraws = J(`N',100,.)
    p = (3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547)
    for (i=1; i<=100; i++) {
    hdrawsburn[,i] = ghalton(`N'+10,p[1,i],0)
    }
    for (i=1; i<=`N'; i++) {
    hdraws[i,] = hdrawsburn[i+10,]
    }
    hdraws = hdraws'
    end
    getmata (f1_*)=hdraws
    However, the getmata command does not work in .ado files, which is where I want it to go. So I need a way to do the same thing, but without getmata. Here's where I've got so far:

    Code:
    clear
    set obs 100
    local nsimulations 100
    local N = _N
    forval i = 1/`nsimulations'{
    gen double f1_`i' = .
    }
    mata
    hdrawsburn = J(`N'+10,100,.)
    hdraws = J(`N',100,.)
    p = (3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547)
    for (i=1; i<=100; i++) {
    hdrawsburn[,i] = ghalton(`N'+10,p[1,i],0)
    }
    for (i=1; i<=`N'; i++) {
    hdraws[i,] = hdrawsburn[i+10,]
    }
    hdraws = hdraws'
    st_store(.,"f1_1",hdraws[,1])
    end
    However, even after reading the documentation, I can't figure out a way to get that penultimate line to loop over f1_1, f1_2 ... etc. I tried this:

    Code:
    for (i=1; i<=`N'; i++) {
    st_store(.,"f1_i",hdraws[,i])
    }
    But it doesn't work. Any ideas?
    Last edited by Alex Stead; 28 Nov 2018, 14:36. Reason: mistake in code

  • #2
    Alex -- I feel your pain, as I find the documentation on st_store() very difficult. (Somewhere there is an article from Bill Gould (?) about how to use st_store(), but I can't find it right now.) And, I'm not sure I agree that -getmata()- won't work in an ado file, about which see below.

    But, to use st_store(), what's needed here, to my understanding, is to create the variable(s) in Stata before using st_store(). -getmata- does this for you, which is why I like it. Try substituting the following code for your last code fragment:
    Code:
    for (i = 1; i <=cols(hdraws); i++) {  // I wouldn't use `N' if I could avoid it
        st_addvar("double", "f1_" + strofreal(i))
        st_store(.,"f1_" + strofreal(i) , hdraws[.,i])
     }
    A couple of comments here:
    1) I would hope there's an easier way to do what I just did, but that's all I know how to do.
    2 I'd find it instructive to know why you think -getmata()- can't be used in ado files. Maybe we can make it work. As an experiment, I just saved the following as "testget.ado" and it worked:
    Code:
    cap prog drop testget
    program testget
    mata: X= runiform(100,10)
    getmata (x*)  = X
    end

    Comment


    • #3
      Mike Lacy - Been walking down the same path as you. The problem is, one cannot create the following code in an ado file
      Code:
      program wjl
      ... some Stata commands
      mata
      ... some Mata stuff
      end
      ... more Stata commands
      end
      because the first end terminates not only mata but also the program definition. At least, that's the explanation I have for the problem I encountered.

      This seems to do the trick. This was run right after a fresh launch of Stata.
      Code:
      . type wjl.ado
      capture program drop wjl
      program wjl
      clear
      set obs 100
      local nsimulations 100
      local N = _N
      mata: hdraws = wjlm(`N')
      getmata (f1_*) = hdraws
      describe, short
      end
      
      capture mata: mata drop wjlm()
      mata matrix function wjlm(N) {
      hdrawsburn = J(N+10,100,.)
      hdraws = J(N,100,.)
      p = (3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 
      > 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 1
      > 97, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 31
      > 1, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431
      > , 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547)
      for (i=1; i<=100; i++) {
      hdrawsburn[,i] = ghalton(N+10,p[1,i],0)
      }
      for (i=1; i<=N; i++) {
      hdraws[i,] = hdrawsburn[i+10,]
      }
      return(hdraws')
      }
      
      . wjl
      number of observations (_N) was 0, now 100
      
      Contains data
        obs:           100                          
       vars:           100                          
       size:        40,000                          
      Sorted by: 
           Note: Dataset has changed since last saved.
      
      .

      Comment


      • #4
        Hi both,

        Thanks for the help. I have taken a suggestions from both of you. Here is some code that works as intended (note I no longer transpose the matrix at the end, that was just me experimenting):

        Code:
        capture mata: mata drop hdraws()
        mata matrix function hdraws(N,D) {
        hdrawsburn = J(N+10,D,.)
        hdraws = J(N,D,.)
        p = (3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547)
        for (i=1; i<=D; i++) {
        hdrawsburn[,i] = ghalton(N+10,p[1,i],0)
        }
        for (i=1; i<=N; i++) {
        hdraws[i,] = hdrawsburn[i+10,]
        }
        for (i=1; i<=D; i++) {
        st_addvar("double", "f1_" + strofreal(i))
        st_store(.,"f1_" + strofreal(i),hdraws[,i])
        }
        }
        clear
        set obs 100
        local nsimulations 50
        local N = _N
        mata hdraws(`N',`nsimulations')
        Mike Lacy
        I'd find it instructive to know why you think -getmata()- can't be used in ado files. Maybe we can make it work.
        It seems like it can work then, but I'm going off the following advice (from the getmata help file):
        putmata and getmata are designed to work interactively and in do-files. The commands are not designed to work with ado-files. An ado-file is something like a do-file, but it defines a program that implements a new command of Stata, and well-written ado-files do not use globals such as the global vectors and matrices that putmata creates. Ado-files use local variables. Ado-file programmers should use the Mata functions st_data() and st_view() (see [M-5] st_data() and [M-5] st_view()) to create vectors and matrices, and if necessary, use st_store() (see [M-5] st_store()) to post the contents of those vectors and matrices back to Stata.
        So I'll use st_store to be on the safe side.

        William Lisowski
        because the first end terminates not only mata but also the program definition. At least, that's the explanation I have for the problem I encountered.
        Yes, this seems to be the problem. I think my approach will be to save the mata command hdraws as a .mo file, then call the command in the .ado file.

        Comment

        Working...
        X