開始使用免費開始

忽略設限的練習

你辦了一場派對,凌晨 1 點賓客突然開始跳舞。你想分析朋友會跳多久,於是開始蒐集資料。問題是,你過了一陣子累了就去睡覺。

你得到下列右設限(right-censored)的舞蹈時間資料,已放在 dancedat

  • name 是你朋友的名字。
  • time 是右設限的跳舞時間。
  • obs_end 表示你是否觀察到朋友停止跳舞的時點(1),或是在他們停下前你就去睡了(0)。

你早上開始分析資料,但因為還很累,一開始忽略了資料中有設限觀測。後來你想起在 DataCamp 上學過的內容,才用正確的方法處理。

本練習已為你載入 survival 套件。

本練習屬於課程

R 的生存分析

檢視課程

練習說明

  • 先假裝所有設限觀測都是真實觀測,估計生存函式。
  • 使用 Kaplan-Meier 從這個資料集估計生存函式。
  • ggsurvplot_combine() 將正確與錯誤的兩條生存曲線繪在一起並比較,留意它們的差異。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create dancedat data
dancedat <- data.frame(
  name = c("Chris", "Martin", "Conny", "Desi", "Reni", "Phil", 
    "Flo", "Andrea", "Isaac", "Dayra", "Caspar"),
  time = c(20, 2, 14, 22, 3, 7, 4, 15, 25, 17, 12),
  obs_end = c(1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0))

# Estimate the survivor function pretending that all censored observations are actual observations.
km_wrong <- survfit(___(time) ~ 1, data = dancedat)

# Estimate the survivor function from this dataset via kaplan-meier.
km <- survfit(___(___, ___) ~ ___, data = dancedat)

# Plot the two and compare
ggsurvplot_combine(list(correct = ___, wrong = ___))
編輯並執行程式碼