# mlr fits and condvis2

#### Catherine B. Hurley

#### 2022-09-14

`mlr` is a R package that offers a unified interface to machine learning models. By writing an interface between `condvis2` and `mlr` a vast number of machine learning fits may be explored with `condvis`. Presently, regression, classification and clustering varieties of `mlr` learners work with `condvis’.

A list of models supported by `mlr` is found on this link: https://mlr.mlr-org.com/articles/tutorial/integrated_learners.html

## Regression

Set up the task, learner and train the model.

    library(mlr)
    library(MASS)
    library(condvis2)
    Boston1 <- Boston[,9:14]

    rtask <- makeRegrTask(id = "bh", data = Boston1, target = "medv")
    rmod <- train(makeLearner("regr.lm"), rtask)
    rmod1 <- train(makeLearner("regr.fnn"), rtask)

Use condvis to explore the models:

    condvis(Boston1, model=list(rmod,rmod1), response="medv", sectionvars="lstat")

Choose tour “Diff fits” to explore differences between the fits

Some tasks, for example linear regression, support standard errors and so confidence intervals. This option needs to be added to `makeLearner`. Then, tell `condvis` to plot an interval using `pinterval="confidence` for that fit.

    rmod <- train(makeLearner("regr.lm", predict.type="se"), rtask)
    condvis(Boston1, model=rmod, response="medv", sectionvars="lstat", predictArgs=list(list(pinterval="confidence")))

## Classification

Set up the task, learner and train the model.

    cltask = makeClassifTask(data = iris, target = "Species")
    cllrn = makeLearner("classif.lda",predict.type = "prob") # need predict.type ="probs" to get probs
    clmod = train(cllrn, cltask)

Explore with `condvis`:

    condvis(iris, model=clmod, response="Species", sectionvars=c("Petal.Length", "Petal.Width"), pointColor="Species")

Click on “Show probs” to see class probabilities.

## Clustering

    ctask = makeClusterTask(data = iris[,-5])
    clrn = makeLearner("cluster.kmeans")
    cmod = train(clrn, ctask)

Add the predicted class to the data to act as the response:

    library(dplyr)
    iris1 <- iris

    iris1$pclass <- cmod %>%
      predict(newdata=iris[,-5]) %>%
      getPredictionResponse() %>%
        as.factor()

    condvis(data = iris1, model = cmod,
            response="pclass",
            sectionvars=c("Petal.Length", "Petal.Width"),
            conditionvars=c("Sepal.Length", "Sepal.Width"),pointColor="Species"
    )