我想比较使用相同预测变量但模型参数不同的一堆模型的模型性能。这似乎是broom
用来创建整洁输出的地方,但我想不通。这是一些无效的代码,有助于建议我在想什么:
seq(1:10) %>%
do(fit = knn(train_Market, test_Market, train_Direction, k=.), score = mean(fit==test_Direction)) %>%
tidy()
有关更多上下文,这是我们正在尝试 tidyverse-ify 的 ISLR 实验室之一的一部分。你可以在这里看到整个实验室:https ://github.com/AmeliaMN/tidy-islr/blob/master/lab3/lab3.Rmd
[更新:可重现的示例] 由于需要在模型拟合之前进行数据整理,因此很难在这里制作一个最小的示例,但这应该是可重现的:
library(ISLR)
library(dplyr)
train = Smarket %>%
filter(Year < 2005)
test = Smarket %>%
filter(Year >= 2005)
train_Market = train %>%
select(Lag1, Lag2)
test_Market = test %>%
select(Lag1, Lag2)
train_Direction = train %>%
select(Direction) %>%
.$Direction
set.seed(1)
knn_pred = knn(train_Market, test_Market, train_Direction, k=1)
mean(knn_pred==test_Direction)
knn_pred = knn(train_Market, test_Market, train_Direction, k=3)
mean(knn_pred==test_Direction)
knn_pred = knn(train_Market, test_Market, train_Direction, k=4)
mean(knn_pred==test_Direction)
等等