Announcement

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

  • Extract Monte Carlo results from Mata

    Dear all,

    I am doing a 2000 times monte carlo simulation of two-stage least square estimation with random numbers in Mata with following codes, but I do not know how to extract the esimation results from Mata to Stata. My idea is to stack each estimation result on top of the other and get a varible with 2000 observations. But the code return errors: st_store(): 3200 conformability error
    <istmt>: - function returned error
    Could anyone help me work this out? Thanks!

    Code:
    clear all
    
    set obs 2000
    gen B_2SLS =.
    mata
    rseed(435632)
    
    for (i=1; i<=2000; i++){
        R  = (1, 0.5\ 0.5, 1)
        C  = cholesky(R)
        E1 = rnormal(2, 200, 0, 1)'
        E  = E1*C'
        Z1 = rnormal(5, 200, 0, 1)'
        Z  = Z1*I(5)
    
        P0 = 0.01*J(5, 1, 1)
        B0 = (1)
    
        X  = Z*P0 + E[., 1]
        Y  = X*B0 + E[., 2]
        P  = Z*luinv(Z'*Z)*Z'
    
        B_2SLS = (X'*P*Y) / (X'*P*X)
    
        st_store((strtoreal("i")::strtoreal("i"+"1")), "B_2SLS", B_2SLS)
    }
    end

  • #2
    Code:
    clear all
    
    set obs 2000
    gen B_2SLS =.
    mata
    rseed(435632)
    
    for (i=1; i<=2000; i++){
        R  = (1, 0.5\ 0.5, 1)
        C  = cholesky(R)
        E1 = rnormal(2, 200, 0, 1)'
        E  = E1*C'
        Z1 = rnormal(5, 200, 0, 1)'
        Z  = Z1*I(5)
    
        P0 = 0.01*J(5, 1, 1)
        B0 = (1)
    
        X  = Z*P0 + E[., 1]
        Y  = X*B0 + E[., 2]
        P  = Z*luinv(Z'*Z)*Z'
    
        B_2SLS = (X'*P*Y) / (X'*P*X)
    
        st_store(i, "B_2SLS", B_2SLS)
    }
    end
    Another possible approach is to create an empty vector in mata to save your simulation results in and then, at the end of the simulation, to store that vector as a variable in Stata.

    Code:
    clear all
    
    set obs 2000
    gen B_2SLS =.
    mata
    rseed(435632)
    
    B_2SLS = J(2000, 1, .)
    
    for (i=1; i<=2000; i++){
        R  = (1, 0.5\ 0.5, 1)
        C  = cholesky(R)
        E1 = rnormal(2, 200, 0, 1)'
        E  = E1*C'
        Z1 = rnormal(5, 200, 0, 1)'
        Z  = Z1*I(5)
    
        P0 = 0.01*J(5, 1, 1)
        B0 = (1)
    
        X  = Z*P0 + E[., 1]
        Y  = X*B0 + E[., 2]
        P  = Z*luinv(Z'*Z)*Z'
    
        B_2SLS[i, 1] = (X'*P*Y) / (X'*P*X)
    
    }
    
    mean(B_2SLS)
    
    st_store(., "B_2SLS", B_2SLS)
    end
    
    su B_2SLS

    Comment


    • #3
      Thank you so much Andrea, it works perfectly!

      Comment


      • #4
        The only thing I would add to Andrea's helpful reply is that in some (many?) instances using the Stata commands getmata and putmata can be much easier than using st_store(...) and st_data(...).

        Comment


        • #5
          Thank you Mullahy for your suggestion.

          Now I have another question. I am trying to write a program that can be used with the simulate command in Stata as I need to frequently change some of parameter values. My code follows, but it returns error . Could you please help me correct the code? Thank you.

          Code:
          clear all
          program define tsls, rclass
              version 14.2
              syntax [, obs(integer 1) vars(integer 1) p0(real 1) ]
              drop _all 
              set obs `obs'
              mata: tsls(`obs', `vars', `p0')
              getmata B_2SLS
              su B_2SLS
              return scalar mean = r(mean)
          end
          
          version 14.2
          mata:
              void tsls(scalar obs, scalar vars, scalar p0)
          {
              R  = (1, 0.5\ 0.5, 1)
              C  = cholesky(R)
              E1 = rnormal(2, obs, 0, 1)'
              E  = E1*C'
              Z1 = rnormal(vars, obs, 0, 1)'
              Z  = Z1*I(vars)
          
              P0 = p0*J(1, vars, 1)
              B0 = (1)
          
              X  = Z*P0 + E[., 1]
              Y  = X*B0 + E[., 2]
              P  = Z*luinv(Z'*Z)*Z'
          
              B_2SLS = (X'*P*Y) / (X'*P*X)
          }
          end
          
          simulate mean = r(mean), reps(2000): tsls, obs(200) vars(5) p0(0.01)
          *: 3200 conformability error
          tsls(): - function returned error
          <istmt>: - function returned error
          an error occurred when simulate executed tsls
          r(3200);

          Comment

          Working...
          X