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

# Data Set -- Iris

```{r}
library(mlr)
task = makeClusterTask(data = iris[,-5])
task
```

# k Means

```{r}
learner = makeLearner("cluster.kmeans", par.vals = list(centers = 3))
model = train(learner, task)
model
```

```{r}
preds = predict(model, task)
preds
```

```{r}
plotLearnerPrediction(learner, task, features = c("Petal.Length", "Petal.Width"))
```

## Predicting Membership Probabilities

```{r}
learner = makeLearner("cluster.kmeans", predict.type = "prob", par.vals = list(centers = 3))
model = train(learner, task)
model
```

```{r}
preds = predict(model, task)
preds
```

```{r}
plotLearnerPrediction(learner, task, features = c("Petal.Length", "Petal.Width"))
```


## Different Number of Clusters

```{r}
learner = makeLearner("cluster.kmeans", par.vals = list(centers = 2))
model = train(learner, task)
preds = predict(model, task)
preds
```

```{r}
plotLearnerPrediction(learner, task, features = c("Petal.Length", "Petal.Width"))
```

```{r}
learner = makeLearner("cluster.kmeans", par.vals = list(centers = 10))
model = train(learner, task)
preds = predict(model, task)
preds
```

```{r}
plotLearnerPrediction(learner, task, features = c("Petal.Length", "Petal.Width"))
```

# Expectation Maximization

```{r}
learner = makeLearner("cluster.EM")
model = train(learner, task)
model
```

```{r}
preds = predict(model, task)
preds
```

```{r}
plotLearnerPrediction(learner, task, features = c("Petal.Length", "Petal.Width"))
```

# DBScan

```{r}
learner = makeLearner("cluster.dbscan")
model = train(learner, task)
model
```

```{r}
preds = predict(model, task)
preds
```

```{r}
plotLearnerPrediction(learner, task, features = c("Petal.Length", "Petal.Width"))
```

# More -- survival analysis

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

getRRPredictions(result)
```

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