Announcement

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

  • Matrix input inside a loop

    I am running a foreach using a numlist that contains four values, sample sizes for different programs previously defined. My code is as follows:

    ------- definition of programs somewhere above here ------
    matrix M = (., ., ., .\ .,.,.,.\ .,.,.,.\ .,.,.,.)

    foreach obs of numlist 50 100 500 1000 {

    bias, obs(`obs')
    return list
    }

    before running the foreach I create a matrix M full of missing values, size 4x4, 4 columns for the different sample sizes and 4 rows for estimator biases that are obtained the program "bias". Is there a way to input said results into a column of the matrix depending on which sample size is currently running? The purpose is to have a full matrix by the end of the loop, but I can't seem to obtain the numbers for the command "matrix M [1,1] = r(something)".

    if it's any use, here's the definition of the program bias

    program define bias, rclass
    drop _all
    syntax [, obs(integer 1)]
    set obs `obs'

    simulate mc0=r(mco0) mc1=r(mco1) iv10=r(ivz10) iv11=r(ivz11) iv20=r(ivz20) ///
    iv21=r(ivz21) m_0=r(m0) m_1=r(m1), reps(1000) : IVPGD, obs(`obs')

    sum mc1
    return scalar b`obs'OLS=r(mean)-1
    sum iv11
    return scalar b`obs'IV1=r(mean)-1
    sum iv21
    return scalar b`obs'IV2=r(mean)-1
    sum m_1
    return scalar b`obs'M=r(mean)-1

    end

  • #2
    Please read the FAQ on asking questions.

    You didn't get an answer because this is an overly complex problem. Also, we need the actual input and output. When I tried running what you sent, I got an error "IVPGD command not found
    r(111);"

    I also don't see where your program says "matrix M [1,1] = r(something)".

    It would also help if you gave us a very concise statement of what you have when you start and what you want to do. It is quite likely that other folks on this list can offer you a much simpler way to accomplish what you're trying to do.

    If I were debugging this, I'd first copy the bias lines into the original program to see if it works that way. This eliminates a set of potential problems around the connections between the main program and bias.

    Comment


    • #3
      Phil,

      I assumed something like this would happen. I only attached my second program in hopes it would show what it did and the variables one would obtain at the end of it. I have simplified my question by now, and it now consists in referencing each step of the foreach command. I will now attach my current code. It will not run without defining the two programs it makes use of. the "bias" program inside what follows is the same as defined in the original post

      matrix M = (., ., ., .\ .,.,.,.\ .,.,.,.\ .,.,.,.)
      *4x4 matrix

      foreach obs of numlist 50 100 500 1000 {

      bias, obs(`obs')
      forvalues i = 1/4 {
      matrix M [1,`i'] = r(b`obs'OLS)
      matrix M [2,`i'] = r(b`obs'IV1)
      matrix M [3,`i'] = r(b`obs'IV2)
      matrix M [4,`i'] = r(b`obs'M)
      *note that this loop is clearly useless
      }
      return list
      }

      as you can see, the loop will modify elements of the M matrix for each obs (50 100 500 and 1000), but in the end, the matrix will be full of b1000* values

      in simple word, as there are four steps in the foreach loop, and four values coming out of the "bias" program, I want to save all four values in each column of the matrix, one column for each sample size (obs). the forvalues loop won't do the trick since it's not referencing the obs value. Perhaps there is a way to define a scalar that takes values from 1 to 4 depending on which obs is currently running, as in (50 = 1) (100 = 2) and so on.

      Sorry if this is still too complex, I am new to stata and this forum in general

      Kind regards

      Comment

      Working...
      X