--- 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) ``` ```{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") ```
Figure 1: Heatmap of a mlr KNN regression fit displaying 2-way interaction strength on the off diagonal and individual variable importance on the diagonal.
#### 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") ```
Figure 2: Generalized pairs partial dependence plot for a mlr KNN regression fit.