r-for循环中的for循环,用于计算CV值



我正在尝试迭代1到30个k最近邻居的值,并将每个计算的CV值存储在一个向量中。我有一个为k=1编写的现有代码,但我认为我需要一个嵌套的for循环,用于1到30k的值。

AutoSd <- Auto %>%
mutate(weight.std = scale(weight),year.std = scale(year)) %>%
select(weight,year,weight.std,year.std,mpg)
y <- AutoSd$mpg
x <- AutoSd %>% select(weight,year)
nfolds=10; n = dim(Auto)[1]
groups = rep(1:nfolds,length=n)
set.seed(2)
cvgroups = sample(groups,n)
LOOCVpredictions = rep(NA,n)
k_vals = seq(1,30, by = 2)
cvgroups = 1:n; nfolds=n
for (iii in 1:length(k_vals)){ #I am trying to do the nested loop here but I'm not sure how to store each value for 1-30
for (ii in 1: nfolds) {
groupii = (cvgroups == ii) 
train.x = x[!groupii,]
train.x.std = scale(train.x)
train.y = y[!groupii]
test.x = x[groupii,]
test.x.std = scale(test.x,
center = attr(train.x.std, "scaled:center"),
scale = attr(train.x.std, "scaled:scale"))
predictions = knn.reg(train.x.std, test.x.std, train.y, k_vals)
LOOCVpredictions[groupii] = predictions$pred
}
}
CV = mean( (y - LOOCVpredictions)^2 )

以下是我正在寻找的的总体布局


Outer loop: iterate from 1 to 30 for each value of k
Inner loop: use the 10 training/test splits
scale each training and test set
use knn.reg with k = the value in the outer loop
calculate the prediction from knn.reg and store it in a vector
calculate CV(10) for the current value of k and store it in a vector

不确定我是否正确理解了你的问题,因此我制作了一些非常通用的代码来遍历2个向量,制作一个简单的预测向量,将其绑定在嵌套列表中,并计算预测向量的均值,并将结果简化为一个没有传统嵌套的列表。

library(dplyr)
library(purrr)
outer <- 1:4
inner <- 1:3
res <- list()
for (iii in 1:length(outer)) {
resi <- list()
for (ii in 1:length(inner)) {
prediction <- c(iii, ii)
resi[[ii]] <- prediction
}
res[[iii]] <- resi
}
purrr::map(res, ~.x %>% purrr::map_dbl(~mean(.x))) 
[[1]]
[1] 1.0 1.5 2.0
[[2]]
[1] 1.5 2.0 2.5
[[3]]
[1] 2.0 2.5 3.0
[[4]]
[1] 2.5 3.0 3.5

最新更新