B Appendix C

## [1] "2024-11-07"

library(readr)
HIV <- read_csv("HIV.csv")


HIV
## # A tibble: 824 × 10
##    gender dob      hiv_risk_factor_f Poverty_numeric hh_size ins_type_f
##    <chr>  <chr>    <chr>                       <dbl>   <dbl> <chr>     
##  1 Hombre 8/21/34  Heterosexual                  0.5       2 Medicaid  
##  2 Mujer  1/25/31  Heterosexual                  0.5       3 Medicaid  
##  3 Hombre 11/18/36 Heterosexual                  0.5       1 Medicaid  
##  4 Mujer  6/29/37  Heterosexual                  0.5       1 Medicaid  
##  5 Hombre 12/26/42 Heterosexual                  0.5       2 Medicaid  
##  6 Mujer  3/21/40  Heterosexual                  0.5       2 Medicaid  
##  7 Hombre 8/17/36  Heterosexual                  0.5       1 Medicaid  
##  8 Mujer  10/17/42 Heterosexual                  0.5       1 Medicaid  
##  9 Mujer  10/17/42 Heterosexual                  0.5       1 Medicaid  
## 10 Hombre 7/17/47  Heterosexual                  0.5       2 Medicaid  
## # ℹ 814 more rows
## # ℹ 4 more variables: vital_status <chr>, date_of_death <chr>, hiv_date <chr>,
## #   lifespan <dbl>
library(tidyverse)

https://www.stat.berkeley.edu/~s133/dates.html

HIV$dob=as.Date(format(as.Date(HIV$dob,format="%m/%d/%y"), "19%y%m%d"), "%Y%m%d")


HIV
## # A tibble: 824 × 10
##    gender dob        hiv_risk_factor_f Poverty_numeric hh_size ins_type_f
##    <chr>  <date>     <chr>                       <dbl>   <dbl> <chr>     
##  1 Hombre 1934-08-21 Heterosexual                  0.5       2 Medicaid  
##  2 Mujer  1931-01-25 Heterosexual                  0.5       3 Medicaid  
##  3 Hombre 1936-11-18 Heterosexual                  0.5       1 Medicaid  
##  4 Mujer  1937-06-29 Heterosexual                  0.5       1 Medicaid  
##  5 Hombre 1942-12-26 Heterosexual                  0.5       2 Medicaid  
##  6 Mujer  1940-03-21 Heterosexual                  0.5       2 Medicaid  
##  7 Hombre 1936-08-17 Heterosexual                  0.5       1 Medicaid  
##  8 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
##  9 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
## 10 Hombre 1947-07-17 Heterosexual                  0.5       2 Medicaid  
## # ℹ 814 more rows
## # ℹ 4 more variables: vital_status <chr>, date_of_death <chr>, hiv_date <chr>,
## #   lifespan <dbl>

Calculate the age of individuals when they were diagnosed with HIV

Calculate the time between the date of diagnosis of HIV and the date of death.

HIV= HIV |>  
  mutate(date_d=mdy(date_of_death),
         hiv_d = mdy(hiv_date))

HIV
## # A tibble: 824 × 12
##    gender dob        hiv_risk_factor_f Poverty_numeric hh_size ins_type_f
##    <chr>  <date>     <chr>                       <dbl>   <dbl> <chr>     
##  1 Hombre 1934-08-21 Heterosexual                  0.5       2 Medicaid  
##  2 Mujer  1931-01-25 Heterosexual                  0.5       3 Medicaid  
##  3 Hombre 1936-11-18 Heterosexual                  0.5       1 Medicaid  
##  4 Mujer  1937-06-29 Heterosexual                  0.5       1 Medicaid  
##  5 Hombre 1942-12-26 Heterosexual                  0.5       2 Medicaid  
##  6 Mujer  1940-03-21 Heterosexual                  0.5       2 Medicaid  
##  7 Hombre 1936-08-17 Heterosexual                  0.5       1 Medicaid  
##  8 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
##  9 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
## 10 Hombre 1947-07-17 Heterosexual                  0.5       2 Medicaid  
## # ℹ 814 more rows
## # ℹ 6 more variables: vital_status <chr>, date_of_death <chr>, hiv_date <chr>,
## #   lifespan <dbl>, date_d <date>, hiv_d <date>
HIV1 <- HIV %>% 
  mutate(hiv_date = mdy(hiv_date))

