Shifting fill date to account for overlapping days supply for persistence analysis (looping if statement with vector indexing)

I’ve been haunted by a problem for some time now, and I knew it was only a matter of time before my developing awareness of the power of R data programming would reveal a steps towards a solution.  One troublesom intermediate step in the over-arching problem involves shifting the fill date for medications to account for overlapping fill date + days supply for previous medication fills.  It is an important intermediate step for the estimation of persistence, and I’ve been sitting on the SAS code that describes this intermediate for some time (thanks to Scott Leslie’s SUGI paper found here (http://wuss.org/proceedings08/08WUSS%20Proceedings/papers/anl/anl09.pdf).

As you can see, the SAS code makes use of a loop, and my problem with R was that my comfort for coding loops in R had not evolved to the point that I was able to work out the R-like translation….

My solution utilizes both a looping if statement and vector indexing.  The other important thing to mention is, to get the code to work properly over a complete set of claims data (by subject) you need to wrap the function within a plyr statement (to run the transformation by subject… I suppose you might also be able to wrap the function within a doBy function, I think there may be a transformBy????).  Anyhow, here is the code:

shiftday <- function(FD,DS){

x <- rep(0, length(FD))

VA <- FD

VB <- DS

for (i in 1:length(VA))

if (i == 1)

x[i] <- VA[i]

else {

if ((((x[i-1] + VB[i-1])) – VA[i]) <= 0)

x[i] <- VA[i]

if ((((x[i-1] + VB[i-1])) – VA[i]) > 0)

x[i] <- ((x[i-1] + VB[i-1])) – VA[i] +VA[i]

}

x

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s