我使用以下包:
library("quantmod")
library("PerformanceAnalytics")
library("termstrc")
数据:
AAA <- matrix(sample(30), ncol = 10)
BBB <- matrix(sample(30), ncol = 10)
CCC <- matrix(sample(30), ncol = 10)
与
print(AAA)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 28 18 16 10 20 21 23 27 5 6
[2,] 19 22 24 13 17 14 15 30 4 8
[3,] 1 25 11 2 29 9 3 7 12 26
> print(BBB)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 26 22 24 21 23 25 11 17 8 13
[2,] 14 18 16 28 12 1 10 6 20 15
[3,] 9 4 30 7 5 27 2 3 19 29
> print(CCC)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 6 29 9 24 26 10 12 21 5 22
[2,] 14 4 28 19 8 23 20 27 16 1
[3,] 7 17 13 18 30 2 3 15 11 25
现在我遇到以下问题: 有3个矩阵(AAA,BBB和CCC),这些矩阵具有相同的观察数(3个障碍物和10个变量)。 我计算了min-&amp; “AAA”中每行或观察的最大位置(时间t的最小值/最大值)。
计算如下:
maxAAA_pos <- max.col(AAA)
minAAA_pos <- max.col(-AAA)
结果:
> print(maxAAA_pos)
[1] 1 8 5
> print(minAAA_pos)
[1] 9 9 1
这些最小/最大变量的位置告诉我现在我必须从矩阵BBB和CCC中取出哪个变量来计算以下(1个观察的例子):
Ft =时间t的BBB变量
St + 1 =在时间t + 1时来自CCC的变量
Result_max =(Ft / St + 1) - 1
Result_min = 1 - (Ft / St + 1)
我现在的问题是选择“Ft”和“St + 1”,它们是从AAA的位置min / max变量以及在时间t的向量maxAAA_pos和minAAA_pos中给出的。 / p>
这意味着对于t = 1或第一次观察,计算应如下所示:
Result_max = (26 / 14) - 1
Result_min = 1 - (8 / 16)
先谢谢!
答案 0 :(得分:0)
我找到了一个解决方案,也许方法有点复杂,但它有效......
> AAA <- matrix(sample(30), ncol = 10)
> BBB <- matrix(sample(30), ncol = 10)
> CCC <- matrix(sample(40), ncol = 10)
>
> print(AAA)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 27 12 15 5 3 25 16 28 11 19
[2,] 4 10 14 2 17 21 13 22 24 26
[3,] 23 1 9 30 18 6 7 29 20 8
> print(BBB)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 20 3 23 2 8 17 19 22 15
[2,] 16 26 4 30 6 10 13 7 24 27
[3,] 18 28 5 11 21 9 12 29 25 14
> print(CCC)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 17 29 11 28 21 39 25 34 1 37
[2,] 36 8 5 19 6 26 33 32 14 3
[3,] 15 16 2 27 22 35 30 40 7 9
[4,] 20 4 12 23 31 24 10 18 38 13
>
>
> maxAAA_pos <- max.col(AAA)
> minAAA_pos <- max.col(-AAA)
>
>
> print(maxAAA_pos)
[1] 8 10 4
> print(minAAA_pos)
[1] 5 4 2
>
>
> pos_AAAmax <- cbind(1:3, maxAAA_pos)
> pos_AAAmin <- cbind(1:3, minAAA_pos)
>
>
> returnmax <- function(Ft, St) {
+ (Ft/St)-1
+ }
>
> returnmin <- function(Ft, St) { 1-(Ft/St)}
>
> returnmax(BBB[pos_AAAmax], CCC[cbind(pos_AAAmax[ ,1]+1, maxAAA_pos)])
[1] -0.4062500 2.0000000 -0.5217391
> returnmin(BBB[pos_AAAmin], CCC[cbind(pos_AAAmin[ ,1]+1, minAAA_pos)])
[1] 0.6666667 -0.1111111 -6.0000000