Announcement

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

  • Interpolation in Mata

    Dear Statalist members,

    Does anyone know how to interpolate values horizontally? The following data shows that there are one-, two-, three-step-ahead, and so on, up to six-step-ahead forecasts for each time period t. But I have missing values for the four- and five-step-ahead forecast and I need to interpolate the data from the three-step-ahead forecast to the six-step-ahead forecast in every period. The only way I can think of is to transpose them in the Mata and perform the interpolation in Mata. However, I am not quite sure how to do the interpolation in Mata (I'm looking at the mm_interpolate() command). Given that all columns and rows are named numerically when I import the data from Stata using st_data(), I don't know how should I select the variable x and y. Also, the help file of mm_interpolate shows that the new variable is assigned as 1.2 in their example. What does that mean by 1.2?


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(t F1 F2 F3 F4 F5 F6)
     2  .1456245 .14484142 .13593246 . . .13580246
     3   .143995 .14467934  .1369658 . .  .1409643
     4 .13837382 .13969778 .13224994 . . .14502968
     5 .13785172 .13984114 .13253349 . .  .1505979
     6   .135864 .13956618 .13360359 . .  .1566629
     7 .13388118 .13695474 .13053758 . .  .1554463
     8 .13279217 .13528617 .12972611 . . .15007657
     9 .13096268 .13305667 .12814745 . . .14102565
    10 .12956016 .13078153 .12464567 . . .13544106
    end
    I would greatly appreciate if anyone could help on this.

    Many thanks!


  • #2
    Janys: This is possible do in Mata, but I think you can do it more directly (if I understand your question):
    Code:
    replace F4=(2/3)*F3+(1/3)*F6
    replace F5=(1/3)*F3+(2/3)*F6

    Comment


    • #3
      Hi John,

      Many thanks for your reply.

      I will try to compute using your approach. But I'm just curious how it can be done in Mata. I would be grateful if you could let me know how to do it in Mata.

      Comment


      • #4
        Janys: Something like this is what I had in mind. The Mata matrix zint is the interpolated version of the Mata matrix z.
        Code:
        mata
        
        z=st_data(.,"t F*")
        zint=z
        wts=(2/3,1/3)\(1/3,2/3)
        zint[.,5..6]=zint[.,(4,7)]*wts
        
        end

        Comment


        • #5
          Hi John

          Many thanks for your reply! May I know why would you define wts as that matrix? Is that 2/3, 1/3 is something related to your code in #2?

          I supposed zint is your own defined matrix, and it is not a command in Stata? Also, I don't quite understand the last line of coding. Could you please explain it to me? Sorry for a lot of troublesome.

          Janys

          Comment


          • #6
            Janys: Yes, the wts matrix (2x2) postmultipies the Nx2 matrix zint that contains (F3,F6) to interpolate those values into the missing (F4,F5) values, just as in the Stata code in #2.

            Everything between mata and end in #4 is Mata code. In words, the expression
            Code:
            zint[.,5..6]=zint[.,(4,7)]*wts
            means to postmultiply by wts the Nx2 matrix containing the 4th and 7th columns of zint (i.e. F3 and F6) and to assign the result to the 5th and 6th colums of zint (i.e. F4 and F5). Now zint will contain the original values for (t,F1,F2,F3,F6) and the interpolated values of F4 and F5.

            Comment


            • #7
              Hi John,

              Many thanks for your help and detailed explanation! I got it now!

              Have a nice day

              Janys

              Comment

              Working...
              X