Time series stata中的150x150交叉表,显示类别之间的时间序列移动

Time series stata中的150x150交叉表,显示类别之间的时间序列移动,time-series,stata,crosstab,Time Series,Stata,Crosstab,我有点不知所措,我希望你能帮助我,或者至少给我指出正确的方向 我得到了一个庞大的数据集(14年来每年580万次观测),其中涉及个人在一段时间内的职业。我需要总结一下这段时间内职业的变化,这样我就可以看到在失业期之后,人们从哪个职业到哪个职业,如下所示: 但是,有150个类别,这意味着我无法让stata在没有换行符的情况下显示整个交叉表。我需要以.csv或其他格式输出交叉表,以便以后在GNU/R中进行进一步操作。第一个问题是: 1) 如何将此大小的交叉表提取到csv/xls文件 一个解决方案是以

我有点不知所措,我希望你能帮助我,或者至少给我指出正确的方向

我得到了一个庞大的数据集(14年来每年580万次观测),其中涉及个人在一段时间内的职业。我需要总结一下这段时间内职业的变化,这样我就可以看到在失业期之后,人们从哪个职业到哪个职业,如下所示:

但是,有150个类别,这意味着我无法让stata在没有换行符的情况下显示整个交叉表。我需要以.csv或其他格式输出交叉表,以便以后在GNU/R中进行进一步操作。第一个问题是:

1) 如何将此大小的交叉表提取到csv/xls文件

一个解决方案是以这样的方式构建数据,我可以将其导入SPSS,SPSS完全能够将如此巨大的交叉表输出到excel工作表

现在,我的数据结构如下

input ///
id year occup
1 1999 1
1 2000 1
1 2001 1
2 1999 1
2 2000 2
2 2001 1
3 1999 1
3 2000 2
3 2001 2
4 1999 .
4 2000 1
4 2001 2
5 1999 1
5 2000 .
5 2001 2
end
list, sepby(id)
如您所见,这是一个长格式的简单数据帧。我需要以这种方式构建数据,这样我就可以创建一个交叉表来显示多年来的变化。例如,一个人可能在1996年被“A”占据,然后在1997年被“B”占据,然后在1998年再次被“A”占据。这意味着在这两年里,他将被计算两次,从A到B,然后从B到A。因此,我们的想法是,它只计算类别之间的转换次数,不管是哪一年

我希望我的问题被问得清楚而准确。我不会轻视你在这方面花费时间的事实,因此,如果我能以任何方式改进我的问题,请这样说,我将尽最大努力改进我的问题,以免浪费你的时间。先谢谢你


这是SPSS中的一个解决方案。我假设您对记录缺失值之间的切换/迁移不感兴趣,但会考虑切换,尽管有效职业代码之间存在缺失

DATA LIST LIST /ID (F1.0) Year (F4.0) Code (A1).
BEGIN DATA.
1   1999    A
1   2000    A
1   2001    A
2   1999    A
2   2000    B
2   2001    A
3   1999    A
3   2000    B
3   2001    B
4   1999    .
4   2000    A
4   2001    B
5   1999    A
5   2000    A
5   2001    B
END DATA.
DATASET NAME DS0.

SORT CASES BY ID Year Code.

DO IF (ID=LAG(ID)).
    IF (Code=".") Code=LAG(Code).
    IF (Code<>LAG(Code) AND LAG(Code)<>".") SwitchInd=1.
END IF.
EXE.

SPLIT FILE SEPARATE BY ID.
CREATE SwitchNb=CSUM(SwitchInd).
SPLIT FILE OFF.

STRING SwitchFrom SwitchTo (A1).
DO IF (NVALID(SwitchInd)=1).
    COMPUTE SwitchFrom=LAG(Code).
    COMPUTE SwitchTo=Code.
END IF.

CTABLES /TABLE SwitchFrom[c] by SwitchTo[c] BY SwitchInd
  /CATEGORIES VARIABLES = SwitchInd[1]
  /CATEGORIES VARIABLES = ALL EMPTY=EXCLUDE
  /TITLES CORNER="Migrations Analysis: From (Vertical) --> To (Horizontal)".
