Announcement

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

  • using forvalues loop and postfile command

    Dear statalist
    i have a sample of only 3 observation, i having problem calculating outer product of the gradient , the gradient and updating formula using forvalues loop and postfile command, i get an error message "post sim not found" another question could i use a variable or macro to hold the opg, gr and theta instead of scalar, any help would be greatly appreciated , my do file is as follows

    clear all
    set obs 100
    input y
    3.5
    1
    1.5
    end
    sca theta1 =1
    gen f = -1/theta1 + y/theta1^2
    tempname sim
    tempfile result
    postfile `sim' opg gr theta using result, replace
    forvalues i=1/100 {
    qui {
    sca opg`i' = 1/3*(-1/theta`i' + y[1]/theta`i'^2 )^2 + 1/3*(-1/theta`i' + y[2]/theta`i'^2)^2+ 1/3*(-1/theta`i' + y[3]/theta`i'^2)^2 /* calculating OPG for every iteration*/

    sca gr`i' = 1/3*(-1/theta`i' + y[1]/theta`i'^2 ) + 1/3*(-1/theta`i' + y[2]/theta`i'^2)+ 1/3*(-1/theta`i' + y[3]/theta`i'^2) /* calculating the gradient at every iteration*/
    loc j=`i'+1
    sca theta`j' = theta`i' + (opg`i')^-1*gr`i' /* updating formula*/
    post sim (opg`i') (gr`i') (theta`j')
    }
    }
    postclose `sim'
    use result, clear
    i get error message!!!
    post sim not found
    r(111);

    end of do-file

    r(111);
    Last edited by lindsy ben; 27 Feb 2021, 15:11.

  • #2
    Code:
    post sim (opg`i') (gr`i') (theta`j')
    is incorrect, because your postfile's handle is not sim, it's `sim'. Use
    Code:
    post `sim' (opg`i') (gr`i') (theta`j')
    Concerning your other question, yes you could use a variable or a macro instead of a scalar to hold those intermediate results. I would advise against using a variable for the purpose because why copy the same number over into every observation in the data set? Admittedly, with a sample size of 3 it's not a big waste of memory, but still, there is no upside to it that I can see. I would strongly advise using a global macro for the purpose, because global macros are inherrently unsafe and should be used only when there is no alternative. But a local macro would be fine, and, in fact, is the usual way of handling these situations.

    Comment


    • #3
      oh many thanks Clyde it is working now

      Comment

      Working...
      X