datos <- datos %>% 
  mutate(
    fecha_nac = make_date(Nac_año, Nac_mes, Nac_dia),
    fecha_mue = make_date(Mue_año, Mue_mes, Mue_dia),
    edad = as.numeric((fecha_mue - fecha_nac) / 365.25)
  )

summary(datos$edad)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##    0.00   59.02   71.91   67.49   82.05   97.71       3
tabla_edades <- datos %>%
mutate(edad_red = floor(edad)) %>%
group_by(edad_red) %>%
summarise(muertes = n()) %>%
arrange(edad_red)

tabla_edades
## # A tibble: 51 × 2
##    edad_red muertes
##       <dbl>   <int>
##  1        0       2
##  2        2       1
##  3       20       2
##  4       21       1
##  5       27       1
##  6       28       1
##  7       34       1
##  8       35       2
##  9       41       1
## 10       45       1
## # ℹ 41 more rows
l0 <- 1000

tabla_vida <- tabla_edades %>%
mutate(
lx = l0 - cumsum(lag(muertes, default = 0)),
dx = muertes,
qx = dx / lx,
Lx = lx - dx / 2
) %>%
mutate(
Tx = rev(cumsum(rev(Lx))),
ex = Tx / lx
)

tabla_vida
## # A tibble: 51 × 8
##    edad_red muertes    lx    dx      qx    Lx     Tx    ex
##       <dbl>   <int> <dbl> <int>   <dbl> <dbl>  <dbl> <dbl>
##  1        0       2  1000     2 0.002    999  48656.  48.7
##  2        2       1   998     1 0.00100  998. 47658.  47.8
##  3       20       2   997     2 0.00201  996  46660   46.8
##  4       21       1   995     1 0.00101  994. 45664   45.9
##  5       27       1   994     1 0.00101  994. 44670.  44.9
##  6       28       1   993     1 0.00101  992. 43676   44.0
##  7       34       1   992     1 0.00101  992. 42684.  43.0
##  8       35       2   991     2 0.00202  990  41692   42.1
##  9       41       1   989     1 0.00101  988. 40702   41.2
## 10       45       1   988     1 0.00101  988. 39714.  40.2
## # ℹ 41 more rows
ggplot(tabla_vida, aes(x = edad_red, y = lx)) +
  geom_line(size = 1.3, color = "blue") +
  labs(title = "Curva de Supervivencia (lx)",
       x = "Edad (años)",
       y = "Sobrevivientes") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).

ggplot(datos, aes(x = edad)) +
geom_histogram(binwidth = 5, color = "black", fill = "skyblue") +
labs(title = "Distribución de edades al morir",
x = "Edad (años)",
y = "Frecuencia") +
theme_minimal()
## Warning: Removed 3 rows containing non-finite outside the scale range
## (`stat_bin()`).