在R

时间:2018-09-15 17:09:15

标签: r r-caret

我试图遍历插入符号包中可用的3种方法的列表(“ BstLm”,“ glmnet”,“惩罚”)。除了为每个循环选择的方法之外,循环中的所有内容都完全相同,因此我认为这应该是一个简单的循环。

我最终希望将每个模型的输出附加到列表中,但是在到达那里之前,我需要了解为什么下面的循环不起作用。如果要删除循环,而只是手动插入模型/方法(方法= glmnet),则代码可以完美地工作。因此,循环中会丢失一些东西。

library(caret)
library(glmnet)

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

# method list
methodlist <- c("BstLm", "glmnet", "penalized")  


for(i in methodlist){

#round 1
fit <- caret::train(y = as.vector(y), 
                      x = x, 
                      method = i, 
                      trControl = ctrl,
                      preProc = c("center", "scale"))

output <- predict(fit ,t)
output
}

数据输入

x <- 
structure(c(1.400807408, 1.364480432, 1.500560251, 1.26586703, 
1.443177251, 1.408981751, 1.561259129, 1.277240132, 1.491666004, 
1.420852347, 1.630816133, 1.35267138, 1.655465536, 0.814866998, 
0.810722787, 0.808929484, 0.761434594, 0.895402351, 0.848954869, 
0.845575348, 0.747585665, 0.874905836, 0.865513017, 0.975609297, 
0.854114665, 1.074622982, 1.353233718, 1.223561632, 1.371179077, 
1.1329647, 1.448655476, 1.30866331, 1.471799742, 1.180619459, 
1.49988356, 1.33803596, 1.547472083, 1.270202835, 1.690135722, 
0.225475353, 0.228977389, 0.206850776, 0.183182427, 0.25879326, 
0.272921939, 0.250835712, 0.229553218, 0.29439353, 0.301015133, 
0.279187879, 0.254124205, 0.363056704, 1.175400257, 1.136855937, 
1.277755633, 1.146206896, 1.230099913, 1.194888116, 1.329147671, 
1.160423782, 1.285397495, 1.214467169, 1.387038265, 1.237106394, 
1.437157096, 2.049086011, 1.956653266, 2.103276536, 2.068954529, 
2.217281284, 2.090954603, 2.257380717, 2.147282634, 2.286721518, 
2.201965244, 2.448933365, 2.364325727, 2.530556444, 3.449703984, 
3.170076434, 3.494695312, 3.308829513, 3.585369619, 3.284206753, 
3.673954689, 3.378767404, 3.623541875, 3.319163827, 3.757435153, 
3.492152087, 3.913718535, 0.172660414, 0.186281058, 0.166285315, 
0.166955598, 0.207640887, 0.212505958, 0.191188765, 0.18421126, 
0.233655689, 0.238705151, 0.210098712, 0.206674868, 0.286080385, 
0.046424641, 0.045790426, 0.047229766, 0.043122515, 0.046346434, 
0.046747417, 0.048258385, 0.043079032, 0.047261931, 0.04612268, 
0.049793972, 0.044768847, 0.051333103, 0.027276678, 0.027857387, 
0.026956659, 0.026311728, 0.029778712, 0.028433388, 0.027334549, 
0.024817468, 0.027470204, 0.02829008, 0.029842615, 0.027500791, 
0.032395826, 0.041693753, 0.039260192, 0.041295334, 0.036761682, 
0.043537555, 0.04108965, 0.043489385, 0.037844418, 0.044414194, 
0.041268713, 0.04404975, 0.039553215, 0.048715961, 0.00680112, 
0.00686115, 0.006123778, 0.005844483, 0.007480583, 0.008010849, 
0.007215084, 0.006941924, 0.008367775, 0.008769686, 0.007945229, 
0.00765007, 0.010195462, 0.040472736, 0.039892902, 0.042624436, 
0.039853511, 0.040968117, 0.040304279, 0.043277726, 0.039822075, 
0.042129873, 0.040744442, 0.044548313, 0.041214776, 0.046065241, 
0.073739438, 0.071306088, 0.074818233, 0.07529674, 0.076494871, 
0.074540589, 0.077997296, 0.076230605, 0.077762856, 0.07637923, 
0.082370045, 0.081061902, 0.083201049, 0.112694966, 0.106541426, 
0.113274592, 0.110175927, 0.113497979, 0.106964452, 0.115059907, 
0.109669032, 0.112113769, 0.10489425, 0.113459493, 0.108813932, 
0.116833805, 0.005320689, 0.005586605, 0.004958118, 0.00508488, 
0.006116573, 0.006336282, 0.005661865, 0.005716691, 0.006735178, 
0.0069404, 0.006069391, 0.006113098, 0.008048097, 1, 0, 0, 0, 
1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0), .Dim = c(13L, 19L), .Dimnames = list(
    c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
    "12", "13"), c("c1372", "c5244", "c5640", "c6164", "b1372", 
    "b5244", "b5640", "b6164", "v1372", "v5244", "v5640", "v6164", 
    "bv1372", "bv5244", "bv5640", "bv6164", "s1", "s2", "s3")))

y <- 
structure(c(719.185, 702.629, 764.002, 700.136, 745.584, 709.971, 
772.7, 700.4, 743.2, 710.37, 787.77, 721.41, 808), .Dim = c(13L, 
1L))

t <-
structure(c(1.460053632, 1.0156739, 1.4143937, 0.338644532, 1.330006012, 
2.754840348, 3.691103295, 0.269560001, 0.04574815, 0.030811228, 
0.041912515, 0.009290976, 0.044005394, 0.091986379, 0.111882971, 
0.007611613, 0, 1, 0), .Dim = c(1L, 19L), .Dimnames = list("14", 
    c("c1372", "c5244", "c5640", "c6164", "b1372", "b5244", "b5640", 
    "b6164", "v1372", "v5244", "v5640", "v6164", "bv1372", "bv5244", 
    "bv5640", "bv6164", "s1", "s2", "s3")))

1 个答案:

答案 0 :(得分:2)

我认为这是您想要的:

fit    <- list()
output <- list()

for(i in seq_along(methodlist)){
    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)
}