JeremyGelb / geocmeans

An R package to perform Spatial Fuzzy C-means
GNU General Public License v2.0
27 stars 2 forks source link

Running Advanced Example raises error #7

Closed Naeemkh closed 1 year ago

Naeemkh commented 1 year ago

Please take a look at the following example. The first block runs successfully, however, the second block raises the following error:

https://jeremygelb.github.io/geocmeans/articles/web_vignettes/advanced_examples.html

> # classical fuzzy cmeans
> result1 <- CMeans(df, 2, 1.5, standardize = TRUE, robust = FALSE,  verbose = FALSE, seed = 176)
Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric
JeremyGelb commented 1 year ago

Dear @Naeemkh,

The error is caused because the chunk of code generating the dataset for this example is hiden from the user. I guess that in your case, you have been using the object df from the previous chunk of code.

So, this full example should work :

library(MASS)

parameters <- list(
  c(150, 0,0.5,0.5,0.75,0.75),
  c(150, 0, 5,-3,5,0.5)
)

all_df <- lapply(parameters, function(x){
  rho <- x[[2]]
  mu <- c(x[[3]],x[[4]])
  s1 <- x[[5]]
  s2 <- x[[6]]
  sigma <- matrix(c(s1**2, s1*s2*rho, s1*s2*rho, s2**2),2)
  gp <- mvrnorm(x[[1]], mu = mu, Sigma = sigma )
  return(gp)
})

df <- data.frame(do.call(rbind, all_df))
names(df) <- c("X","Y")

# preparing the functions to scale and unscale the data
data_scales <- standardizer(df)

# classical fuzzy cmeans
result1 <- CMeans(df, 2, 1.5, standardize = TRUE, robust = FALSE,  verbose = FALSE, seed = 176)

# plotting the membership matrix
plots <- lapply(1:2, function(i){
  df2 <- df
  df2$proba <- result1$Belongings[,i]
  df3 <- data.frame(result1$Centers)
  names(df3) <- c("X","Y")
  df3$X <- data_scales$X$unscale(df3$X)
  df3$Y <- data_scales$Y$unscale(df3$Y)
  myplot <- ggplot(df2) + 
    geom_point(aes(x = X, y = Y, color = proba)) + 
    geom_point(data = df3, mapping = aes(x = X, y = Y), color = "red") +
    labs(color = paste0("group ",i))
  return(myplot)
})

ggarrange(plotlist = plots, ncol = 1, nrow = 2)

I will edit the vignette to show the missing part of the code.

JeremyGelb commented 1 year ago

This missing par of the code can now be seen by the user. see here : https://jeremygelb.github.io/geocmeans/articles/web_vignettes/advanced_examples.html#robust-fcm