从嵌套循环返回完整列表

时间:2018-10-07 21:59:26

标签: r r-caret

以下代码可以正常工作,但是它仅返回列表methodlist中16个变量的最后一个值。如下所示,我想为(z in seq(1:6))中的每个变量返回6个变量methodlist。所以我想返回一个6x16矩阵,而不是当前的1x16矩阵。代码是什么样的,所以为methodlist中的每个变量附加了6行的列表。

rm(list = ls()) #clears the workspace

library(caret)
library(scales)
library(foreach) 
library(iterators) 
library(parallel) 
library(doParallel)

registerDoParallel(cores = 16) 

# read data
proj_path = "P:/R"
Macro <- read.csv("P:/Earnest/Old/R/Input.csv")


#select trainControl

ctrl = trainControl(method = "cv", number = 5, repeats = 5, verboseIter = TRUE, savePredictions = TRUE)


methodlist <- c("BstLm", "glmnet", "penalized", "bridge", "bayesglm", "spikeslab", "leapForward",
                "glmboost", "blassoAveraged", "blasso", "gaussprPoly", "earth", "cubist", "pcr",
                "leapSeq", "leapBackward")
fit    <- list()
output <- list()
forecast <- list()

for(i in seq_along(methodlist)){
  for(z in seq(1:6)) {

    x <- Macro[1:(14-z),3:21]
    x <- as.matrix(x)
    y <- Macro[1:(14-z),2:2]
    y <- as.matrix(y)
    t <- Macro[(15-z):(15-z),3:21]
    t <- as.matrix(t)

    fit[[i]] <- caret::train(y = as.vector(y), 
                        x = x, 
                        method = methodlist[i], 
                        trControl = ctrl,
                        preProc = c("center", "scale"))
    output[i] <- predict(fit[[i]], t) 
  } }

数据输入

