---
title: "Regression"
output:
    pdf_document:
        fig_height: 3
---

# Data Set -- Boston Housing

```{r}
library(mlr)

# mlr comes with example tasks
bh.task
```

```{r}
# this is what it looks like...
head(getTaskData(bh.task))
```

```{r}
# ...and this is what it looks like plotted
library(ggplot2)
ggplot(getTaskData(bh.task), aes(lstat, rm)) +
    geom_point(aes(color = medv))
```

# Linear Model

```{r}
learner = makeLearner("regr.lm")
rdesc = makeResampleDesc(method = "Holdout", split = 2/3)
result = resample(learner, bh.task, rdesc, models = TRUE)

getRRPredictions(result)
```

```{r}
getLearnerModel(result$models[[1]])
```

```{r}
plot(getLearnerModel(result$models[[1]]))
```

```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat"))
```


```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat", "rm"))
```

# Regression Splines

```{r}
learner = makeLearner("regr.earth")
result = resample(learner, bh.task, rdesc, models = TRUE)

getRRPredictions(result)
```

```{r}
getLearnerModel(result$models[[1]])
```

```{r}
plot(getLearnerModel(result$models[[1]]))
```

```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat"))
```

```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat", "rm"))
```

# Boosting

```{r}
learner = makeLearner("regr.blackboost")
result = resample(learner, bh.task, rdesc, models = TRUE)

getRRPredictions(result)
```

```{r}
getLearnerModel(result$models[[1]])
```

```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat"))
```

```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat", "rm"))
```

# Support Vector Machine

```{r}
learner = makeLearner("regr.ksvm")
result = resample(learner, bh.task, rdesc, models = TRUE)

getRRPredictions(result)
```

```{r}
getLearnerModel(result$models[[1]])
```

```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat"))
```

```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat", "rm"))
```

# Regression Forests

```{r}
learner = makeLearner("regr.randomForest")
result = resample(learner, bh.task, rdesc, models = TRUE)

getRRPredictions(result)
```

```{r}
getLearnerModel(result$models[[1]])
```

```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat"))
```

```{r}
plotLearnerPrediction(learner, bh.task, features = c("lstat", "rm"))
```

```{r}
imp = getFeatureImportance(result$models[[1]])
imp
sort(imp$res)
```

# More

```{r}
getLearnerProperties(learner)
getTaskDesc(bh.task)
getTaskType(bh.task)
```
