Announcement

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

  • Saving and retrieving Mata environment (or results from optimize())

    Dear Statalist,

    I am working on a project with two basic steps:

    Step 1: perform a time-consuming (~45 minutes) optimization in Mata using optimize().

    Step 2: use the results from the optimization, including the estimated parameters, the gradient vector, etc.

    I have completed Step 1 more or less satisfactorily for now and am working on Step 2. I would like to avoid re-running Step 1 unnecessarily. My question is this:

    At the end of Step 1, is it possible to save, and later retrieve, either the full Mata environment or the results from optimize? Then, to work on Step 2, I could load the results from Step 1 and proceed without re-running the optimization.

    The (probably imperfect) analogy to working in Stata would be to save estimation results to a .ster file using estimates save, then retrieve these results using estimates use.

    Since the Mata environment and results from optimize are Mata vectors, I imagine a workaround would be to store all of these in a Stata dataset using st_store(), save the Stata dataset, and then read the results back into Mata using st_data(). If this is in fact the best (or only) way to go, I'd appreciate any advice on how best to do this, especially since the Mata objects are likely to have different dimensions.

    Many thanks,

    Bert

    PS - Stata 17.0 MP

  • #2
    Somehow I was unaware of the existence of mata matsave and fopen(), and my initial searches did not come across these.

    I suspect the latter will be better suited to my purpose given the Remark in the help file for mata matsave:

    These commands are for interactive use; they are not for use inside programs.
    See [M-5] fopen( ) for Mata’s programming functions for reading and writing files.
    I am adding this post rather than deleting my question in case it is useful to others with the same question in the future.

    Relevant helpfiles:

    https://www.stata.com/manuals/m-3matamatsave.pdf
    https://www.stata.com/manuals/m-5fopen.pdf

    Comment


    • #3
      Hi Bert
      I think you need to look into "mata matsave" and "mata matuse".
      They allow you to save a list of "objects" you are using in Mata into a binary file, to recover it later on.
      I have use it for saving "class" objects. But you can probably approach technical support to see how to save results from "optimize" (create the object and save it.
      Since Optimize works with classes as well, you probably can save that class directly.
      F

      Comment

      Working...
      X