#Paquetes
library(readxl)
library(dplyr)
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(survival)
para leer base de datos
tabla_vida <- read_excel("TablasVida.xlsx")
la base de datos
View(tabla_vida)
tabla_vida$Nacimiento <- as.Date(tabla_vida$Nacimiento)
tabla_vida$Muerte <- as.Date(tabla_vida$Muerte)
tabla_vida$edad_muerte <- ifelse(
is.na(tabla_vida$Muerte),
as.numeric(difftime(Sys.Date(), tabla_vida$Nacimiento, units = "days")),
as.numeric(difftime(tabla_vida$Muerte, tabla_vida$Nacimiento, units = "days"))
)
supervivencia_general <- Surv(
time = tabla_vida$edad_muerte / 365,
event = !is.na(tabla_vida$Muerte)
)
km_general <- survfit(supervivencia_general ~ 1)
plot(km_general,
main = "Curva de supervivencia general",
xlab = "Edad (años)",
ylab = "Probabilidad de supervivencia",
col = "orange",
lwd = 2)
##Curva de supervivencia en hombres
tabla_vida_h <- tabla_vida %>% filter(Sexo == "M")
supervivencia_h <- Surv(
time = tabla_vida_h$edad_muerte / 365,
event = !is.na(tabla_vida_h$Muerte)
)
km_h <- survfit(supervivencia_h ~ 1)
plot(km_h,
main = "Curva de supervivencia hombres",
xlab = "Edad (años)",
ylab = "Probabilidad de supervivencia",
col = "purple",
lwd = 2)
##Curva de supervivencia en mujeres
tabla_vida_m <- tabla_vida %>% filter(Sexo == "F")
supervivencia_m <- Surv(
time = tabla_vida_m$edad_muerte / 365,
event = !is.na(tabla_vida_m$Muerte)
)
km_m <- survfit(supervivencia_m ~ 1)
plot(km_m,
main = "Curva de supervivencia mujeres",
xlab = "Edad (años)",
ylab = "Probabilidad de supervivencia",
col = "magenta",
lwd = 2)