数据列表/ID(F1.0)年份(F4.0)代码(A1)。
开始数据。
1 1999 A
1 2000年A
2001年1月A
2 1999 A
2000年B月2日
2001年2月A日
1999年3月A日
2000年3月B日
2001年3月B日
4   1999    .
4 2000年A
2001年4月B日
5 1999年A
5 2000年A
5 2001 B
结束数据。
数据集名称DS0。
按ID年份代码对案例进行排序。
如果(ID=滞后(ID))则执行。
如果(Code=“.”)代码=滞后(Code)。
如果(CodeLAG(代码)和LAG(代码)”),则开关ind=1。
结束如果。
EXE。
按ID分隔文件。
创建SwitchNb=CSUM(SwitchInd)。
分割文件。
字符串从开关切换到(A1)。
如果(NVALID(SwitchInd)=1),则执行此操作。
计算SwitchFrom=滞后(代码)。
COMPUTE SwitchTo=代码。
结束如果。
CTABLES/TABLE从[c]通过switch切换到[c]通过SwitchInd
/类别变量=SwitchInd[1]
/类别变量=全部为空=排除
/TITLES CORNER=“迁移分析:从(垂直)-->到(水平)”。
结果产生(来自TestForStackExtange.csv中提供的演示数据):

A-->B:4


B-->A:1这是SPSS中的一个解决方案。我假设您对记录缺失值之间的切换/迁移不感兴趣,但会考虑切换,尽管有效职业代码之间存在缺失

DATA LIST LIST /ID (F1.0) Year (F4.0) Code (A1).
BEGIN DATA.
1   1999    A
1   2000    A
1   2001    A
2   1999    A
2   2000    B
2   2001    A
3   1999    A
3   2000    B
3   2001    B
4   1999    .
4   2000    A
4   2001    B
5   1999    A
5   2000    A
5   2001    B
END DATA.
DATASET NAME DS0.

SORT CASES BY ID Year Code.

DO IF (ID=LAG(ID)).
    IF (Code=".") Code=LAG(Code).
    IF (Code<>LAG(Code) AND LAG(Code)<>".") SwitchInd=1.
END IF.
EXE.

SPLIT FILE SEPARATE BY ID.
CREATE SwitchNb=CSUM(SwitchInd).
SPLIT FILE OFF.

STRING SwitchFrom SwitchTo (A1).
DO IF (NVALID(SwitchInd)=1).
    COMPUTE SwitchFrom=LAG(Code).
    COMPUTE SwitchTo=Code.
END IF.

CTABLES /TABLE SwitchFrom[c] by SwitchTo[c] BY SwitchInd
  /CATEGORIES VARIABLES = SwitchInd[1]
  /CATEGORIES VARIABLES = ALL EMPTY=EXCLUDE
  /TITLES CORNER="Migrations Analysis: From (Vertical) --> To (Horizontal)".
数据列表/ID(F1.0)年份(F4.0)代码(A1)。
开始数据。
1 1999 A
1 2000年A
2001年1月A
2 1999 A
2000年B月2日
2001年2月A日
1999年3月A日
2000年3月B日
2001年3月B日
4   1999    .
4 2000年A
2001年4月B日
5 1999年A
5 2000年A
5 2001 B
结束数据。
数据集名称DS0。
按ID年份代码对案例进行排序。
如果(ID=滞后(ID))则执行。
如果(Code=“.”)代码=滞后(Code)。
如果(CodeLAG(代码)和LAG(代码)”),则开关ind=1。
结束如果。
EXE。
按ID分隔文件。
创建SwitchNb=CSUM(SwitchInd)。
分割文件。
字符串从开关切换到(A1)。
如果(NVALID(SwitchInd)=1),则执行此操作。
计算SwitchFrom=滞后(代码)。
COMPUTE SwitchTo=代码。
结束如果。
CTABLES/TABLE从[c]通过switch切换到[c]通过SwitchInd
/类别变量=SwitchInd[1]
/类别变量=全部为空=排除
/TITLES CORNER=“迁移分析:从(垂直)-->到(水平)”。
结果产生(来自TestForStackExtange.csv中提供的演示数据):

A-->B:4


B-->SSC的A:1
选项卡
可能适合您:

clear
set more off

*----- example data set -----

input ///
id year occup
1 1999 1
1 2000 1
1 2001 1
2 1999 1
2 2000 2
2 2001 1
3 1999 1
3 2000 2
3 2001 2
4 1999 .
4 2000 1
4 2001 2
5 1999 1
5 2000 .
5 2001 2
end

list, sepby(id)

*----- what you want -----

bysort id (year): gen fromoccup = occup[_n-1]

tabout fromoccup occup using tabtest.csv, replace cells(freq) format(0c)
我在计算中做了一些假设,但我的重点放在表格上。请参见
ssc描述选项卡

您还可以考虑导出数据集本身。比如:

*----- what you want -----

