## サンプルデータを作成する
まず、生存時間解析用のサンプルデータを作りましょう。
現在あるデータは消えるので注意してください。
```stata
clear
input id treatment month event
1 0 7 1
2 0 5 1
3 0 8 1
4 0 6 0
5 0 4 1
6 0 3 1
7 0 5 0
8 0 6 1
9 0 7 1
10 0 4 1
11 0 5 1
12 0 3 0
13 0 6 1
14 0 5 1
15 0 4 1
16 1 12 1
17 1 14 1
18 1 16 0
19 1 15 1
20 1 13 1
21 1 18 1
22 1 14 0
23 1 15 1
24 1 13 1
25 1 17 1
26 1 14 1
27 1 12 0
28 1 16 1
29 1 15 1
30 1 13 0
end
```
次に以下を実行しましょう。
`stset` のあとに、時間の変数を記載します。そのあと、`failure()` のなかにイベント(1がイベントあり)の変数を記載します。
```stata
stset month, failure(event)
```
`list in 1/5` で 1-5行目を表示すると以下のようになっています。
`_` アンダースコアから始まる変数がいくつかつくられた状態です。これによって生存時間解析の準備がととのいました。
```stata
. list in 1/5
+-----------------------------------------------------+
| id treatm~t month event _st _d _t _t0 |
|-----------------------------------------------------|
1. | 1 0 7 1 1 1 7 0 |
2. | 2 0 5 1 1 1 5 0 |
3. | 3 0 8 1 1 1 8 0 |
4. | 4 0 6 0 1 0 6 0 |
5. | 5 0 4 1 1 1 4 0 |
+-----------------------------------------------------+
```
## 生存時間曲線をつくる
生存時間曲線(カプランマイヤー曲線)を書いてみましょう。
```stata
sts graph
```
たったこれだけで以下のようなグラフが書けます。
![[Pasted image 20250318202858.png]]
段階的に複雑にしてみます。
打ちきりの症例にヒゲ(tick mark)をつけます。
```stata
sts graph, censored(single)
```
![[Pasted image 20250318203000.png]]
治療で2群に分かれているので、2群を示します。
```stata
sts graph, censored(single) by(treatment)
```
![[Pasted image 20250318203151.png]]
95%信頼区間をつけることもできます。
```stata
sts graph, censored(single) by(treatment) risktable ci
```
![[Pasted image 20250318203203.png]]
群毎に色をわけることもできます。
```stata
sts graph, censored(single) by(treatment) risktable ci ///
plot1opts(lcolor(red) lpattern(solid)) ///
ci1opts(color(red%30)) ///
plot2opts(lcolor(green) lpattern(solid)) ///
ci2opts(color(green%30)) ///
legend(order(2 "Control" 1 "95%CI" 4 "Treatment" 3 "95%CI"))
```
![[Pasted image 20250318203215.png]]
## ログランク検定をする
ログランク検定もできます。
```stata
sts test treatment
```
結果は以下の通りです。
`Pr>chi2 = 0.0000` がログランク検定の結果です。
```stata
. sts test treatment
Failure _d: event
Analysis time _t: month
Equality of survivor functions
Log-rank test
| Observed Expected
treatment | events events
----------+-------------------------
0 | 12 4.06
1 | 11 18.94
----------+-------------------------
Total | 23 23.00
chi2(1) = 27.31
Pr>chi2 = 0.0000
```
## Cox比例ハザードモデルを行う
`stcox treatment` をすることで Cox比例ハザードモデルでの解析も行えます。
## 他のデータでも試してみよう
もっと大きなデータのサンプルもあります。
`help stset`
`help sts graph`
をコマンド入力して Example のセクションに行くと、色々サンプルがのっています