Announcement

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

  • Enter regression coefficients into a matrix

    As part of a randomization inference test (https://dimewiki.worldbank.org/Randomization_Inference) -- one that is more complex than -ritest- can accommodate -- I am trying to create a matrix that contains coefficients from a series of individual regressions, each on a different dataset (the observed data, with some random perturbations that fit the context of the test). But I am having trouble figuring out how to enter regression coefficients into a matrix (despite having read up on putmata, and some other commands that I thought might do the trick). I am surely missing something obvious.

    The relevant parts of the code are like this:
    Code:
    local sims = 1000
    mata:d=J(`sims',2,.) // creates a `sims'x2 matrix of missing values
    forval i=1/`sims' {
    use mydata_`i', clear
    nl (y = {b1}*d1+{b2}*d2+{b3}*d3+{b4}*log(1+{b1}*zo1+{b2}*zo2)+{b5}*log(1+{b1}*zu1+{b2}*zu2))
    mata:d[`i',1] = _b[/b4] 
    mata:d[`i',2] = _b[/b5] 
    }
    clear
    set obs `sims'
    getmata (beta_four beta_five) = d
    The lines
    Code:
    mata:d[`i',1] = _b[/b4] 
    mata:d[`i',2] = _b[/b5]
    returned the error "expression invalid", so I tried replacing those lines with:

    Code:
    gen beta_four = _b[/b4] 
    gen beta_five = _b[/b5] 
    mata:d[`i',1] = beta_four[1]
    mata:d[`i',2] = beta_five[1]
    And that returned the error " <istmt>: 3499 beta_four not found", which suggests I'm getting somewhere, but it's still an error. How can I enter these coefficients from each regression in the -forval- loop into the matrix?

  • #2
    Try replacing _b[/b4] with `=_b[/b4]' and likewise for b5. The returned values _b are Stata scalars and must be converted.
    As you saw, it is a bad idea to save as variables using generate. You'll get constant variables, where you would have to draw the value of a single row.
    Kind regards

    nhb

    Comment


    • #3
      Perfect. Thank you very much!

      Comment

      Working...
      X