问题描述
本质上,我正在尝试进行分层随机抽样。我想对异性恋夫妇的数据进行分析。我需要随机选择 50% 的女性和随机 50% 的未婚男性。我知道如何过滤掉总样本的随机百分比,但不知道如何确保每个家庭只选择一个人。
我的数据如下所示:
couple person gender Q1 Q2 Q3 Q4 Q5
1 1 0 3.5 4.2 2.3 3.3 4.3
1 2 1 3.2 2.5 2.1 3.7 5.6
2 1 1 3.7 2.6 3.3 4.2 5.1
2 2 0 3.0 3.5 2.1 3.6 5.4
它是长格式,所以每一行代表一个人,每对有两个人。
编辑以获取更多详细信息
hhid
= 情侣
hhidpn
= 人
ragender
= 性别,其中 1 = 男性,2 = 女性
SPAQ1-8
= 年龄自我认知量表第 1-8 项
[1]: https://i.stack.imgur.com/yKQ7k.png
解决方法
我的建议是将夫妇随机分成两个相等的组,然后选择一组中的女性和另一组中的男性。
首先,我将重建您的示例数据以进行演示:
data list list/couple person gender (3f1) Q1 Q2 Q3 Q4 Q5 (5f2.1).
begin data
1 1 0 3.5 4.2 2.3 3.3 4.3
1 2 1 3.2 2.5 2.1 3.7 5.6
2 1 1 3.7 2.6 3.3 4.2 5.1
2 2 0 3.0 3.5 2.1 3.6 5.4
3 1 0 3.5 4.2 2.3 3.3 4.3
3 2 1 3.2 2.5 2.1 3.7 5.6
4 1 1 3.7 2.6 3.3 4.2 5.1
4 2 0 3.0 3.5 2.1 3.6 5.4
end data.
现在我们有了一个数据集,我们可以根据需要进行采样:
编辑了一个更短的过程 - 使用@rossum 建议的一个版本:
* first we give each couple a random number,and then use it to sort the couples randomly.
sort cases by couple.
compute randorder=uniform(100).
if couple=lag(couple) randorder=lag(randorder).
sort cases by randorder.
* now we create a running index for the couples,and use it to select males
or females according to odd or even index.
compute coupleNum=1.
if $casenum>1 coupleNum=lag(coupleNum)+(couple<>lag(couple)).
compute selected=(mod(coupleNum,2)=gender).
exe.
现在您创建了选择变量,您可以将其与 filter
或 select
一起使用以继续分析。
编辑:
上面的代码适用于值为 0,1 的性别。对 OP 的编辑显示性别值实际上是 1,2。所以 selected
的最终计算应该以这种方式完成,而不是像上面那样:
compute selected=(mod(coupleNum,2)+1=gender).