R 同时在循环中执行两个参数
不知怎的,我挂断了一个小问题,但找不到解决方案:R 同时在循环中执行两个参数,r,loops,matrix,foreach,R,Loops,Matrix,Foreach,不知怎的,我挂断了一个小问题,但找不到解决方案: library(fGarch) library(foreach) 建立一些矩阵, 必须填入损失的矩阵 Loss <- matrix( , nrow = 2000, 1) Loss这是什么语言?我用R编写程序,还是很讽刺?我用标签编辑了帖子。 realized <- matrix(rnorm(2000*4, mean = 10, sd = 100), nrow = 4, ncol = 2000) evaluated <-
library(fGarch)
library(foreach)
建立一些矩阵,
必须填入损失的矩阵
Loss <- matrix( , nrow = 2000, 1)
Loss这是什么语言?我用R编写程序,还是很讽刺?我用标签编辑了帖子。
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)
}
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")
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")
}
}
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")
}