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

`msm` utility functions #36

Open andkov opened 8 years ago

andkov commented 8 years ago

Development of custom function for working with objects produced by msm calls

andkov commented 8 years ago

@GracielaMuniz @annierobi

I have updated the word document on dropbox with the results from MAP. It seems that the production of tables of this form will be ubiquitous in reports. To automate this task, I have developed the function print_hazards(), which takes an model object produced by msm() call and prints the hazards the format organized in the way described in the table.

function definition

print_hazards <- function(model, dense=T){
  hz <- hazard.msm(model)
  a <- plyr::ldply(hz, .id = "predictor") 
  b <- a %>% 
    dplyr::mutate(
      transition = rep(rownames(hz[[1]]), length(hz)),
      hr    = sprintf("%0.2f", HR),
      lo    = sprintf("%0.2f", L),
      hi    = sprintf("%0.2f", U),
      dense = sprintf("%4s (%4s,%5s)", hr, lo, hi)
    )
  if(dense){
     c <- b %>% 
    dplyr::select(transition, predictor, dense)
  }
  if(!dense){
    c <- b %>% 
      dplyr::select(transition, predictor, HR, L, U)
  }
  return(c)
}

function application

> hz <- print_hazards(model, dense=T) %>% print()
          transition    predictor             dense
1  State 1 - State 2          age 1.08 (1.07, 1.09)
2  State 1 - State 4          age 1.09 (1.05, 1.12)
3  State 2 - State 1          age 0.98 (0.97, 0.99)
4  State 2 - State 3          age 1.06 (1.03, 1.08)
5  State 2 - State 4          age 1.07 (1.01, 1.13)
6  State 3 - State 4          age 1.07 (1.05, 1.10)
7  State 1 - State 2         male 1.34 (1.14, 1.56)
8  State 1 - State 4         male 1.56 (1.00, 2.41)
9  State 2 - State 1         male 1.00 (1.00, 1.00)
10 State 2 - State 3         male 0.85 (0.63, 1.16)
11 State 2 - State 4         male 1.99 (1.12, 3.55)
12 State 3 - State 4         male 1.46 (1.11, 1.91)
13 State 1 - State 2  edu_low_med 0.77 (0.48, 1.26)
14 State 1 - State 4  edu_low_med 1.37 (0.29, 6.37)
15 State 2 - State 1  edu_low_med 1.00 (1.00, 1.00)
16 State 2 - State 3  edu_low_med 0.89 (0.39, 2.00)
17 State 2 - State 4  edu_low_med 1.07 (0.11,10.74)
18 State 3 - State 4  edu_low_med 1.12 (0.50, 2.51)
19 State 1 - State 2 edu_low_high 0.59 (0.41, 0.85)
20 State 1 - State 4 edu_low_high 1.03 (0.28, 3.82)
21 State 2 - State 1 edu_low_high 1.00 (1.00, 1.00)
22 State 2 - State 3 edu_low_high 0.82 (0.46, 1.48)
23 State 2 - State 4 edu_low_high 1.41 (0.25, 7.82)
24 State 3 - State 4 edu_low_high 0.86 (0.50, 1.46)

or

> hz <- print_hazards(model, dense=F) %>% print()
          transition    predictor        HR         L          U
1  State 1 - State 2          age 1.0810352 1.0679642  1.0942662
2  State 1 - State 4          age 1.0851747 1.0541584  1.1171036
3  State 2 - State 1          age 0.9798409 0.9656587  0.9942314
4  State 2 - State 3          age 1.0562092 1.0328517  1.0800948
5  State 2 - State 4          age 1.0669022 1.0117297  1.1250835
6  State 3 - State 4          age 1.0732165 1.0487328  1.0982718
7  State 1 - State 2         male 1.3352491 1.1398553  1.5641372
8  State 1 - State 4         male 1.5558672 1.0043040  2.4103487
9  State 2 - State 1         male 1.0000000 1.0000000  1.0000000
10 State 2 - State 3         male 0.8549199 0.6289040  1.1621617
11 State 2 - State 4         male 1.9935253 1.1192838  3.5506126
12 State 3 - State 4         male 1.4556315 1.1071181  1.9138547
13 State 1 - State 2  edu_low_med 0.7746750 0.4779028  1.2557391
14 State 1 - State 4  edu_low_med 1.3660242 0.2927307  6.3745357
15 State 2 - State 1  edu_low_med 1.0000000 1.0000000  1.0000000
16 State 2 - State 3  edu_low_med 0.8851032 0.3914057  2.0015236
17 State 2 - State 4  edu_low_med 1.0667119 0.1059017 10.7446268
18 State 3 - State 4  edu_low_med 1.1239639 0.5033243  2.5099023
19 State 1 - State 2 edu_low_high 0.5892899 0.4103330  0.8462946
20 State 1 - State 4 edu_low_high 1.0275460 0.2767308  3.8154442
21 State 2 - State 1 edu_low_high 1.0000000 1.0000000  1.0000000
22 State 2 - State 3 edu_low_high 0.8221826 0.4573361  1.4780909
23 State 2 - State 4 edu_low_high 1.4076005 0.2532502  7.8236442
24 State 3 - State 4 edu_low_high 0.8566714 0.5022316  1.4612500

see details and native environment at ./sandbox/elect-utilities/print-hazards.R