---
title: "mlr3 - k-nearest Neighbours"
output: html_document
vignette: >
  %\VignetteEncoding{UTF-8}
  %\VignetteIndexEntry{mlr3 - k-nearest Neighbours}
  %\VignetteEngine{knitr::rmarkdown}
editor_options:
  chunk_output_type: console
---


```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "vig/"
)
options(rmarkdown.html_vignette.check_title = FALSE)
```

<!-- avoid border around images -->
<style>
img {
  border: 0;
  }
</style>

```{r, message=FALSE}
library('vivid')
library("mlr3")
library("mlr3learners")
```

This guide is designed as a quick-stop reference of how to use some of the more popular machine learning R packages with `vivid`.  In the following examples, we use the air quality data for regression. 

### mlr3 - k-nearest Neighbours
The `mlr3` package in R offers a modern, object-oriented framework for machine learning tasks in R, providing tools for data preprocessing, model training, evaluation, and tuning. Here, we fit a k-nearest neighbours model to the data.

### Regression 
```{r, eval = F}
# load data
aq <- na.omit(airquality)

# Define a regression task
task <- TaskRegr$new(id = "airquality", backend = aq, target = "Ozone")


# Train a k-nearest neighbours model using mlr3
learner <- lrn("regr.kknn")
m3 <- learner$train(task)

# vivid
vi <- vivi(data = aq, fit = m3, response = 'Ozone')
```


#### Heatmap
```{r, mlr_r_heat,  out.width = '100%', eval = F}
viviHeatmap(mat = vi)
```


```{r, echo = F,  out.width = '100%'}
knitr::include_graphics("https://raw.githubusercontent.com/AlanInglis/vivid/master/vignettes/vig/mlr_r_heat-1.png")
```
<center>
<caption><span id="fig1:fig1">Figure 1: </span>Heatmap of a mlr KNN regression fit displaying 2-way interaction strength on the off diagonal and individual variable importance on the diagonal.</caption>
  </center>

#### PDP
```{r, mlr_r_pdp, out.width='100%', eval = F}
pdpPairs(data = aq, 
         fit =  m3, 
         response = "Ozone", 
         nmax = 50, 
         gridSize = 4,         
         nIce = 10)
```



```{r, echo = F,  out.width = '100%'}
knitr::include_graphics("https://raw.githubusercontent.com/AlanInglis/vivid/master/vignettes/vig/mlr_r_pdp-1.png")
```
<center>
<caption><span id="fig2:fig2">Figure 2: </span>Generalized pairs partial dependence plot for a mlr KNN regression fit.</caption>
</center>




<!-- ### Classification -->
<!-- ```{r, eval = T} -->
<!-- library("mlr3") -->

<!-- # Define a classification task with iris dataset -->
<!-- task <- TaskClassif$new(id = "iris", backend = iris, target = "Species") -->

<!-- # Train a k-nearest neighbours model using mlr3 -->
<!-- learner <- lrn("classif.kknn", predict_type = 'prob') -->
<!-- m3 <- learner$train(task) -->


<!-- # vivid -->
<!-- vi <- vivi(data = iris, fit = m3, response = 'Species') -->
<!-- ``` -->



<!-- #### Heatmap -->
<!-- ```{r, mlr_c_heat,  out.width = '100%', eval = T} -->
<!-- viviHeatmap(mat = vi) -->
<!-- ``` -->


<!-- ```{r, echo = F,  out.width = '100%'} -->
<!-- knitr::include_graphics("https://raw.githubusercontent.com/AlanInglis/vivid/master/vignettes/vig/mlr_c_heat-1.png") -->
<!-- ``` -->
<!-- <center> -->
<!-- <caption><span id="fig3:fig3">Figure 3: </span>Heatmap of a mlr KNN classification fit displaying 2-way interaction strength on the off diagonal and individual variable importance on the diagonal.</caption> -->
<!--   </center> -->

<!-- #### PDP -->
<!-- ```{r, mlr_c_pdp, eval = F} -->
<!-- pdpPairs(data = iris,  -->
<!--          fit =  m3,  -->
<!--         response = 'Species',  -->
<!--         class = 'setosa', -->
<!--          nmax = 50,  -->
<!--          gridSize = 4,          -->
<!--          nIce = 10) -->

<!-- ``` -->

<!-- ```{r, echo = F,  out.width = '100%'} -->
<!-- knitr::include_graphics("https://raw.githubusercontent.com/AlanInglis/vivid/master/vignettes/vig/mlr_c_pdp-1.png") -->
<!-- ``` -->
<!-- <center> -->
<!-- <caption><span id="fig4:fig4">Figure 4: </span>Generalized pairs partial dependence plot for a mlr KNN classification fit.</caption> -->
<!-- </center> -->


