政务大数据应用与分析 (80700673)
清华大学社会科学学院
今天,让我们成为生物学家
Terminology
在分析前要想清楚的
⇓
策略选项
就是字面意思,count出来得数据。
关注事件发生点,解释与何因素有关,通常被称作“事件史”(event history)分析。
分析前认知:
主要步骤:
| unit | time | event | ev | risk |
|---|---|---|---|---|
| 1 | 1 | 0 | 4.3 | 1 |
| 1 | 2 | 0 | 1.2 | 1 |
| 1 | 3 | 1 | 4.2 | 1 |
| 1 | 4 | NA | 1.6 | 0 |
| 2 | 5 | 0 | 6.1 | 1 |
| 2 | 6 | 0 | 3.2 | 1 |
| 2 | 7 | 0 | 7.2 | 1 |
| 2 | 4 | 1 | 3.9 | 1 |
公式对于理解持续时间分析非常之重要!🥁🥁🥁
Survival rate: the probability that an event will continue to exist (\(T\)) beyond a certain period of time (\(t\)).
\[S(t|X) = P(T > t | X).\]
PDF: \(f(t|x) = P(T = t | X).\)
CDF: \(F(t) = P(T\leq t | X) = 1 - S(t).\)
Hazard: the instantaneous risk at that time of the event happening, given that it hasn’t happened yet.
\[P(T = t|T\geq t, X) = \frac{f(t|X)}{S(t|X)} = \frac{P(T \color{red}{=} t | X)}{P(T \color{red}{>} t | X)}.\]
The probability that the event occurs during a specific time point, given that it hasn’t already occurred.
令 \(Y^*\) 为导致可见事件( \(Y_{it}\) )发生的不可见变量:
\[ Y_{it} = \begin{cases} 1 & \text{if } Y^*\geq 0, \\ 0 & \text{if } Y^*< 0. \end{cases} \]
那么
\[ \begin{align} P(Y_{it} = 1|X) =& P(Y^* \geq 0|X)\\ =& P(X\beta + \epsilon\geq 0|X)\\ =& P(\epsilon\geq 0 - X\beta|X)\\ =& 1 - P(\epsilon\leq - X\beta|X)\\ =& 1 - F(-X\beta|X) \end{align} \]
令 \(\epsilon\sim logistic\),1 则 \[P(Y_it = 1|X) = 1 - \Lambda(-X\beta) = \Lambda(X\beta).\]
代入logistic分布可得,
\[ \begin{align} P(Y_{it} = 1|X) =& 1 - \frac{e^{-X\beta}}{1 + e^{-X\beta}}\\ =& \frac{e^{X\beta}}{1 + e^{X\beta}}\\ P(Y_{it} = 0|X) =& \frac{1}{1 + e^{X\beta}} \end{align} \]
研究美国印第安部落与州协调法案(Indian Gaming Compact)推行情况
解释变量:
religion), the number of federally recognized tribes (fedtrib)totpop)rpcpinc)ideology)fedtribe)| state | stateno | year | totpop | rpcpinc | ideology | fedtribe | religion | adopt_year |
|---|---|---|---|---|---|---|---|---|
| AL | 1 | 1989 | 4.102 | 18.02829 | -23.1 | 1 | 33.9 | NA |
| AL | 1 | 1990 | 4.118 | 17.85045 | -23.1 | 1 | 33.9 | NA |
| AL | 1 | 1991 | 4.087 | 18.02460 | -23.1 | 1 | 33.9 | NA |
| AL | 1 | 1992 | 4.131 | 18.56343 | -23.1 | 1 | 33.9 | NA |
| AL | 1 | 1993 | 4.181 | 18.66846 | -23.1 | 1 | 33.9 | NA |
| AL | 1 | 1994 | 4.215 | 19.08401 | -23.1 | 1 | 33.9 | NA |
\(R(t)\equiv \{i, y_{it} = 0 | y_{it} = 1\}.\)
| year | sum_adopt | sum_risk | hazard | survival |
|---|---|---|---|---|
| 1989 | 0 | 48 | 0.00 | 0.96 |
| 1990 | 5 | 48 | 0.10 | 0.96 |
| 1991 | 2 | 43 | 0.05 | 0.86 |
| 1992 | 8 | 41 | 0.20 | 0.82 |
| 1993 | 4 | 33 | 0.12 | 0.66 |
| 1994 | 3 | 29 | 0.10 | 0.58 |
| 1995 | 2 | 26 | 0.08 | 0.52 |
| 1996 | 0 | 24 | 0.00 | 0.48 |
| 1997 | 0 | 24 | 0.00 | 0.48 |
| 1998 | 0 | 24 | 0.00 | 0.48 |
| 1999 | 0 | 24 | 0.00 | 0.48 |
EH vs. Duration
Event history:事件发生的时间点
Duration:总体时间段
| duration | opposition | polarization | Beilgium | Canada |
|---|---|---|---|---|
| 3 | -0.86792 | 11 | 1 | 0 |
| 7 | -0.86792 | 11 | 1 | 0 |
| 20 | -0.14103 | 11 | 1 | 0 |
| 6 | -0.14103 | 11 | 0 | 1 |
| 17 | -0.58065 | 6 | 0 | 1 |
| 7 | -0.79592 | 3 | 0 | 1 |
最常见的四种参数模型
特点:用p调节baseline方向
p < 1, 单减
p = 1, = exponential
p > 1, 单增
\[ \begin{align} S(u)_{exp} =& e^{-u}\rightarrow S(u)_{weibull} = e^{-u^\color{red}{p}},\\ F(u) =& 1 - e^{-u^p}; f(u) = pu^{p - 1}e^{-u^p},\\ h(u) =& pu^{p - 1} \end{align} \]
特点: 允许非单调hazard, \(ln(u_i)\sim N(\mu, \sigma^2).\)
p: the scale parameter
κ: the shape parameter
\[ f(u) = pu^{p - 1}e^{-u^p}\rightarrow f(u) = \frac{pu^{p\color{red}{\kappa} - 1}e^{-u^p}}{\color{red}{\Gamma(\kappa)}}. \]
\[f(u) = \frac{\color{orange}{p}u^{\color{orange}{p}\color{blue}{\kappa} - 1}e^{-u^\color{orange}{p}}}{\color{red}{\beta}^{\color{orange}{p}\color{blue}{\kappa}}\Gamma(\color{blue}{\kappa})}\]
β = 1 ⇒ Gamma;
κ = 0 ⇒ log-normal;
κ = 1 ⇒ Weibull;
κ = p = 1 ⇒ Exponential.
Expected value
Hazard ratio
\[ \begin{align} \frac{h(Y_i|X + 1)}{h(Y_i|X)} =& \frac{pexp[-(X_i + 1)\beta]^py_i^{p - 1}}{pexp(-X_i\beta)^py_i^{p - 1}}\\ =& exp(-\beta)^p = e^{\color{red}{-\beta p}} \end{align} \]
-βp: proportional hazard metric;
β: accumulative failure time metric.
\(Y_i = e^{X_i\beta}u_i\Rightarrow u_i = \frac{Y_i}{e^{X_i\beta}} = Y_ie^{-X_i\beta}\).
令\(\lambda\equiv e^{-X_i\beta}\), 那么,对于exponential
\[ \begin{align} u_i =& Y_i\lambda_i \\ F(Y_i|X) =& 1 - e^{-Y_i\lambda_i}\\ f(Y_i|X) =& \lambda_ie^{-Y_i\lambda_i}\\ h(Y_i|X) =& \lambda_i = e^{-Y_i\lambda_i} \end{align} \]
同理,对于Weibull, \(h(Y_i|X) = p\lambda^pY^{p - 1}.\)
Cox model:不对Baseline hazard形状做假设
令\(\lambda\equiv e^{-X_i\beta}\),
\[ \begin{align} h(t|x) =& \overbrace{h_0(t)}^{Non-parametric}\lambda_i^p,\\ P(1fails@t_1|someone\ f@t_1) =& \frac{h_0(t_1)\lambda_i^p}{\sum^n_{i = 1}h_0(t_1)\lambda_i^p},\\ P(2fails@t_2|someone\ f@t_2) =& \frac{h_0(t_2)\lambda_i^p}{\sum^n_{i = 2}h_0(t_1)\lambda_i^p} \end{align} \]
Baseline hazard随时间改变
解决方法:
对于小概率事件(< 5%)→ 使用logit会高估Pr(Y = 1)。
解决方法:1
假定没有其他更好sample
三种将时间纳入事件的方式 (Count, binary, duration),选哪个?
| Pros | Cons | |
|---|---|---|
| Count | Aggregative, prevent measurement error | Lose the ability to record any identified variations over time and the time series |
| Duration | Record the time and changing moment and sequences | Lose the precision of when within each time unit an event occurs |
| Binary | Most disaggregate | Lose when the change happens in a time line |
Censored point为c, 则:
\[ L = \prod^{n}_{i=1}f(y_i|x_i)^{1 - c}S(y^c|X_i)^c, \] where
\[c = \begin{cases} 1 & \text{if } y_i\geq y_c, \\ 0 & \text{if } y_i< y_c. \end{cases} \]
当censored部分扩大,模型的efficiency会降低。
如将selection effect也考虑进去的时候,需要modeling selection process1
Assumption (Cox): Covariates do not vary over time.
Discrete model: Combine in the logit
Parametric: Conditional likelihood function (CTD: Continuous Time Duration)
\[ \begin{align} P(y_i>1|X_1(1)) =& \frac{P(y_i>2, y_i>1|X_1(2))}{P(y_i>1|X_1(2))}\\ =& S(1|X_1(1))p(y_i>2|y_i>1, X_1(2))\\ =& \frac{S(2|X_1(2))}{S(1|X_1(2))} \end{align} \]
Cox: put the value of that time moment, \(X_i(t)\), at failure time t in continuing the partial likelihood.
Type:
⇒ Risk set:
两种方法:
\[ \begin{align} h(y_1) =& h_0(y)exp(X\beta + v_1)\\ h(y_2) =& h_0(y)exp(X\beta + v_2) \end{align} \]
用\((v_1, v_2)\) jointed distributed 来纳入非独立性。
可能有两种意义:
解决方法:
对Cure进行建模,\(Z_i\): 1 (cured), 0 (not cured), 其pdf为 \(P(Z_i = 1)\) , 那么对于0来说其survial model为 \(S(y|x)P(Z = 0)\).
每个观测点的basedline hazard function都一样吗?
解法
\[ y_i = X\beta_1 + Xln(t)\beta_2 + ln(t)\beta_3. \]