---
title: "Input Processing"
output:
    pdf_document:
        fig_height: 4
---

# Data Inspection

```{r,cache=TRUE}
data = iris
data$Sepal.Length[sample(1:150, 20)] = NA
data$Sepal.Width[sample(1:150, 20)] = NA
data$Petal.Length[sample(1:150, 20)] = NA
data$Petal.Width[sample(1:150, 20)] = NA
data$foo = 1
data$bar = 2
head(data)

library(Amelia)
missmap(data)

library(GGally)
ggpairs(data)

plot(data)
```

# Imputing missing values

## Learner with missing values

```{r,error=TRUE}
library(mlr)

task = makeClassifTask(data = data, target = "Species")
learner = makeLearner("classif.randomForest")
rdesc = makeResampleDesc("CV")

resample(learner, task, rdesc)
```

## Imputing

```{r,error=TRUE}
learner = makeImputeWrapper("classif.randomForest",
    classes = list(numeric = imputeMedian()))

resample(learner, task, rdesc)
```

# Imputing with dummy columns

```{r,error=TRUE}
learner = makeImputeWrapper("classif.randomForest",
    classes = list(numeric = imputeMedian()),
    dummy.classes = c("numeric"))

resample(learner, task, rdesc)
```

# Removing constant features

```{r}
head(getTaskData(task))
task = removeConstantFeatures(task)
head(getTaskData(task))
```

# Unbalanced classes

```{r}
task = makeClassifTask(data = iris[51:102,], target = "Species")
table(getTaskTargets(task))
learner = makeLearner("classif.rpart")
res = resample(learner, task, rdesc)
table(getPredictionResponse(getRRPredictions(res)))
```

## Undersampling

```{r}
learner.undersample = makeUndersampleWrapper(learner, usw.rate = .03)
res = resample(learner.undersample, task, rdesc)
table(getPredictionResponse(getRRPredictions(res)))
```

## Oversampling

```{r}
learner.oversample = makeOversampleWrapper(learner, osw.rate = 10)
res = resample(learner.oversample, task, rdesc)
table(getPredictionResponse(getRRPredictions(res)))
```
