Skip to content

Commit

Permalink
tests: change tests to use "id" and "time" columns for catching evalu…
Browse files Browse the repository at this point in the history
…ation errors (#151)
  • Loading branch information
niekdt committed Apr 3, 2024
1 parent 6b2cd97 commit c8b0048
Show file tree
Hide file tree
Showing 22 changed files with 138 additions and 134 deletions.
12 changes: 6 additions & 6 deletions tests/testthat/setup-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ if (requireNamespace('ggplot2', quietly = TRUE)) {

testLongData = generateLongData(
sizes = c(20L, 30L),
fixed = Value ~ 1 + Assessment,
cluster = ~ 1 + Assessment,
fixed = Value ~ 1 + time,
cluster = ~ 1 + time,
random = ~ 1,
id = 'Traj',
data = data.frame(Assessment = seq(0, 1, by = .1)),
id = 'id',
data = data.frame(time = seq(0, 1, by = .1)),
fixedCoefs = c(0, 0),
clusterCoefs = cbind(c(-2, 1), c(2, -1)),
randomScales = cbind(.1, .1),
noiseScales = c(.1, .1),
clusterNames = c('A', 'B'),
shuffle = TRUE
) %>%
.[, .(Traj, Assessment, Value, Class)] %>%
.[, .(id, time, Value, Class)] %>%
.[, Constant := 1] %>%
.[, Cluster := Class] %>%
.[, Traj := factor(Traj)] %>%
.[, id := factor(id)] %>%
.[]

# set up capture functions to test for wrong column name handling
Expand Down
24 changes: 14 additions & 10 deletions tests/testthat/setup-init.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
options(deparse.max.lines=5)

options(latrend.id = 'Traj')
options(latrend.time = 'Assessment')
options(latrend.id = 'id')
options(latrend.time = 'time')
options(latrend.verbose = R.utils::Verbose())
options(latrend.warnModelDataClusterColumn = FALSE)
options(latrend.warnNewDataClusterColumn = FALSE)
Expand Down Expand Up @@ -87,28 +87,32 @@ expect_valid_lcModel = function(object) {

# Predict
# cluster-specific prediction
pred = predict(object, newdata=data.frame(Cluster='T1', Assessment=time(object)[c(1,3)]))
pred = predict(object, newdata = data.frame(Cluster = 'T1', time = time(object)[c(1,3)]))
expect_is(pred, 'data.frame', info='predictClusterTime')
expect_true('Fit' %in% names(pred), info='predictClusterTime')
expect_equal(nrow(pred), 2, info='predictClusterTime')

# prediction for all clusters; list of data.frames
pred2 = predict(object, newdata=data.frame(Assessment=time(object)[c(1,3)]))
pred2 = predict(object, newdata = data.frame(time = time(object)[c(1,3)]))
expect_is(pred2, 'list', info='predictTime')
expect_length(pred2, nClusters(object))
expect_true('Fit' %in% names(pred2$T1), info='predictTime')

# id-specific prediction for a specific cluster; data.frame
pred3 = predict(object, newdata=data.frame(Cluster=rep('T1', 4),
Traj=c(ids(object)[c(1,1,2)], tail(ids(object), 1)),
Assessment=c(time(object)[c(1,3,1,1)])))
pred3 = predict(object, newdata = data.frame(
Cluster = rep('T1', 4),
id = c(ids(object)[c(1,1,2)], tail(ids(object), 1)),
time = c(time(object)[c(1,3,1,1)])
))
expect_is(pred3, 'data.frame', info='predictClusterIdTime')
expect_true('Fit' %in% names(pred3), info='predictClusterIdTime')
expect_equal(nrow(pred3), 4, info='predictClusterIdTime')

# id-specific prediction for all clusters; list of data.frames
pred4 = predict(object, newdata=data.frame(Traj=c(ids(object)[c(1,1,2)], tail(ids(object), 1)),
Assessment=c(time(object)[c(1,3,1,1)])))
pred4 = predict(object, newdata = data.frame(
id = c(ids(object)[c(1,1,2)], tail(ids(object), 1)),
time = c(time(object)[c(1,3,1,1)])
))
expect_is(pred4, 'list', info='predictIdTime')
expect_length(pred4, nClusters(object))
expect_true('Fit' %in% names(pred4$T1), info='predictIdTime')
Expand All @@ -126,7 +130,7 @@ expect_valid_lcModel = function(object) {
# predictForCluster
predClus = predictForCluster(
object,
newdata = data.frame(Assessment = time(object)[c(1,3)]),
newdata = data.frame(time = time(object)[c(1,3)]),
cluster = 'T1'
)
expect_is(predClus, 'numeric', info='predictForCluster')
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/setup-method-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lcMethodTestLcmmGBTM = function(..., init = NULL, seed = 1) {

# lme4 ####
lcMethodTestGCKM = function(...) {
lcMethodGCKM(formula = Value ~ (1 | Traj), ...)
lcMethodGCKM(formula = Value ~ (1 | id), ...)
}


Expand All @@ -36,7 +36,7 @@ lcMethodTestStratify = function(...) {
}

lcMethodTestLMKM = function(...) {
lcMethodLMKM(formula = Value ~ Assessment, ..., seed = 1)
lcMethodLMKM(formula = Value ~ time, ..., seed = 1)
}


Expand Down
26 changes: 13 additions & 13 deletions tests/testthat/test-assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ test_that('no_trajectories_duplicate_time', {
)

expect_true(
no_trajectories_duplicate_time(testLongData, id = 'Traj', time = 'Assessment')
no_trajectories_duplicate_time(testLongData, id = 'id', time = 'time')
)

expect_false(
Expand All @@ -131,7 +131,7 @@ test_that('no_empty_trajectories', {
)

expect_true(
no_empty_trajectories(testLongData, id = 'Traj')
no_empty_trajectories(testLongData, id = 'id')
)

expect_true(
Expand All @@ -152,50 +152,50 @@ test_that('no_empty_trajectories', {

test_that('have_trajectories_noNA', {
expect_true(
have_trajectories_noNA(testLongData, id = 'Traj', response = 'Value')
have_trajectories_noNA(testLongData, id = 'id', response = 'Value')
)

expect_false(
have_trajectories_noNA(copy(testLongData)[2, Value := NA], id = 'Traj', response = 'Value')
have_trajectories_noNA(copy(testLongData)[2, Value := NA], id = 'id', response = 'Value')
)

expect_true(
have_trajectories_noNA(copy(testLongData)[3, Value := Inf], id = 'Traj', response = 'Value')
have_trajectories_noNA(copy(testLongData)[3, Value := Inf], id = 'id', response = 'Value')
)
})

test_that('are_trajectories_length', {
expect_true(
are_trajectories_length(testLongData, id = 'Traj', time = 'Assessment', min = 1L)
are_trajectories_length(testLongData, id = 'id', time = 'time', min = 1L)
)

expect_true(
are_trajectories_length(testLongData, id = 'Traj', time = 'Assessment', min = uniqueN(testLongData$Assessment))
are_trajectories_length(testLongData, id = 'id', time = 'time', min = uniqueN(testLongData$time))
)

expect_false(
are_trajectories_length(testLongData, id = 'Traj', time = 'Assessment', min = uniqueN(testLongData$Assessment) + 1L)
are_trajectories_length(testLongData, id = 'id', time = 'time', min = uniqueN(testLongData$time) + 1L)
)
})

test_that('are_trajectories_equal_length', {
expect_true(
are_trajectories_equal_length(testLongData, id = 'Traj', time = 'Assessment')
are_trajectories_equal_length(testLongData, id = 'id', time = 'time')
)

expect_false(
are_trajectories_equal_length(testLongData[2:.N], id = 'Traj', time = 'Assessment')
are_trajectories_equal_length(testLongData[2:.N], id = 'id', time = 'time')
)
})

test_that('no_trajectories_allNA', {
expect_true(
no_trajectories_allNA(testLongData, id = 'Traj', response = 'Value')
no_trajectories_allNA(testLongData, id = 'id', response = 'Value')
)

naData = copy(testLongData) %>%
.[Traj == unique(Traj)[2], Value := NA]
.[id == unique(id)[2], Value := NA]
expect_false(
no_trajectories_allNA(naData, id = 'Traj', response = 'Value')
no_trajectories_allNA(naData, id = 'id', response = 'Value')
)
})
30 changes: 15 additions & 15 deletions tests/testthat/test-cluslong.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ test_that('method var', {
})

test_that('method name', {
refMethod = lcMethodLMKM(formula = Value ~ Assessment, seed = 1)
model = latrend('lcMethodLMKM', formula = Value ~ Assessment, seed = 1, data = testLongData)
refMethod = lcMethodLMKM(formula = Value ~ time, seed = 1)
model = latrend('lcMethodLMKM', formula = Value ~ time, seed = 1, data = testLongData)

newMethod = getLcMethod(model)
expect_equal(newMethod$seed, refMethod$seed)
Expand Down Expand Up @@ -48,10 +48,10 @@ test_that('new method arguments', {
})

test_that('subset', {
model = latrend(mTest, data = testLongData[Assessment < .5])
model = latrend(mTest, data = testLongData[time < .5])

expect_is(model, 'lcModel')
expect_equal(deparse(getCall(model)$data), 'testLongData[Assessment < 0.5]')
expect_equal(deparse(getCall(model)$data), 'testLongData[time < 0.5]')
})

test_that('data call', {
Expand All @@ -72,7 +72,7 @@ test_that('specify id and time with matrix input', {
})

test_that('envir', {
method = lcMethodLMKM(nClusters = a, formula = Value ~ Assessment)
method = lcMethodLMKM(nClusters = a, formula = Value ~ time)
env = list2env(list(a = 1))

model = latrend(method, data = testLongData, envir = env)
Expand All @@ -97,7 +97,7 @@ test_that('matrix input', {

test_that('custom id and time', {
nameData = copy(testLongData) %>%
setnames(c('Traj', 'Assessment'), c('Device', 'Observation'))
setnames(c('id', 'time'), c('Device', 'Observation'))
method = lcMethodLMKM(Value ~ Observation, id = 'Device', time = 'Observation')
model = latrend(method, data = nameData)

Expand All @@ -108,45 +108,45 @@ test_that('custom id and time', {
test_that('id with NA', {
set.seed(1)
naData = copy(testLongData) %>%
.[sample(.N, 10), Traj := NA]
.[sample(.N, 10), id := NA]

expect_error(latrend(mTest, data = naData))
})

test_that('factor id', {
facData = copy(testLongData) %>%
.[, Traj := factor(Traj)]
.[, id := factor(id)]

model = latrend(mTest, data = facData)

expect_is(model, 'lcModel')
expect_equal(ids(model), levels(facData$Traj))
expect_equal(ids(model), levels(facData$id))
})

test_that('factor id, out of order', {
facData = copy(testLongData) %>%
.[, Traj := factor(Traj, levels = rev(unique(Traj)))]
.[, id := factor(id, levels = rev(unique(id)))]

model = latrend(mTest, data = facData)

expect_is(model, 'lcModel')
expect_equal(ids(model), levels(facData$Traj))
expect_equal(ids(model), levels(facData$id))
})

test_that('factor id with empty levels', {
facData = copy(testLongData) %>%
.[, Traj := factor(Traj, levels = seq(0, uniqueN(Traj) + 1))]
.[, id := factor(id, levels = seq(0, uniqueN(id) + 1))]

expect_warning({
model = latrend(mTest, data = facData)
}, regexp = 'mpty traj')
}, regexp = 'mpty')

expect_is(model, 'lcModel')
})

test_that('id with NA', {
naData = copy(testLongData) %>%
.[Traj == 1, Traj := NA]
.[id == 1, id := NA]

expect_error(latrend(mTest, data = naData))
})
Expand Down Expand Up @@ -226,7 +226,7 @@ test_that('trajectory length warning', {

test_that('"time" column', {
timeData = copy(testLongData)
setnames(timeData, 'Assessment', 'time')
setnames(timeData, 'time', 'time')
method = lcMethodTestKML(time = 'time')
model = latrend(method, data = timeData)
expect_is(model, 'lcModel')
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-cluslongBatch.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ test_that('multiple datasets', {
models = latrendBatch(
methods,
data = .(testLongData,
testLongData[Assessment < .5],
testLongData[Assessment >= .5]),
testLongData[time < .5],
testLongData[time >= .5]),
cartesian = TRUE)

expect_is(models, 'lcModels')
expect_length(models, 2*3)

expect_equal(deparse(getCall(models[[1]])$data), 'testLongData')
expect_equal(deparse(getCall(models[[2]])$data), 'testLongData[Assessment < 0.5]')
expect_equal(deparse(getCall(models[[3]])$data), 'testLongData[Assessment >= 0.5]')
expect_equal(deparse(getCall(models[[2]])$data), 'testLongData[time < 0.5]')
expect_equal(deparse(getCall(models[[3]])$data), 'testLongData[time >= 0.5]')
})


Expand All @@ -60,8 +60,8 @@ test_that('datasets list', {
methods = lcMethods(mTest, nClusters = 1:2)
dataList = list(
testLongData,
testLongData[Assessment < .5],
testLongData[Assessment >= .5]
testLongData[time < .5],
testLongData[time >= .5]
)

models = latrendBatch(methods, data = dataList, cartesian = TRUE)
Expand All @@ -78,8 +78,8 @@ test_that('datasets list, cartesian=FALSE', {
methods = lcMethods(mTest, nClusters = 1:3)
dataList = list(
testLongData,
testLongData[Assessment < .5],
testLongData[Assessment >= .5]
testLongData[time < .5],
testLongData[time >= .5]
)
models = latrendBatch(methods, data = dataList, cartesian = FALSE)

Expand All @@ -95,8 +95,8 @@ test_that('datasets list, cartesian=FALSE', {
test_that('single method, multiple datasets', {
dataList = list(
testLongData,
testLongData[Assessment < .5],
testLongData[Assessment >= .5]
testLongData[time < .5],
testLongData[time >= .5]
)

models = latrendBatch(mTest, data = dataList, cartesian = TRUE)
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-cluslongBoot.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ test_that('latrendBoot', {
expect_length(models, 3)

# test if data bootstrap sample calls are correct
expect_equal(deparse(getCall(models[[1]])$data), 'bootSample(testLongData, "Traj", 1140350788L)')
expect_equal(deparse(getCall(models[[2]])$data), 'bootSample(testLongData, "Traj", 312928385L)')
expect_equal(deparse(getCall(models[[3]])$data), 'bootSample(testLongData, "Traj", 866248189L)')
expect_equal(deparse(getCall(models[[1]])$data), 'bootSample(testLongData, "id", 1140350788L)')
expect_equal(deparse(getCall(models[[2]])$data), 'bootSample(testLongData, "id", 312928385L)')
expect_equal(deparse(getCall(models[[3]])$data), 'bootSample(testLongData, "id", 866248189L)')
})

test_that('latrendBoot without seed', {
Expand Down
Loading

0 comments on commit c8b0048

Please sign in to comment.