【问题标题】:R: Calculate decile ranks by groupR:按组计算等分等级
【发布时间】:2019-10-10 09:49:31
【问题描述】:

我有一个数据框crsppofo,其中包含带有多个变量的月度财务数据。对我的问题很重要的是:

   PERMNO monthyear BetaShr
1:  85814 199501    0.5
2:  12345 199501    1.0
3:  85814 200002    1.5
4:  56789 200002    2.0
5:  12345 200002    2.5

"PERMNO" 描述每只股票,"monthyear" 显然显示年份和月份,"BetaShr" 是我按升序排序的风险度量。

我想要完成的是根据"BetaShr" 分配十分位等级(1 到 10),但按"monthyear" 分组。最低十分位等级应分配给每个月“BetaShr"”的最低 10%。输出应如下所示:

   PERMNO monthyear BetaShr BetaDecileRank
1:  85814 199501    0.5     1
2:  12345 199501    1.0     10
3:  85814 200002    1.5     1
4:  56789 200002    2.0     5
5:  12345 200002    2.5     10

当然,这只是一个简化的示例,其中仅分配了三个十分位数来为您提供我想要的输出示例(假设 199501 的 "BetaShr" 范围在 0.5 到 1.0 之间,200002 的范围在 1.5 到 2.5 之间) .你明白了。

通过研究我想出了这个代码:

library(purrr)
library(StatMeasures)
library(dplyr)
crsppofo <- crsppofo %>%
  split(crsppofo$monthyear) %>%
  map_df(~ mutate(., BetaDecileRank = decile(crsppofo$BetaShr)))

导致错误:

Error: Column `BetaDecileRank` must be length 2524 (the group size) or one, not 896935

任何有关此问题的帮助将不胜感激。随意改进我的代码或提出完全不同的方法。如果您需要任何进一步的信息,请通过 cmets 告诉我。我也愿意改进我的问题和我在 SO 的存在,因为我只是这个论坛和 R 的新手。

【问题讨论】:

    标签: r dplyr time-series finance rank


    【解决方案1】:

    问题是在split 组内,decile 应用于整个数据集列“BetaShr”,而不是该拆分数据集中的行

    ... %>%
        map_df(~ mutate(., BetaDecileRank = decile(crsppofo$BetaShr)))
                                                   ^^^^
    

    应该是

    decile(.$BetaShr)
    

    -全码

    library(dplyr)
    library(purrr)
    library(StatMeasures)
    crsppofo <- crsppofo %>%
                  split(crsppofo$monthyear) %>%
                  map_df(~ mutate(., BetaDecileRank = decile(.$BetaShr)))
    crsppofo
    #  PERMNO monthyear BetaShr BetaDecileRank
    #1  85814    199501     0.5              1
    #2  12345    199501     1.0             10
    #3  85814    200002     1.5              1
    #4  56789    200002     2.0              5
    #5  12345    200002     2.5             10
    

    请注意,我们不需要 split 然后使用 map 循环。相反,它可以通过group_by/mutate 选项来完成

    crsppofo %>% 
       group_by(monthyear) %>% 
       mutate(BetaDecileRank = decile(BetaShr))
    # A tibble: 5 x 4
    # Groups:   monthyear [2]
    #  PERMNO monthyear BetaShr BetaDecileRank
    #   <int>     <int>   <dbl>          <int>
    #1  85814    199501     0.5              1
    #2  12345    199501     1               10
    #3  85814    200002     1.5              1
    #4  56789    200002     2                5
    #5  12345    200002     2.5             10
    

    数据

    crsppofo <- structure(list(PERMNO = c(85814L, 12345L, 85814L, 56789L, 12345L
    ), monthyear = c(199501L, 199501L, 200002L, 200002L, 200002L), 
        BetaShr = c(0.5, 1, 1.5, 2, 2.5)), class = "data.frame",
        row.names = c("1:", 
    "2:", "3:", "4:", "5:"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2011-08-07
      • 1970-01-01
      相关资源
      最近更新 更多