---
title: "Analyse discriminante"
output: pdf_document
---

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

# Donnees

```{r}
data(iris)
n <- 100
x <- scale(iris[1:n,1:2])
y <- as.numeric(iris[1:n,5])-1
fisher <- data.frame(y=as.factor(y),x1=x[,1],x2=x[,2])
# 0 = setosa
# 1 = versicolor
plot(x,col=y+1)
```

# Analyses discriminantes lineaire et quadratique

```{r}
library(MASS)
model.lda <- lda(y~x1+x2,data=fisher)
model.qda <- qda(y~x1+x2,data=fisher)
```


```{r}
len <- 50
x1 <- seq(min(x[,1]),max(x[,1]),length=len)
x2 <- seq(min(x[,2]),max(x[,2]),length=len)
grille <- expand.grid(x1=x1,x2=x2)
affect.lda <- as.numeric(predict(model.lda,grille)$class)-1
affect.qda <- as.numeric(predict(model.qda,grille)$class)-1
plot(x,col=y+1)
points(grille,col=affect.lda+1,pch="+")
plot(x,col=y+1)
points(grille,col=affect.qda+1,pch="+")
contour(x1,x2,matrix(affect.lda,len,len),levels=0.5)
contour(x1,x2,matrix(affect.qda,len,len),levels=0.5,add=TRUE,col="red")
```


```{r}
library(caret)
model.lda <- train(y~.,data=fisher,method="lda",trControl = trainControl(method="repeatedcv",number=5,repeats=100))
model.qda <- train(y~.,data=fisher,method="qda",trControl = trainControl(method="repeatedcv",number=5,repeats=100))

```

