我有这个网格矩阵:
cutoff <- c(pi/48, 2*pi/48, 3*pi/48, pi/12)
lambda <- c(5:10)
eta <- seq(1, 1.5, by=0.1)
grid <- expand.grid(cutoff, lambda, eta)
这是我在函数之后得到的输出(计算 sharpe 向量):
best_grid <- grid[max(sharpe),]
cutoff lambda eta
[17] 0.1963495 5 1.5
但我想得到这个:
cutoff lambda eta
[17] 3*pi/48 5 1.5
你有什么想法吗?
答案 0 :(得分:1)
您可以创建与cutoff
中的值对应的截止值的单独字符串向量,然后与之匹配。我只选择了四个随机行进行说明。
cutoff.string = c("pi/48", "2*pi/48", "3*pi/48", "pi/12")
best_grid = grid[c(1,20,50,120),]
best_grid$cutoff = cutoff.string[match(best_grid$cutoff, cutoff)]
best_grid
cutoff lambda eta 1 pi/48 5 1.0 2 pi/12 9 1.0 3 2*pi/48 5 1.2 4 pi/12 10 1.4
或者,@ HaddE.Nuff的建议:
cutoff = quote(c(pi/48, 2*pi/48, 3*pi/48, pi/12))
grid <- expand.grid(cutoff=eval(cutoff), lambda=lambda, eta=eta)
best_grid = grid[c(1,20,50,120),]
best_grid$cutoff = gsub(" ","", as.character(cutoff[-1]))[match(best_grid$cutoff, eval(cutoff))]