同时在循环中执行两个参数

时间:2016-03-04 14:05:47

标签: r loops matrix foreach

不知怎的,我挂了一个小问题,但找不到解决方案:

library(fGarch)
library(foreach)

设置一些矩阵, 矩阵必须填补损失

 Loss <- matrix( , nrow = 2000, 1) 

我从建模中获得的假结果

realized <- matrix(rnorm(2000*4, mean = 10, sd = 100), nrow = 4, ncol = 2000)

我从建模中获得的假结果

evaluated <- matrix(rnorm(2000*4, mean = 10, sd = 100), nrow = 4, ncol = 2000)

我想申请的损失函数

MLossVol <- function (realized, evaluated, which) {
  ### What methods should be availible
  if (all(which != c("Euclidean", "MSE", "Frobenius", "Stein", "L_b"))) 
    stop("Not valid choice of \"which\". Valid choices are \"Euclidean\",\"Euclidean_w\",\"Frobenius\",\"Stein\",\"L_b\" ")
### Evaluated/forcast must be a matrix
if (!is.matrix(evaluated)) {
evaluated = as.matrix(evaluated)
}
### Realized covariance forecast must be a matrix
if (!is.matrix(realized)) {
realized = as.matrix(realized)
}
# They must have the same dimensions in the colums
if (ncol(realized) != ncol(evaluated)) 
stop("the colum numbers of realized and evaluated observation must be the same")
# They must have the same dimensions in the rows
if (nrow(realized) != nrow(evaluated)) 
stop("the row numbers of realized and evaluated observation must be the same")
    if (which == "Euclidean") 
      loss = t(t(vech(realized - evaluated)))%*%t(vech(realized - evaluated))
    if (which == "MSE") # MSE, see Clements et al. "Evaluating multivariate       volatility forecasts" (2009)
      loss = (1/ncol(realized)^2)*t(vec(realized - evaluated))%*%vec(realized - evaluated)
    if (which == "Frobenius")
      loss <- tr(t(realized - evaluated)%*%(realized - evaluated))
    if (which == "L_b") # b = 3 according to "On the forecasting accuracy"
      loss = (1/(3*(3-1)))*tr((realized^3) - (evaluated^3)) - (1/(3-1))*tr(((evaluated^(3-1))%*% ((realized)-(evaluated))))
    return(loss)
    }

我想做什么:从实现和评估中取出每个4x4矩阵,并计算将存储在单列中的损失矩阵中的损失。

手动应该发生什么

assign(paste0("evaluated_", 1), evaluated[, 1:4])
assign(paste0("realized_", 1),  realized[, 1:4])
Loss[1, ] <- MLossVol(realized = get(paste0("realized_", 1)) , evaluated = get(paste0("evaluated_", 1)), which = "Euclidean")


assign(paste0("evaluated_", 2), evaluated[, 5:8])
assign(paste0("realized_", 2),  realized[, 5:8])
Loss[2, ] <- MLossVol(realized = get(paste0("realized_", 2)) , evaluated = get(paste0("evaluated_", 2)), which = "Euclidean")

assign(paste0("evaluated_", 3), evaluated[, 9:12])
assign(paste0("realized_", 3),  realized[, 9:12])
Loss[3, ] <- MLossVol(realized = get(paste0("realized_", 3)) , evaluated = get(paste0("evaluated_", 3)), which = "Euclidean")

assign(paste0("evaluated_", 4), evaluated[, 13:16])
assign(paste0("realized_", 4),  realized[, 13:16])
Loss[4, ] <- MLossVol(realized = get(paste0("realized_", 4)) , evaluated = get(paste0("evaluated_", 4)), which = "Euclidean")

我最好的尝试:1。使用for循环,我的问题:同时执行两者

seq <- paste0(paste0(seq(from = 1, to = 1997, by = 4), ":"), seq(from = 4, to = 2000, by = 4))
for( y in seq){ 
  for( i in 1:c(1:ncol(realized))) {
  assign(paste0("evaluated_", i), evaluated[ , eval(parse(text = y))])
  assign(paste0("realized_", i),  realized[ , eval(parse(text = y))])
  Loss[i, ] <- MLossVol(realized = get(paste0("evaluated_", i)) , 
                    evaluated = get(paste0("evaluated_", i)), which = "Euclidean")
  }
}

和2.与foreach

c <-c(1:ncol(realized))
foreach( y = seq, i = c)  %dopar% {
  assign(paste0("evaluated_", i), evaluated[ , eval(parse(text = y))])
  assign(paste0("realized_", i),  realized[ , eval(parse(text = y))])
  Loss[i, ] <- MLossVol(realized = get(paste0("evaluated_", i)) , 
                        evaluated = get(paste0("evaluated_", i)), which = "Euclidean")
}

如果您需要任何信息,请不要犹豫是否发表评论。我非常感谢你的帮助!

0 个答案:

没有答案