Skip to content

Commit

Permalink
Added converged slot to lcModelPartition
Browse files Browse the repository at this point in the history
  • Loading branch information
niekdt committed Nov 4, 2022
1 parent c95c151 commit 9135ee8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
3 changes: 1 addition & 2 deletions R/modelLMKM.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
setClass('lcModelLMKM',
representation(
coefNames = 'character',
trajectoryCoefs = 'matrix',
converged = 'logical'
trajectoryCoefs = 'matrix'
),
contains = 'lcModelPartition'
)
Expand Down
13 changes: 10 additions & 3 deletions R/modelPartition.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ setClass(
representation(
name = 'character',
center = 'function',
trajectoryClusterIndices = 'integer'
trajectoryClusterIndices = 'integer',
converged = 'ANY'
),
contains = 'lcApproxModel'
)
Expand Down Expand Up @@ -50,6 +51,7 @@ lcModelPartition = function(
name = 'part',
center = meanNA,
method = NULL,
converged = TRUE,
model = NULL,
envir = parent.frame()
) {
Expand Down Expand Up @@ -169,7 +171,8 @@ lcModelPartition = function(
id = id,
time = time,
response = response,
estimationTime = 0
estimationTime = 0,
converged = converged
)

if (!is.null(method)) {
Expand Down Expand Up @@ -255,7 +258,11 @@ setMethod('clusterTrajectories', 'lcModelPartition',
#. converged ####
#' @rdname interface-custom
setMethod('converged', 'lcModelPartition', function(object, ...) {
TRUE
if (methods::.hasSlot(object, 'converged')) {
object@converged
} else {
TRUE
}
})


Expand Down
1 change: 1 addition & 0 deletions man/lcModelPartition.Rd

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

30 changes: 30 additions & 0 deletions tests/testthat/test-partition.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test_that('integer assignments', {
expect_valid_lcModel(model)
expect_equivalent(trajectoryAssignments(model), trajectoryAssignments(refmodel))
expect_equivalent(nClusters(model), nClusters(refmodel))
expect_true(converged(model))
})


Expand Down Expand Up @@ -137,3 +138,32 @@ test_that('clusterTrajectories at subset of times', {
)
)
})

test_that('non-converged partition result', {
intAssignments = trajectoryAssignments(refmodel) %>% as.integer()

model = lcModelPartition(
testLongData,
response = 'Value',
trajectoryAssignments = intAssignments,
nClusters = nClusters(refmodel),
converged = FALSE
)

expect_false(converged(model))
})

test_that('numeric converged partition result', {
intAssignments = trajectoryAssignments(refmodel) %>% as.integer()

model = lcModelPartition(
testLongData,
response = 'Value',
trajectoryAssignments = intAssignments,
nClusters = nClusters(refmodel),
converged = 3
)

expect_equal(converged(model), 3)
})

0 comments on commit 9135ee8

Please sign in to comment.