In this vignette, we want to show how to create a FCMres
object from results obtained with other classification or clustering
methods. This can be very useful to compare results with methods
available in other packages. We give a practical example here with the
hclust
function.
We start by clustering the observations in the LyonIris
dataset with the hclust
function and retaining 4
groups.
library(geocmeans)
library(tmap)
library(dplyr)
library(ggplot2)
library(spdep)
library(raster)
data("LyonIris")
# selecting the columns for the analysis
<-c("Lden","NO2","PM25","VegHautPrt","Pct0_14",
AnalysisFields "Pct_65","Pct_Img","TxChom1564","Pct_brevet","NivVieMed")
# rescaling the columns
<- LyonIris@data[AnalysisFields]
Data for (Col in names(Data)){
<- scale(Data[[Col]])
Data[[Col]]
}
# applying the hclust function
<- hclust(dist(Data), method = "ward")
clust
# getting the groups
$Hclust_groups <- as.character(cutree(clust, k = 4))
LyonIris$Hclust_groups <- as.character(cutree(clust, k = 4))
Data
# mapping the groups
tm_shape(LyonIris) +
tm_polygons(col = "Hclust_groups", title = "groups")
Now, if we want to use the functions provided by
geocemans, we must create a FCMres
object
manually. This is basically a list with some required slots:
In this case, we calculate the centres of the groups as the mean of each variable in each group.
<- Data %>%
centers group_by(Hclust_groups) %>%
summarise_all(mean)
<- as.matrix(centers[2:ncol(centers)]) centers
The membership matrix is a simple binary matrix. We can create it
with the function cat_to_belongings
.
<- cat_to_belongings(Data$Hclust_groups) member_mat
And we can now create our FCMres object.
$Hclust_groups <- NULL
Data
<- FCMres(list(
hclustres "Centers" = centers,
"Belongings" = member_mat,
"Data" = Data,
"m" = 1,
"algo" = "hclust"
))
It is now possible to use almost all the functions in the geocmeans package to investigate the results.
# quick summaries about the groups
summary(hclustres)
violinPlots(hclustres$Data, hclustres$Groups)
spiderPlots(hclustres$Data, hclustres$Belongings)
mapClusters(LyonIris, hclustres)
# some indices about classification quality
calcqualityIndexes(hclustres$Data,
$Belongings,
hclustres$m)
hclustres
# spatial diagnostic
<- poly2nb(LyonIris,queen = TRUE)
Neighbours <- nb2listw(Neighbours,style="W",zero.policy = TRUE)
WMat spatialDiag(hclustres, nblistw = WMat)
# investigation with the shiny app
sp_clust_explorer(hclustres, spatial = LyonIris)
When working with raster data, a little more work must be done to
create a FCMres
object. We show here a complete example
with the Arcachon
dataset.
We start here by applying the k-means algorithm to a set of rasters.
data("Arcachon")
# loading each raster as a column in a matrix
# and scale each column
<- do.call(cbind, lapply(names(Arcachon), function(n){
all_data <- Arcachon[[n]]
rast return(raster::values(raster::scale(rast)))
}))
# removing the rows with missing values
<- complete.cases(all_data)
missing <- all_data[missing,]
all_data
# applying the kmeans algorithm with 7 groups
<- kmeans(all_data, 7) kmean7
We must now create three objects:
# creating Data (do not forget the standardization)
<- lapply(names(Arcachon), function(n){
Data <- Arcachon[[n]]
rast return(raster::scale(rast))
})names(Data) <- names(Arcachon)
# creating rasters
<- Arcachon[[1]]
ref_raster
<- lapply(1:7, function(i){
rasters # creating a vector with only 0 values
<- rep(0, ncell(ref_raster))
vals # filling it with values when the pixels are not NA
<- ifelse(kmean7$cluster == i,1,0)
vals[missing] # setting the values in a rasterLayer
<- ref_raster
rast ::values(rast) <- vals
rasterreturn(rast)
})
# creating centers
<- as.data.frame(all_data)
all_data names(all_data) <- names(Arcachon)
$kmean_groups <- as.character(kmean7$cluster)
all_data
<- all_data %>%
centers group_by(kmean_groups) %>%
summarise_all(mean)
<- as.matrix(centers[2:ncol(centers)]) centers
We can now create a FCMres object !
<- FCMres(list(
myFCMres "Data" = Data,
"Centers" = centers,
"rasters" = rasters,
"m" = 1,
"algo" = "kmeans"
))
And again, we can use the functions provided in geocmeans !
# quick summaries about the groups
summary(myFCMres)
violinPlots(myFCMres$Data, myFCMres$Groups)
spiderPlots(myFCMres$Data, myFCMres$Belongings)
mapClusters(object = myFCMres)
# some indices about classification quality
calcqualityIndexes(myFCMres$Data,
$Belongings,
myFCMres$m)
myFCMres
# spatial diagnostic
<- matrix(1, nrow = 3, ncol = 3)
w1 spatialDiag(myFCMres, window = w1, nrep = 5)
# investigation with the shiny app
sp_clust_explorer(myFCMres)