Para acabar el iris data set
========================================================
Cargamos nuestro querido iris data set (notemos que no hay valores perdidos.)
```{r}
data(iris)
str(iris)
```
Estadísticos descriptivos muy básicos:

```{r}
desc.est.iris=aggregate(iris[,c(1:4)],
                        by=list(Tipo=iris$Species),FUN=
function(x) {
c(mean = mean(x,na.rm=TRUE), sd = sd(x,na.rm=TRUE),max=min(x,na.rm=TRUE),min=min(x,na.rm=TRUE),n=length(na.omit(x)),missing=length(x)-length(na.omit(x))) } ,simplify=TRUE)
desc.est.iris
```

```{r fig.width=7, fig.height=6}
#par(mfrow=c(2,2))
for(k in c(1:4)){
  boxplot(iris[,k]~iris$Species,main=paste(names(iris)[k+1]," by Species"))
}
#par(mfrow=c(1,1))

```


```{r fig.width=7, fig.height=6}
cor(iris[,1:4])
```
```{r fig.width=7, fig.height=6}
pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species",pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)])
```
```{r fig.width=7, fig.height=6}
panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...)
{
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r <- abs(cor(x, y))
    txt <- format(c(r, 0.123456789), digits=digits)[1]
    txt <- paste(prefix, txt, sep="")
    if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
    text(0.5, 0.5, txt, cex = cex.cor * r)
}
panel.hist <- function(x, ...)
{
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(usr[1:2], 0, 1.5) )
    h <- hist(x, plot = FALSE)
    breaks <- h$breaks; nB <- length(breaks)
    y <- h$counts; y <- y/max(y)
    rect(breaks[-nB], 0, breaks[-1], y, col="cyan", ...)
}

pairs(iris[,1:4], lower.panel=panel.smooth, upper.panel=panel.cor,   diag.panel=panel.hist,pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)])
```



Clustering
```{r}
kmeans3=kmeans(iris[,1:4],centers=3,algorithm="Lloyd")
str(kmeans3)
kmeans3$cluster
pairs(iris[1:4], main = "Anderson's Iris Data -- 3 kmeans partition",pch = 21, bg = c("red", "green3", "blue")[unclass(kmeans3$cluster)])
```

Matriz de confusión (confusion matrix)
```{r}
table(iris$Species,kmeans3$cluster)
```

Clustering jerárquico:

```{r}
jerarquico=hclust(dist(iris[,1:4],method = "euclidean"),method="average")
plot(jerarquico)

cluster3.h=cutree(h,3)
cluster3.h
```

No se ve bien 

```{r}
plot(jerarquico,labels=iris$Species)
plot(jerarquico,labels=kmeans3$cluster)
```

Componentes principales


```{r}
sol.pca=princomp(iris[,1:4],cor=TRUE)
summary(sol.pca)
screeplot(sol.pca)
biplot(sol.pca)
```

¿Como representar mejor el círculo de correlación?....

Visualización de todos  los componentes por species

```{r}
pairs(sol.pca$scores[,c(1:4)],pch = 21, bg = 
c("red", "green3", "blue")[unclass(iris$Species)])
```

Y ahora por las 3 clases de kmeans

```{r}
pairs(sol.pca$scores[,c(1:4)],pch = 21, bg = 
c("red", "green3", "blue")[unclass(kmeans3$cluster)])
```










