Skip to contents

This function takes medication data in long format (multiple rows per patient ID), calculates MME using the local calculation method (calculate_mme_local()), and adds prescription-level values as new columns. It also returns two additional data frames with patient-level MME summaries (one with buprenorphine included and one without).

Usage

calculate_mme_df(
  data,
  id_col = "patient_id",
  medication_col = "medication_name",
  dose_col = "dose",
  doses_per_day_col = "doses_per_24_hours",
  days_col = "days_of_medication",
  therapy_days_col = "therapy_days",
  observation_days_col = "observation_window_days",
  therapy_days_without_col = NULL,
  observation_days_without_col = NULL
)

Arguments

data

A data.frame or tibble in long format with one row per medication per patient or participant (id_col)

id_col

Name of the column containing patient identifier; default is "patient_id"

medication_col

Name of the column containing medication names; default is "medication_name"

dose_col

Name of the column containing dose values; default is "dose"

doses_per_day_col

Name of the column containing doses per 24 hours; "doses_per_24_hours"

days_col

Name of the column containing days of medication; default is "days_of_medication"

therapy_days_col

Name of the column containing therapy days with buprenorphine (up to one unique value per patient); default is "therapy_days"

observation_days_col

Name of the column containing observation window days with buprenorphine (up to one unique value per patient); default is "observation_window_days"

therapy_days_without_col

Name of the column containing therapy days without buprenorphine (up to one unique value per patient). If NULL (default), uses the value from therapy_days_col.

observation_days_without_col

Name of the column containing observation window days without buprenorphine (up to one unique value per patient). If NULL (default), uses the value from observation_days_col.

Value

A list containing three elements:

  • medications: The original data.frame with added prescription-level MME columns

  • patient_summary_with_buprenorphine: Patient-level MME summary including buprenorphine

  • patient_summary_without_buprenorphine: Patient-level MME summary excluding buprenorphine

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
# Calculate MME using long-format data
# Subset of opioid_trial data used for speedier example
mme <- calculate_mme_df(
  data = opioid_trial |> dplyr::filter(patient_id %in% sprintf("P%03d", 1:100)),
  therapy_days_without_col = "therapy_days_without",
  observation_days_without_col = "observation_window_days_without"
  )
  
head(mme$medications)
#> # A tibble: 6 × 12
#>   patient_id medication_name          dose doses_per_24_hours days_of_medication
#>   <chr>      <chr>                   <dbl>              <dbl>              <dbl>
#> 1 P001       Fentanyl patch (mcg/hr)  12.5                  1                  7
#> 2 P001       tramadol (mg) LA        125                    1                 30
#> 3 P002       Hydrocodone (mg)         10                    3                 14
#> 4 P002       tramadol (mg)            60                    3                 14
#> 5 P003       Meperidine HCL (mg)      50                    4                  7
#> 6 P003       tramadol (mg)            90                    3                  7
#> # ℹ 7 more variables: therapy_days <dbl>, observation_window_days <dbl>,
#> #   therapy_days_without <dbl>, observation_window_days_without <dbl>,
#> #   factor <dbl>, mme <dbl>, single_day_mme <dbl>

head(mme$patient_summary_with_buprenorphine)
#>   patient_id therapy_days observation_window_days total_mme total_days
#> 1       P001            7                      30    960.00         37
#> 2       P002           14                      30    924.00         28
#> 3       P003            7                      30   2228.00         31
#> 4       P004            7                      30    210.00          7
#> 5       P005            7                      30     47.25          7
#> 6       P006            7                      30   3395.25         31
#>        mme1     mme2      mme3   mme4
#> 1  25.94595 137.1429  32.00000  55.00
#> 2  33.00000  66.0000  30.80000  66.00
#> 3  71.87097 318.2857  74.26667 204.00
#> 4  30.00000  30.0000   7.00000  30.00
#> 5   6.75000   6.7500   1.57500   6.75
#> 6 109.52419 485.0357 113.17500 393.75

head(mme$patient_summary_without_buprenorphine)
#>   patient_id therapy_days observation_window_days total_mme total_days
#> 1       P001            7                      30    960.00         37
#> 2       P002           14                      30    924.00         28
#> 3       P003            7                      30   2228.00         31
#> 4       P004            7                      30    210.00          7
#> 5       P005            7                      30     47.25          7
#> 6       P006            7                      30   3395.25         31
#>        mme1     mme2      mme3   mme4
#> 1  25.94595 137.1429  32.00000  55.00
#> 2  33.00000  66.0000  30.80000  66.00
#> 3  71.87097 318.2857  74.26667 204.00
#> 4  30.00000  30.0000   7.00000  30.00
#> 5   6.75000   6.7500   1.57500   6.75
#> 6 109.52419 485.0357 113.17500 393.75

# Cleanup
rm(mme)