Question

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 THE DATA: The last column is what I am trying to get. The data here is just an example and not actual results.

> DATE <- c("01/10/2011","01/10/2012","01/10/2010","01/08/2010","01/10/2011","01/12/2011","01/09/2011","01/10/2011","01/09/2012","01/08/2012","01/02/2010","01/01/2011","01/09/2010","01/06/2010","01/07/2010","01/01/2012","01/01/2012","01/11/2011","01/09/2011","01/10/2011")
> NAME<-c("HOEK'S MACHINE DEAD - DELIST.","WORLD SCOPE (CADB TEST STOCK)","BRILL (KON.)",   "BBL DEAD - 30/06/465", "GENK LOGISTICS","GROENIJK.YLCBN. DEAD - DELIST.31/05/479", "NOORD-EUR.HOUTH.","PALTHE DEAD - 4/2/475","GENERALE BANQUE DEAD - DEL. 30/12/490","STORK DEAD - TAKEOVER 905099","LOUVAIN-LA-NEUVE","VENTOS DEAD - 06/06/384","BRAINE-LE-COMTE SUSP 14/02/460","VILENZO DEAD - 25/11/370","ECONOSTO KON. DEAD - 07/07/374","ELECTRORAIL DEAD - DELIST 21/02/387","BLYSTEIN FL.1384","OBOURG (CIMENTS)","BRUGEFI DEAD - 31/07/475","GIB NEW")
> VOLATILITY<-c(0.3383, 0.084,  0.046,  0.0945, 0.0465, 0.2008, 0.1361, 0.2183, 0.1032, 0.1083, 0.0494, 0.0538, 0.0357, 0.037,  0.0386, 0.073,  0.073,  0.0393, 0.0687, 0.3308)
> VOLQUINTILE<-c(4,1,1,2,2,3,2,3,4,2,3,2,4,1,2,1,1,2,3,4)
>   
> x<-data.frame(DATE,NAME,VOLATILITY, VOLQUINTILE)
> x
         DATE                                    NAME VOLATILITY VOLQUINTILE
1  01/10/2011           HOEK'S MACHINE DEAD - DELIST.     0.3383           4
2  01/10/2012           WORLD SCOPE (CADB TEST STOCK)     0.0840           1
3  01/10/2010                            BRILL (KON.)     0.0460           1
4  01/08/2010                    BBL DEAD - 30/06/465     0.0945           2
5  01/10/2011                          GENK LOGISTICS     0.0465           2
6  01/12/2011 GROENIJK.YLCBN. DEAD - DELIST.31/05/479     0.2008           3
7  01/09/2011                        NOORD-EUR.HOUTH.     0.1361           2
8  01/10/2011                   PALTHE DEAD - 4/2/475     0.2183           3
9  01/09/2012   GENERALE BANQUE DEAD - DEL. 30/12/490     0.1032           4
10 01/08/2012            STORK DEAD - TAKEOVER 905099     0.1083           2
11 01/02/2010                        LOUVAIN-LA-NEUVE     0.0494           3
12 01/01/2011                 VENTOS DEAD - 06/06/384     0.0538           2
13 01/09/2010          BRAINE-LE-COMTE SUSP 14/02/460     0.0357           4
14 01/06/2010                VILENZO DEAD - 25/11/370     0.0370           1
15 01/07/2010          ECONOSTO KON. DEAD - 07/07/374     0.0386           2
16 01/01/2012     ELECTRORAIL DEAD - DELIST 21/02/387     0.0730           1
17 01/01/2012                        BLYSTEIN FL.1384     0.0730           1
18 01/11/2011                        OBOURG (CIMENTS)     0.0393           2
19 01/09/2011                BRUGEFI DEAD - 31/07/475     0.0687           3
20 01/10/2011                                 GIB NEW     0.3308           4
Was it helpful?

Solution

Does this work for you?

