如何在 Stata 中重现这种类型的 table?
How do I reproduce this type of table in Stata?
我的数据目前是这样的:
Group Team Ind. 1990 1991
Group 1 Blue a 1 1
Group 1 Blue b 1 1
Group 1 Blue c 1
Group 1 Green a 1 1
Group 1 Green b 1 1
Group 1 Green c 1 1
Group 1 Orange a 1
Group 1 Orange b 1 1
Group 1 Orange c 1 1
Group 2 Black a 1 1
Group 2 Black b 1 1
Group 2 Black c 1 1
Group 3 Grey a 1
Group 3 Grey b 1
Group 3 Grey c 1
Group 3 Yellow a 1 1
Group 3 Yellow b 1 1
Group 3 Yellow c
而我想总结成这种table:
1990 1991
Group 1 No. of Teams with all ind. (i.e. a,b,AND c) 1 3
Group 1 No. of Teams missing a value in a,b,OR c 2 0
Group 2 No. of Teams with all ind. (i.e. a,b,AND c) 1 1
Group 2 No. of Teams missing a value in a,b,OR c 0 0
Group 3 No. of Teams with all ind. (i.e. a,b,AND c) 0 0
Group 3 No. of Teams missing a value in a,b,OR c 2 2
我尝试重塑我的数据(并将 Year 作为一个变量),然后使用 egen 命令:
egen test=count(Team), by (Group Year Team)
但那时它只计算每个年份和组的完整行数。无论如何它都没有考虑独特的团队。
假设在您的示例数据中,空白是缺失,相应的变量是数字。然后可以通过以下方式实现与您想要的类似的东西:
clear
set more off
input ///
group str10 team str1 ind y1990 y1991
1 Blue a 1 1
1 Blue b 1 1
1 Blue c . 1
1 Green a 1 1
1 Green b 1 1
1 Green c 1 1
1 Orange a . 1
1 Orange b 1 1
1 Orange c 1 1
2 Black a 1 1
2 Black b 1 1
2 Black c 1 1
3 Grey a 1 .
3 Grey b . 1
3 Grey c . 1
3 Yellow a 1 1
3 Yellow b 1 1
3 Yellow c . .
end
list, sepby(group team)
*----- (similar to) what you want -----
// set years
local years 1990 1991
// number of teams per -group-
bysort group (team) : egen numteam = total(team != team[_n-1])
// comply with "and" condition
foreach y of local years {
bysort group team (y`y') : gen and`y' = y`y'[1] == y`y'[_N]
}
// compute counts of "and" conditions
collapse (first) and* numteam, by(group team)
collapse (sum) and* (first) numteam, by(group)
// compute counts of "exclusive or" conditions
foreach y of local years {
gen xor`y' = numteam - and`y'
}
// print
drop numteam
list
对于任何年份和 group team
组合,请注意检查是否所有个体都存在的策略:sort
指示变量,如果第一个和最后一个观察值相同,则所有个体存在(暂时丢弃所有值都缺失的情况)。如果它们不相同,意味着第一次观察中 1
而最后一次观察中 .
,则至少有一个人不存在。
(我把把它变成你最初要求的精确结构的练习留给你了。)
但通常的建议是使用 long 形式的数据,因为 Stata 中的大多数统计分析这样更容易。为此,先reshape
,然后再做其余的:
<snip>
*----- what you want -----
// reshape data
gen i = _n
reshape long y, i(i) j(year)
// number of teams per -group-
bysort group (team) : egen numteam = total(team != team[_n-1])
// comply with "and" condition
rename (ind y) (id indicat)
bysort year group team (indicat) : gen and = indicat[1] == indicat[_N]
// compute counts of "and" conditions
collapse (first) and numteam, by(year group team)
collapse (sum) and (first) numteam, by(year group)
// compute counts of "exclusive or" conditions
gen xor = numteam - and
// pretty print
drop numteam
order group
sort group year
list, sepby(group)
第一段代码输出:
. list
+-----------------------------------------------+
| group and1990 and1991 xor1990 xor1991 |
|-----------------------------------------------|
1. | 1 1 3 2 0 |
2. | 2 1 1 0 0 |
3. | 3 0 0 2 2 |
+-----------------------------------------------+
第二个:
. list, sepby(group)
+--------------------------+
| group year and xor |
|--------------------------|
1. | 1 1990 1 2 |
2. | 1 1991 3 0 |
|--------------------------|
3. | 2 1990 1 0 |
4. | 2 1991 1 0 |
|--------------------------|
5. | 3 1990 0 2 |
6. | 3 1991 0 2 |
+--------------------------+
最后,注意任何年份的所有缺失值的团队,例如1990 年 Red 队,以下:
+--------------------------------------+
| group team ind y1990 y1991 |
|--------------------------------------|
1. | 1 Blue a 1 1 |
2. | 1 Blue b 1 1 |
3. | 1 Blue c . 1 |
|--------------------------------------|
.
.
.
|--------------------------------------|
19. | 4 Red a . . |
20. | 4 Red b . 1 |
21. | 4 Red c . 1 |
|--------------------------------------|
22. | 4 White a 1 1 |
23. | 4 White b 1 1 |
24. | 4 White c . . |
+--------------------------------------+
因为这些被视为所有人都在场。
您需要确定计数在这种情况下的表现。一种调整方法是使用
bysort year group team (indicat) : gen and = indicat[1] == indicat[_N] ///
& !missing(indicat[1])
而不是对应的原文。在 sort
ing 之后,我们只需要检查第一个值是否不缺失,以丢弃所有值都缺失的情况。
编辑
运行 带有修改行的代码,连同我的原始示例数据,加上第四组所有缺失的组(见上文),结果为:
. list, sepby(group)
+--------------------------+
| group year and xor |
|--------------------------|
1. | 1 1990 1 2 |
2. | 1 1991 3 0 |
|--------------------------|
3. | 2 1990 1 0 |
4. | 2 1991 1 0 |
|--------------------------|
5. | 3 1990 0 2 |
6. | 3 1991 0 2 |
|--------------------------|
7. | 4 1990 0 2 |
8. | 4 1991 0 2 |
+--------------------------+
这就是我所期望的,根据我定义的逻辑:如果团队有所有缺失值,则用 排除或 条件进行计数。如果您想遵循不同的逻辑,您需要 i) 将其拼写出来,并且 ii) 将其转换为代码。
编辑 2
如果像您的评论中那样,指标变量可以取任何正值,您可以概括计算 和 条件的规则。在示例数据中添加第五组:
.
.
.
|--------------------------------------|
25. | 5 Blue a 1 . |
26. | 5 Blue b 1 . |
27. | 5 Blue c 1 . |
28. | 5 Green a 2 . |
29. | 5 Green b 2 . |
30. | 5 Green c . . |
31. | 5 Orange a 3 . |
32. | 5 Orange b 2 . |
33. | 5 Orange c 55 . |
+--------------------------------------+
并将对应行修改为:
bysort year group team (indicat) : gen and = !missing(indicat[_N])
我只检查最后一个值(_N
),因为排序后,如果最后一个值不丢失,则根本没有丢失。
检查 help subscripting
和 help bysort
,如果不熟悉这些概念。
我的数据目前是这样的:
Group Team Ind. 1990 1991
Group 1 Blue a 1 1
Group 1 Blue b 1 1
Group 1 Blue c 1
Group 1 Green a 1 1
Group 1 Green b 1 1
Group 1 Green c 1 1
Group 1 Orange a 1
Group 1 Orange b 1 1
Group 1 Orange c 1 1
Group 2 Black a 1 1
Group 2 Black b 1 1
Group 2 Black c 1 1
Group 3 Grey a 1
Group 3 Grey b 1
Group 3 Grey c 1
Group 3 Yellow a 1 1
Group 3 Yellow b 1 1
Group 3 Yellow c
而我想总结成这种table:
1990 1991
Group 1 No. of Teams with all ind. (i.e. a,b,AND c) 1 3
Group 1 No. of Teams missing a value in a,b,OR c 2 0
Group 2 No. of Teams with all ind. (i.e. a,b,AND c) 1 1
Group 2 No. of Teams missing a value in a,b,OR c 0 0
Group 3 No. of Teams with all ind. (i.e. a,b,AND c) 0 0
Group 3 No. of Teams missing a value in a,b,OR c 2 2
我尝试重塑我的数据(并将 Year 作为一个变量),然后使用 egen 命令:
egen test=count(Team), by (Group Year Team)
但那时它只计算每个年份和组的完整行数。无论如何它都没有考虑独特的团队。
假设在您的示例数据中,空白是缺失,相应的变量是数字。然后可以通过以下方式实现与您想要的类似的东西:
clear
set more off
input ///
group str10 team str1 ind y1990 y1991
1 Blue a 1 1
1 Blue b 1 1
1 Blue c . 1
1 Green a 1 1
1 Green b 1 1
1 Green c 1 1
1 Orange a . 1
1 Orange b 1 1
1 Orange c 1 1
2 Black a 1 1
2 Black b 1 1
2 Black c 1 1
3 Grey a 1 .
3 Grey b . 1
3 Grey c . 1
3 Yellow a 1 1
3 Yellow b 1 1
3 Yellow c . .
end
list, sepby(group team)
*----- (similar to) what you want -----
// set years
local years 1990 1991
// number of teams per -group-
bysort group (team) : egen numteam = total(team != team[_n-1])
// comply with "and" condition
foreach y of local years {
bysort group team (y`y') : gen and`y' = y`y'[1] == y`y'[_N]
}
// compute counts of "and" conditions
collapse (first) and* numteam, by(group team)
collapse (sum) and* (first) numteam, by(group)
// compute counts of "exclusive or" conditions
foreach y of local years {
gen xor`y' = numteam - and`y'
}
// print
drop numteam
list
对于任何年份和 group team
组合,请注意检查是否所有个体都存在的策略:sort
指示变量,如果第一个和最后一个观察值相同,则所有个体存在(暂时丢弃所有值都缺失的情况)。如果它们不相同,意味着第一次观察中 1
而最后一次观察中 .
,则至少有一个人不存在。
(我把把它变成你最初要求的精确结构的练习留给你了。)
但通常的建议是使用 long 形式的数据,因为 Stata 中的大多数统计分析这样更容易。为此,先reshape
,然后再做其余的:
<snip>
*----- what you want -----
// reshape data
gen i = _n
reshape long y, i(i) j(year)
// number of teams per -group-
bysort group (team) : egen numteam = total(team != team[_n-1])
// comply with "and" condition
rename (ind y) (id indicat)
bysort year group team (indicat) : gen and = indicat[1] == indicat[_N]
// compute counts of "and" conditions
collapse (first) and numteam, by(year group team)
collapse (sum) and (first) numteam, by(year group)
// compute counts of "exclusive or" conditions
gen xor = numteam - and
// pretty print
drop numteam
order group
sort group year
list, sepby(group)
第一段代码输出:
. list
+-----------------------------------------------+
| group and1990 and1991 xor1990 xor1991 |
|-----------------------------------------------|
1. | 1 1 3 2 0 |
2. | 2 1 1 0 0 |
3. | 3 0 0 2 2 |
+-----------------------------------------------+
第二个:
. list, sepby(group)
+--------------------------+
| group year and xor |
|--------------------------|
1. | 1 1990 1 2 |
2. | 1 1991 3 0 |
|--------------------------|
3. | 2 1990 1 0 |
4. | 2 1991 1 0 |
|--------------------------|
5. | 3 1990 0 2 |
6. | 3 1991 0 2 |
+--------------------------+
最后,注意任何年份的所有缺失值的团队,例如1990 年 Red 队,以下:
+--------------------------------------+
| group team ind y1990 y1991 |
|--------------------------------------|
1. | 1 Blue a 1 1 |
2. | 1 Blue b 1 1 |
3. | 1 Blue c . 1 |
|--------------------------------------|
.
.
.
|--------------------------------------|
19. | 4 Red a . . |
20. | 4 Red b . 1 |
21. | 4 Red c . 1 |
|--------------------------------------|
22. | 4 White a 1 1 |
23. | 4 White b 1 1 |
24. | 4 White c . . |
+--------------------------------------+
因为这些被视为所有人都在场。
您需要确定计数在这种情况下的表现。一种调整方法是使用
bysort year group team (indicat) : gen and = indicat[1] == indicat[_N] ///
& !missing(indicat[1])
而不是对应的原文。在 sort
ing 之后,我们只需要检查第一个值是否不缺失,以丢弃所有值都缺失的情况。
编辑
运行 带有修改行的代码,连同我的原始示例数据,加上第四组所有缺失的组(见上文),结果为:
. list, sepby(group)
+--------------------------+
| group year and xor |
|--------------------------|
1. | 1 1990 1 2 |
2. | 1 1991 3 0 |
|--------------------------|
3. | 2 1990 1 0 |
4. | 2 1991 1 0 |
|--------------------------|
5. | 3 1990 0 2 |
6. | 3 1991 0 2 |
|--------------------------|
7. | 4 1990 0 2 |
8. | 4 1991 0 2 |
+--------------------------+
这就是我所期望的,根据我定义的逻辑:如果团队有所有缺失值,则用 排除或 条件进行计数。如果您想遵循不同的逻辑,您需要 i) 将其拼写出来,并且 ii) 将其转换为代码。
编辑 2
如果像您的评论中那样,指标变量可以取任何正值,您可以概括计算 和 条件的规则。在示例数据中添加第五组:
.
.
.
|--------------------------------------|
25. | 5 Blue a 1 . |
26. | 5 Blue b 1 . |
27. | 5 Blue c 1 . |
28. | 5 Green a 2 . |
29. | 5 Green b 2 . |
30. | 5 Green c . . |
31. | 5 Orange a 3 . |
32. | 5 Orange b 2 . |
33. | 5 Orange c 55 . |
+--------------------------------------+
并将对应行修改为:
bysort year group team (indicat) : gen and = !missing(indicat[_N])
我只检查最后一个值(_N
),因为排序后,如果最后一个值不丢失,则根本没有丢失。
检查 help subscripting
和 help bysort
,如果不熟悉这些概念。