I would like to compare model performance for a bunch of models using the same predictors but different model parameters. This seems like the place to use broom
to create a tidy output, but I can't figure it out.
Here's some non-working code that helps suggest what I'm thinking about:
seq(1:10) %>%
do(fit = knn(train_Market, test_Market, train_Direction, k=.), score = mean(fit==test_Direction)) %>%
tidy()
For more context, this is part of one of the ISLR labs that we are trying to tidyverse-ify. You can see the entire lab here: https://github.com/AmeliaMN/tidy-islr/blob/master/lab3/lab3.Rmd
[Update: reproducible example] It's hard to make a minimal example here because of the need for data wrangling before model fitting, but this should be reproducible:
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)
etc.