knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "60%",
  fig.align = "center"
)
# options(tibble.print_min=5, tibble.print_max=5)

FCMm

CRAN status Lifecycle: stable Launch binder R-CMD-check

Author: Shun Bi
Date: r Sys.Date()
Version: r gsub("Version: ","",readLines('DESCRIPTION')[stringr::str_which(readLines('DESCRIPTION'), 'Version')])
E-mail: r 'bishun1994@foxmail.com'

Overview

FCMm is a package for fuzzy clustering water spectra (or called water color). Given that the most of water color spectra data sets are considered as the high dimensional set, the advantage of this method is making FCM assign the membership (sum as 1) harder, ensuring the desired water type are restricted to its belongings (not too soft). It is possible to cluster the harm algal bloom water type which can not be produced by FCM with m=2.

Installation

The GitHub version requires the package devtools

# install it from GitHub quickly by `devtools` package
devtools::install_github('bishun945/FCMm')

# or use `remotes` package
remotes::install_github('bishun945/FCMm')

# install it and build vignettes 
devtools::install_github('bishun945/FCMm', build_vignettes=TRUE)

Usage

1. Classification of water spectra based on pre-defined cluster centers

This chunk introduces the usage of applying the FCMm package (and its built-in centrodis) to cluster water spectra. The details of obtaining the centrodis could be found in Bi et al. (2019). Also, run vignette("Builtin_centrodis") to see more about this application.

# Load testing data
library(FCMm)
library(ggplot2)
data("WaterSpec35")
data("Bi_clusters")
Rrs <- WaterSpec35[,3:17]
# Plot the spectra
plot_spec_from_df(Rrs) + 
  labs(x='Wavelength (nm)',y=expression(Rrs~(sr^-1))) + 
  theme_bw() + 
  theme(legend.position='none', text=element_text(size=13))
# Applying FCMm
result <- apply_FCM_m(Rrs=Rrs, option.plot=TRUE, do.stand=TRUE)
plot(result$p.group + theme(text=element_text(size=13)))
# plot(result$p.group+facet_wrap(~cluster, ncol=2))

2. Algorithms blending via membership values from FCMm (version of Bi et al., 2019)

library(magrittr)
dt_Chla <- FCM_m_Chla_estimation(Rrs=data.frame(Rrs665=Rrs$`665`,
                                                Rrs709=Rrs$`708.75`,
                                                Rrs754=Rrs$`753.75`),
                                 U=result$u)
dt_Chla$cluster <- result$cluster %>% as.character
dt_Chla$Chla_true <- WaterSpec35$Chla

# oldoptions <- options(scipen=1000)

subset(dt_Chla, select=c('cluster','Chla_true','BR','TBA','Bloom','conc.Blend')) %>%
  reshape2::melt(., id=c('cluster','Chla_true')) %>%
  ggplot(data=.) + 
  geom_point(aes(x=Chla_true,y=value,group=cluster,color=cluster),
             alpha=0.8, size=4) +
  scale_x_log10(limits=c(1,900)) + 
  scale_y_log10(limits=c(1,900)) +
  scale_color_manual(values=RdYlBu(result$K)) + 
  labs(x='True value of Chla concentration (ug/L)',
       y='Estimated value of Chla concentration (ug/L)',
       color='Cluster') + 
  geom_abline(intercept=0, slope=1, linetype=2) + 
  facet_wrap(~variable, nrow=2) + 
  theme_bw() + 
  theme(axis.text.x.bottom = element_text(hjust=1),
        strip.background = element_blank())

# on.exit(options(oldoptions))

3. Assessment of Chla concentration algorithms

FCMm provides an useful function to assess the performance of algorithms, i.e., Assessment_via_cluster(). Users can decide to select the FCMm-supported error measures (metrics) by themselves (run cal.metrics.names() or cal.metrics.vector.names() to see what metrics could be used). A more detailed vignette is given to introduce the usage of the assessment work (run vignette("Assessment") to see more).

pred = dt_Chla[,c("BR","TBA","Bloom","conc.Blend")] %>% 
  setNames(., c("BR","TBA","Bloom","Blend"))
meas = dt_Chla[,"Chla_true"]
memb = dt_Chla[,paste0("M.", 1:7)] %>% 
  setNames(., 1:7)
Assess_soft <- Assessment_via_cluster(pred = pred,
                                      meas = meas,
                                      memb = memb,
                                      metrics = c("MAE", "MAPE"),
                                      log10 = TRUE,
                                      total = TRUE,
                                      hard.mode = FALSE,
                                      cal.precision = FALSE,
                                      na.process = TRUE,
                                      plot.col = TRUE)
Assess_soft$res_plot_facet
knitr::kable(Assess_soft$MAE %>% round(3))
knitr::kable(Assess_soft$MAPE %>% round(2))

4. Algorithms blending frameworks (updated in Feb 22, 2021)

data(WaterSpec35)
res_Jac17 = Blend_Jac17(WaterSpec35[, -c(1, 2)])
res_Moo14 = Blend_Moo14(WaterSpec35[, -c(1, 2)])
res_Bi21  = Blend_Bi21(WaterSpec35[, -c(1, 2)])
res_BiPHD = Blend_FCMm(WaterSpec35[, -c(1, 2)])

dt_compare <- data.frame(
  Chla_true = WaterSpec35$Chla,
  Jac17  = res_Jac17$Chla_blend,
  Moo14  = res_Moo14$Chla_blend,
  Bi21   = res_Bi21$Chla_blend,
  BIPHD  = res_BiPHD$Chla_blend,
  BIPHD2 = res_BiPHD$Chla_reparam
) %>% reshape2::melt(., id = "Chla_true")

er_agg <- function(x) {
  stats::aggregate(x, list(dt_compare$variable), mean)[, 2]
}
dt_compare$er1 <- dt_compare$Chla_true - dt_compare$value
dt_compare$er2 <- (dt_compare$Chla_true - dt_compare$value) / dt_compare$Chla_true * 100

er_df <- data.frame(
  BIAS = er_agg(dt_compare$er1),
  MAE  = er_agg(abs(dt_compare$er1)),
  MRPE = er_agg(dt_compare$er2),
  MAPE = er_agg(abs(dt_compare$er2))
) %>% cbind(variable = levels(dt_compare$variable), .)
er_df$label <- sprintf("BIAS=%.3f\nMAE=%.3f\nMRPE=%.2f%%\nMAPE=%.2f%%", 
                       er_df$BIAS, er_df$MAE, er_df$MRPE, er_df$MAPE)

ggplot(dt_compare) + 
  geom_point(aes(x = Chla_true, y = value, fill = variable),
             size = 2.5, alpha = 0.8, color = "black", 
             shape = "circle filled") +
  geom_abline(slope = 1, intercept = 0, linetype = 2) + 
  geom_text(inherit.aes = FALSE, data = er_df,
            aes(x = min(dt_compare$Chla_true), y=max(dt_compare$Chla_true),
                label = label), vjust = 1, hjust = 0) + 
  facet_wrap(~variable) + 
  labs(y="predictions") + 
  scale_x_log10() + 
  scale_y_log10()

Getting help

References



bishun945/FCMm documentation built on Oct. 15, 2021, 6:43 p.m.