dput(Macro)
structure(list(qtrs = structure(1:14, .Label = c("16_Q2", "16_Q3", 
"16_Q4", "17_Q1", "17_Q2", "17_Q3", "17_Q4", "18_Q1", "18_Q2", 
"18_Q3", "18_Q4", "19_Q1", "19_Q2", "QQ_New"), class = "factor"), 
    y = c(17427, 17613, 21626, 16177, 16154, 16423, 20661, 15995, 
    16410, 16647, 22734, 16556, 17552, 17550.6), c1372 = c(52.38107607, 
    51.71910264, 66.04439265, 48.7435049, 52.84235574, 52.45234009, 
    66.60212761, 48.00370834, 53.27819725, 53.4036627, 73.41349958, 
    51.24441724, 58.80001938, 58.26812139), c5244 = c(27.18948635, 
    26.44530248, 34.00832812, 25.34750922, 27.82252627, 26.87902356, 
    34.15057986, 25.60616679, 29.11586519, 27.66748031, 39.66005562, 
    28.78471195, 34.15138012, 34.05864161), c5640 = c(40.4431936, 
    39.28350352, 51.04846142, 37.25188584, 41.15752543, 41.08080649, 
    51.73736768, 36.88113619, 42.25997532, 42.21743585, 57.81514276, 
    39.73877542, 47.44410618, 46.40224715), c6164 = c(24.94812191, 
    25.3, 30.71137161, 23.17995059, 26.86871377, 28.7449476, 
    35.42080406, 25.30866569, 29.36705061, 30.39925678, 40.20550413, 
    28.31441758, 34.99314256, 34.99500917), b1372 = c(58.28673781, 
    57.43780252, 72.94086917, 60.56258739, 61.15138265, 61.63152251, 
    74.78137432, 61.45308406, 63.49305917, 63.48869267, 84.41035843, 
    65.42555003, 69.93057227, 69.86501992), b5244 = c(72.67600678, 
    73.17343986, 94.23074183, 84.03045989, 84.72343232, 85.24495216, 
    105.0116727, 87.49923648, 89.37263925, 88.98222187, 120.6690755, 
    96.10955339, 97.36718121, 101.7633261), b5640 = c(105.412433, 
    101.027769, 125.8418584, 108.8459417, 102.8725409, 105.3201174, 
    126.6188705, 106.9247911, 106.545478, 107.8489509, 140.2524354, 
    111.4552219, 114.9787081, 117.2442333), b6164 = c(27.9437266, 
    28.79918951, 36.23272036, 31.36799287, 32.61711727, 33.0039884, 
    39.57137571, 33.8573912, 35.51335532, 36.05804281, 46.99019762, 
    37.78925823, 40.49508975, 41.03555772), v1372 = c(0.894, 
    0.908194185, 1.05126864, 0.852367402, 0.897574838, 0.925303822, 
    1.094709584, 0.850266106, 0.925, 0.945399812, 1.22551744, 
    0.926201463, 1.036983254, 1.04), v5244 = c(0.490619506, 0.492131527, 
    0.579979842, 0.463819664, 0.496379414, 0.501965507, 0.60256246, 
    0.478, 0.532945695, 0.531012629, 0.710329807, 0.549488271, 
    0.630092163, 0.635811317), v5640 = c(0.622618968, 0.627586484, 
    0.743424141, 0.588809668, 0.628539746, 0.651820208, 0.781763961, 
    0.595489092, 0.66419102, 0.676916428, 0.886929371, 0.651849221, 
    0.752827781, 0.753805427), v6164 = c(0.418380085, 0.442455804, 
    0.496217777, 0.415503322, 0.461154591, 0.516586013, 0.603938678, 
    0.4807425, 0.5378356, 0.569872101, 0.718373269, 0.561622352, 
    0.664532192, 0.681), bv1372 = c(1.125049204, 1.163584621, 
    1.341847365, 1.161431345, 1.165026048, 1.20311151, 1.399386298, 
    1.188293134, 1.223648195, 1.25, 1.599388376, 1.280592762, 
    1.361357962, 1.383316822), bv5244 = c(1.555199027, 1.615436514, 
    1.923769618, 1.763617138, 1.786232543, 1.839815369, 2.171862846, 
    1.850221722, 1.906545506, 1.938117792, 2.516019557, 2.03585812, 
    2.068596964, 2.195524499), bv5640 = c(1.91698232, 1.955269185, 
    2.288843372, 2.00814652, 1.927080282, 2.001341412, 2.320656843, 
    1.986254618, 1.997858329, 2.054757524, 2.599910718, 2.081192693, 
    2.161265445, 2.23468007), bv6164 = c(0.50964551, 0.545488286, 
    0.636741752, 0.574599203, 0.59206156, 0.615815337, 0.711999807, 
    0.633746358, 0.661835227, 0.688012691, 0.866096559, 0.725703651, 
    0.773148669, 0.796223323), s1 = c(1L, 0L, 0L, 0L, 1L, 0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L), s2 = c(0L, 1L, 0L, 0L, 0L, 
    1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L), s3 = c(0L, 0L, 1L, 0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L), date = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "8/4/2018", class = "factor")), class = "data.frame", row.names = c(NA, 
-14L))

1 个答案:

答案 0 :(得分:1)

您当前的循环结构将使之output[i]捕获每个内部循环的最终输出。内部z循环的所有6次运行都写入output的同一元素中,因此前5次运行被连续覆盖。相反,output应该成为列表列表(fit应该以相同的方式进行更改以匹配)。我无法自己运行您的代码,但据我所知,只需要进行一些小更改,这些小更改已标记为注释:

for(i in seq_along(methodlist)){

  output[[i]] <- list() # treat output as a list-of-lists 
  fit[[i]] <- list() # treat fit as a list-of-lists

  for(z in seq(1:6)) {

    x <- Macro[1:(14-z),3:21]
    x <- as.matrix(x)
    y <- Macro[1:(14-z),2:2]
    y <- as.matrix(y)
    t <- Macro[(15-z):(15-z),3:21]
    t <- as.matrix(t)

    # treat 'fit' as a list-of-lists
    fit[[i]][[z]] <- caret::train(y = as.vector(y), 
                        x = x, 
                        method = methodlist[i], 
                        trControl = ctrl,
                        preProc = c("center", "scale"))
    output[[i]][[z]] <- predict(fit[[i]][[z]], t) # assign the loop outputs to the nested list
  } }