Skip to content

Instantly share code, notes, and snippets.

@arslnb
Last active November 2, 2018 17:33
Show Gist options
  • Save arslnb/b8d8e519c1f9a113831f6f2722eaa25c to your computer and use it in GitHub Desktop.
Save arslnb/b8d8e519c1f9a113831f6f2722eaa25c to your computer and use it in GitHub Desktop.
# Import the libraries
library(zoo)
library(dplyr)
Dataset <- Dataset %>% group_by(PermNo)
Dataset <- mutate(Dataset, cumRet = (((Price/lag(Price, n=12, order_by = Month))-1)))
top <- Dataset[Dataset$ME >= Dataset$BP_ME,]
top <- top[!(top$Month<="Dec 1990"),]
bottom <- Dataset[Dataset$ME < Dataset$BP_ME,]
sliceAndDice = function(vec){
cats <- cut(vec, breaks=c(quantile( vec, probs = seq(0, 1, by = 0.1))),
labels=c("0-10","10-20","20-30","30-40","40-50",
"50-60","60-70","70-80","80-90","90-100"))
table(cats)
}
topStocksDist <- aggregate(cumRet ~ Month, top, FUN=sliceAndDice)
bottomStocksDist <- aggregate(cumRet ~ Month, bottom, FUN=sliceAndDice)
head(topStocksDist)
head(bottomStocksDist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment