Announcement

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

  • Good practice and examples for programming an iterative estimator needed: How to best set up, and terminate the loop?

    Good afternoon,

    I am looking for advice on good practice, and for sample code for programming an iterative estimator in Stata.

    For concreteness say I have a system of two equations:
    (1) y = x'b + e
    (2) w = z'g + v.

    I want to implement the following procedure due to Telser, L. G. (1964). Iterative estimation of a set of linear regression equations. Journal of the American Statistical Association, 59(307), 845-862.

    Estimate (2) by OLS, get the residuals v(0) = w - z'g(0).
    Estimate modification of (1) by OLS: y = x'b(0) + a*v(0) + error. Get the residual e(0) = y - x'b(0).
    Estimate modification of (2) by OLS: w = z'g(1) + c*e(0) + error. Get the residual v(1) = w - z'g(1)
    Estimate modification of (1) by OLS: y = x'b(1) + a*v(1) + error. Get the residual e(1) = y - x'b(1).
    Estimate modification of (2) by OLS: w = z'g(2) + c*e(1) + error. Get the residual v(2) = w - z'g(2)
    ................. Repeat until b(n) converges

    I did some reading over the internet, and a couple of issues came up:

    1. Stata does not have a function to evaluate the maximum element of a vector. (Mata apparently has, but I would prefer not to switch between Stata and Mata if possible).
    I would have naturally defined tolerance as something like Do the above procedure until max(b(n)) - max(b(n-1))<0.0001. Apparently I am not able to do this in Stata.

    2. The only matrix function that I found relevant for the problem of defining tolerance was mreldif(X,Y). Then I encountered some reactions of Stata that I cannot really understand: matrix functions that return scalars sometimes are treated as scalars, sometimes not treated as scalars:
    Code:
    . mat a = 1
    
    . mat b = 1
    
    . mat diff = mreldif(a,b)
    
    . assert diff<0.001
    matrix operators that return matrices not allowed in this context
    r(509);
    
    . assert diff[1,1]<0.001
    
    . assert el(diff,1,1)<0.001
    that is, the last two assertions went through (so I can use this in defining the loop) but the first failed for reasons not entirely clear to me. mreldif returns a scalar, why would Stata think that I am using a matrix in the first assertion?

    So for such and other Gotchas, I thought to ask first how people in the know set up such iterative loops.


  • #2
    You certainly could set this up as a loop and use the change in b as the determinant of when to exit. Something like this might work:

    sysuse auto,clear

    local db=1
    local blength=10
    local bprice=10

    reg weight price
    predict res1,res

    while abs(`db')>.1 {
    reg mpg length res1
    predict res2,res
    local blength=_b[length]
    drop res1

    reg weight price res2
    predict res1, res
    local db=`bprice' - _b[price]
    local bprice=_b[price]
    drop res2

    di "db `db' "
    }

    Comment

    Working...
    X