I am working to get more familiar with {tidymodels} and have run into an issue with a knn model using {kknn}. I posted this on StackOverflow yesterday, but it's not getting any interest over there (<20 views).
I would like to use knn regression to predict multiple outcomes, but I get a single output variable. Here's a reprex:
library(tidyverse)
library(tidymodels)
y1 <- rnorm(100, 5)
y2 <- rnorm(100, 6)
y3 <- rnorm(100, 7)
x1 <- rnorm(100, 8)
x2 <- rnorm(100, 12)
x3 <- rnorm(100, 6)
dat <- tibble(y1, y2, y3, x1, x2, x3)
data_split <- initial_split(dat)
train_data <- training(data_split)
form <- formula(y1 + y2 + y3 ~ x1 + x2 + x3)
rec <- recipe(form, data = dat) %>%
step_normalize(all_predictors()) %>%
prep()
model <-
nearest_neighbor() %>%
set_engine("kknn") %>%
set_mode("regression") %>%
set_args(dist_power = 2, neighbors = 5)
wflow <- workflow() %>%
add_model(model) %>%
add_recipe(rec)
fit1 <- fit(wflow, train_data)
predict(fit1, new_data = testing(data_split))
# Version info
── Attaching packages ──────────────────────────────────────────────── tidymodels 0.1.1 ──
✓ broom 0.7.2 ✓ recipes 0.1.14
✓ dials 0.0.9 ✓ rsample 0.0.8
✓ infer 0.5.3 ✓ tune 0.1.1
✓ modeldata 0.1.0 ✓ workflows 0.2.1
✓ parsnip 0.1.4 ✓ yardstick 0.0.7
I was attempting to predict y1, y2, y3 rather than a single output. Any suggestions?