Date: 2025-06-16

# Heterogeneity & Demographic Analysis

#### 2025-06-16

## Introduction

Heterogeneity analysis is a way to explore how the results of a model can vary depending on the characteristics of individuals in a population, and demographic analysis estimates the average values of a model over an entire population.

In practice these two analyses naturally complement each other: heterogeneity analysis runs the model on multiple sets of parameters (reflecting different characteristics found in the target population), and demographic analysis combines the results.

For this example we will use the result from the assessment of a new total hip replacement previously described in `vignette("d-non-homogeneous", "heemod")`.

## Population characteristics

The characteristics of the population are input from a table, with one column per parameter and one row per individual. Those may be for example the characteristics of the indiviuals included in the original trial data.

For this example we will use the characteristics of 100 individuals, with varying sex and age, specified in the data frame `tab_indiv`:

    tab_indiv

    ## # A tibble: 100 × 2
    ##      age   sex
    ##    <dbl> <int>
    ##  1    50     0
    ##  2    77     0
    ##  3    66     1
    ##  4    63     1
    ##  5    47     1
    ##  6    50     0
    ##  7    73     0
    ##  8    46     1
    ##  9    59     1
    ## 10    64     0
    ## # ℹ 90 more rows

    library(ggplot2)
    ggplot(tab_indiv, aes(x = age)) +
      geom_histogram(binwidth = 2)

## Running the analysis

`res_mod`, the result we obtained from `run_model()` in the Time-varying Markov models vignette, can be passed to `update()` to update the model with the new data and perform the heterogeneity analysis.

    res_h <- update(res_mod, newdata = tab_indiv)

    ## No weights specified in update, using equal weights.

    ## Updating strategy 'standard'...

    ## Updating strategy 'np1'...

## Interpreting results

The `summary()` method reports summary statistics for cost, effect and ICER, as well as the result from the combined model.

    summary(res_h)

    ## An analysis re-run on 100 parameter sets.
    ##
    ## * Unweighted analysis.
    ##
    ## * Values distribution:
    ##
    ##                                  Min.      1st Qu.      Median        Mean
    ## standard - Cost          500.08967163  592.3687128 621.5892423 665.8928387
    ## standard - Effect         10.06345874   23.3226486  26.3577780  25.3341131
    ## standard - Cost Diff.               -            -           -           -
    ## standard - Effect Diff.             -            -           -           -
    ## standard - Icer                     -            -           -           -
    ## np1 - Cost               607.16692250  632.1186231 640.0588497 652.9544113
    ## np1 - Effect              10.13073146   23.4706053  26.6329927  25.5680133
    ## np1 - Cost Diff.        -159.96283707  -99.5031416  18.4696074 -12.9384274
    ## np1 - Effect Diff.         0.05767389    0.1756522   0.2101911   0.2339002
    ## np1 - Icer              -351.98058303 -304.0330575  83.6463073  69.4877163
    ##                             3rd Qu.         Max.
    ## standard - Cost         786.6690449  871.1621236
    ## standard - Effect        29.0596426   30.8545173
    ## standard - Cost Diff.             -            -
    ## standard - Effect Diff.           -            -
    ## standard - Icer                   -            -
    ## np1 - Cost              687.1659033  711.1992865
    ## np1 - Effect             29.2683350   31.0829546
    ## np1 - Cost Diff.         39.7499103  107.0772509
    ## np1 - Effect Diff.        0.3272774    0.4544649
    ## np1 - Icer              226.2989208 1856.5985016
    ##
    ## * Combined result:
    ##
    ## 2 strategies run for 60 cycles.
    ##
    ## Initial state counts:
    ##
    ## PrimaryTHR = 1000L
    ## SuccessP = 0L
    ## RevisionTHR = 0L
    ## SuccessR = 0L
    ## Death = 0L
    ##
    ## Counting method: 'beginning'.
    ##
    ## Values:
    ##
    ##           utility     cost
    ## standard 25334.11 665892.8
    ## np1      25568.01 652954.4
    ##
    ## Efficiency frontier:
    ##
    ## np1
    ##
    ## Differences:
    ##
    ##     Cost Diff. Effect Diff.      ICER     Ref.
    ## np1  -12.93843    0.2339002 -55.31602 standard

