温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
作者:李琼琼 (山东大学)
Email: lqqflora@163.com
目录
生存分析法 (survival analysis) 源于生物统计学和人口学,主要对人或动物的寿命或生存时间进行分析,现在也广泛应用于社会科学和商业等领域。Cameron and Trival (2005) 从计量的角度认为生存分析主要研究 从一种状态转变到另一种状态所经历的时间。其应用非常广泛,比如:
目前,国内已有较多的学者使用生存分析来研究中国出现的社会经济问题,研究主题有:
生存分析 (survival analysis) 又被称为久期分析 (duration analysis) 、转换分析 (transition analysis)、风险分析 (hazrd analysis)、信度分析(reliability analysis)、失效时间分析(failure-time analysis)、历史事件分析 (event history analysis) 等,主要研究 关注事件 发生所需要的时间,具体可归纳为三类 (Paul D. Allision,2018):
有 400 名犯人刑满被释放,从监狱出来后,研究人员开始追踪调查他们的情况,追踪时间为 2 年,以期观察他们是否会再次被捕。在这里,研究人员关注事件是 再次被逮捕, 解释变量为救助金、教育、工作状态等。
研究方法 A: 用二元被解释变量 (逮捕 = 1, 没有逮捕 = 0) 对解释变量进行线性回归;
研究方法 B: 把被解释变量定义为犯人从释放到第一次被捕的时间,再进行线性回归。
评价: 对于 "关注事件发生所需要的时间" 这一类的研究,用传统模型做实证,会面临截堵 (censoring), 以及解释变量发生了时变等主要问题, 而使用生存分析法则可以解决这些问题。
截堵分为 3 种类型:即右截堵 (right censoring)、左截堵 (left censoring) 和区间截堵 (interval censoring), 下面以生存分析的例子来进行区分。
设定一个恒定的值
右截堵: 将
左截堵: 将
区间截堵: 将
其中,右截堵是最常见的类型, 也是生存分析主要研究对象。
假设: 在生存分析中, 持续时间
含义: 在时间
或 以内,失败 (死亡) 的概率。
由此可以定义关注事件发生的概率对应的密度函数:
含义: 生存超过某个时间
的概率,即关注事件在时间 或 以内都未发生的概率
风险函数
含义: 关注事件到
时刻为止发生的概率,相比较风险函数更容易被精确估计。
当
解释: 指数分布的风险函数为常数, 说明瞬间关注事件发生的概率并不依赖已持续的时间。
当
解释:冈珀茨分布的风险函数呈指数变化率
, 并为单调函数:
当
解释:与冈珀茨模型不同, 威布尔分布的风险函数呈幂函数变化率
, 也为单调函数:
以上 3 种模型统一被称为 比例风险模型 (proportional hazards models, PH), 是随机变量
注:
为半弹性, 即某个解释变量增加 1 个单位,将导致风险函数平均增加百分之 。 为风险比率 (Hazard Ratio), 即某个解释变量变量增加 1 单位, 将导致新风险比率变为原来的 倍。 另外, 不含解释变量 的风险函数通常被称为 基准风险 。
此外,如果使用比例风险模型,需要满足 比例风险假定, 即风险函数的形式为:
检验的方式有: 对数-对数图 (log-log plot), 观测-预测图 (observed versus expected plot), 舍恩菲尔德残差检验 (Schoenfeld residuals tests)
加速失效时间模型 (accelerated failure-time model, AFT) 主要研究解释变量
加速失效时间模型的设定为:
其中,
持续时间 (生存时间):
那么,
风险函数:
把
风险函数满足上面的形式,则为加速失效时间模型。若
Cameron and Trival (2005) 总结有 5 种加速失效时间模型, 其中指数模型和威布尔模型既是比例风险模型 (PH) 又是加速失效时间模型 (AFT).

