lundi 1 juillet 2019

rdiscrete in Mata

I am attempting to uses the function rdiscrete in Mata to draw random numbers. I get an error that the probabilities do not sum to one even after I have tried to standardize them to one.

My question is similar to the question asked at a Stata forum here:

https://www.stata.com/statalist/archive/2014-02/msg00732.html

Here is my code:

clear

global wave_of_cy    =  3
global wave_numtrips = 34

set obs $wave_numtrips

local wave_obs = _N

clear
use "fakedata_July1.dta", replace
putmata mydata=(napples pcount1 pcount2 pcount3), replace

mata:

     p = mydata[.,$wave_of_cy+1]
     p :/ sum(p)

     rdiscrete(`wave_obs', 1, p)

end

Here is the code I used to create the external data file:

clear

input napples pcount1 pcount2 pcount3 pcount4
0 . .33263497 .22213458  .483706
1 . .18085447 .0592791 .07308878
2 . .09918489 .17381628 .06245227
3 . .12741563 .12649602 .04291577
4 . .07782318 .05691737 .05342419
5 . .03847268 .03891907 .04361086
6 . .02907396 .03870039 .03463672
7 . .00939872 .02071542 .02554072
8 . .01879743 .01602958 .01832358
9 . .01923634 .0242933 .01647887
10 . .00983762 .01197392 .03653411
11 . .01967525 .03063997 .01564033
12 . 0 .02302153 .02067067
13 . .01879743 .0234988 .01317734
14 . .00939872 .02846567 .00248811
15 . 0 .00786653 .01114919
16 . .00939872 .01106286 .00873202
17 . 0 .00353461 .00346993
18 . 0 .01751844 .00989827
19 . 0 .00731278 .00044811
20 . 0 .0041034 .00288536
21 . 0 .00064227 .00133846
22 . 0 .01064502 .00661907
23 . 0 .00933989 .0065631
24 . 0 .00209015 .00099294
25 . 0 .00236378 .00276476
26 . 0 .00709134 0
27 . 0 0 0
28 . 0 .00497991 0
29 . 0 0 0
30 . 0 .00268613 .00188223
31 . 0 .00685134 0
32 . 0 0 .00056822
33 . 0 .00701054 0
end

list

save fakedata_July1




Aucun commentaire:

Enregistrer un commentaire