# Extending tidyndr

While the {tidyndr} package focuses on generating line-list of common PEPFAR indicators and providing summaries for these, a few indicators are derived from mathematical calculation of existing indicators. These indicators include:

1. TX_NET_NEW

2. Continuity of Treatment (Retention) Proxy

3. Viral Load Testing Coverage

4. Viral Load Suppression Coverage

5. Duration of ARV Dispensed

    library(tidyndr)
    library(dplyr)

### TX_NET_NEW

This indicator measures the increase (or decrease) in the number of clients on ART over a period of time. It is calculated by subtracting the number of active clients in a previous period from the number of active clients from the current period.

\(tx\_net\_new\ =\ tx\_curr\ current\ period - tx\_curr\ previous\ period\)

To create this in R, we will:

1. Import the line-list of the previous NDR data and the current NDR data.

2. Generate the line-list of the `tx_curr` for each of the imported data.

3. Summarize our line-list at the desired level (“ip”, “state” or “facility”) using the `summarise_ndr()`.

4. Subtract the value of the previous tx_curr from the current `tx_curr`.

    ## import both previous and current NDR line-lists
    file_path <- "https://raw.githubusercontent.com/stephenbalogun/example_files/main/ndr_example.csv"

    prev_data <- read_ndr(file_path,
                         time_stamp = "2020-12-31")
    current_data <- read_ndr(file_path,
                         time_stamp = "2020-02-28")

    ## generate the tx_curr for each of the imported line-lists
    tx_curr_prev <- tx_curr(prev_data)

    tx_curr_now <- tx_curr(current_data)

    ## summarise the treatment currents at state level
    tx_currs <- summarise_ndr(tx_curr_prev,
                  tx_curr_now,
                  level = "state",
                  names = c("tx_curr_prev", "tx_curr_now"))

    ## create a new column that calculates the tx_net_new
    tx_net_new <- tx_currs %>%
      mutate(tx_net_new = tx_curr_now - tx_curr_prev)

    print(tx_net_new)
    #> # A tibble: 4 x 5
    #>   ip      state   tx_curr_prev tx_curr_now tx_net_new
    #>   <chr>   <chr>          <int>       <int>      <int>
    #> 1 IP_name State 1         5645        5645          0
    #> 2 IP_name State 2         7929        7929          0
    #> 3 IP_name State 3        13446       13446          0
    #> 4 Total   -              27020       27020          0

### Continuity of Treatment (Retention) Proxy

Previously called “Retention Proxy”. It measures how many of the clients who started the period of interest remained on treatment at the end of that period. It is generally calculated using two different formulas:

\(Continuity\ of\ Treatment\ Proxy\ = \frac{tx\_curr\ at\ end\ of\ reporting\ period}{tx\_curr\ at\ beginning\ of\ period\ +\ tx\_new\ during\ period}\)

or using the standard PEPFAR formula -

\(Continuity\ of\ Treatment\ Proxy\ = 1\ +\ \frac{(tx\_net\_new\ *\ 4)\ -\ (tx\_new\ *\ 4)}{[tx\_curr\ -\ tx\_net\_new\ +\ (tx\_new\ *\ 4)]}\)

To calculate the Continuity of Treatment Proxy using the second approach, we will:

- continue with the summary table calculated under `tx_net_new` above.

- generate and summarise the new clients within the reporting period.

- join the first two tables together using another {dplyr} function `left_join()`.

- create a new column that calculates the continuity of treatment proxy using the formula above.

    ## calculate the tx_new between the two periods
    tx_new <- tx_new(current_data, from = "2021-01-01", to = "2021-02-28") %>%
      summarise_ndr(level = "state", names = "tx_new")

    ## add the tx_new summary to the initial table and calculate the tx_net_new
    tx_net_new %>%
      left_join(tx_new, by = c("ip", "state")) %>%
      mutate(cot_proxy = 1 + (
      ((tx_net_new * 4) - (tx_new) * 4 ) /
        (tx_curr_now - tx_net_new + (tx_new * 4)))
      )
    #> # A tibble: 4 x 7
    #>   ip      state   tx_curr_prev tx_curr_now tx_net_new tx_new cot_proxy
    #>   <chr>   <chr>          <int>       <int>      <int>  <int>     <dbl>
    #> 1 IP_name State 1         5645        5645          0    272     0.838
    #> 2 IP_name State 2         7929        7929          0    300     0.869
    #> 3 IP_name State 3        13446       13446          0    984     0.774
    #> 4 Total   -              27020       27020          0   1556     0.813

### Viral Load Testing Coverage

The standard viral load testing coverage is calculated by dividing the number of clients with a viral load test result in the period of interest by the number of active clients within the same program 6 months prior.

\(1.\ Viral\ Load\ Testing\ Coverage = \frac{tx\_pvls\_den}{tx\_curr\ 6\ months\ prior}\ *\ 100\)

This means that you will be needing two different datasets (i.e. the current dataset and the dataset of 6 months ago). In the absence of these two datasets, viral load testing coverage can be estimated by calculating the number of clients with a viral load result and dividing by the total number of clients estimated to be eligible for viral load during the period under review.

\(2.\ Viral\ Load\ Testing\ Coverage = \frac{tx\_pvls\_den}{tx\_vl\_eligible}\ *\ 100\)

Below is an example using the second approach:

    vl_eligible <- tx_vl_eligible(current_data, ref = "2021-06-30")

    vl_result <- tx_pvls_den(current_data, ref = "2021-06-30")

    nrow(vl_result) / nrow(vl_eligible) * 100
    #> [1] 41.79948

### Viral Load Suppression Coverage

This is calculated using:

\(Viral\ Load\ Suppression\ Coverage = \frac{tx\_pvls\_num\ (no.\ of\ clients\ who\ are\ virally\ suppressed)}{tx\_pvls\_den\ (no.\ of\ clients\ with\ viral\ load\ results)}\)

    vl_result <- tx_pvls_den(current_data, ref = "2021-06-30")

    vl_suppressed <- tx_pvls_num(current_data, ref = "2021-06-30")

    nrow(vl_suppressed) / nrow(vl_result) * 100
    #> [1] 38.63636

### Duration of ARV Dispensed

The months of ARV dispensed is sometimes disaggregated into “<3 months”, “3 - 5 months”, “6+ months”. To calculate these we execute a similar line of code as below:

    less_than_three <- tx_mmd(ndr_example, months = c(0, 1, 2))

    three_to_five <- tx_mmd(ndr_example, months = c(3, 4, 5))

    six_plus <- tx_mmd(ndr_example, months = c(6:12))

    summarise_ndr(less_than_three,
                  three_to_five,
                  six_plus,
                  level = "state",
                  names = c(
                    "< 3months",
                    "3-5months",
                    "6+ months"
                  ))
    #> # A tibble: 5 x 5
    #>   ip        state   `< 3months` `3-5months` `6+ months`
    #>   <chr>     <chr>         <int>       <int>       <int>
    #> 1 NGOHealth Okun              5         196         440
    #> 2 NGOHealth Abaji            43         986        2167
    #> 3 NGOHealth Arewa            54        1316        2718
    #> 4 NGOHealth Ayetoro          32         821        1868
    #> # ... with 1 more row