0

I am using the following packages:

library("quantmod")
library("PerformanceAnalytics")
library("termstrc")

Data:

AAA <- matrix(sample(30), ncol = 10)
BBB <- matrix(sample(30), ncol = 10)
CCC <- matrix(sample(30), ncol = 10)

with

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

Now I have the following problem: There are 3 matrices (AAA, BBB and CCC), these matrices have all the same nummer of observations (3 obs. and 10 var.). I calculated the min- & max-position for each row or observation in "AAA" (min/max for time t).

Calculated the following:

maxAAA_pos <- max.col(AAA)
minAAA_pos <- max.col(-AAA)

Result:

> print(maxAAA_pos)
[1] 1 8 5
> print(minAAA_pos)
[1] 9 9 1

The position of these min/max variables are telling me now which variable I have to take from the matrices BBB and CCC to calculate the following (example for the 1 observation):

Ft = variable from BBB at time t

St+1 = variable from CCC at time t+1

Result_max = (Ft / St+1) - 1

Result_min = 1 - (Ft / St+1)

My problem now is to select "Ft" and "St+1", which are given from the positions min/max variables from AAA and in the vector maxAAA_pos and minAAA_pos at time t.

This means the calculation should look like this for t=1 or the first observation:

Result_max = (26 / 14) - 1
Result_min = 1 - (8 / 16)

Thanks in advanced!

  • Please produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including some sample data and expected output. Also please provide any packages that you are using (I don't have `max.col`, for instance). – r2evans Mar 30 '16 at 16:51

1 Answers1

0

I found a solution, maybe the way is a bit complicated, but it works....

> 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