bysort id (year): gen fromoccup = occup[_n-1]

collapse (count) counter=id, by(fromoccup occup)
dropmiss, obs any force

// pretty list
order fromoccup occup
sort fromoccup occup
list
相应地调整缺失

编辑 既然您已经说明了您的目标(邻接矩阵),那么就更容易提供精确的帮助了

下面将创建邻接矩阵并将其导出到MS Excel。最后一个命令
putexcel
需要Stata 13。如果不可用,可以导出在此之前创建的Stata数据集,这与导出的矩阵几乎相同

clear
set more off

*----- example data set -----

input ///
id year occup
1 1999 1
1 2000 1
1 2001 1
2 1999 1
2 2000 3
2 2001 1
3 1999 1
3 2000 3
3 2001 3
4 1999 .
4 2000 1
4 2001 3
5 1999 1
5 2000 .
5 2001 3
6 1999 4
6 2000 4
6 2001 3
end

list, sepby(id)

*----- what you want -----

bysort id (year): gen fromoccup = occup[_n-1]

collapse (count) counter=id, by(fromoccup occup)
dropmiss, obs any force

// pretty list
order fromoccup occup
sort fromoccup occup

// add combinations with zero counts
fillin fromoccup occup
replace counter = 0 if _fillin
drop _fillin

// adjacency matrix
replace counter = counter > 0
reshape wide counter, i(fromoccup) j(occup)
rename counter* occup*

list

// put into matrix, adjust, and export
mkmat _all, matrix(adjmat)

unab coln : occup*
matrix t = . , adjmat[ 1... , "fromoccup"]'

matrix colnames t = fromoccup `coln'
matrix rownames t = r0

matrix a = t \ adjmat
matrix list a

putexcel B2=matrix(a) using adjtest.xls, replace
结果如下所示:


例如,有从4到3的作业流,但没有从3到4的作业流。

tabout
来自SSC的作业流可能适合您:

clear
set more off

*----- example data set -----

input ///
id year occup
1 1999 1
1 2000 1
1 2001 1
2 1999 1
2 2000 2
2 2001 1
3 1999 1
3 2000 2
3 2001 2
4 1999 .
4 2000 1
4 2001 2
5 1999 1
5 2000 .
5 2001 2
end

list, sepby(id)

*----- what you want -----

bysort id (year): gen fromoccup = occup[_n-1]

tabout fromoccup occup using tabtest.csv, replace cells(freq) format(0c)
我在计算中做了一些假设,但我的重点放在表格上。请参见
ssc描述选项卡

您还可以考虑导出数据集本身。比如:

*----- what you want -----

bysort id (year): gen fromoccup = occup[_n-1]

collapse (count) counter=id, by(fromoccup occup)
dropmiss, obs any force

// pretty list
order fromoccup occup
sort fromoccup occup
list
相应地调整缺失

编辑 既然您已经说明了您的目标(邻接矩阵),那么就更容易提供精确的帮助了

下面将创建邻接矩阵并将其导出到MS Excel。最后一个命令
putexcel
需要Stata 13。如果不可用,可以导出在此之前创建的Stata数据集,这与导出的矩阵几乎相同

clear
set more off

*----- example data set -----

input ///
id year occup
1 1999 1
1 2000 1
1 2001 1
2 1999 1
2 2000 3
2 2001 1
3 1999 1
3 2000 3
3 2001 3
4 1999 .
4 2000 1
4 2001 3
5 1999 1
5 2000 .
5 2001 3
6 1999 4
6 2000 4
6 2001 3
end

list, sepby(id)

*----- what you want -----

bysort id (year): gen fromoccup = occup[_n-1]

collapse (count) counter=id, by(fromoccup occup)
dropmiss, obs any force

// pretty list
order fromoccup occup
sort fromoccup occup

// add combinations with zero counts
fillin fromoccup occup
replace counter = 0 if _fillin
drop _fillin

// adjacency matrix
replace counter = counter > 0
reshape wide counter, i(fromoccup) j(occup)
rename counter* occup*

list

// put into matrix, adjust, and export
mkmat _all, matrix(adjmat)

unab coln : occup*
matrix t = . , adjmat[ 1... , "fromoccup"]'

matrix colnames t = fromoccup `coln'
matrix rownames t = r0

matrix a = t \ adjmat
matrix list a

putexcel B2=matrix(a) using adjtest.xls, replace
结果如下所示:


例如,有从4到3的工作流程,但不是从3到4。

这不是我自己的答案,而是克劳迪亚·埃哈德给我的答案