Announcement

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

  • Optimal lag



    Hello Everybody,

    Once again needing help.


    I will like some help with a loop over varsoc.

    The programme I´ve iobtained comes form here:

    http://www.stata.com/statalist/archi.../msg00264.html

    I have adapted it. I have set of time series running over 45 years for 26 countries in 14 variables.

    I want a matrix that summarizes all the varsocs, runned through the contires and variables.This should give me a matrix of 14*26*5 (number of lags used 0/4)=1820

    My code is:


    set matsize 2000

    levelsof Cou, local(levels)

    foreach l of local levels {

    foreach var of varlist Y Yx D Px PX e M m X x PM R Kp KP {

    varsoc `var' if Cou == `l'

    matrix A = r(stats)

    if `l' > 1 {

    matrix C = C \ A
    }
    else {
    matrix C = A
    }
    }
    }

    It works perfectly for all stimates and gives me a C matrix that summarizes all the varsocs. however it jumps the first 13 variables from Y to Kp of the first country, So instead of having a matrrix of 1820 is of 1755.


    My guess is that the error is in here

    if `l' > 1 {

    matrix C = C \ A
    }
    else {
    matrix C = A
    }

    I´ve tried, changing it to :

    if `l' >= 1 {

    matrix C = C \ A
    }

    or other things but it doesn´t work or loops over and over....


    Please help!


    Thankyou very much.

    Manuel.





  • #2
    Statalisters please I really need some help on this.

    Tries many things. None work.

    Comment


    • #3
      Well, it appears that what you are trying to do is set C = A the first time through the loop, and C = C \ A on subsequent iterations. You are trying to do that by relying on the level of Cou that is being worked on. But there are two problems with that. First, is it really true that the first value of Cou is 1? That's a big assumption that may not be true. But second of all, you are, within that loop, also looping over Y Yx, etc. Consequently, every time Cou = 1 is encountered, matrix C would get overwritten by a new A. I'm pretty sure you actually want to colulmn-join all of those A's.

      Try getting rid of

      Code:
      if `l' > 1 {
      
          matrix C = C \ A
      }
      else {
          matrix C = A
      }
      with

      Code:
      matrix C = nullmat(C) \ A
      See -help nullmat()- for a full explanation of how it works. It was designed for exactly this situation.

      Comment


      • #4

        Thank you very much for your reply.

        It worked!!

        Thank you so much I had been trying many things to solve this and your solution is perfect, simple and correct.

        Just to answer to your question, my Cou are coded from 1 onwards, so it worked that way. Youa´re totally write that my problem was of overwritting in Cou=1.

        I really apreciate your help.

        thank you so much.


        NOTE:


        This post reallutalks about: iteratively building up a matrix


        Last edited by Manuel Rodriguez; 30 May 2016, 03:14.

        Comment

        Working...
        X