HIV1
## # A tibble: 824 × 12
##    gender dob        hiv_risk_factor_f Poverty_numeric hh_size ins_type_f
##    <chr>  <date>     <chr>                       <dbl>   <dbl> <chr>     
##  1 Hombre 1934-08-21 Heterosexual                  0.5       2 Medicaid  
##  2 Mujer  1931-01-25 Heterosexual                  0.5       3 Medicaid  
##  3 Hombre 1936-11-18 Heterosexual                  0.5       1 Medicaid  
##  4 Mujer  1937-06-29 Heterosexual                  0.5       1 Medicaid  
##  5 Hombre 1942-12-26 Heterosexual                  0.5       2 Medicaid  
##  6 Mujer  1940-03-21 Heterosexual                  0.5       2 Medicaid  
##  7 Hombre 1936-08-17 Heterosexual                  0.5       1 Medicaid  
##  8 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
##  9 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
## 10 Hombre 1947-07-17 Heterosexual                  0.5       2 Medicaid  
## # ℹ 814 more rows
## # ℹ 6 more variables: vital_status <chr>, date_of_death <chr>, hiv_date <date>,
## #   lifespan <dbl>, date_d <date>, hiv_d <date>
HIV2 <- HIV1 %>% 
  mutate(edad = dob %--% hiv_date / years())

HIV2
## # A tibble: 824 × 13
##    gender dob        hiv_risk_factor_f Poverty_numeric hh_size ins_type_f
##    <chr>  <date>     <chr>                       <dbl>   <dbl> <chr>     
##  1 Hombre 1934-08-21 Heterosexual                  0.5       2 Medicaid  
##  2 Mujer  1931-01-25 Heterosexual                  0.5       3 Medicaid  
##  3 Hombre 1936-11-18 Heterosexual                  0.5       1 Medicaid  
##  4 Mujer  1937-06-29 Heterosexual                  0.5       1 Medicaid  
##  5 Hombre 1942-12-26 Heterosexual                  0.5       2 Medicaid  
##  6 Mujer  1940-03-21 Heterosexual                  0.5       2 Medicaid  
##  7 Hombre 1936-08-17 Heterosexual                  0.5       1 Medicaid  
##  8 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
##  9 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
## 10 Hombre 1947-07-17 Heterosexual                  0.5       2 Medicaid  
## # ℹ 814 more rows
## # ℹ 7 more variables: vital_status <chr>, date_of_death <chr>, hiv_date <date>,
## #   lifespan <dbl>, date_d <date>, hiv_d <date>, edad <dbl>
HIV3 <- HIV2 %>% 
  mutate(date_of_death = mdy(date_of_death))

HIV3
## # A tibble: 824 × 13
##    gender dob        hiv_risk_factor_f Poverty_numeric hh_size ins_type_f
##    <chr>  <date>     <chr>                       <dbl>   <dbl> <chr>     
##  1 Hombre 1934-08-21 Heterosexual                  0.5       2 Medicaid  
##  2 Mujer  1931-01-25 Heterosexual                  0.5       3 Medicaid  
##  3 Hombre 1936-11-18 Heterosexual                  0.5       1 Medicaid  
##  4 Mujer  1937-06-29 Heterosexual                  0.5       1 Medicaid  
##  5 Hombre 1942-12-26 Heterosexual                  0.5       2 Medicaid  
##  6 Mujer  1940-03-21 Heterosexual                  0.5       2 Medicaid  
##  7 Hombre 1936-08-17 Heterosexual                  0.5       1 Medicaid  
##  8 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
##  9 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
## 10 Hombre 1947-07-17 Heterosexual                  0.5       2 Medicaid  
## # ℹ 814 more rows
## # ℹ 7 more variables: vital_status <chr>, date_of_death <date>,
## #   hiv_date <date>, lifespan <dbl>, date_d <date>, hiv_d <date>, edad <dbl>
HIV4 <- HIV3 %>% 
  mutate(diagnosed_death = hiv_date %--% date_of_death / years())

HIV4
## # A tibble: 824 × 14
##    gender dob        hiv_risk_factor_f Poverty_numeric hh_size ins_type_f
##    <chr>  <date>     <chr>                       <dbl>   <dbl> <chr>     
##  1 Hombre 1934-08-21 Heterosexual                  0.5       2 Medicaid  
##  2 Mujer  1931-01-25 Heterosexual                  0.5       3 Medicaid  
##  3 Hombre 1936-11-18 Heterosexual                  0.5       1 Medicaid  
##  4 Mujer  1937-06-29 Heterosexual                  0.5       1 Medicaid  
##  5 Hombre 1942-12-26 Heterosexual                  0.5       2 Medicaid  
##  6 Mujer  1940-03-21 Heterosexual                  0.5       2 Medicaid  
##  7 Hombre 1936-08-17 Heterosexual                  0.5       1 Medicaid  
##  8 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
##  9 Mujer  1942-10-17 Heterosexual                  0.5       1 Medicaid  
## 10 Hombre 1947-07-17 Heterosexual                  0.5       2 Medicaid  
## # ℹ 814 more rows
## # ℹ 8 more variables: vital_status <chr>, date_of_death <date>,
## #   hiv_date <date>, lifespan <dbl>, date_d <date>, hiv_d <date>, edad <dbl>,
## #   diagnosed_death <dbl>

Calculate the difference in life between the date of diagnosis and death

