I with to draw the same random numbers with Stata
and R
. Essentially I want to obtain the same series of random numbers with sample
in R
and rdiscrete
in Stata
. However, I have tried to provide a complete, but small, reproducible example in each language.
I think the sample
function is doing the same thing as the rdiscrete
function, but I am not certain. Assuming these functions are doing the same thing I simply need them to return the same random numbers.
I am using Stata 12
.
Here is my R
code:
set.seed(1234)
wave_of_cy = 2
wave_obs = 20
fake_dat <- read.table(text = '
nobs p1 p2
0 .20 .10
1 .10 .15
2 .10 .15
3 .05 .10
4 .05 .10
5 .20 .05
6 .10 .05
7 .05 .05
8 .05 .05
9 .10 .20
', header = TRUE, stringsAsFactors = FALSE)
p_hrand = fake_dat[, (wave_of_cy+1)]
pp_hrand = p_hrand / sum(p_hrand)
my_rdata = sample(nrow(fake_dat), wave_obs, prob=pp_hrand, replace = TRUE)
my_rdata
hrand = fake_dat[my_rdata, 1]
hrand
Here is my Stata
code:
clear
set seed 1234
global wave_of_cy = 2
set obs 20
local wave_obs = _N
clear
input nobs p1 p2
0 .20 .10
1 .10 .15
2 .10 .15
3 .05 .10
4 .05 .10
5 .20 .05
6 .10 .05
7 .05 .05
8 .05 .05
9 .10 .20
end
list
save fake_dat
clear
use "fake_dat.dta", replace
putmata fake_data = (nobs p1 p2), replace
mata:
p_hrand = fake_data[., $wave_of_cy+1]
pp_hrand = p_hrand :/ sum(p_hrand)
my_rdata = rdiscrete(`wave_obs', 1, pp_hrand)
my_rdata
hrand = fake_data[my_rdata, 1]
hrand
end