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
Create a new variable that computed the difference in scores between one-year and baseline
Filter for all values where that difference was greater than 0
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
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.