library(plyr)
vol1<-ddply(mydata,.(DATE), transform, max.name=NAME[which.max(quantile(VOLATILITY))])
             DATE                                    NAME VOLATILITY                                max.name
    1  01/01/2011                 VENTOS DEAD - 06/06/384     0.0538                 VENTOS DEAD - 06/06/384
    2  01/01/2012     ELECTRORAIL DEAD - DELIST 21/02/387     0.0730     ELECTRORAIL DEAD - DELIST 21/02/387
    3  01/01/2012                        BLYSTEIN FL.1384     0.0730     ELECTRORAIL DEAD - DELIST 21/02/387
    4  01/02/2010                        LOUVAIN-LA-NEUVE     0.0494                        LOUVAIN-LA-NEUVE
    5  01/06/2010                VILENZO DEAD - 25/11/370     0.0370                VILENZO DEAD - 25/11/370
    6  01/07/2010          ECONOSTO KON. DEAD - 07/07/374     0.0386          ECONOSTO KON. DEAD - 07/07/374
    7  01/08/2010                    BBL DEAD - 30/06/465     0.0945                    BBL DEAD - 30/06/465
    8  01/08/2012            STORK DEAD - TAKEOVER 905099     0.1083            STORK DEAD - TAKEOVER 905099
    9  01/09/2010          BRAINE-LE-COMTE SUSP 14/02/460     0.0357          BRAINE-LE-COMTE SUSP 14/02/460
    10 01/09/2011                        NOORD-EUR.HOUTH.     0.1361                                    <NA>
    11 01/09/2011                BRUGEFI DEAD - 31/07/475     0.0687                                    <NA>
    12 01/09/2012   GENERALE BANQUE DEAD - DEL. 30/12/490     0.1032   GENERALE BANQUE DEAD - DEL. 30/12/490
    13 01/10/2010                            BRILL (KON.)     0.0460                            BRILL (KON.)
    14 01/10/2011           HOEK'S MACHINE DEAD - DELIST.     0.3383                                    <NA>
    15 01/10/2011                          GENK LOGISTICS     0.0465                                    <NA>
    16 01/10/2011                   PALTHE DEAD - 4/2/475     0.2183                                    <NA>
    17 01/10/2011                                 GIB NEW     0.3308                                    <NA>
    18 01/10/2012           WORLD SCOPE (CADB TEST STOCK)     0.0840           WORLD SCOPE (CADB TEST STOCK)
    19 01/11/2011                        OBOURG (CIMENTS)     0.0393                        OBOURG (CIMENTS)
    20 01/12/2011 GROENIJK.YLCBN. DEAD - DELIST.31/05/479     0.2008 GROENIJK.YLCBN. DEAD - DELIST.31/05/479

Updated solution:

library(plyr)    


   vol2<-ddply(x,.(DATE), transform,quantile=ifelse(VOLATILITY<quantile(VOLATILITY,p=0.25),1,
ifelse(((VOLATILITY>quantile(VOLATILITY,p=0.25))& (VOLATILITY<quantile(VOLATILITY,p=0.5))),2,ifelse(((VOLATILITY>quantile(VOLATILITY,p=0.5))& VOLATILITY<quantile(VOLATILITY,p=0.75)),3,4))))

       DATE                                    NAME VOLATILITY   quantile
1  01/01/2011                 VENTOS DEAD - 06/06/384     0.0538        4
2  01/01/2012     ELECTRORAIL DEAD - DELIST 21/02/387     0.0730        4
3  01/01/2012                        BLYSTEIN FL.1384     0.0730        4
4  01/02/2010                        LOUVAIN-LA-NEUVE     0.0494        4
5  01/06/2010                VILENZO DEAD - 25/11/370     0.0370        4
6  01/07/2010          ECONOSTO KON. DEAD - 07/07/374     0.0386        4
7  01/08/2010                    BBL DEAD - 30/06/465     0.0945        4
8  01/08/2012            STORK DEAD - TAKEOVER 905099     0.1083        4
9  01/09/2010          BRAINE-LE-COMTE SUSP 14/02/460     0.0357        4
10 01/09/2011                        NOORD-EUR.HOUTH.     0.1361        4
11 01/09/2011                BRUGEFI DEAD - 31/07/475     0.0687        1
12 01/09/2012   GENERALE BANQUE DEAD - DEL. 30/12/490     0.1032        4
13 01/10/2010                            BRILL (KON.)     0.0460        4
14 01/10/2011           HOEK'S MACHINE DEAD - DELIST.     0.3383        4
15 01/10/2011                          GENK LOGISTICS     0.0465        1
16 01/10/2011                   PALTHE DEAD - 4/2/475     0.2183        2
17 01/10/2011                                 GIB NEW     0.3308        3
18 01/10/2012           WORLD SCOPE (CADB TEST STOCK)     0.0840        4
19 01/11/2011                        OBOURG (CIMENTS)     0.0393        4
20 01/12/2011 GROENIJK.YLCBN. DEAD - DELIST.31/05/479     0.2008        4
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top