Table 17.5. Standard Parametric Models and Their Hazard and Survivor Functions
Parametric Model | Hazard Function | Survivor Function | Type |
---|---|---|---|
Exponential | PH, AFT | ||
Weibull | PH, AFT | ||
Generalized Weibull | PH | ||
Gompertz | PH | ||
Log-normal | AFT | ||
Log-logistic | AFT | ||
Gamma | AFT |
Source: Cameron and Trival (2005), Table 17.5
比例风险模型和加速失效时间模型均为参数模型, 需要对风险函数的具体形式作出假设,再用最大似然法估计。但是截堵数据可能会导致风险函数设定错误, 此时会出现不一致的 MLE 估计。Cox (1972, 1975) 以 PH 模型为基础提出估计
由于构成风险函数
此外,在过去的推文曾经介绍过使用 Tobit 模型来解决数据截堵的问题, Cox PH 模型和 Tobit 模型的方法存在很大差异,Tobit 模型无法很好地估计风险率,而且总体上学者们对 Cox PH 模型的认可度更高 (Cameron and Trival, 2005)。
本章节主要介绍做生存分析的基本步骤及实现的 Stata 命令, 以便于更好地分析生存数据。在介绍完操作程序后, 再通过介绍中国工业经济的一篇文章, 来让大家对生存分析的应用有更好的理解。基本步骤有:
使用的数据是来自 Stata-press 官网的 hip.dta 数据(右击可以另存到本地)。该数据集主要调查一种新的可穿戴充气防护装置是否可以保护老年人,即降低老年人发生跌倒骨折的风险。邀请了 48 位年龄为 60 岁以上的女性展开一项试验,任意挑选 28 名女性发放并让她们穿戴防护装置,作为实验组, 剩下的 20 名女性则不穿防护装置作为控制组。
研究中的关键变量定义如下:
首先,我们了解一下原始数据的概况:
. use http://www.stata-press.com/data/cggm3/hip, clear //调用 hip.dta 数据
. describe //对数据集进行描述
*-----------使用 describe 命令 table2 ------
Contains data from http://www.stata-press.com/data/cggm3/hip.dta
obs: 106 hip fracture study
vars: 7 30 Jan 2009 11:58
size: 1,060
-------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------
id byte %4.0g patient ID
time0 byte %5.0g begin of span
time1 byte %5.0g end of span
fracture byte %8.0g fracture event
protect byte %8.0g wears device
age byte %4.0g age at enrollment
calcium float %8.0g blood calcium level
-------------------------------------------------------------------
stset 命令将数据设定为适合生存分析的形式, 其语法为,
. stset tvar, failure(fail) id(idvar) origin(time torig)
各要素的含义如下:
tvar
为结束时间failure(fail)
设定关注事件, 默认 fail==1
为关注事件发生id(idvar)
设定样本中的用以标示每个观察值的变量origin(time torig)
设定 torig 为开始时间, 默认为 0。进行 stset 设定后,Stata 呈现结果如下:
. stset time1, origin(time time0) id(id) failure(fracture == 1)
*------------使用 stset 命令 table3 -------
id: id
failure event: fracture == 1
obs. time interval: (time1[_n-1], time1]
exit on or before: failure
t for analysis: (time-origin)
origin: time time0
--------------------------------------------------------------------------
106 total observations
0 exclusions
--------------------------------------------------------------------------
106 observations remaining, representing
48 subjects
31 failures in single-failure-per-subject data
744 total analysis time at risk and under observation
at risk from t = 0
earliest observed entry t = 0
last observed exit t = 39
据表 3 显示, 样本中共有 106 个观测值, 48 个 被试, 在调查期间有 31 个被试发生过摔倒骨折。另外, 执行 stset
命令后会自动生成 4 个变量,分别为:_st, _d, _t, _t0,含义为:
stdescribe
命令是在 stset 命令之后使用的, 用于描述生存分析数据的基本特征
. stdescribe
*------------使用 stdescribe 命令 table4 -------
failure _d: fracture == 1
analysis time _t: (time1-origin)
origin: time time0
id: id
|-------------- per subject --------------|
Category total mean min median max
------------------------------------------------------------------------------
no. of subjects 48
no. of records 106 2.208333 1 2 3
(first) entry time 0 0 0 0
(final) exit time 15.5 1 12.5 39
subjects with gap 0
time on gap if gap 0 . . . .
time at risk 744 15.5 1 12.5 39
failures 31 .6458333 0 1 1
------------------------------------------------------------------------------
据表 4 显示, 48 个被试一共被记录过 106 次, 平均记录超过 2 次, 一共骨折了 31 次, 平均每人发生 0.64 次骨折。第 3、4 行表示若记录开始平均为 0, 终止 (发生骨折,或者没有发生骨折但是退出记录) 时间平均为 15.5 个月, 即大概 48 个被试平均 15.5 个月会摔倒一次,最短为 1 个月,最长为 39 个月。
生存分析的第二步是画生存函数、累积风险函数和风险函数图,主要使用 sts graph
命令,可以绘制 Kaplan-Meier 生存估计量, Nelson-Aalen 累积风险估计量和风险函数图。
Kaplan-Meier 估计量大致等于样本存活 (关注事件未发生) 时间超过
*-----使用 sts graph 命令 figure5 -----
//数据已设置成生存分析数据
. use http://www.stata-press.com/data/cggm3/hip2, clear
. sts graph //生存分析图
. graph export figure5.png
从总体生存函数图无法得知防护装置对生存的功效,下面画分组的生存函数图。
*-----使用 sts graph 命令 figure6-----
* 以是否穿防护装置分组画生存分析图
* plot2opts(lp("-"))表示第二个图用虚线表示
. sts graph, by(protect) plot2opts(lp("-"))
. graph export figure6.png
从图 6 可以看出是否穿戴防护装置对生存函数有明显影响, 相比较控制组,实验组有更好的生存经历。
Nelson-Aalen 累积风险估计量是对局部风险率加总得出的,估计量为
*-----使用 sts graph 命令 figure7-----
sts graph, cumhaz ci //cumhaz 选项表示画累积风险函数, ci 显示 95% 的置信区间。
graph export figure7.png
风险率估计量可表示为:
其中,
*-----使用 sts graph 命令 figure8-----
sts graph, cumhaz ci //cumhaz by(protect)选项表示画累积风险函数, ci 显示 95% 的置信区间。
graph export figure7.png
由图 8 可知,是否穿戴防护装置对风险函数影响较大, 控制组的发生骨折的风险一直远远高于实验组。
上述分析均没有考虑解释变量,只是对被解释变量进行初步的描述性分析, 下面进行更为准确的模型估计分析。
. streg protect age calcium, nohr nolog dist(weib)
*- dist(weib) 代表威布尔回归的风险函数,
*- dist(e) 为指数回归,dist(gom) 为刚珀茨回归,
*- nohr 显示回归系数而不显示风险比率
*-----使用 streg 命令 table5-----
failure _d: fracture
analysis time _t: time1
id: id
Weibull PH regression
No. of subjects = 48 Number of obs = 106
No. of failures = 31
Time at risk = 714
LR chi2(3) = 35.22
Log likelihood = -41.687862 Prob > chi2 = 0.0000
------------------------------------------------------------------------------
_t | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
protect | -2.192387 .4078599 -5.38 0.000 -2.991778 -1.392996
age | .0801079 .0550936 1.45 0.146 -.0278735 .1880893
calcium | -.1744454 .2194006 -0.80 0.427 -.6044626 .2555718
_cons | -7.787834 5.734033 -1.36 0.174 -19.02633 3.450665
-------------+----------------------------------------------------------------
/ln_p | .5207567 .1369941 3.80 0.000 .2522531 .7892602
-------------+----------------------------------------------------------------
p | 1.683301 .2306023 1.286922 2.201767
1/p | .5940709 .0813842 .4541807 .777048
------------------------------------------------------------------------------
解读: 据表 5 显示, 穿戴防护装置会导致老年人骨折的风险降低 2.19%, 在 1% 的显著性水平下显著, 年龄上升会导致骨折风险的增加, 血钙的增加会降低骨折, 但不显著。 倒数第 3 行 ln_p = 0 的 p 值为 0, 显著拒绝指数回归的原假设, 认为应该使用威布尔回归。倒数第 2 行的 p 值大于 1, 说明风险函数随时间而递增。
*-----使用 stcurve 命令 figure 9-----
. stcurve, hazard //画风险函数图
选择加速失效时间模型的对数正态回归
*-----使用 streg 命令 table6-----
. streg protect age calcium, nolog dist(logn)
failure _d: fracture
analysis time _t: time1
id: id
Lognormal AFT regression
No. of subjects = 48 Number of obs = 106
No. of failures = 31
Time at risk = 714
LR chi2(3) = 35.50
Log likelihood = -41.758706 Prob > chi2 = 0.0000
------------------------------------------------------------------------------
_t | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
protect | 1.44531 .2495669 5.79 0.000 .9561681 1.934452
age | -.0639809 .041565 -1.54 0.124 -.1454468 .0174849
calcium | .0692953 .1668585 0.42 0.678 -.2577412 .3963319
_cons | 5.74614 4.416337 1.30 0.193 -2.909722 14.402
-------------+----------------------------------------------------------------
/lnsigma | -.294334 .1271106 -2.32 0.021 -.5434661 -.0452018
-------------+----------------------------------------------------------------
sigma | .7450276 .0947009 .5807319 .9558046
------------------------------------------------------------------------------
解读: 对数回归的系数符号与比例风险模型的系数符号相反, 在 2.3 小节曾做过解释, 比例风险模型研究风险率的变化而 AFT 研究的是平均持续时间。防护装置可以显著降低风险率, 增加平均持续时间。
*-----使用 stcurve 命令 figure 10-----
. stcurve, hazard //画风险函数图
对参数回归的具体分布无法确定, 故下面使用半参数 Cox PH 回归。
. stcox protect age calcium,r nohr nolog
*-----使用 streg 命令 table 7-----
Cox regression -- Breslow method for ties
No. of subjects = 48 Number of obs = 106
No. of failures = 31
Time at risk = 714
Wald chi2(3) = 31.21
Log pseudolikelihood = -82.062396 Prob > chi2 = 0.0000
(Std. Err. adjusted for 48 clusters in id)
------------------------------------------------------------------------------
| Robust
_t | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
protect | -2.242575 .4328035 -5.18 0.000 -3.090855 -1.394296
age | .0676642 .0438904 1.54 0.123 -.0183594 .1536878
calcium | -.2114179 .1600249 -1.32 0.186 -.5250609 .1022251
------------------------------------------------------------------------------
解读: 从表 7 可以看出, Cox PH 回归和前面参数回归的结果类似, 但不依赖于具体的分布假设, 故结果更稳健。
*-----使用 stcurve 命令 figure 11-----
. stcurve, hazard //画风险函数图
本文从介绍的角度讨论了以上模型,实际做实证时挑选适合的部分模型做基本模型即可, 挑选的规则有对数似然值和 AIC 等。
比例风险模型和 Cox PH 模型的重要假设是
若对数 - 对数图中的曲线相互平行, 则比例风险设定是成立的。
*-----使用 stphplot 命令 figure 12-----
. stphplot, by(protect)
两条曲线大致是平行的, 满足比例风险假定。
若观测值和预测值非常接近, 则比例风险设定是成立的。
*-----使用 stcoxkm 命令 figure 13-----
. stcoxkm, by(protect)
生存函数的预测值和观测值总体上相距比较近, 基本满足比例风险假定。
对于检验比例风险假定最有用的是舍恩尔德残差,如果比例风险假设成立, 则舍恩尔德残差不应该随时间呈现规律性变化。 对于每个变量
quitely stcox protect age calcium,r nohr nolog
estat phtest, detail
*-----使用 estat phtest命令 table8 -----
Test of proportional-hazards assumption
Time: Time
----------------------------------------------------------------
| rho chi2 df Prob>chi2
------------+---------------------------------------------------
protect | 0.00064 0.00 1 0.9974
age | -0.10011 0.17 1 0.6828
calcium | 0.00017 0.00 1 0.9997
------------+---------------------------------------------------
global test | 0.26 3 0.9674
----------------------------------------------------------------
三个解释变量都没有拒绝原舍恩尔德残差对时间回归的斜率为 0 的原假设, 故支持比例风险假定。
*-----使用 estat phtest命令 figure14 -----
. estat phtest, plot(protect)
从残差与时间的拟合图来看其斜率大致为 0, 满足比例风险假定。
吴先明等 (2017) 考察了工资扭曲 (劳动者实际所得收入小于劳动价值应得收入) 与企业成长之间的关系, 并引入种群密度研究三者之间的作用机制。
下载地址:http://www.ciejournal.org/Magazine/show/?id=51567
【Data & Stata Files】,右击另存即可。
作者将企业的生命周期划分为三个阶段,分别为初创期、成长成熟期和衰退期, 使用 Cox PH 模型分析了工资扭曲对企业从一个阶段跨越到另一个阶段的影响,并用 AFT 模型做相关稳定性检验。下面截取文章基本回归结果及作者给出的相关代码。
Source: 吴先明等 (2017),表 3. Data & Stata Files
从初创期到成长成熟期, 工资扭曲 (Wdist) 的系数显著为负, 表明工资扭曲在整体上促进初创企业的成长, 从成长成熟期到衰退期,工资扭曲 (Wdist) 的系数为负但不显著。
对应的 Stata 估计命令如下:
*-吴先明等 (2017) 表 3 - Stata 命令 - 基于 Cox PH 的回归分析
*-完整文件地址:http://www.ciejournal.org/Magazine/show/?id=51567
/////以企业进入成长成熟期作为兴趣事件构建生存分析数据/////
stset year, id(panelid2) failure(life==1) origin(xkysj)
/////表3基于Cox PH模型的回归分析-从初创期到成长成熟期部分/////
stcox wdist pdens_100 size lever cdens_100 subs roa export state foreig ///
HHI grow mature decline , nohr
est store model1
stcox wdist pdens_100 wdist_pdens_100 size lever cdens_100 subs roa export ///
state foreig HHI grow mature decline , nohr
est store model2
stcox wdist pdens_100 wdist_pdens_100 size lever cdens_100 subs roa export ///
state foreig HHI grow mature decline _Iindu* , nohr
est store model3
stcox wdist pdens_100 wdist_pdens_100 size lever cdens_100 subs roa export ///
state foreig HHI grow mature decline _I* , nohr
est store model4
stset, clear
/////以企业进入衰退期作为兴趣事件
/////但以企业进入成长成熟期作为进入研究的时间,构建生存分析数据
stset year, id(panelid2) failure(life==2) origin(xkysj) enter(life==1)
/////表3基于Cox PH模型的回归分析-从成长成熟期到衰退期部分/////
stcox wdist pdens_100 size lever cdens_100 subs roa export state foreig ///
HHI grow mature decline , nohr
est store model5
stcox wdist pdens_100 wdist_pdens_100 size lever cdens_100 subs roa export ///
state foreig HHI grow mature decline , nohr
est store model6
stcox wdist pdens_100 wdist_pdens_100 size lever cdens_100 subs roa export ///
state foreig HHI grow mature decline _Iindu* , nohr
est store model7
stcox wdist pdens_100 wdist_pdens_100 size lever cdens_100 subs roa export ///
state foreig HHI grow mature decline _I* , nohr
est store model8
///导出表3基于Cox PH模型的回归分析
outreg2 [*] using 表3基于Cox PH模型的回归分析.xls, replace
生存分析可以有效地解决数据截堵问题,是研究风险率、生存时间的一种优良的计量方法, 本文通过对生存分析的概念、模型、分析步骤及 Stata 命令等进行介绍,来让大家初步地了解这一方法。考虑到这篇文章是作为生存分析入门使用, 相关模型估计并没有做深入的说明, 这也是本文非常大的不足之处。 如果大家想更深入地学习理论, 推荐 Cameron and Trival (2005, Chapter17-19)。
use http://www.stata-press.com/data/cggm3/hip, clear //调用 hip.dta 数据
describe //对数据集进行描述
stset time1, origin(time time0) id(id) failure(fracture == 1) //设定数据
stdescribe //在 stset 命令之后使用的, 用于描述生存分析数据的基本特征
*-----使用 sts graph 命令 figure5 -----
use http://www.stata-press.com/data/cggm3/hip2, clear //数据已设置成生存分析数据
sts graph //生存分析图
graph export figure5.png
*-----使用 sts graph 命令 figure6-----
sts graph, by(protect) plot2opts(lp("-")) //以是否穿防护装置分组画生存分析图 plot2opts(lp("-"))表示第二个图用虚线表示
graph export figure6.png
*-----使用 sts graph 命令 figure7-----
sts graph, cumhaz ci //cumhaz 选项表示画累积风险函数, ci 显示 95% 的置信区间。
graph export figure7.png
*-----使用 sts graph 命令 figure8-----
sts graph, cumhaz ci //cumhaz by(protect)选项表示画累积风险函数, ci 显示 95% 的置信区间。
graph export figure7.png
*-----使用 streg 命令 table5-----
streg protect age calcium, nohr nolog dist(weib)
* dist(weib)代表威布尔回归的风险函数,
* dist(e)指数回归,
* dist(gom)刚珀茨回归,
* nohr显示回归系数而不显示风险比率
*-----使用 stcurve 命令 figure 9-----
stcurve, hazard //画风险函数图
*-----使用 stcurve 命令 figure 10-----
stcurve, hazard //画风险函数图
*-----使用 streg 命令 table6-----
streg protect age calcium, nolog dist(logn)
*-----使用 streg 命令 table 7-----
. stcox protect age calcium,r nohr nolog
*-----使用 stcurve 命令 figure 11-----
stcurve, hazard //画风险函数图
*-----使用 stphplot 命令 figure 12-----
. stphplot, by(protect)
*-----使用 stcoxkm 命令 figure 13-----
. stcoxkm, by(protect)
*-----使用 estat phtest命令 table8 -----
quitely stcox protect age calcium,r nohr nolog
estat phtest, detail
*-----使用 estat phtest命令 figure14 -----
estat phtest, plot(protect)
连享会-直播课 上线了!
http://lianxh.duanshu.com
免费公开课:
直击面板数据模型 - 连玉君,时长:1小时40分钟 Stata 33 讲 - 连玉君, 每讲 15 分钟. 部分直播课 课程资料下载 (PPT,dofiles等)
支持回看,所有课程可以随时购买观看。
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 ⭐ | DSGE, 因果推断, 空间计量等 | |
⭕ Stata数据清洗 | 游万海 | 直播, 2 小时,已上线 |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会学习群-常见问题解答汇总:
✨ https://gitee.com/arlionn/WD