1
我有以下的自定義函數,我想申請成爲一個滾動的基礎上:應用自定義函數的滾動的基礎上
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
我最初以爲我可以做到這一點全光照來自包裝zoo
的g rollapply
。但我無法得到它的工作:
rollcorr = rollapply(data = df,
width = 6,
FUN = function(x) AvgTotCorr(x, weights = wv))
有什麼可能的解決方案呢?謝謝。
太好了!謝謝! – AK88