HIV$months= floor(time_length(difftime(HIV$date_d, HIV$hiv_d), "months"))
HIV$months
##   [1] 182  17   3  11 159  46 143 190 188 265 196 272  97 332 307 282 179 368
##  [19] 363 363 228 192 195  80  55 299 299  17   1 273 216  86 201 201 154 110
##  [37]  55  32 182 123  74 324 223 213 127  72 264 116 100  95  57   4 220 204
##  [55] 102  10   4 339 306 294 208 205 167 102  86   1 320 320 240 181 176 115
##  [73]  33   2 314 197 203 181 183 137  47  47  46  27 373 239 239 239 231 223
##  [91] 201 195 200 174 160 158  81  27  17 326 332 312 312 280 226 226 211 171
## [109] 163 129 105  80  76  58  11  10 309 275 243 234 117  56 329 302 271 266
## [127] 196 161 124 118  72  71 313 209 198 157 158 155  44 267 273 254 188 134
## [145] 110  77  48   6 261 218 110  90  25 252 154  65  18  14 276 227 157 161
## [163]  29 273 243 118  79  51  42   1 262 229  87 246 158   3 256 144 143  61
## [181]  31  19   2 141 173  23  15   5 102 102 179  67  36 177 132   0   5 197
## [199] 121   2 225 225 164 166   2 132 132 106  66  87  67 295 236 346 211 204
## [217] 286 334 150 166  90 215  24  69   4 132   2 230 222 169 112  27 233  65
## [235]   2 279 271 238 232  21 228 223 191 207 164 151 162  65  65  67  17 286
## [253]  97 212  19 324  86 110  68 208 203 183 284  62  52  30 137  10 193  22
## [271] 101 101  97  96  90   0   0 266 298 227  97 128  10 312 323   4   0   4
## [289] 274 241 209 168 168 316 207  84 287 100 461 237 237 235 139 214  98 355
## [307] 323 280 283 315 298 298 231 180 212 224 221 221 221 337 174 142 423 371
## [325] 166 282 265 165 164 391 394 348 352 352 338 242 196 162 128 291 263 235
## [343] 235 269 204 214 201 175 160 146 154  81 339 291 277 259 219 213 207 213
## [361] 180 132 140 119 111 102 392 283 281 229 189  38 274 168 168 170 164 157
## [379] 150 150 135  87  79 322 322 322 274 259 216 167 138 317 289 275 237 186
## [397] 150 144 125  80  72  49  56 315 292 233 221 219 195 195 198 177 152 144
## [415] 103  67 384 381 379 345 332 210 170 159 143 142 132  99  90  95  95  87
## [433]  33  33  33  26 377 340 307 301 225 179 166 115  97  93  54 416 335 249
## [451] 222 213 201 189 141  52  42 318 300 300 300 244 238 218 186 176 136 142
## [469] 131 131 127 124 109  76  54 264 214 185 181 151 154 154  73  70 403 336
## [487] 281 228 216 187 190 179 144 131 100  47  43 347 289 277 259 259 259 218
## [505] 218 211 157 141  53   4 281 257 229 224 217 218 134 134  90  95  56  34
## [523]  35  21  22 386 284 248 235 235 207 199 146 144 122 110  66 273 197 153
## [541] 107 100  67 254 209 173 122 122 114  56   2 247 127 106  85  76  67   3
## [559] 193 198 199 164 108  65 127 112 103 179  34  14 212 191 150 144  80 171
## [577] 133  66 121  95 182 166  21   6   1  31  23   6 388 388  62  20  26 158
## [595] 332 370 274 242 228  98  15 277 222 306  52 187 186 269 295 195 172 214
## [613] 184 123   6  55   3 142 148  93 139 316 270 271 262 179 182 240 189  73
## [631]  32 106 134  83 281 236 137 137 144 249 304 192  34 259 197   3 155 223
## [649] 165 152 184 329 329 253 107 135  50 134 141 227 142 124  57 223 175 171
## [667] 241 215  71   1 269 230 292 252  25 248  63 248 261 188  91 190 175  69
## [685]  25 192 162 211 248 261 278 199 233 456 455 229 156 111 124  22 233 323
## [703] 344 340 114 193 118 246 267 254 195 168 229 236 219 280 332 313 244 244
## [721] 295 187 137 138 182  86 125  17  57 361 384 128 358 212 374 310 157  99
## [739]  99  84 126 214 125 321 129 111  58  69 190 190  52  24 226 226 321 296
## [757] 211 118 101 276 258 222 115 182 137 251 208  32   8   2   0 206 210 186
## [775] 112 475  57 316 231 351 143 252 159 327 336  65 143 254 254   3  15  90
## [793] 209 183 183 271  81 235 163 283 248 190  59 119 230 288 410 216  24 120
## [811]  54   1 290 279 268 293 260  81 269 272 309 361 336 315
HIV %>%
  mutate(Period_HIV_death = as.duration(hiv_d %--% date_d)) |> 
  summarise(mean_time=mean(Period_HIV_death)) 
## # A tibble: 1 × 1
##    mean_time
##        <dbl>
## 1 462043713.