我正在尝试对矢量执行滚动计算。我无法使用ifelse
包中的zoo
函数来使代码变得漂亮和整洁,我很难找到reduce
函数如何解决我的问题问题。因此,我知道这不是首选方法,我一直在使用for
循环。
我的错误是,我相信,因为新的向量中包含NULL
值 - 但是,我手动完成了测试(参考引用的单元格并执行if
语句)并且它应该管用。
上面说过,它不是,我被卡住了。请在下面找到我的代码;
数据
Prob <- 0.9
Bet <- 10
Trans <- Bet
Spread <- (1-0.002)
StartingBalance <- 1000000
Days <- 3
> IndTest
N225.Open Close High Low S.High S.Low S.OC
2007-01-04 17322.50 17353.67 17379.46 17315.76 56.96094 6.740234 31.16992
2007-01-05 17315.54 17091.59 17327.13 17011.10 11.59180 304.439453 -223.94922
2007-01-09 17018.89 17237.77 17261.03 16983.97 242.13867 34.919922 218.87891
2007-01-10 17192.42 16942.40 17199.42 16847.57 7.00000 344.849610 -250.01953
2007-01-11 16958.57 16838.17 17057.45 16758.46 98.87891 200.109374 -120.40039
2007-01-12 16979.73 17057.01 17160.77 16941.39 181.03906 38.339844 77.27930
2007-01-15 17160.25 17209.92 17273.58 17144.44 113.33008 15.810547 49.66992
2007-01-16 17190.90 17202.46 17287.96 17175.85 97.06055 15.050782 11.56055
2007-01-17 17153.25 17261.35 17335.03 17002.67 181.77930 150.580078 108.09961
2007-01-18 17248.14 17370.93 17408.62 17220.42 160.47852 27.720703 122.78906
D.Pos D.Neg H.S.Avg L.S.Avg P.D.Up P.D.Dn
2007-01-04 1 0 0.00000 0.00000 0 0
2007-01-05 1 0 0.00000 0.00000 0 0
2007-01-09 1 0 103.56380 115.36654 1 0
2007-01-10 1 0 86.91016 228.06966 1 0
2007-01-11 1 0 116.00586 193.29297 1 0
2007-01-12 1 0 95.63932 194.43294 1 0
2007-01-15 1 0 131.08268 84.75325 1 0
2007-01-16 1 0 130.47656 23.06706 1 0
2007-01-17 1 0 130.72331 60.48047 1 0
2007-01-18 1 0 146.43945 64.45052 1 0
代码
IndTest <-Ind[1:10,]
IndTest[is.na(IndTest)] <- 0
IndTest[is.null(IndTest)] <- 0
IndTest$P.Bal <- rep(0,nrow(IndTest))
for (i in 2:(nrow(IndTest)-2)){
if (IndTest[i+2,5]> IndTest[i+1,10] & IndTest[i+1,12] > Prob){
Ind$P.Bal[i+3] = Bet*IndTest[i+1,10]*Spread-Trans
} else {
Ind$P.Bal[i+2] = Bet*IndTest[i+2,7]*Spread-Trans
}
}
分解错误
我也尝试解构代码,我甚至收到错误;
基本
X <- if(IndTest[4,5] > IndTest[3,10]) {1}
基本if - 错误
Error in if (IndTest[4, 5] > IndTest[3, 10]) { :
argument is of length zero
这是dput()
,正如之前要求的@Coatless out put所要求的那样。看来这可能是由于POSIXT指数?
structure(c(17322.5, 17315.539062, 17018.890625, 17192.419922,
16958.570312, 16979.730469, 17160.25, 17190.900391, 17153.25,
17248.140625, 17353.669922, 17091.589844, 17237.769531, 16942.400391,
16838.169922, 17057.009766, 17209.919922, 17202.460938, 17261.349609,
17370.929688, 17379.460938, 17327.130859, 17261.029297, 17199.419922,
17057.449219, 17160.769531, 17273.580078, 17287.960938, 17335.029297,
17408.619141, 17315.759766, 17011.099609, 16983.970703, 16847.570312,
16758.460938, 16941.390625, 17144.439453, 17175.849609, 17002.669922,
17220.419922, 56.9609380000002, 11.591797000001, 242.138672000001,
7, 98.8789069999984, 181.039062000003, 113.330077999999, 97.060547000001,
181.779297000001, 160.478515999999, 6.74023400000078, 304.439452999999,
34.919922000001, 344.849610000001, 200.109374, 38.3398439999983,
15.810547000001, 15.0507819999984, 150.580077999999, 27.720702999999,
31.169922000001, -223.949218000002, 218.878906000002, -250.019531000002,
-120.400389999999, 77.279297000001, 49.669922000001, 11.560547000001,
108.099609000001, 122.789063, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103.563802333334, 86.910156333334,
116.005859666666, 95.6393230000006, 131.082682333334, 130.476562333334,
130.723307333334, 146.439453333334, 0, 0, 115.366536333334, 228.069661666667,
193.292968666667, 194.432942666666, 84.7532549999996, 23.0670576666659,
60.4804689999995, 64.4505209999988, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), .Dim = c(10L, 14L), .Dimnames = list(NULL, c("N225.Open",
"Close", "High", "Low", "S.High", "S.Low", "S.OC", "D.Pos", "D.Neg",
"H.S.Avg", "L.S.Avg", "P.D.Up", "P.D.Dn", "P.Bal")), index = structure(c(1167868800,
1167955200, 1168300800, 1168387200, 1168473600, 1168560000, 1168819200,
1168905600, 1168992000, 1169078400), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1457986774.61692, class = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"))
答案 0 :(得分:0)
该问题涉及从xts
对象进行子集化后的条件错误。要解决此问题,请通过numeric
将值子集投射为as.numeric
。此外,如果(nrow(IndTest)-2)
i+3
i=8
index = 11
给(nrow(IndTest)-3)
,您也将超出界限。因此,我也将其更改为:library("xts")
Prob <- 0.9
Bet <- 10
Trans <- Bet
Spread <- (1-0.002)
StartingBalance <- 1000000
Days <- 3
IndTest = structure(c(17322.5, 17315.539062, 17018.890625, 17192.419922,
16958.570312, 16979.730469, 17160.25, 17190.900391, 17153.25,
17248.140625, 17353.669922, 17091.589844, 17237.769531, 16942.400391,
16838.169922, 17057.009766, 17209.919922, 17202.460938, 17261.349609,
17370.929688, 17379.460938, 17327.130859, 17261.029297, 17199.419922,
17057.449219, 17160.769531, 17273.580078, 17287.960938, 17335.029297,
17408.619141, 17315.759766, 17011.099609, 16983.970703, 16847.570312,
16758.460938, 16941.390625, 17144.439453, 17175.849609, 17002.669922,
17220.419922, 56.9609380000002, 11.591797000001, 242.138672000001,
7, 98.8789069999984, 181.039062000003, 113.330077999999, 97.060547000001,
181.779297000001, 160.478515999999, 6.74023400000078, 304.439452999999,
34.919922000001, 344.849610000001, 200.109374, 38.3398439999983,
15.810547000001, 15.0507819999984, 150.580077999999, 27.720702999999,
31.169922000001, -223.949218000002, 218.878906000002, -250.019531000002,
-120.400389999999, 77.279297000001, 49.669922000001, 11.560547000001,
108.099609000001, 122.789063, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103.563802333334, 86.910156333334,
116.005859666666, 95.6393230000006, 131.082682333334, 130.476562333334,
130.723307333334, 146.439453333334, 0, 0, 115.366536333334, 228.069661666667,
193.292968666667, 194.432942666666, 84.7532549999996, 23.0670576666659,
60.4804689999995, 64.4505209999988, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), .Dim = c(10L, 14L), .Dimnames = list(NULL, c("N225.Open",
"Close", "High", "Low", "S.High", "S.Low", "S.OC", "D.Pos", "D.Neg",
"H.S.Avg", "L.S.Avg", "P.D.Up", "P.D.Dn", "P.Bal")), index = structure(c(1167868800,
1167955200, 1168300800, 1168387200, 1168473600, 1168560000, 1168819200,
1168905600, 1168992000, 1169078400), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1457986774.61692, class = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"))
Ind = IndTest
for (i in 2:(nrow(IndTest)-3)){
a = as.numeric(IndTest[i+2,5])
b = as.numeric(IndTest[i+1,10])
c = as.numeric(IndTest[i+1,12])
if (a > b & c > Prob){
Ind$P.Bal[i+3] = Bet*IndTest[i+1,10]*Spread-Trans
}else{
Ind$P.Bal[i+2] = Bet*IndTest[i+2,7]*Spread-Trans
}
}
X