Hi,

I am trying in R to indicate in which quintile a value of a variable is for
every month of my data frame in this case based on volatility. For each
month I want to know for each stock if it is in the most volatile quintile
of if it is in one of the others.

So far I have come up with the following function (see below).
Unfortunately, the function only works in some cases and often gives the
following error:

Error in cut.default(df$VOLATILITY, unique(breaks), label = FALSE, na.rm
=TRUE): 

  invalid number of intervals

Could you give me some advice on how to improve this code so that it works
properly.

It's relatively urgent. Many thanks!

quintilesVolByMonth <- function(x){

  months<-as.vector(unique(x$DATE))  

  dfx<-data.frame()

  for(n in seq(1,length(months))){

    num<-5

    print(paste("Appending month",months[n],sep=""))

    df<-subset(x,DATE==months[n])

    breaks<-quantile(df$VOLATILITY,probs=seq(0,1, 1/num),na.rm=TRUE)

    df$volquintile <- cut(df$VOLATILITY,unique(breaks), 

                       label=FALSE, na.rm=TRUE)

    dfx<-rbind(dfx,df)

  }

  return(dfx)

}

Frame.Quintile <- quintilesVolByMonth(x) 

 

 

Example of data: (I have 30 years of monthly data for over 1000 stocks)
Variable 'VolQuintile' is what I am trying to get.

 

DATE         STOCK     VOLATILITY     VolQuintile

01/01/1990         STOCK 1               0.3
1

01/01/1991         STOCK 2               0.15
4

01/05/1992         STOCK 3               0.1
3

.


	[[alternative HTML version deleted]]

