mardi 17 décembre 2019

Stata simulate doesn't seem to use seed option

Say that I define this simple program in Stata that creates 100 observations populated with random numbers and returns the mean:

clear all

program define a, rclass
    set obs 100
    gen a = runiform()
    sum a
    return scalar mean=r(mean)
end

I then run this program using simulate:

simulate mean = r(mean), reps(1): a seed(123)

I get .5793895. I run the exact same line, expecting to get the same result:

simulate mean = r(mean), reps(1): a seed(123)

But I don't. I get another result! I thought that because the seed was equivalent in both cases, the seed would control what is generated by runiform(), thus giving me the same mean.

I did another test using seed:

clear all
set obs 100
set seed 123
gen a = runiform()
sum a

This gives .4948977. I then run the same code again:

clear all
set obs 100
set seed 123
gen a = runiform()
sum a

This also gives .4948977 as expected. What is different about the seed from simulate and not using simulate, and how can I get reproducible results using simulate?




Aucun commentaire:

Enregistrer un commentaire