Skip to content

Commit

Permalink
fix cluster ids (#239)
Browse files Browse the repository at this point in the history
* fix cluster ids

* extract logic into function

* fix name of df

---------

Co-authored-by: Antonia Runge <antonia.runge@inwt-statistics.de>
  • Loading branch information
f-lukas and arunge authored Aug 16, 2024
1 parent ee9858f commit 0aa1bb8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: DSSM
Title: Pandora & IsoMemo spatiotemporal modeling
Version: 24.08.0
Version: 24.08.1
Authors@R: c(
person("Marcus", "Gross", email = "marcus.gross@inwt-statistics.de", role = c("cre", "aut")),
person("Antonia", "Runge", email = "antonia.runge@inwt-statistics.de", role = c("aut"))
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# DSSM 24.08.1

## Bug Fixes
- fixes cluster ids being non continuous in some cases (#238)

# DSSM 24.08.0

## New Features
Expand Down
5 changes: 5 additions & 0 deletions R/01-estimateMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,8 @@ estimateMapKernel <- function(data,
cluster_centers$cluster <- 1:nrow(cluster_centers)
data2 <- merge(data2, cluster_centers, sort = FALSE)
colnames(data2)[colnames(data2)=="cluster"] <- "spatial_cluster"

data2$spatial_cluster <- data2$spatial_cluster %>% makeClusterIdsContinuous()
}
if(!is.null(Weighting) & !(Weighting == "")){
model <- try(lapply(1:nSim, function(x){
Expand Down Expand Up @@ -2526,6 +2528,9 @@ estimateMap3DKernel <- function(data,
clust$cluster <- 1:nrow(clust)
data <- merge(data, clust, sort = FALSE)
colnames(data)[colnames(data)=="cluster"] <- "temporal_group"

data$temporal_group <- data$temporal_group %>% makeClusterIdsContinuous()
data$spatial_cluster <- data$spatial_cluster %>% makeClusterIdsContinuous()
}
if ( class(model)[1] == "try-error") {return("Error in Model Fitting.")}
sc <- NULL
Expand Down
15 changes: 15 additions & 0 deletions R/01-predictNearestCluster.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ predictNearestCluster <- function(object, newdata){
dist_mat <- dist_mat[-seq(n_centers), seq(n_centers)]
max.col(-dist_mat)
}

#' Make cluster ids continuous
#'
#' When Mclust is used with e.g. 10 clusters, it can still happen that some clusters are empty
#' In this case we would see a jump in cluster ids e.g. 1,2,5,...
#' To prevent this, we change the cluster ids in the last step.
#'
#' @param column_with_ids A vector with cluster ids
makeClusterIdsContinuous <- function(column_with_ids) {
if (length(column_with_ids) == 0) {
return(column_with_ids)
}

match(column_with_ids, sort(unique(column_with_ids)))
}
16 changes: 16 additions & 0 deletions man/makeClusterIdsContinuous.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0aa1bb8

Please sign in to comment.