Announcement

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

  • Can we call a Stata program in a Mata function??

    Hi everyone,

    I am wondering whether we can call a Stata program in a Mata function. I need to do the quantile regression for 1000 times, meaning I need to use the qreg Stata command in a loop. I am afraid that Stata would be too slow for this task and I would like to do this in a Mata function. The problem is that I could not find a way to use the qreg command in a mata function. I am wondering whether there is a way to do this. I tried to convert the quantile regression function into a Mata function, but it works much slower than the qreg command. Any thoughts is welcome. Thanks!

  • #2
    Code:
    mata:stata("qreg y x")
    But I don't think it will speed up your program considerably

    Comment


    • #3
      Mmm, but I think looping in Mata could save some time comparing to looping in Stata.

      Comment


      • #4
        Ingrid, no you will not save any notable time, just as Christophe has said. The same regression code will be executed regardless whether it is called from Stata or Mata. The looping overhead is trivial compared to the regression code itself, even if you have a small number of observations, which you probably don't, since you've mentioned quantile regressions.
        Best, Sergiy.

        Comment


        • #5
          Hi Sergiy,
          Thank you for clarifying this. I am considering writing a mata function to do quantile regression right now.

          Comment


          • #6
            This a rather ambitious goal. qreg is based on the C-optimized _regress inside, which is in the core of many Stata commands, and, I suspect, is optimized to the limits. Rewriting the same in mata will be subject to human error and will not produce a speed improvement. So unless you are working with some data of special structure, there is probably no gain to expect.

            I am afraid that Stata would be too slow for this task
            Why don't you do some speed measurements?

            If the computing time is less then a day, would you still invest in writing/testing/debugging your own command?

            Best, Sergiy

            Comment


            • #7
              Thank you for your suggestions, Sergiy. I know it is ambitious to write a quantile regression function by using Mata. But I met some problems when using qreg. In some cases the results given by qreg are quite different from those given by the Matlab function. And my attempt told me that the Mata function performs not as well as the qreg command in terms of computation speed but because of the algorithm, it can give the same estimation results as those from Matlab.

              And thank you for clarifying the qreg command. I am wondering how I can learn more about the computation speed of Stata or Mata, since this would be useful for writing an efficient program.

              Comment

              Working...
              X