knn ( ) , tidyr unnest ( purrr map rep_along:
library(class)
library(purrr)
library(tidyr)
set.seed(1)
predictions <- data_frame(k = 1:5) %>%
unnest(prediction = map(k, ~ knn(train_Market, test_Market, train_Direction, k = .))) %>%
mutate(oracle = rep_along(prediction, test_Direction))
predictions :
k prediction oracle
<int> <fctr> <fctr>
1 1 Up Up
2 1 Down Up
3 1 Up Down
4 1 Up Up
5 1 Up Up
6 1 Down Up
7 1 Down Down
8 1 Down Up
9 1 Down Up
10 1 Up Up
:
predictions %>%
group_by(k) %>%
summarize(accuracy = mean(prediction == oracle))
, , , , tidy augment, .
tidyr crossing ( expand.grid) invoke_rows . , l k:
crossing(k = 2:5, l = 0:1) %>%
invoke_rows(knn, ., train = train_Market, test = test_Market, cl = train_Direction) %>%
unnest(prediction = .out) %>%
mutate(oracle = rep_along(prediction, test_Direction)) %>%
group_by(k, l) %>%
summarize(accuracy = mean(prediction == oracle))
:
Source: local data frame [8 x 3]
Groups: k [?]
k l accuracy
<int> <int> <dbl>
1 2 0 0.5396825
2 2 1 0.5277778
3 3 0 0.5317460
4 3 1 0.5317460
5 4 0 0.5277778
6 4 1 0.5357143
7 5 0 0.4841270
8 5 1 0.4841270