本文介绍了从给定变量中提取随机整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的任务是在时间间隔500
时间内随机挑选3
年用于模拟目的。更具体地说,我想从2007
到2016
(10
年)中选择3
随机年份,例如2008
、2012
和2014
。因此,这或多或少等同于从变量中提取随机整数。我的解决方案如下:
* The following (empty) dataset will be used to append the results of the Monte Carlo simulations
use "recession_parms.dta", clear
save "ind_simulations.dta", replace
forvalues i=1(1)500 {
use reg_sample.dta, clear
di "SIMULATION `i'"
scalar define lowest_year=2007
scalar define highest_year=2016
// randomly select "faked" year1: not inclue real treated years
gen year1_random=(lowest_year+int((highest_year-lowest_year+1)*runiform())) //To generate random integers over [a,b], use a+int((b-a+1)*runiform()) (see STATA help)
gen temp1 = inlist(year1_random,2008,2012,2014)
while temp1==1 {
replace year1_random =(lowest_year+int((highest_year-lowest_year+1)*runiform()))
cap drop temp1
gen temp1 = inlist(year1_random,2008,2012,2014)
}
// randomly select "faked" year2: (1)not inclue real treated years and (2)not equal to year1
gen year2_random=(lowest_year+int((highest_year-lowest_year+1)*runiform()))
gen temp2 = inlist(year2_random,2008,2012,2014)
while temp2==1|year1_random==year2_random {
replace year2_random = lowest_year+int((highest_year-lowest_year+1)*runiform()))
cap drop temp2
gen temp2 = inlist(year2_random,2008,2012,2014)
}
// randomly select ""faked" year3_random:(1)not inclue real treated years and (2)not equal to year1 or year2
gen year3_random=(lowest_year+int((highest_year-lowest_year+1)*runiform()))
gen temp3 = inlist(year2_random,2008,2012,2014)
while temp3==1|year1_random==year3_random|year2_random==year3_random {
replace year3_random =(lowest_year+int((highest_year-lowest_year+1)*runiform()))
cap drop temp3
gen temp3 = inlist(year3_random,2008,2012,2014)
}
drop temp*
* Generate the new treated year dummies
gen recession = (year==year1_random|year==year2_random |year==year3_random)
* Regression
di "SIMULATION `i'"
qui xtreg freq recession $city_control trend trend_sq ,fe cluster(city)
parmest,format(estimate min95 max95 %8.2f p %8.3f) saving("temp.dta", replace)
* Append the results of the simulation
use "temp.dta", clear
keep if parm=="recession"
append using "ind_simulations.dta"
save "ind_simulations.dta", replace
}
erase "temp.dta"
use "ind_simulations.dta", clear
drop if estimate==.
save "ind_simulations.dta", replace
有没有什么很好的方法来实现我的目标,而不是编写几个while
循环?
推荐答案
以下适用于我:
sysuse uslifeexp, clear
set seed 12345
tempname sim
postfile `sim' id year1 year2 year3 using results, replace
forvalues i = 1 / 500 {
generate random = runiform()
sort random
post `sim' (`i') (year[1]) (year[2]) (year[3])
drop random
}
postclose `sim'
下面您可以看到生成的results
数据集中的前十个观察结果:
use results, clear
list in 1/10
+----------------------------+
| id year1 year2 year3 |
|----------------------------|
1. | 1 1927 1920 1910 |
2. | 2 1946 1917 1925 |
3. | 3 1927 1926 1946 |
4. | 4 1916 1908 1963 |
5. | 5 1983 1913 1967 |
|----------------------------|
6. | 6 1926 1967 1974 |
7. | 7 1976 1947 1927 |
8. | 8 1908 1960 1982 |
9. | 9 1947 1989 1915 |
10. | 10 1950 1920 1975 |
+----------------------------+
这篇关于从给定变量中提取随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!