2

在这种情况下,我试图在 R 中根据波动性来指示我的数据框每个月的变量值在哪个五分位数中。对于每个月,我都想知道每只股票是否处于波动最大的五分之一,或者是否在其他股票中。

到目前为止,我已经提出了以下功能(见下文)。不幸的是,该功能仅在某些情况下有效,并且经常出现以下错误:

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

您能否就如何改进此代码以使其正常工作给我一些建议。

比较紧急。非常感谢!

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)   

数据示例:最后一列是我想要得到的。这里的数据只是一个例子,而不是实际结果。

> 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
4

1 回答 1

0

这对你有用吗?

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

更新的解决方案:

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
于 2013-04-06T01:31:08.730 回答