The variation of cost or effect can then be plotted.

    plot(res_h, result = "effect", binwidth = 5)

    plot(res_h, result = "cost", binwidth = 50)

    plot(res_h, result = "icer", type = "difference",
         binwidth = 500)

    plot(res_h, result = "effect", type = "difference",
         binwidth = .1)

    plot(res_h, result = "cost", type = "difference",
         binwidth = 30)

The results from the combined model can be plotted similarly to the results from `run_model()`.

    plot(res_h, type = "counts")

## Weighted results

Weights can be used in the analysis by including an optional column `.weights` in the new data to specify the respective weights of each strata in the target population.

    tab_indiv_w

    ## # A tibble: 100 × 3
    ##      age   sex .weights
    ##    <dbl> <int>    <dbl>
    ##  1    65     1   0.300
    ##  2    48     1   0.599
    ##  3    63     1   0.0521
    ##  4    61     0   0.529
    ##  5    50     1   0.418
    ##  6    41     1   0.0801
    ##  7    46     1   0.693
    ##  8    77     1   0.888
    ##  9    73     0   0.278
    ## 10    71     1   0.756
    ## # ℹ 90 more rows

    res_w <- update(res_mod, newdata = tab_indiv_w)

    ## Updating strategy 'standard'...

    ## Updating strategy 'np1'...

    res_w

    ## An analysis re-run on 100 parameter sets.
    ##
    ## * Weights distribution:
    ##
    ##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
    ## 0.006434 0.257895 0.503276 0.493604 0.757841 0.998732
    ##
    ## Total weight: 49.36041
    ##
    ## * Values distribution:
    ##
    ##                                  Min.      1st Qu.      Median        Mean
    ## standard - Cost          438.70535048  592.3687128 626.3537753 683.1157611
    ## standard - Effect          5.05860925   21.9825691  25.9857701  24.7769772
    ## standard - Cost Diff.               -            -           -           -
    ## standard - Effect Diff.             -            -           -           -
    ## standard - Icer                     -            -           -           -
    ## np1 - Cost               590.76054210  632.1186231 641.3547975 657.9069949
    ## np1 - Effect               5.07524179   22.2578591  26.1614223  25.0252254
    ## np1 - Cost Diff.        -165.40882382  -99.5031416  15.0010223 -25.2087662
    ## np1 - Effect Diff.         0.01159912    0.1756522   0.2086924   0.2482483
    ## np1 - Icer              -354.56585682 -304.0330575  65.6679900 248.0752439
    ##                             3rd Qu.         Max.
    ## standard - Cost         786.6690449 8.787814e+02
    ## standard - Effect        29.0596426 3.129948e+01
    ## standard - Cost Diff.             -            -
    ## standard - Effect Diff.           -            -
    ## standard - Icer                   -            -
    ## np1 - Cost              687.1659033 7.133726e+02
    ## np1 - Effect             29.2683350 3.153286e+01
    ## np1 - Cost Diff.         39.7499103 1.520552e+02
    ## np1 - Effect Diff.        0.3272774 4.665109e-01
    ## np1 - Icer              226.2989208 1.310920e+04
    ##
    ## * Combined result:
    ##
    ## 2 strategies run for 60 cycles.
    ##
    ## Initial state counts:
    ##
    ## PrimaryTHR = 1000L
    ## SuccessP = 0L
    ## RevisionTHR = 0L
    ## SuccessR = 0L
    ## Death = 0L
    ##
    ## Counting method: 'beginning'.
    ##
    ## Values:
    ##
    ##           utility     cost
    ## standard 24776.98 683115.8
    ## np1      25025.23 657907.0
    ##
    ## Efficiency frontier:
    ##
    ## np1
    ##
    ## Differences:
    ##
    ##     Cost Diff. Effect Diff.      ICER     Ref.
    ## np1  -25.20877    0.2482483 -101.5466 standard

## Parallel computing

Updating can be significantly sped up by using parallel computing. This can be done in the following way:

- Define a cluster with the `use_cluster()` functions (i.e. `use_cluster(4)` to use 4 cores).
- Run the analysis as usual.
- To stop using parallel computing use the `close_cluster()` function.

Results may vary depending on the machine, but we found speed gains to be quite limited beyond 4 cores.