Created
August 28, 2013 21:23
-
-
Save jfreels/6371494 to your computer and use it in GitHub Desktop.
Drawdown function for use with plyr / data.table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Drawdown using plyr / data.table | |
vami<-function (ror) { cumprod(na.omit(ror) + 1) } | |
dd<-function (ror) { -(1 - vami(ror)/cummax(c(1, cummax(vami(ror))))[-1]) } | |
require(PerformanceAnalytics) | |
require(plyr) | |
require(reshape2) | |
require(data.table) | |
data(edhec) | |
edhec_melt<-melt(data.frame(date=index(edhec),coredata(edhec)),id.vars='date') | |
## plyr method | |
ddply(edhec_melt,.(variable),transform,dd=dd(value)) | |
## data.table method | |
edhec_dt<-data.table(edhec_melt,key='variable') | |
edhec_dt[,dd:=dd(value),by=variable] | |
edhec_dt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment