Dear Community
I'm currently trying to do an balance check for experimental data, in which I use randomization inference to calculate a p-value for an omnibus test. I am currently using the code below. However, I'm not able to reproduce these p-values, even though I have set a seed beforehand. The changes are quite large when re-running the code (range between 0.998 and 0.020). What am I missing here? Do I need to set anything else for this to output me the same value each time? Thank you so much for the help and inputs!
I'm currently trying to do an balance check for experimental data, in which I use randomization inference to calculate a p-value for an omnibus test. I am currently using the code below. However, I'm not able to reproduce these p-values, even though I have set a seed beforehand. The changes are quite large when re-running the code (range between 0.998 and 0.020). What am I missing here? Do I need to set anything else for this to output me the same value each time? Thank you so much for the help and inputs!
Code:
snapshot restore 1
estpost sum $testvar1 if treat==0 & empty!=.
matrix cgmean=e(mean)
matrix cgsd=e(sd)
local cgN = e(N)
estpost sum $testvar1 if treat==1 & empty!=.
matrix tgmean=e(mean)
matrix tgsd=e(sd)
local tgN = e(N)
* RI omnibus test
regress treat $testvar1 if empty != ., vce(cluster cluster)
test $testvar1
scalar F_obs = r(F)
local reps = 1000
tempname Fvals
matrix `Fvals' = J(`reps', 1, .)
set seed 210725
forvalues i = 1/`reps' {
preserve
gen treat_perm = treat
gen rand = runiform()
sort rand
replace treat_perm = treat[_n]
quietly regress treat_perm $testvar1 if empty!= ., vce(cluster cluster)
quietly test $testvar1
matrix `Fvals'[`i',1] = r(F)
restore
}
local count = 0
forvalues i = 1/`reps' {
if `Fvals'[`i',1] >= F_obs {
local count = `count' + 1
}
}
local pRI = `count' / `reps'
scalar pvalue = int(`pRI' * 1000) / 1000
di as result " RI p-value : " %6.3f pvalue
matrix pvalues = J(1, `: word count $testvar1', .)
local counter = 1
gen cg = treat==0
replace cg = . if treat==.
foreach var of global testvar1 {
if strpos("$nonbin", "`var'") {
ranksum `var' if empty != ., by(cg)
local pvalue = r(p)
}
else {
if strpos("$bin", "`var'") {
tabulate `var' cg if empty != ., chi2
local pvalue = r(p)
}
}
matrix pvalues[1, `counter'] = `pvalue'
local counter = `counter' + 1
}
matrix colnames pvalues = $testvar1
matrix list pvalues

Comment