Announcement

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

  • Problem with MC simulation

    Hi

    I have the following problem; my code generates only one value and the rest 999 is just a dot. My code is

    program drop _all
    set seed 12345


    gen x1 =.
    gen ind =.
    gen pd2mc = .
    gen ead2mc = .
    gen fm2mc=.
    gen sumead2mc = .

    program define monte
    replace x1 = runiform()
    replace ind = 1 if pr1 > x1
    replace disincome = 102.04*4.348 if ind ==1
    replace fm2mc = disincome +dl20001 + hb23001 + hi01001+ hi02101 +h50
    replace pd2mc = 0 if (fm2mc)>=0
    replace pd2mc = 0 if (abs(fm2mc)*33) <= (dl30001)
    replace pd2mc = (1 - (dl30001)/(abs(fm2mc)*33)) if ((fm2mc)<0) & (abs(fm2mc)*33) > (dl30001)
    replace ead2mc = (pd2mc * dl1000)
    replace sumead2mc = sum(ead2mc)
    summarize sumead2mc
    scalar m1 = r(sum)
    generate ratio1 = r(sum)/sumdl1000
    end

    simulate mean=r(mean) ratio1 = ratio1 , reps(1000) nodots: monte
    summarize

    Thanks
    Ilias

  • #2
    Hi Ilias,
    there are several problems with your code.

    First of all, your example is not working. The variables pr1, disincome, dl*, hi* and sumdl* are not defined or generated. Also, you define the program monte after you initialized the first set of variables. When you use the simulate command, Stata calls for each repetition the program monte and processes only the code of this program. Thus it is practice to put programs at the very beginning of the do file, to make sure that Stata knowns the program. If you would call the program before you defined, it is unknown to Stata.

    You receive dots, because you do not define any macros, scalars or matrices to be returned. Therefore when running simulate, r(mean) and ratio1 are unknown. To solve it, you need to define the monte program as either rclass or eclass. Then the program can return values which the simulate then saves to your dataset. Also note, that your variables will be overwritten after simulate. To put this in, the following lines need to be altered or added:
    Code:
    program define monte, rclass
    and then to return the mean of sumead2mc (?):
    Code:
    sum sumead2mc
    return scalar mean = r(mean)
    and to return the mean of ratio1
    Code:
    sum ratio1
    return scalar ratio1= r(mean)
    You then call the simulation with
    Code:
    simulate mean=r(mean) ratio1 = r(ratio1) , reps(1000) nodots: monte
    Hopefully this helps!

    Jan

    Comment


    • #3
      Thank you JanDitzen. Very helpful input.

      Comment

      Working...
      X