Announcement

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

  • IF loop within a WHILE loop and cumulating info into a vector

    Hi, there seems to be a problem with the while loop below, although there are no specific error messages that lead me to understand the problem. I am estimating beta with Ordinary Least Square (Betachap) and then I am doing 3 t-tests (10%, 5%, and 1%). In every iteration inside the loop, if the t-statistic is greater than a level, I will reject the null hypothesis. I want to accumulate the rejections (rejections equal 1) in the (1000 x 1) column vectors, which is why you see the code: rejetHo10[i,1] == 1. The result is that not a single hypothesis is rejected. I wonder if the loop is not working. Does anyone have an idea of my errors in the while loop code? Thanks.

    clear
    set obs 10
    gen y2 = .
    gen x2 = .
    gen betachap = .
    mata
    n = 10
    alfa = 0.5
    beta = 0
    mu_x = 5
    sigma2_x = 4
    sigma2_e = 2
    //**************************
    //*** GENERATE MODEL ***
    //**************************
    cst = J(n,1,1)
    x = J(n,1,mu_x) + sqrt(sigma2_x) * invnormal(runiform(n,1))
    e = sqrt(sigma2_e) * invnormal(runiform(n,1))
    y = cst*alfa + x*beta + e
    //**********************************
    //*** ESTIMATE 1000 FOIS ***
    //**********************************
    X = (cst,x)
    i = 1
    n = 1000
    betachap = J(n, 1, 1)
    rejetHo10 = J(n, 1, 0)
    rejetHo5 = J(n, 1, 0)
    rejetHo1 = J(n, 1, 0)
    while (i <= n) {
    E_MCO = luinv(X'X) * X'y
    betachap[i,1] = E_MCO[2,1]
    k = cols(X)
    error = y - X * E_MCO
    s2 = (error' * error) / (n-k)
    var_E_MCO = s2*luinv(X'X)
    std_E_MCO = sqrt(var_E_MCO)
    t = E_MCO[2,1]/std_E_MCO[2,2]

    if (t>1.8331){
    rejetHo10[i,1] == 1
    }

    if (t>2.2622){
    rejetHo5[i,1] == 1
    }
    if (t>3.2498){
    rejetHo1[i,1] == 1
    }

    i = i + 1
    }

    vecteurHo = J(1,n,1)
    rejetsHo10 = vecteurHo*rejetHo10
    rejetsHo10
    rejetsHo5 = vecteurHo*rejetHo5
    rejetsHo5
    rejetsHo1 = vecteurHo*rejetHo1
    rejetsHo1
    end

  • #2

    Code:
    rejetHo5[i,1] == 1
    Should be

    Code:
    rejetHo5[i,1] = 1
    Because "==" is checking equality and "=" is for assigning values


    Cheers,
    S

    Comment

    Working...
    X