Skip to content

Commit

Permalink
Added check to lcModelKML postprob()
Browse files Browse the repository at this point in the history
  • Loading branch information
niekdt committed Jun 27, 2022
1 parent cd2580f commit fbb06eb
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions R/modelKML.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,27 @@ setMethod('postprob', signature('lcModelKML'), function(object) {
ppRaw = getKMLPartition(object)@postProba
trajClusters = kml::getClusters(object@model, nbCluster = nClusters(object), asInteger = TRUE)

assert_that(
length(trajClusters) == nIds(object),
msg = sprintf('kml package returned fewer cluster assignments than expected: %d instead of %d', length(trajClusters), nIds(object))
)

pp = matrix(NA_real_, nrow = nIds(object), ncol = nClusters(object))
pp[which(is.finite(trajClusters)), ] = ppRaw
}

# bugfix for KML: insert missing rows for uniform postprob
if (anyNA(pp)) {
naMsk = is.na(pp[, 1L])
warning(
sprintf(
'The kml package has outputted NA posterior probabilities for %d trajectories. Inserting uniform postprob for trajectories: \n%s',
sum(naMsk),
paste0(' ', ids(object)[naMsk], collapse = '\n')
# bugfix for KML: insert missing rows for uniform postprob
if (anyNA(pp)) {
naMsk = is.na(pp[, 1L])
warning(
sprintf(
'The kml package has outputted NA posterior probabilities for %d trajectories. Inserting uniform postprob for trajectories: \n%s',
sum(naMsk),
paste0(' ', ids(object)[naMsk], collapse = '\n')
)
)
)

pp[naMsk, ] = 1 / nClusters(object)
pp[naMsk, ] = 1 / nClusters(object)
}
}

colnames(pp) = clusterNames(object)
Expand Down

0 comments on commit fbb06eb

Please sign in to comment.