\subsection{Aplicação a dados independentes}

Nesta secao nos aplicamos o SKATER a dados 
independentes de referencia espacial e temporal. 
Nos aplicamos aos dados ``iris'', 
``Edgar Anderson's Iris Data''.

     This famous (Fisher's or Anderson's) iris data set gives the
     measurements in centimeters of the variables sepal length and
     width and petal length and width, respectively, for 50 flowers
     from each of 3 species of iris.  The species are: Iris setosa,
     versicolor, and virginica.

Nos comparamos o resultado do skater com 3 metodos 
hierarquicos e 3 metodos partitioning de analise 
de cluster, disponiveis no pacote \pkg{cluster} do 
\pkg{R}. 

\begin{Schunk}
\begin{Sinput}
> data(iris)
> n <- nrow(dpad <- scale(iris[, 1:4]))
\end{Sinput}
\end{Schunk}

 each observation is neighboor of all other
\begin{Schunk}
\begin{Sinput}
> nb.ir <- lapply(1:n, function(i) as.integer((1:n)[-i]))
> class(nb.ir) <- "nb"
\end{Sinput}
\end{Schunk}

\begin{Schunk}
\begin{Sinput}
> require(spdep)
\end{Sinput}
\end{Schunk}

 calculing costs with Euclidean distance between observations
\begin{Schunk}
\begin{Sinput}
> e.costs.ir <- nbcosts(nb.ir, dpad)
\end{Sinput}
\end{Schunk}

\begin{Schunk}
\begin{Sinput}
> e.nbw.ir <- nb2listw(nb.ir, e.costs.ir, style = "B")
\end{Sinput}
\end{Schunk}

 find a minimum spanning tree
\begin{Schunk}
\begin{Sinput}
> e.mst.ir <- mstree(e.nbw.ir)
\end{Sinput}
\end{Schunk}

 generating 3 groups
\begin{Schunk}
\begin{Sinput}
> system.time(res1 <- skater(e.mst.ir[, 1:2], dpad, 2))
\end{Sinput}
\begin{Soutput}
   user  system elapsed 
  2.616   0.012   2.632 
\end{Soutput}
\begin{Sinput}
> table(res1$gr, iris[, 5])
\end{Sinput}
\begin{Soutput}
    setosa versicolor virginica
  1      0          8        31
  2     50          0         0
  3      0         42        19
\end{Soutput}
\end{Schunk}


 Hierarquical cluster analysis
\begin{Schunk}
\begin{Sinput}
> e.dist <- dist(dpad)
> h3 <- cutree(hclust(e.dist), 3)
\end{Sinput}
\end{Schunk}

 other algorithims
\begin{Schunk}
\begin{Sinput}
> require(cluster)
\end{Sinput}
\end{Schunk}

Hierarquical

 AGNES: Computes agglomerative hierarchical clustering of the dataset.
\begin{Schunk}
\begin{Sinput}
> ag3 <- cutree(agnes(e.dist), 3)
\end{Sinput}
\end{Schunk}
 DIANA: Computes a divisive hierarchical clustering of the dataset
     returning an object of class 'diana'.
\begin{Schunk}
\begin{Sinput}
> di3 <- cutree(diana(e.dist), 3)
\end{Sinput}
\end{Schunk}

 Partitioning Methods
 PAM: Partitioning Around Medoids
\begin{Schunk}
\begin{Sinput}
> pam3 <- pam(e.dist, 3)$clust
\end{Sinput}
\end{Schunk}
 CLARA: Clustering Large Applications 

\begin{Schunk}
\begin{Sinput}
> cla3 <- clara(e.dist, 3)$clust
\end{Sinput}
\end{Schunk}

 FANNY: Computes a fuzzy clustering of the data into 'k' clusters. 
\begin{Schunk}
\begin{Sinput}
> fan3 <- fanny(e.dist, 3)$clust
\end{Sinput}
\end{Schunk}

 Funcao para reagrupar, para fazer comparacao
\begin{Schunk}
\begin{Sinput}
> reagr <- function(gr, ref) {
+     tab <- table(gr, ref)
+     mx <- apply(tab, 2, function(x) max(x/sum(x)))
+     if (any(duplicated(mx))) 
+         tab <- t(tab)
+     id <- apply(tab, 2, function(x) which.max(x/sum(x)))
+     tab <- rbind(tab[id, ], tab[-id, ])
+     tab <- cbind(tab, matrix(0, nrow(tab), nrow(tab) - ncol(tab)))
+     if (any(duplicated(mx))) 
+         tab <- t(tab)
+     class(tab) <- "table"
+     tab
+ }
\end{Sinput}
\end{Schunk}

 fazendo as tabelas
\begin{Schunk}
\begin{Sinput}
> lapply(data.frame(res1$gr, ag3, di3, pam3, cla3, fan3), function(x) reagr(x, 
+     iris[, 5]))
\end{Sinput}
\begin{Soutput}
$res1.gr
  setosa versicolor virginica
2     50          0         0
3      0         42        19
1      0          8        31

$ag3
  setosa versicolor virginica
1     50          0         0
2      0         50        47
3      0          0         3

$di3
  setosa versicolor virginica
1     50          0         0
3      0         39        17
2      0         11        33

$pam3
  setosa versicolor virginica
1     50          0         0
3      0         41        14
2      0          9        36

$cla3
  setosa versicolor virginica
1     50          0         0
3      0         45        13
2      0          5        37

$fan3
  setosa versicolor virginica
1     50          0         0
3      0         38        11
2      0         12        39
\end{Soutput}
\end{Schunk}
 calculando a proporco de elementos na diagonal 
\begin{Schunk}
\begin{Sinput}
> apply(cbind(ska = res1$gr, ag3, di3, pam3, cla3, fan3), 2, function(x) sum(diag(reagr(x, 
+     iris[, 5])))/n)
\end{Sinput}
\begin{Soutput}
      ska       ag3       di3      pam3      cla3      fan3 
0.8200000 0.6866667 0.8133333 0.8466667 0.8800000 0.8466667 
\end{Soutput}
\begin{Sinput}
> sum(diag(table(h3, iris[, 5])))/n
\end{Sinput}
\begin{Soutput}
[1] 0.7866667
\end{Soutput}
\end{Schunk}

 Observamos que o desempenho do skater compara-se 
 ao depempenho dos Partitioning Methods e ganha dos 
 metodos hierarquicos 
