hesim-dev / hesim

Health economic simulation modeling and decision analysis
https://hesim-dev.github.io/hesim/
63 stars 17 forks source link

Easy way to to plot subgroups individually? #109

Open rafanmir opened 1 year ago

rafanmir commented 1 year ago

When we have subgroups in our models, is there an easy way to plot the subgroups as individual plots, instead of panels in the same plot? I tried to use dplyr's filter to a cea object and I get the error message: no applicable method for 'filter' applied to an object of class "cea". I know I can filter the dataset before using the cea function and plot the individual groups but it'd be easier if we could do this on a cea object. Thanks

dincerti commented 11 months ago

You can access any of the outputs like a normal list. For instance, to subset the NMB to the second subgroup

n_samples <- 30
sim <- data.table(
  sample = rep(seq(n_samples), 4),
  c = c(
    rlnorm(n_samples, 5, .1), 
    rlnorm(n_samples, 5, .1), 
    rlnorm(n_samples, 11, .1), 
    rlnorm(n_samples, 11, .1)
  ),
  e = c(
    rnorm(n_samples, 8, .2), 
    rnorm(n_samples, 8.5, .1), 
    rnorm(n_samples, 11, .6), 
    rnorm(n_samples, 11.5, .6)
  ),
  strategy_id = rep(1:2, each = n_samples * 2),
  grp_id = rep(rep(1:2, each = n_samples), 2)
)

# Cost-effectiveness analysis
cea_out <- cea(
  sim, 
  k = seq(0, 200000, 500), 
  sample = "sample", 
  strategy = "strategy_id", grp = "grp_id", 
  e = "e", 
  c = "c"
)

cea_out$evpi[grp_id == 2]
     grp_id      k best       enmbci       enmbpi          evpi
  1:      2      0    1    -152.6449    -152.6449  2.842171e-14
  2:      2    500    1    4085.0383    4085.0383  0.000000e+00
  3:      2   1000    1    8322.7216    8322.7216 -1.818989e-12
  4:      2   1500    1   12560.4048   12560.4048  0.000000e+00
  5:      2   2000    1   16798.0880   16798.0880  0.000000e+00
 ---                                                           
397:      2 198000    2 2191845.5819 2191845.5819 -4.656613e-10
398:      2 198500    2 2197534.7203 2197534.7203  0.000000e+00
399:      2 199000    2 2203223.8588 2203223.8588  4.656613e-10
400:      2 199500    2 2208912.9972 2208912.9972 -4.656613e-10
401:      2 200000    2 2214602.1356 2214602.1356  4.656613e-10

The output is a data.table. If you want to use dplyr, then convert the data.table to a tibble. You can then use your preferred package to make a plot.