【问题标题】:R: Calculate Standard Deviations by Group with a Moving WindowR:使用移动窗口按组计算标准偏差
【发布时间】:2020-10-30 10:09:34
【问题描述】:

我很想分析多家公司每月股票回报(面板数据)。但是,我正在努力计算公司过去 X 个月的标准差

基本上,我想在现有的 data.frame 中添加另一列,其中显示了根据公司 X 个月移动窗口的标准偏差。请在下面找到我的数据的简化示例以及我希望实现的目标。

#My data:
company = c("1","1","1","1","1","2","2","2","2","2","2","2","3","3","3","3","4","4","4")
return = c(0.01,0.015,-0.01,0.02,0.023,-0.04,-0.02,-0.01,0.05,0.06,0.03,-0.09,0.2,0.3,-0.04,-0.02,-0.01,0.023,-0.04)
stock = data.frame(company,return)

鉴于这种初始情况,我很想计算另一列中的标准差,例如基于3 次观察。

#Column to be filled with the respective value
stock["std_3obs"] = NA
#However, I do not manage to fill this column accordingly. The following result for a given row is expected:
#row 1 = Not possible, as there are not enough prior observations available
#row 2 = Not possible, as there are not enough prior observations available
#row 3 = sd(c(0.01,0.015,-0.01) = 0.01322876
#row 7 = Not possible, as there are not enough prior observations available
#row 8 = sd(c(-0.040,-0.020,-0.010)) = 0.01527525

提前非常感谢!非常感谢任何帮助!请温柔一点,因为我对 R 还很陌生。

*旁注: 研究这个问题和调整其他解决方案总是会导致这个错误:replacement has X rows, data has Y * where X >>> Y

【问题讨论】:

    标签: r standard-deviation panel-data


    【解决方案1】:

    您可以使用 zoo 包中的滚动功能:

    library(dplyr)
    
    stock %>%
      group_by(company) %>%
      mutate(std_3obs = zoo::rollapplyr(return, 3, sd, fill = NA))
    
    #  company return std_3obs
    #   <chr>    <dbl>    <dbl>
    # 1 1        0.01   NA     
    # 2 1        0.015  NA     
    # 3 1       -0.01    0.0132
    # 4 1        0.02    0.0161
    # 5 1        0.023   0.0182
    # 6 2       -0.04   NA     
    # 7 2       -0.02   NA     
    # 8 2       -0.01    0.0153
    # 9 2        0.05    0.0379
    #10 2        0.06    0.0379
    #11 2        0.03    0.0153
    #12 2       -0.09    0.0794
    #13 3        0.2    NA     
    #14 3        0.3    NA     
    #15 3       -0.04    0.175 
    #16 3       -0.02    0.191 
    #17 4       -0.01   NA     
    #18 4        0.023  NA     
    #19 4       -0.04    0.0315
    

    【讨论】:

    • 太棒了!非常感谢!
    【解决方案2】:

    这是data.table 方法

    library(data.table)
    setDT(stock)[, std_3obs := frollapply(return, 3, sd), by = company]
    

    输出

    > stock[]
        company return   std_3obs
     1:       1  0.010         NA
     2:       1  0.015         NA
     3:       1 -0.010 0.01322876
     4:       1  0.020 0.01607275
     5:       1  0.023 0.01824829
     6:       2 -0.040         NA
     7:       2 -0.020         NA
     8:       2 -0.010 0.01527525
     9:       2  0.050 0.03785939
    10:       2  0.060 0.03785939
    11:       2  0.030 0.01527525
    12:       2 -0.090 0.07937254
    13:       3  0.200         NA
    14:       3  0.300         NA
    15:       3 -0.040 0.17473790
    16:       3 -0.020 0.19078784
    17:       4 -0.010         NA
    18:       4  0.023         NA
    19:       4 -0.040 0.03151190
    

    【讨论】:

    • 很棒的解决方案!非常感谢!
    猜你喜欢
    • 2020-09-18
    • 2016-04-24
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多