Datastructure: I使用面板数据,其中观察结果代表了特定年份(2015-2021年)的某个个体。只包括对15至25岁之间的个人的观察。总共对1373个人进行了2857次观察。
目标:目标是调查2018年政策变化的影响。在这样做的过程中,我设计了一个准实验设计,其中有两个控制组和一个根据年龄定义的治疗组:
A组: 15-17岁old
个体
根据不同的机会将个体分为治疗组和对照组:由于方法学上的原因,可能不会成为治疗组的一部分(随着时间的推移而衰老),反之亦然。因此,我面临的问题是如何为治疗和控制组选择合适的个体(考虑到他们的年龄和年龄)。为了确保每年都有对各个年龄段个体的观察,我想出了以下设计(见图)。在我的数据中,理论上有17个年龄超过7岁(2015-2021)的个人(垂直的和图片中的一样)。我想根据下面表格中提到的机会,将这些人抽样到治疗和控制组中,以确保所有年龄的人在所有年份都有代表性。

Programming i构造了一个变量(1-17),表示个人代表的数字(如上表中的垂直数字)。
gen individualnumber=(age-year)+2007我构建了三个变量,以下列方式表示处于控制组A、B或治疗的可能性:
gen Chanceofbeingcontrol_1517=0
replace Chanceofbeingcontrol_1517=1 if individualnumber==1 | individualnumber==2 | individualnumber==3
replace Chanceofbeingcontrol_1517=0.75 if individualnumber==4
replace Chanceofbeingcontrol_1517=0.60 if individualnumber==5
replace Chanceofbeingcontrol_1517=0.50 if individualnumber==6
replace Chanceofbeingcontrol_1517=0.43 if individualnumber==7
replace Chanceofbeingcontrol_1517=0.29 if individualnumber==8
replace Chanceofbeingcontrol_1517=0.14 if individualnumber==9
gen Chanceofbeingcontrol_2325=0
replace Chanceofbeingcontrol_2325=1 if individualnumber==15 | individualnumber==16 | individualnumber==17
replace Chanceofbeingcontrol_2325=0.75 if individualnumber==14
replace Chanceofbeingcontrol_2325=0.60 if individualnumber==13
replace Chanceofbeingcontrol_2325=0.50 if individualnumber==12
replace Chanceofbeingcontrol_2325=0.43 if individualnumber==11
replace Chanceofbeingcontrol_2325=0.29 if individualnumber==10
replace Chanceofbeingcontrol_2325=0.14 if individualnumber==9
gen Chanceofbeingtreated=1-(Chanceofbeingcontrol_1517+Chanceofbeingcontrol_2325)在那之后我想建造样本..。
splitsample, generate(treatedornot) split(Chanceofbeingcontrol_1517 Chanceofbeingtreated Chanceofbeingcontrol_2325) cluster(individualnumber) rround show...but I收到了一个错误,因为拆分( numlist )子命令中可能只使用numlist。
问题:如何有效地构造样本或克服此错误?
示例:2015年15岁(1岁控制组)的个体(表中第7位),2018年将是18岁(即治疗年龄)。但是这个人可能不是治疗组和控制组的一部分,因此应该是这两个组中的一个成员。因此,我想从所有的7个个体中随机抽取三个样本。让我们来说明一下,在表中有100个人,如个人7。
sample
发布于 2022-06-06 15:42:52
对于所有在2015年9岁、2016年10岁、2017年11岁的人来说,最常见的是他们出生于2006年。2015年所有10岁的人都是2005年出生的。因此,与其创建一个很难理解的变量individualnumber,不如创建一个名为birthyear的变量。这将使你更容易向同龄人解释你的设计。
无论您将变量称为什么或它包含的值代表什么,我都会这样解决它。您可能需要修改这段代码。如果您想要一个可复制的答案,请提供数据的可复制子集(请参阅命令dataex)。
* Example generated by -dataex-. For more info, type help dataex
clear
input byte id int year double age
1 2017 15
1 2017 15
2 2017 15
2 2017 15
3 2017 15
3 2017 15
4 2017 15
4 2017 15
5 2015 12
5 2015 12
end
* Create the var that will display the
gen birthyear = year-age
preserve
* Collapse year-person level data to person level so
* that each individual only get one treatment status.
* You must have an individual id number for this
* Get standard deviation to test that data is good and the birthyear
* is identical for each individual across the panel data set
collapse (mean) birthyear (sd) bysd=birthyear, by(id)
* Test that birthyear is same across all indivudals - this is not needed,
* but good data quality assurance test. Then drop the var as it is not needed
assert bysd == 0
drop bysd
* Set seed to make replicable. Replace this seed when you have tested this
* script using a new random seed. For example from here:
* https://www.random.org/integers/?num=1&min=100000&max=999999&col=5&base=10&format=html&rnd=new
set seed 123456
*Generate a random number based on the seed
gen random_draw = runiform()
* For each birthyear, get the rank of the random number divided by the number
* of individuals in each birthyear
sort birthyear random_draw
by birthyear : gen percent_rank = _n/_N
*Initiate treatmen variable
gen tmt_status = .
label define tmt_status 0 "Treated" 1 "ControlA" 2 "ControlB"
*Assign birthyear 2006-2004 that are all the same
replace tmt_status = 1 if birthyear == 2006
replace tmt_status = 1 if birthyear == 2005
replace tmt_status = 1 if birthyear == 2004
*Assign birthyear 2003
replace tmt_status = 0 if birthyear == 2003 & percent_rank <= .25
replace tmt_status = 1 if birthyear == 2003 & percent_rank > .25
*Assign birthyear 2002
replace tmt_status = 0 if birthyear == 2002 & percent_rank <= .40
replace tmt_status = 1 if birthyear == 2002 & percent_rank > .40
*Fill in birthyear 2001-1999
*Assign year 1998
replace tmt_status = 0 if birthyear == 1998 & percent_rank <= .72
replace tmt_status = 1 if birthyear == 1998 & percent_rank > .72 & percent_rank <= .86
replace tmt_status = 2 if birthyear == 1998 & percent_rank > .86
*Fill in birthyear 1997-1990
* Do some tabulates etc to convince yourself the randomization is as expected
* Save tempfile of data to be merged to later
* (Consider saving this as a master data set https://worldbank.github.io/dime-data-handbook/measurement.html#constructing-master-data-sets)
tempfile assignment_results
save `assignment_results'
restore
merge m:1 id using `assignment_results'使用循环可以使这段代码更加简洁,但是随机赋值是如此重要,因为我个人在这样做时总是追求清晰而不是简洁。
这并不是专门回答关于splitsample的问题,但是它解决了您想要做的事情。您必须决定如何处理没有可拆分成您所喜欢的确切比例的组。
https://stackoverflow.com/questions/72489381
复制相似问题