Announcement

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

  • Unexpected end of line - Mata not going through loops

    Hi, I'm currently doing an introductory econometrics class.

    In this assignment we have to do 1000 simulations on three different sample sizes.
    This is done via two for-loops (Ts = column vector containing the three sample sizes):

    Code:
    for(j=1; j<=length(Ts);j++) {
        // select the current sample size
        T = Ts[1,j]
        
        //loop for the replications
        for(i=1; i<=M; i++) {
            
            // random number generation
            y = AR_gen(1,T)
            x = AR_gen(1,T)
            
            //OLS regression
            X = J(T,1,1), x
            aux_ols = luinv(cross(X,X)*cross(X,y)
            beta[i,j] = aux_ols[2,1]
            
            // tstat
            err = y-X*aux_ols
            RSS = cross(err,err)
            sigma = luinv(cross(X,X))*RSS/(T-2)
            tstat[i,j] = beta[i,j]/sqrt(sigma[2,2])
            
            // R2
            TSS = cross(y-mean(y)*J(T,1,1),y-mean(y)*J(T,1,1))
            R2[i,j] = 1- RSS/TSS
            
            // DW
            DW[i,j] = cross(err[2::T,1]-err[1::T-1,1],err[2::T,1]-err[1::T-1,1])/RSS
            
        }
    }
    where the AR_gen function is made previous in the do.file:
    Code:
    function AR_gen(phi, T) {
    
    c = J(T+1,1,0)
    eps = rnormal(T,1,0,1)
    
    c[1,1] = 0 // 0, 0.S/(1-phi)
    
    for (i = 1; i<=T; i++) {
        c[i+1,1] = phi*c[i,1]+eps[i,1]
    }
    
    v = c[2::T+1,1]
    
    return(v)
    
    }
    The do.file starts with the 'mata' command and ends with 'end'.
    When i run the do.file it instantly reports back 'unexpected end of line', but it seems that everything before the loop has been processed.
    It saved all the zero-matrices i made to store the betas, tstats, DW and R2 in.

    Im quite a novice at both Mata and econometrics and I just can't figure out where i made a mistake or why mata doesn't try to iterate the actions in the loops

    Any help appreciated

  • #2
    One mistake I can spot is that this line:
    Code:
    aux_ols = luinv(cross(X,X)*cross(X,y)
    has unbalanced parentheses. That error, for me, is a common reason for messages like "unexpected end of line."

    A simple debugging trick for Mata programs is to print out something at several points in the code, thereby letting you narrow down exactly which line is creating the problem. I have in mind something like this:
    Code:
    Several lines of mata code ....
    "1"
    More mata code ...
    " 2"
    More code yet ...
    "3"
    Running the code will let you narrow down the error to (say) between 1 and 2. At that point, you can add in another "1.5" or whatever to further narrow down the location of the error, and so forth.

    Comment


    • #3
      Hi, I'm a bit late on this one but thanks for your help, the entire problem was exactly that one missing paranthese.

      The debugging trick sounds like a good idea, but it doesn't seem to do anything when the problem lies within a loop.
      In this case mata didn't even begin to execute the loop but everything before it worked fine.

      Anyway thanks

      Comment

      Working...
      X