IALSA / ialsa-2016-amsterdam

Multi-study and multivariate evaluation of healthy life expectancy (HLE): An IALSA workshop on multistate modeling using R
GNU General Public License v2.0
0 stars 0 forks source link

Study: Moving age of death into stacked variable #8

Closed andkov closed 8 years ago

andkov commented 8 years ago

Exposition

For the data of the shape

> ds
    X id fu_year   age_bl age_death died age_at_visit dementia
1   1  3       0 78.53799  85.26215    1     78.53799        0
2   2  3       1 78.53799  85.26215    1     79.53730        0
3   3  3       2 78.53799  85.26215    1     80.54757        0
4   4  3       3 78.53799  85.26215    1     81.54415        0
5   5  3       4 78.53799  85.26215    1     82.54346        0
6   6  3       5 78.53799  85.26215    1     83.57837        0
7   7  3       6 78.53799  85.26215    1     84.57221        0
8   8  4       0 83.62765        NA    0     83.62765        0
9   9  4       1 83.62765        NA    0     85.36893        0
10 10  5       0 78.65572  85.43190    1     78.65572        0
11 11  5       1 78.65572  85.43190    1     79.68789        0
12 12  5       2 78.65572  85.43190    1     80.64339        1
13 13  5       3 78.65572  85.43190    1     81.67830        0
14 14  5       4 78.65572  85.43190    1     82.68309        0
15 15  5       5 78.65572  85.43190    1     83.64134        0
16 16  5       6 78.65572  85.43190    1     84.67077        0

Write a function to create a variable age which would combine age_at_visit and age_death. This would add an additional row for each unique id. The ultimate purpose is to bring the data to the shape

id state age ybirth
3   1   -0.50   19
3   1   1.50    19
3   1   3.50    19
3   1   5.50    19
3   2   7.50    19
3   3   8.64    19

where state==1 indicates that the person alive and healthy(dementia==0), state==2 indicates that the person is alive but has a condition (dementia==1), and state==3 is a dead state. (see details in ELECT vignette, p.3)

Starter code

ds <- structure(list(X = 1:16, id = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), fu_year = c(0L, 1L, 2L, 3L, 
4L, 5L, 6L, 0L, 1L, 0L, 1L, 2L, 3L, 4L, 5L, 6L), age_bl = c(78.53798768, 
78.53798768, 78.53798768, 78.53798768, 78.53798768, 78.53798768, 
78.53798768, 83.62765229, 83.62765229, 78.65571526, 78.65571526, 
78.65571526, 78.65571526, 78.65571526, 78.65571526, 78.65571526
), age_death = c(85.26214921, 85.26214921, 85.26214921, 85.26214921, 
85.26214921, 85.26214921, 85.26214921, NA, NA, 85.43189596, 85.43189596, 
85.43189596, 85.43189596, 85.43189596, 85.43189596, 85.43189596
), died = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), age_at_visit = c(78.53798768, 79.53730322, 80.54757016, 
81.54414784, 82.54346338, 83.57837098, 84.57221081, 83.62765229, 
85.36892539, 78.65571526, 79.68788501, 80.64339493, 81.67830253, 
82.68309377, 83.64134155, 84.67077344), dementia = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L)), .Names = c("X", 
"id", "fu_year", "age_bl", "age_death", "died", "age_at_visit", 
"dementia"), class = "data.frame", row.names = c(NA, -16L))
wibeasley commented 8 years ago

For the sake of demonstration, I changed the last dementia point for subject 5.

library(magrittr)

