STATA数据模拟中种子的作用(Seeds)
(2014-05-16 16:02:08)
标签:
体育 |
分类: 04STATA数据处理 |
种子的设置关系到模拟的结果,一定要设置好。
设置好了种子,下次模拟结果保持一致,无论repeat多少次;
还有,在一个程序中,如果有多次的
gen=随机函数,那么,前后两次的结果是不一样的,除非你在每一条的gen语句前定义一样的SEEDS,例如以下例子的y0105
and y0106。
SIMULATIONS EXAMPLE:
clear
set obs 10000
set seed 1000000
gen y0101=0+invnormal(uniform())*1
set seed 1000000
gen y0102=0+invnormal(uniform())*1
set seed 100002
gen y0103=0+invnormal(uniform())*1
set seed 100002
gen y0104=0+invnormal(uniform())*1
set seed 10000003
gen y0105=0+invnormal(uniform())*1
gen y0106=0+invnormal(uniform())*1
summ y0101-y0106
You can just call rng(mySeed) to set the seed for the global
stream (tested in Matlab R2011b). This affects the rand, randn, and
randi functions.
The same page that James linked to lists this as the
recommended alternative to various old methods (see the middle cell
of the right column of the table).
Here's some example code:
format long;
% Display numbers with full precision
format compact;
% Get rid
of blank lines between output
mySeed = 10;
rng(mySeed);
% Set the seed
disp(rand([1,3]));
disp(randi(10,[1,10]));
disp(randn([1,3]));
disp(' ');
rng(mySeed);
% Set the seed again to duplicate the
results
disp(rand([1,3]));
disp(randi(10,[1,10]));
disp(randn([1,3]));
Its output is: