Taller Análisis Tiempo hasta el Evento

Con R

Dra. Daniela Toledo

2025-06-30

Parte 1: Introducción

Outcome:

  • Variable aleatoria discreta o continua positiva.

  • Es el tiempo desde: un inicio definido –> un evento definido.

Censura

  • Cuando los eventos de inicio o fin no son observados.

  • En la censura a la derecha, sólo sabemos que la ocurrencia del evento excedió un valor particular.

  • Censura a la izquierda, el evento ocurrió antes de cierto momento.

  • Censura intervalar: el evento ocurrió entre determinado intervalo de tiempo.

Clasificación de censura:

  • Tipo I: Los tiempos de censura están pre especificados.

  • Tipo II: Los individuos (u objetos) son observados hasta que una fracción del total han presentado el evento \(\rightarrow\) contexto industrial.

  • Aleatorio: importante que así sea para evitar sesgo. El abandono del estudio por parte de los pacientes (debe ser no informativa) o los eventos competitivos. Otra forma es la censura administrativa \(\rightarrow\) es independiente del mecanismo de generación del evento.

Los objetivos fundamentales de un análisis de sobrevida son:

  • Describir la distribución de los tiempos hasta un evento.

  • Comparar dos o más curvas de sobrevida.

  • Evaluar el efecto que distintos factores tienen sobre la supervivencia.

    Importante

    • En estos análisis, la variable de resultado (el *outcome*) es el tiempo, una medida siempre positiva y que a menudo incluye observaciones censuradas.

Formato de datos en las BD

# Verifica si el paquete está instalado, si no, 
#lo instala desde un repositorio específico 
if (!require("asaur")) {   install.packages("asaur", 
                repos = "https://cloud.r-project.org/") } 
# Carga la librería library(asaur)
library(asaur)

BD Cáncer Gástrico

gastricXelox [23:27,]  
   timeWeeks delta
23        42     1
24        43     1
25        43     0
26        46     1
27        48     0


BD Cáncer de Páncreas

head(pancreatic)
  stage    onstudy progression      death
1     M 12/16/2005    2/2/2006 10/19/2006
2     M   1/6/2006   2/26/2006  4/19/2006
3    LA   2/3/2006    8/2/2006  1/19/2007
4     M  3/30/2006           .  5/11/2006
5    LA  4/27/2006   3/11/2007  5/29/2007
6     M   5/7/2006   6/25/2006 10/11/2006

BD Cáncer de Próstata

# Hay dos outcomes de interes, muerte por cáncer de próstata 
#y muerte por cualquier causa.

prostateSurvival[88:95,]
   grade stage ageGroup survTime status
88  poor    T2    75-79       33      0
89  mode    T2    75-79        6      0
90  mode   T1c    75-79       15      2
91  mode    T2    70-74        6      2
92  mode  T1ab      80+       93      1
93  poor    T2      80+       60      2
94  mode    T2      80+        1      0
95  mode  T1ab    75-79       34      0

BD Terapia para de dejar de fumar

#|code-overflow: scroll
pharmacoSmoking [1:6.2:8]
     id ttr relapse         grp age gender     race employment
