Announcement

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

  • for loop in mata

    Dear statalist members
    I am new to Mata language, in the code below i simulate a VAR(2) using Stata and it works fine, does anyone know how to do the last part concerning the forvalues loop in Mata ,
    the two equation for VAR(2) : y1 and y2 are simulated individually in STATA , in matrix form the values for y1 and y2 are derived as :
    y=cons+A1*l.y+A2*l.2y+ u
    where y is vector containing y1 and y2 / A1, A2 are matrices defined in Mata, l.y amd l2.y are lag 1 and lag2 of variables y1 and y2. and u is a vector of errors
    Code:
    clear all
    local t = 1200
    set obs 1200
    gen t = _n
    tsset t
    forvalues i=1/2 {
    gen y`i'=rnormal() in 1/2
    }
    matrix M = 2, 0
    matrix V = (1, 0.5\ 0.5, 1 ) // covariance matrix of errors
    drawnorm u1 u2 , n(1200) cov(V) means(M) // drawing bivariate normal errors
    mata:
    t=1200
    cons = (0.1\0.3)
    A1 = (0.6,-0.2\0.4,0.2)
    A2 = (0.2,0.4\-0.1,0.1)
    Sigma = (1,0.5\0.5,1)
    y=st_data(.,"y1 y2")
    u=st_data(.,"u1 u2")
    end
    forvalues i=3/`t' {
    qui {
    replace y1 = 0.1 + 0.6*l.y1 - 0.2*l.y2 + 0.2*l2.y1 + 0.4*l2.y2 + u1 in `i'
    replace y2 = 0.3 + 0.4*l.y1 + 0.2*l.y2 - 0.1*l2.y1 + 0.1*l2.y2 + u2 in `i'
    }
    * equations under forvalues loop in matrix form are y= cons+A1*l.y+ A2*l.2y + u
    }
    drop in 1/200
    THANKS
    Last edited by lindsy ben; 02 Aug 2021, 21:53.

  • #2
    Lindsy: An incrementing Mata loop would look something like this
    Code:
    local t=1200
    mata
    for ( i=3; i<=`t'; i++) {
     j=i
    }
    j
    end
    which gives this result
    Code:
    . local t=1200
    
    . mata
    ------------------------------------------------- mata (type end to exit) -------------------------------------
    : for ( i=3; i<=`t'; i++) {
    >  j=i
    > }
    
    : j
      1200
    
    : end

    Comment


    • #3
      thanks john feedback, I don't know how to get the increment(i) inside the matrices to update the vector y at each step in order to simulate VAR(2), for example
      y1= known initial value
      y2= known initial value
      // get the rest values according to :
      vector y= constant vector+ cons matrix A1*[L.y(i)]+ cons matrixA2*[l.y(i)] +vector u
      i would like to write this part of code in Mata
      PHP Code:
        forvalues i=3/`t' {
                      qui {
         replace y1 = 0.1 + 0.6*l.y1 - 0.2*l.y2 + 0.2*l2.y1 + 0.4*l2.y2 + u1 in 
      `i'
         replace y2 = 0.3 + 0.4*l.y1 + 0.2*l.y2 - 0.1*l2.y1 + 0.1*l2.y2 + u2 in `i'
                         
      }            
       } 

      when I applied john's suggestion I get an error message
      Code:
      . clear all
      . local t = 1200
      .  set obs 1200
      number of observations (_N) was 0, now 1,200
      .  gen t = _n
      .  tsset t 
              time variable:  t, 1 to 1200
                      delta:  1 unit
      .  forvalues i=1/2 {
        2.   gen y`i'=rnormal() in 1/2
        3.    }  
      (1,198 missing values generated)
      (1,198 missing values generated)
      . matrix M = 2, 0
      . matrix V = (1, 0.5\ 0.5, 1 )  // covariance matrix of errors
      . drawnorm u1 u2 , n(1200) cov(V) means(M)  // drawing bivariate normal errors
      .  mata:
      ------------------------------------------------- mata (type end to exit) --------------------------
      : cons = (0.1\0.3)
      
      : A1 = (0.6,-0.2\0.4,0.2)
      
      : A2 = (0.2,0.4\-0.1,0.1)
      
      : Sigma = (1,0.5\0.5,1)
      
      : y=st_data(.,"y1 y2")
      
      : u=st_data(.,"u1 u2")
      
      : for ( i=3; i<=`t'; i++) {
      >  j=i
      >  replace y1 = 0.1 + 0.6*l.y1 - 0.2*l.y2 + 0.2*l2.y1 + 0.4*l2.y2 + u1 in `i'
      type mismatch:  exp.exp:  transmorphic found where struct expected
      (3 lines skipped)
      ----------------------------------------------------------------------------------------------------
      r(3000);
      
      end of do-file
      
      r(3000);
      Last edited by lindsy ben; 03 Aug 2021, 18:09.

      Comment


      • #4
        the below code doesn't work either, Could anyone please help me to get around this problem
        Code:
        clear all
        local t = 1200
         set obs 1200
         gen t = _n
         tsset t 
         forvalues i=1/2 {
          gen y`i'=rnormal() in 1/2
           }  
          g y1_1=l.y1
          g y1_2=l2.y1
          g y2_1=l.y2
          g y2_2=l2.y2
        
        matrix M = 2, 0
        matrix V = (1, 0.5\ 0.5, 1 )  // covariance matrix of errors
        drawnorm u1 u2 , n(1200) cov(V) means(M)  // drawing bivariate normal errors
         mata:
        cons = (0.1\0.3)
        A1 = (0.6,-0.2\0.4,0.2)
        A2 = (0.2,0.4\-0.1,0.1)
        Sigma = (1,0.5\0.5,1)
        y=st_data(.,"y1 y2")
        u=st_data(.,"u1 u2")
        y11=st_data(.," y1_1 y2_1")
        y22=st_data(.," y1_2 y2_2")
        for ( i=3; i<=`t'; i++) {
        y= cons+ A1*y11+ A2*y22+ u 
        }
        end

        Comment


        • #5
          I don't understand the objective of the program but when I ran your code in #4 I checked for conformability. A1 and A2 are 2x2 while y11 and y22 are 1200x2 before entering the Mata for loop. Your code in #3 uses the Stata command replace inside the Mata environment. You can issue a Stata command from inside Mata by using Mata's stata(...) function.

          Comment

          Working...
          X