以滚动方式应用自定义功能

时间:2017-08-07 09:49:18

标签: r function apply

我有以下自定义功能,我想以滚动方式应用它:

AvgTotCorr = function(R, weights){
  options(scipen = 999)
  cv = cov(R)
  VARs = diag(cv)
  cr = cor(R)

  # double summation in numerator (including multiplier 2)
  diag(cr) <- 0
  double_sum.1 <- c(crossprod(weights, cr %*% weights))

  # single summation in denominator
  single_sum.1 <- c(crossprod(weights))
  ATC = double_sum.1/(1 - single_sum.1)
}

此处R是一个数据框:

                  DDM          DDN         DDO          DDR           DDC
Sep 2014 -0.039749349 -0.046725883 -0.07165751 -0.041101729 -0.0405825697
Oct 2014 -0.019015151  0.030691351  0.02229647 -0.022410688  0.0321796762
Nov 2014  0.020675795 -0.037794427 -0.01102216  0.021366553 -0.0386273876
Dec 2014 -0.046899009 -0.022367029 -0.04680322 -0.040628129 -0.0262800957
Jan 2015  0.005151610  0.011781256 -0.00199897  0.006401454  0.0177665515
Feb 2015  0.060934104  0.060653431  0.04656980  0.060199852  0.0623440161
Mar 2015 -0.024379782  0.005873559 -0.02055282 -0.025283819  0.0031299557
Apr 2015  0.042419793  0.034595214  0.07413768  0.045284691  0.0325663255
May 2015  0.002830872 -0.005836018 -0.03553548  0.002944174 -0.0030217009
Jun 2015 -0.032727478 -0.024746098 -0.02552883 -0.031593800 -0.0170672371
Jul 2015  0.026676613 -0.005407006 -0.06262244  0.024492561  0.0003899826
Aug 2015 -0.070191318 -0.076771003 -0.09864298 -0.066551790 -0.0783862698
                   DDG          DDS
Sep 2014 -0.0726463957 -0.016924381
Oct 2014  0.0077937769  0.026900190
Nov 2014 -0.0128892718  0.031539076
Dec 2014 -0.0417678522 -0.007125182
Jan 2015 -0.0002124871 -0.017743131
Feb 2015  0.0421096048  0.068226081
Mar 2015 -0.0136735199 -0.007703681
Apr 2015  0.0726257592  0.000000000
May 2015 -0.0364584275  0.017228766
Jun 2015 -0.0294631646 -0.016211165
Jul 2015 -0.0655703334  0.032250580
Aug 2015 -0.0935620960 -0.062634476

wv是一个向量:

[1] 0.142860 0.142877 0.142813 0.142896 0.142806 0.142861 0.142886

我最初认为我可以使用包rollapply中的zoo来执行此操作。但我无法让它发挥作用:

rollcorr = rollapply(data = df,
                     width = 6,
                     FUN = function(x) AvgTotCorr(x, weights = wv))

有什么可能的解决方案?感谢。

1 个答案:

答案 0 :(得分:1)

这对我有用:

sapply(1:(nrow(R)-5), FUN = function(x) AvgTotCorr(R[x:(x+5),], weights = wv))

我们从数据框x1重叠(nrow(R)-5)x并将子行x+5重复到R

包含一小部分数据的示例:

R =read.table(text="month DDM          DDN         DDO          DDR           DDC
-0.039749349 -0.046725883 -0.07165751 -0.041101729 -0.0405825697
-0.019015151  0.030691351  0.02229647 -0.022410688  0.0321796762
0.020675795 -0.037794427 -0.01102216  0.021366553 -0.0386273876
-0.046899009 -0.022367029 -0.04680322 -0.040628129 -0.0262800957
0.005151610  0.011781256 -0.00199897  0.006401454  0.0177665515
0.060934104  0.060653431  0.04656980  0.060199852  0.0623440161
-0.024379782  0.005873559 -0.02055282 -0.025283819  0.0031299557
0.042419793  0.034595214  0.07413768  0.045284691  0.0325663255
0.002830872 -0.005836018 -0.03553548  0.002944174 -0.0030217009
-0.032727478 -0.024746098 -0.02552883 -0.031593800 -0.0170672371
0.026676613 -0.005407006 -0.06262244  0.024492561  0.0003899826
-0.070191318 -0.076771003 -0.09864298 -0.066551790 -0.0783862698",header=T)
wv = c(0.142860, 0.142877, 0.142813, 0.142896, 0.142806)

library(zoo)
sapply(1:(nrow(R)-5), FUN = function(x) AvgTotCorr(R[x:(x+5),], weights = wv))

输出:

[1] 0.3541615 0.3242225 0.3632350 0.4199588 0.4118846 0.3737221 0.3875652