ds_alive <- structure(list(X = 1:16, id = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
  4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), fu_year = c(0L, 1L, 2L, 3L, 
  4L, 5L, 6L, 0L, 1L, 0L, 1L, 2L, 3L, 4L, 5L, 6L), age_bl = c(78.53798768, 
  78.53798768, 78.53798768, 78.53798768, 78.53798768, 78.53798768, 
  78.53798768, 83.62765229, 83.62765229, 78.65571526, 78.65571526, 
  78.65571526, 78.65571526, 78.65571526, 78.65571526, 78.65571526
  ), age_death = c(85.26214921, 85.26214921, 85.26214921, 85.26214921, 
  85.26214921, 85.26214921, 85.26214921, NA, NA, 85.43189596, 85.43189596, 
  85.43189596, 85.43189596, 85.43189596, 85.43189596, 85.43189596
  ), died = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L), age_at_visit = c(78.53798768, 79.53730322, 80.54757016, 
  81.54414784, 82.54346338, 83.57837098, 84.57221081, 83.62765229, 
  85.36892539, 78.65571526, 79.68788501, 80.64339493, 81.67830253, 
  82.68309377, 83.64134155, 84.67077344), dementia = c(0L, 0L, 
  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L)), .Names = c("X", 
  "id", "fu_point", "age_bl", "age_death", "dead_currently", "age_at_visit", 
  "dementia"), class = "data.frame", row.names = c(NA, -16L)
)

ds_alive <- ds_alive %>% 
  dplyr::select(-X) %>% 
  dplyr::rename(dementia_now=dementia) %>% 
  dplyr::group_by(id) %>% 
  dplyr::mutate(
    dead_currently = as.logical(dead_currently),
    dementia_now   = as.logical(dementia_now),
    dementia_ever  = any(dementia_now)
  ) %>% 
  dplyr::ungroup()
ds_alive

ds_dead <- ds_alive %>% 
  dplyr::filter(!is.na(age_death)) %>% 
  dplyr::group_by(id) %>% 
  dplyr::arrange(fu_point) %>% 
  dplyr::summarize(
    fu_point       = max(fu_point) + 1L,
    age_bl         = max(age_bl), #Assumes same for all points within a person
    age_death      = max(age_death),
    dead_currently = TRUE,
    age_at_visit   = NA_real_,
    dementia_last  = dplyr::last(dementia_now),
    dementia_now   = ifelse(dementia_last, TRUE, NA),
    dementia_ever  = any(dementia_now)
  ) %>% 
  dplyr::ungroup() %>% 
  dplyr::select(-dementia_last)
ds_dead

ds <- ds_alive %>%
  dplyr::union(ds_dead) %>%
  dplyr::arrange(id, fu_point)
ds
> ds
Source: local data frame [18 x 8]

      id fu_point   age_bl age_death dead_currently age_at_visit dementia_now dementia_ever
   (int)    (int)    (dbl)     (dbl)          (lgl)        (dbl)        (lgl)         (lgl)
1      3        0 78.53799  85.26215           TRUE     78.53799        FALSE         FALSE
2      3        1 78.53799  85.26215           TRUE     79.53730        FALSE         FALSE
3      3        2 78.53799  85.26215           TRUE     80.54757        FALSE         FALSE
4      3        3 78.53799  85.26215           TRUE     81.54415        FALSE         FALSE
5      3        4 78.53799  85.26215           TRUE     82.54346        FALSE         FALSE
6      3        5 78.53799  85.26215           TRUE     83.57837        FALSE         FALSE
7      3        6 78.53799  85.26215           TRUE     84.57221        FALSE         FALSE
8      3        7 78.53799  85.26215           TRUE           NA           NA            NA
9      4        0 83.62765        NA          FALSE     83.62765        FALSE         FALSE
10     4        1 83.62765        NA          FALSE     85.36893        FALSE         FALSE
11     5        0 78.65572  85.43190           TRUE     78.65572        FALSE          TRUE
12     5        1 78.65572  85.43190           TRUE     79.68789        FALSE          TRUE
13     5        2 78.65572  85.43190           TRUE     80.64339         TRUE          TRUE
14     5        3 78.65572  85.43190           TRUE     81.67830        FALSE          TRUE
15     5        4 78.65572  85.43190           TRUE     82.68309        FALSE          TRUE
16     5        5 78.65572  85.43190           TRUE     83.64134        FALSE          TRUE
17     5        6 78.65572  85.43190           TRUE     84.67077         TRUE          TRUE
18     5        7 78.65572  85.43190           TRUE           NA         TRUE          TRUE