gpt4 book ai didi

r - dplyr中的k折交叉验证?

转载 作者:行者123 更新时间:2023-12-04 12:51:22 25 4
gpt4 key购买 nike

Hadley Wickham proposed可以使用 dplyr 进行引导包,他的建议was improved然后 implemented in broom package .是否也可以实现 k 折交叉验证?

我猜第一步(选择火车组)很简单:

crossvalidate <- function (df, k = 5) {
n <- nrow(df)
idx <- sample(rep_len(1:k, n))
attr(df, "indices") <- lapply(1:k, function(i) which(idx != i))
attr(df, "drop") <- TRUE
attr(df, "group_sizes") <- nrow(df) - unclass(table(idx))
attr(df, "biggest_group_size") <- max(attr(df, "group_sizes"))
attr(df, "labels") <- data.frame(replicate = 1:k)
attr(df, "vars") <- list(quote(replicate))
class(df) <- c("grouped_df", "tbl_df", "tbl", "data.frame")
df
}

但不知何故,我在任何地方都找不到 attr(, "indices") 的任何文档。了解是否可以以某种方式使用索引“其他”选择用于选择测试组索引的索引。你有什么想法?

最佳答案

https://rpubs.com/dgrtwo/cv-modelr - 这里有一个使用 dplyr 的 k 折交叉验证示例包裹:

library(ISLR)
library(dplyr)
library(purrr)
library(modelr)
library(broom)
library(tidyr)

set.seed(1)

models <- Smarket %>%
select(Today, Lag1:Lag5) %>%
crossv_kfold(k = 20) %>%
mutate(model = map(train, ~ lm(Today ~ ., data = .)))

predictions <- models %>%
unnest(map2(model, test, ~ augment(.x, newdata = .y)))

predictions %>%
summarize(MSE = mean((Today - .fitted) ^ 2),
MSEIntercept = mean((Today - mean(Today))^2))

关于r - dplyr中的k折交叉验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40766014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com