gpt4 book ai didi

r - 使用滚动功能将嵌套的For循环转换为适用

转载 作者:行者123 更新时间:2023-12-04 11:10:00 25 4
gpt4 key购买 nike

当前,有一个包含多列的XTS变量,我的目标是对数据集的每一列应用某个滚动函数。我在下面使用的嵌套的for循环方法非常慢,并且我了解apply语句可能有助于加快处理过程。我想知道是否有人可以指导我转换嵌套的for循环以应用语句的最佳方法,或者只是总体上提高代码速度的方法。

数据集的一部分的头部:

Data <-
structure(c(-0.003703704, 0.038104089, -0.000895255, -0.002389486,
0.00988024, 0.00889416, 0.002514368, 0.020781082, 0.002457002,
-0.023459384, 0.019361778, 0.004220893, -0.048253968, 0.105737158,
0.04147813, -0.03070239, 0.039593605, 0.033774073, -0.002636625,
0.020908435, -0.003766478, -0.002126654, 0.004972768, 0.005655042,
-0.003175947, 0.013169074, -0.01572327, 0.003833866, 0.00466794,
-0.004223865, 0.010963195, -0.004802479, -0.005292653, -0.003286385,
0.011775789, 0.000310366, 0.002399232, 0.038774533, -0.014746544,
-0.004209542, 0.039924847, -0.004968383, 0.029471545, -0.000987167,
-0.005928854, -0.005964215, 0.007, -0.002979146, 0.005352645,
0.002818666, 0, -0.00843223, 0.004724409, -0.005642633, -0.013913043,
0.005291005, 0.026315789, 0.015384615, -0.03030303, 0.029513889,
0.076259947, -0.017868145, -0.010037641, 0.002534854, -0.003792668,
-0.021573604, 0.037435657, 0.00947226, -0.023235031, 0.005032022,
-0.017296313, -0.004168597, 0.020424195, 0.056197075, 0.021137026,
-0.017130621, -0.007262164, 0.00658376, -0.014313598, -0.027062706,
0.05156038, 0.059354839, -0.018879415, 0.037243948, 0, 0.009724257,
-0.00171504, -0.021540901, 0.014721772, -0.012777852), class = c("xts",
"zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
index = structure(c(1073001600, 1073260800, 1073347200, 1073433600, 1073520000,
1073606400), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 15L),
.Dimnames = list(NULL, c("WTI", "BRENT", "NATGAS", "GOLD", "SILVER", "ALUM",
"COPPER", "CORN", "SOY", "SUGAR", "WHEAT", "LHOG", "ARACOF", "COCOA", "COT")))


嵌套循环代码:adjFutData =上面显示的数据集,window =滚动窗口大小

test = function(adjFutData, window) {

test = xts(matrix(data = NA,
nrow = nrow(adjFutData),
ncol = (ncol(adjFutData))),
order.by = index(adjFutData))

for (j in 1:ncol(adjFutData)){
for (i in 1:nrow(adjFutData)){
if (i>window){
start = i - window
end = i-1
test[i,j] = log(prod(1+adjFutData[start:end,j]))
}
}
}
return(test)
}

最佳答案

试试这个:

rollapply(as.zoo(Data), list(-seq(3)), function(x) log(prod(1+x)), fill = NA)


这也适用:

rollapplyr(Data, 4, function(x) log(prod(1+x[1:3])), fill = NA)

关于r - 使用滚动功能将嵌套的For循环转换为适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26596543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com