Given an example of a matrix below:
index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
1 | 0.1 | 0.1 | 0.1 | 0.2 | 0.2 | 0.2 | 0.7 | 0.7 | 0.4 | 0.7 |
2 | 0.6 | 0.6 | 0.6 | 0.1 | 0.1 | 0.1 | 0.1 | 0.1 | 0.5 | 0.1 |
3 | 0.3 | 0.3 | 0.3 | 0.7 | 0.7 | 0.7 | 0.2 | 0.2 | 0.1 | 0.2 |
I would like to multiply all possible combination, selecting a value from each column multiply it over the row eg:
0.1* 0.1* 0.1* 0.2* 0.2* 0.2* 0.7* 0.7* 0.4* 0.7
0.1* 0.6* 0.1* 0.2* 0.2* 0.2* 0.7* 0.7* 0.4* 0.7
0.1* 0.3* 0.1* 0.2* 0.2* 0.2* 0.7* 0.7* 0.4* 0.7
0.1* 0.1* 0.6* 0.2* 0.2* 0.2* 0.7* 0.7* 0.4* 0.7
0.1* 0.1* 0.3* 0.2* 0.2* 0.2* 0.7* 0.7* 0.4* 0.7
0.1* 0.1* 0.1* 0.1* 0.2* 0.2* 0.7* 0.7* 0.4* 0.7
0.1* 0.1* 0.1* 0.7* 0.2* 0.2* 0.7* 0.7* 0.4* 0.7
And so on... Aim is to find the maximum value and get the row index selected for each 10 columns
I thought of creating all possible combinations into a row and the perform row multiplication for each row(which would be a combination) then use maximum.
How do I create a matrix with all possible "path" into a row, but then it would be difficult to identify which "path" the max value be for.
CodePudding user response:
To make every permutations, where I let your data as dummy
, use expand.grid
grid <- expand.grid(rep(list(1:3), 10))
Because grid
is very huge, I'll use first five of grid
grid <- grid[c(1:5),]
Let's define a function to pull out grid rownumberd(?) values of dummy
.
func <- function(df, idx) {
vec <- c()
for (i in 1:length(idx)){
vec[i] <- df[as.numeric(idx[i]),i]
}
vec
}
Finally, get multiplications of those values like
apply(grid, 1, function(x) prod(func(dummy, x)))
1 2 3 4 5
1.09760e-06 6.58560e-06 3.29280e-06 6.58560e-06 3.95136e-05
If you want what maximize that is
grid[which.max(apply(grid, 1, function(x) prod(func(dummy, x)))),]
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10
5 2 2 1 1 1 1 1 1 1 1
This is Second way
HOWEVER I think this way is much more efficient to get maximum value and it's index. Because all elements are positive,
x <- apply(dummy, 2, function(x) which.max(x))
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
2 2 2 3 3 3 1 1 2 1
prod(func(dummy,x))
[1] 0.01270609
Large size example for second way
microbenchmark::microbenchmark(test ={x <- runif(250000,0,1)
y <- matrix(x, nrow = 5)
xx <- apply(y, 2, function(x) which.max(x))
prod(func(y,xx))}
)
Unit: milliseconds
expr min lq mean median uq max neval
test 86.0542 91.41575 111.9721 96.9571 140.7912 171.8394 100