---
title: "Model performance audit"
author: "Alicja Gosiewska"
date: "`r Sys.Date()`"
output: html_document
vignette: >
  %\VignetteEngine{knitr::knitr}
  %\VignetteIndexEntry{Model performance audit}
  %\usepackage[UTF-8]{inputenc}
---


```{r setup, echo = FALSE}
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
```


## Data

To illustrate application of `auditor` we will use dataset "dragons" available in the [`DALEX`](https://github.com/ModelOriented/DALEX) package. The dataset contains characteristics of fictional creatures (dragons), like year of birth, height, weight, etc (see below). The goal is to predict the length of life of dragons (a regression problem). 

```{r}
library(DALEX)
data(dragons)
head(dragons)
```


## Models
First, we need models to compare. We selected linear regression and random forest because of their different structures. Linear regression model linear relationships between target response and independent variables. While random forest should be able to capture also non-linear relationships between variables.

```{r}
# Linear regression
lm_model <- lm(life_length ~ ., data = dragons)

# Random forest
library(randomForest)
set.seed(59)
rf_model <- randomForest(life_length ~ ., data = dragons)
```


## Preparation for analysis of performance

Analysis begins with creation of an explainer object with `explain` function from `DALEX` package. Explainer wraps a model with its meta-data, such as dataset that was used for training or observed response.

```{r results = 'hide'}
lm_exp <- DALEX::explain(lm_model, label = "lm", data = dragons, y = dragons$life_length)
rf_exp <- DALEX::explain(rf_model, label = "rf", data = dragons, y = dragons$life_length)
```

Next step requires creation of `model_performance` objects of each explained model. The function computes chosen performance measures for passed model. 

```{r}
library(auditor)
lm_mp <- model_performance(lm_exp)
rf_mp <- model_performance(rf_exp)

lm_mp
```



## Model ranking radar plot

Model performance measures may be plotted together to easily compare model performances. A result further from the center of the plot means a better model performance. Parameter `verbose` indicates whether the table with scores should be generated. On the plot scores are inversed and scaled to [0,1].

```{r}
plot(lm_mp, rf_mp)
# alternative:
# plot_radar(lm_mp, rf_mp, verbose = FALSE)

```

There is a possibility to define functions with custom model performance measure.

```{r}
new_score <- function(object) sum(sqrt(abs(object$residuals)))

lm_mp <- model_performance(lm_exp,  
                          score = c("mae", "mse", "rec", "rroc"), 
                          new_score = new_score)

rf_mp <- model_performance(rf_exp,  
                          score = c("mae", "mse", "rec", "rroc"), 
                          new_score = new_score)

plot(lm_mp, rf_mp)
```


## Other methods

Other methods and plots are described in vignettes: 

* [Model residuals audit](https://modeloriented.github.io/auditor/articles/model_residuals_audit.html)
* [Model evaluation audit](https://modeloriented.github.io/auditor/articles/model_evaluation_audit.html)
* [Model fit audit](https://modeloriented.github.io/auditor/articles/model_fit_audit.html)
* [Observation influence audit](https://modeloriented.github.io/auditor/articles/observation_influence_audit.html)