1    21 182       0   patchOnly  36   Male    white         ft
2   113  14       1   patchOnly  41   Male    white      other
3    39   5       1 combination  25 Female    white      other
4    80  16       1 combination  54   Male    white         ft
5    87   0       1 combination  45   Male    white      other
6    29 182       0 combination  43   Male hispanic         ft
7    16  14       1   patchOnly  66   Male    black         pt
8    35  77       1   patchOnly  78 Female    black      other
9    54   2       1   patchOnly  40 Female    black         ft
10   70   0       1   patchOnly  38   Male    black         ft
11   84  12       1   patchOnly  64 Female    black      other
12   85 182       0 combination  51   Male    black         ft
13   25  21       1   patchOnly  37 Female    white         pt
14   47   3       1   patchOnly  65   Male    white      other
15   59 170       1   patchOnly  42 Female    white         ft
16   63  25       1   patchOnly  40   Male    white      other
17  102   4       1   patchOnly  65 Female    white      other
18    3 182       0 combination  52 Female    white      other
19   15 140       1 combination  43   Male    white         ft
20   32  63       1 combination  34 Female    white         ft
21   79  15       1 combination  46 Female    white      other
22   90 140       1 combination  60   Male    white         ft
23  110 110       1 combination  49 Female    white      other
24  127 182       0 combination  58 Female    white         ft
25  119   0       1   patchOnly  48   Male hispanic      other
26   33 182       0 combination  54 Female hispanic      other
27   62  15       1   patchOnly  49 Female    black         pt
28   67 182       0   patchOnly  55   Male    black         ft
29  112   4       1   patchOnly  33   Male    black         ft
30   60  56       1 combination  49 Female    black         ft
31   93   2       1 combination  46   Male    black         ft
32  122  80       1   patchOnly  34 Female    other         ft
33  130 182       0 combination  46 Female    white         pt
34   19  56       1   patchOnly  52   Male    black      other
35   65   0       1   patchOnly  52 Female    black         ft
36    4  14       1   patchOnly  48 Female    white         ft
37   20  14       1   patchOnly  48 Female    white         ft
38   22  28       1   patchOnly  56 Female    white         ft
39   26 182       0   patchOnly  58   Male    white      other
40   43   6       1   patchOnly  60   Male    white         ft
41  107 182       0   patchOnly  55   Male    white         ft
42  111  14       1   patchOnly  43 Female    white         ft
43  117  15       1   patchOnly  55 Female    white         ft
44    8 182       0 combination  70 Female    white      other
45   12  75       1 combination  62 Female    white         pt
46   13  30       1 combination  86   Male    white      other
47   23   4       1 combination  52   Male    white         ft
48   30  56       1 combination  27 Female    white         ft
49   34 182       0 combination  52   Male    white         ft
50   36 182       0 combination  62   Male    white         ft
51   38   5       1 combination  57 Female    white         ft
52   44   8       1 combination  40   Male    white         ft
53   61 140       1 combination  49   Male    white      other
54   68  20       1 combination  68 Female    white      other
55   69  63       1 combination  47 Female    white         pt
56   82  30       1 combination  46 Female    white         ft
57   97   8       1 combination  55   Male    white         pt
58  106  50       1 combination  29   Male    white         ft
59  114  14       1 combination  64 Female    white         pt
60  120   0       1 combination  52   Male    white         ft
61   40  84       1   patchOnly  38 Female hispanic         ft
62   49   0       1   patchOnly  35   Male hispanic      other
63  125 105       1   patchOnly  50 Female hispanic      other
64  123 182       0 combination  63 Female hispanic      other
65    7 182       0   patchOnly  58   Male    black         ft
66    9 182       0   patchOnly  56   Male    black         ft
67   37   7       1   patchOnly  44 Female    black      other
68   52 182       0   patchOnly  34 Female    black         ft
69   86   0       1   patchOnly  49 Female    black         ft
70   94   8       1   patchOnly  43 Female    black      other
71  104   1       1   patchOnly  39 Female    black         ft
72   42 182       0 combination  41 Female    black         ft
73   75  12       1 combination  46 Female    black      other
74  100 182       0 combination  46 Female    black         ft
75    1  49       1   patchOnly  53 Female    white         pt
76    6 182       0   patchOnly  58   Male    white         ft
77   11 182       0   patchOnly  40 Female    white         ft
78   24   2       1   patchOnly  62 Female    white      other
79   27 182       0   patchOnly  53   Male    white         ft
80   31  56       1   patchOnly  44 Female    white         ft
81   56 182       0   patchOnly  64 Female    white      other
82   72   0       1   patchOnly  50 Female    white         ft
83   78  28       1   patchOnly  47   Male    white         ft
84   81 155       1   patchOnly  49 Female    white         pt
85   83   2       1   patchOnly  51   Male    white      other
86   88   0       1   patchOnly  41 Female    white      other
87   91   0       1   patchOnly  22 Female    white         pt
88   95   1       1   patchOnly  22 Female    white         pt
89   99 140       1   patchOnly  34 Female    white         ft
90  101   1       1   patchOnly  48 Female    white         ft
91  115  28       1   patchOnly  30 Female    white         ft
92  116   1       1   patchOnly  31   Male    white         ft
93  124 182       0   patchOnly  44 Female    white         ft
94  126  77       1   patchOnly  56 Female    white         ft
95  129  56       1   patchOnly  29 Female    white         ft
96    2 182       0 combination  69   Male    white      other
97    5 182       0 combination  41   Male    white         ft
98   14 182       0 combination  52 Female    white      other
99   18 182       0 combination  52 Female    white         ft
100  51 182       0 combination  53   Male    white         ft
101  57  21       1 combination  31 Female    white         ft
102  64  60       1 combination  70 Female    white      other
103  76   0       1 combination  43   Male    white      other
104  92 182       0 combination  58 Female    white         ft
105  98  65       1 combination  48 Female    white         ft
106 103 182       0 combination  72   Male    white      other
107 105 182       0 combination  61 Female    white      other
108 121 182       0 combination  53 Female    white         ft
109  96 182       0 combination  63 Female    other      other
110  46   2       1 combination  40 Female hispanic         pt
111  28  40       1   patchOnly  39 Female    black         ft
112  53 100       1   patchOnly  60 Female    black         ft
113  55   1       1   patchOnly  54 Female    black      other
114  73  45       1   patchOnly  68 Female    black         ft
115  77  14       1   patchOnly  54 Female    black         ft
116 109  30       1   patchOnly  51   Male    black      other
117  17  42       1 combination  39 Female    black         ft
118  45   2       1 combination  47 Female    black         ft
119  48 182       0 combination  33 Female    black         ft
120  50  60       1 combination  27 Female    black         ft
121  74  10       1 combination  45 Female    black         ft
122  89   0       1 combination  36 Female    black         ft
123 108 170       1 combination  39   Male    black         ft
124 118  15       1 combination  56 Female    black      other
125 128 182       0 combination  50 Female    black         pt
#Es posible que en esta última base de datos 
#sea prudente analizar por estratos y 
#no analizar todos los datos en un único modelo.

