longdiff

Kayla Esser

Overview

  1. What’s the gap?

  2. Objective

  3. Meet the function

  4. Examples and applications

  5. Strengths

  6. Future directions

Imagine you are a researcher

You are investigating longitudinal trends in depression, and would like to know which patients had a positive change in PHQ-9 score between their one-year follow up and baseline.

Other applications

You are measuring spending over time, and would like to see the proportion of subjects that spent less money at year 5 compared to year 1.

Or you are interested in whether a tumour has grown, shrunk, or stayed the same across follow up appointments.

In the olden days, you had to

  1. Create a new variable that computed the difference in scores between one-year and baseline
  2. Filter for all values where that difference was greater than 0
  3. Manually pull all IDs corresponding to a positive difference

This is you (before longdiff)

longdiff to the rescue!

The longdiff function allows you to specify the time interval of interest, and magnitude and direction of change in a certain variable compared to baseline, facilitating the creation of a subset of your study population that meets your criteria.

Objective

The purpose of this function is to output a vector of all records in a longitudinal data set wherein there was a specific difference in a variable from time(n) compared to time(0)

Meet the function

longdiff <- function(data, ID, timevar, var, timeparam, operator, cutoff)

Arguments

Parameter Definition
data Name of your data set
ID Variable name corresponding to individual record or subject IDs
timevar Variable corresponding to timepoint (year, month, etc) of your data
var Variable you are interested in looking at differences when compared to baseline

Arguments

timeparam Timepoint you are interested in comparing to baseline (such as the year) - a value of timevar
operator Comparison operator you would like to use to compare the difference to the cutoff, this can be “=”, “<”, “>”, “<=”, “>=” or “!=”
cutoff Cutoff value (numerical) that you set for your variable of interest

What’s happening inside?

longdiff <- function(data, ID, timevar, var, timeparam, operator, cutoff) {
  names(data)[names(data) == deparse(substitute(ID))] <- "ID"
  names(data)[names(data) == deparse(substitute(timevar))] <- "timevar"
  names(data)[names(data) == deparse(substitute(var))] <- "var"

  if (operator == ">")
    alldiff <- data |>
    dplyr::select(ID, timevar, var) |>
    dplyr::arrange(ID, timevar) |>
    dplyr::group_by(ID) |>
    dplyr::filter(timevar == timeparam & (var - var[1L]) > cutoff) |>
    dplyr::select(ID) |>
    dplyr::pull(ID)

  if (operator == "<")
    alldiff <- data |>
    dplyr::select(ID, timevar, var) |>
    dplyr::arrange(ID, timevar) |>
    dplyr::group_by(ID) |>
    dplyr::filter(timevar == timeparam & (var - var[1L]) < cutoff) |>
    dplyr::select(ID) |>
    dplyr::pull(ID)

  if (operator == "=")
    alldiff <- data |>
    dplyr::select(ID, timevar, var) |>
    dplyr::arrange(ID, timevar) |>
    dplyr::group_by(ID) |>
    dplyr::filter(timevar == timeparam & ((var - var[1L]) == cutoff)) |>
    dplyr::select(ID) |>
    dplyr::pull(ID)

  if (operator == "<=")
    alldiff <- data |>
    dplyr::select(ID, timevar, var) |>
    dplyr::arrange(ID, timevar) |>
    dplyr::group_by(ID) |>
    dplyr::filter(timevar == timeparam & (var - var[1L]) <= cutoff) |>
    dplyr::select(ID) |>
    dplyr::pull(ID)

  if (operator == ">=")
    alldiff <- data |>
    dplyr::select(ID, timevar, var) |>
    dplyr::arrange(ID, timevar) |>
    dplyr::group_by(ID) |>
    dplyr::filter(timevar == timeparam & (var - var[1L]) >= cutoff) |>
    dplyr::select(ID) |>
    dplyr::pull(ID)

  if (operator == "!=")
    alldiff <- data |>
    dplyr::select(ID, timevar, var) |>
    dplyr::arrange(ID, timevar) |>
    dplyr::group_by(ID) |>
    dplyr::filter(timevar == timeparam & (var - var[1L]) != cutoff) |>
    dplyr::select(ID) |>
    dplyr::pull(ID)

  return(alldiff)
}

Sample data

library(dplyr)
diffdata <- read.csv("diffdata.csv")
head(diffdata)
  record_id timepoint test_score
1         1      2000         43
2         1      2001         65
3         1      2002         23
4         2      2000         76
5         2      2001         98
6         2      2002         34

Example

pos_set <- longdiff(diffdata, record_id, timepoint, test_score, 2002, ">", 0)
pos_set
[1] 4 7 8

Check if patient is in subset

4 %in% pos_set
[1] TRUE
1 %in% pos_set
[1] FALSE

Check proportion of subset

(length(pos_set)/length(unique(diffdata$record_id)))*100 
[1] 30

Another example

neq_set <- longdiff(diffdata, record_id, timepoint, test_score, 2001, "<=", 5)
neq_set
[1]  4  5  6  7  9 10

Strengths

The function is generalizable to all possible comparison operators in r

=, <, >, <=, >=, !=

Any numerical cutoff can be used (i.e. a clinically meaningful cutoff)

longdiff(diffdata, record_id, timepoint, test_score, 2002, ">=", 30)

This function can accept both numerical and character timepoint values

visit = "Follow up 1"
longdiff(diffdata, record_id, visit, test_score, "Follow up 1", "=", 10)

Future directions

One modification to increase the utility of this function would be to add the comparator timepoint as an argument (the default is currently baseline [1L]) so that comparisons could be made across any time point


Another modification would be to make the function more robust against missing data, as it currently only accepts complete data sets.

Download longdiff today!

install.packages("devtools")
library(devtools)
devtools::install_github("kayserena/longdiff")
library(longdiff)

Questions?

Thank you!