BD Hepatocelular

#|code-forflow: scroll
hepatoCellular[c(1,2,3,65,71),c(2,3,16:20,24,47)]
   Age Gender OS Death RFS Recurrence   CXCL17T CD4N     Ki67
1   57      0 83     0  13          1 113.94724    0  6.04350
2   58      1 81     0  81          0  54.07154   NA       NA
3   65      1 79     0  79          0  22.18883   NA       NA
65  38      1  5     1   5          1 106.78169    0 44.24411
71  57      1 11     1  11          1  98.49680    0 99.59232

Ejercicio 1

  1. Reescriba estos tiempos de supervivencia en términos del tiempo del paciente, y cree un conjunto de datos simple que incluya el tiempo de supervivencia y el indicador de censura para cada paciente.
# --- Parte 1: Crear el data set ---

# 1. Definir los datos para cada paciente
pacientes <- c(1, 2, 3, 4, 5)

# Tiempo de supervivencia en "tiempo del paciente" (años)
# Paciente 1: 1995 - 1990 = 5 años
# Paciente 2: 1995 - 1990 = 5 años
# Paciente 3: 1995 - 1991 = 4 años
# Paciente 4: 1994 - 1991 = 3 años
# Paciente 5: 1993 - 1992 = 1 año
survival_time <- c(5, 5, 4, 3, 1)

# Indicador de censura (1 = evento/muerte, 0 = censurado)
# Paciente 1: Censurado (0)
# Paciente 2: Censurado (0)
# Paciente 3: Muerte (1)
# Paciente 4: Muerte (1)
# Paciente 5: Muerte (1)
censoring_indicator <- c(0, 0, 1, 1, 1)

# 2. Crear el data frame
survival_data <- data.frame(
  ID_Paciente = pacientes,
  Tiempo = survival_time,
  Estado = censoring_indicator
)

# 3. Mostrar el data frame resultante
print("Data set de supervivencia:")
print(survival_data)
[1] "Data set de supervivencia:"
  ID_Paciente Tiempo Estado
1           1      5      0
2           2      5      0
3           3      4      1
4           4      3      1
5           5      1      1
  1. ¿Cuántos pacientes murieron?
  2. ¿Cuántos años-persona hay en este ensayo?
  3. ¿Cuál es la tasa de mortalidad por año-persona?
numero_de_muertes <- sum(survival_data$Estado)
numero_de_muertes
[1] 3


pers_anio<-sum(survival_data$Tiempo)
pers_anio
[1] 18


tasa_de_mortalidad <- round(c(numero_de_muertes / pers_anio),3)
cat("En promedio, ocurren", tasa_de_mortalidad, 
    "muertes por cada año que un paciente es observado en el estudio.\n")
En promedio, ocurren 0.167 muertes por cada año que un paciente es observado en el estudio.

La tasa de incidencia que calculaste (eventos / años-persona) te da una excelente fotografía promedio de la velocidad a la que ocurren los eventos en tu población. Es un único número que resume el riesgo global.

Sin embargo, el análisis de sobrevida va un paso más allá. Es necesario porque:

  1. Analiza el “Cuándo”, no solo el “Cuántos”: La pregunta principal del análisis de sobrevida no es solo cuántas personas tuvieron el evento, sino cuánto tiempo tardaron en tenerlo. El tiempo es la variable principal de interés.
  1. El riesgo no siempre es constante: La tasa de incidencia asume que el riesgo de un evento es más o menos constante durante el período de estudio. En la realidad, esto rara vez es cierto. Por ejemplo, el riesgo de muerte después de una cirugía es mucho más alto en los primeros 30 días que en los 5 años siguientes. El análisis de sobrevida te permite ver cómo cambia la probabilidad de “sobrevivir” (o de no tener el evento) a lo largo del tiempo.
  1. Permite comparar grupos de forma visual y robusta: Con el análisis de sobrevida, puedes crear curvas (como las de Kaplan-Meier) que muestran visualmente si un grupo (ej. con tratamiento A) sobrevive más tiempo que otro (ej. con tratamiento B). Además, te permite calcular si esa diferencia es estadísticamente significativa (usando pruebas como el log-rank test).
  1. Evalúa el efecto de otras variables: Mediante modelos más avanzados como la regresión de Cox, puedes investigar cómo otras variables (edad, sexo, gravedad de la enfermedad, etc.) influyen en el tiempo hasta el evento, todo mientras se maneja correctamente la censura.