Skip to content

Commit

Permalink
Move some slow tests into main
Browse files Browse the repository at this point in the history
  • Loading branch information
chjackson committed Aug 24, 2023
1 parent d2d8ae7 commit 9399e1b
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 98 deletions.
22 changes: 0 additions & 22 deletions tests/slow/test_draic.r

This file was deleted.

57 changes: 1 addition & 56 deletions tests/slow/test_phase.r
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
context("phase-type models")

test_that("one phased state", {
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, fixedpars=TRUE,
phase.states=c(1))
expect_equal(psor.msm$minus2loglik, 1308.35590680014, tol=1e-06)
}
)
test_that("one phased state, model fit", {
## num overflow in lik with bfgs, even with fnscale. NM method does better. TODO does bad inits cause that?
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q,
phase.states=c(1), pci=5, method="Nelder-Mead")
phase.states=c(1), pci=5, method="Nelder-Mead", control=list(fnscale=1000))
expect_equal(psor.msm$minus2loglik, 1234.568505251202, tol=1e-06)
psor0.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q)
## better fit compared to unphased
Expand All @@ -31,31 +25,6 @@ test_that("one phased state, model fit", {
# phasemeans.msm(psor.msm, ci="boot", B=4, cores=4)
})

test_that("two phased states", {
## note phase transition within state 2 appears hard to estimate.
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, fixedpars=TRUE,
phase.states=c(1,2))
expect_equal(psor.msm$minus2loglik, 1303.768173132893, tol=1e-06)
})

test_that("supplying initial values",{
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1,2),
phase.inits = list(
list(trans=c(0.42032), exit=c(0.17404,0.04809)),
list(trans=c(0.42032), exit=c(0.17404,0.04809))),
fixedpars=TRUE)
expect_equal(psor.msm$minus2loglik, 1280.788860486683, tol=1e-06)
})

test_that("errors in initial values",{
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1,2), phase.inits = "foo"), "phase.inits should be a list")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1,2), phase.inits = list(1,2,3)), "phase.inits of length 3, but there are 2 phased states")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1,2), phase.inits = list(1,2)), "phase.inits.+ list of length 1, should be 2")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1), phase.inits = list(list(trans=c(0.42032, 1), exit=c(0.17404,0.04809)))), "phase.inits.+trans of length 2, should be 1")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1), phase.inits = list(list(trans=c(0.42032), exit=c(0.17404,0.04809,1,1)))), "phase.inits.+exit has 4 columns, should be 2")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1), phase.inits = list(list(trans=c(0.42032), exit=matrix(c(0.17404,0.04809,1,1),ncol=2,byrow=TRUE)))), "phase.inits.+exit has 2 rows, but there are 1 exit states from this state")
})

test_that("with covariates", {
## Names for transition-specific covs are interpreted as state numbers in new state space
psorc.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, # fixedpars=TRUE,
Expand All @@ -70,27 +39,3 @@ test_that("with qconstraint", {
phase.states=c(1), method="Nelder-Mead", control=list(fnscale=1000,maxit=10000))
psorc.msm
})

test_that("with censoring", {
psor2 <- psor
psor2$state[c(16,18)] <- 99
psorc.msm <- msm(state ~ months, subject=ptnum, data=psor2, qmatrix = psor.q, fixedpars=TRUE,
censor = 99, censor.states=c(3,4))
expect_equal(psorc.msm$minus2loglik, 1290.189703452163, tol=1e-06)
})

test_that("HMM on top", {
### Note 1.6 clarifies obstrue not supported for hmmCat: so test result in 1.5 wrong
miscnew.msm <- msm(state ~ years, subject = PTNUM, data = cav,
qmatrix = oneway4.q, death = 5, #obstrue=firstobs,
fixedpars=TRUE,
# could try to fit, but weakly identifiable. fit seems improved though (-2LL 3864 vs 3951)
# control=list(trace=1,REPORT=1,fnscale=4000, maxit=10000),
phase.states = 1,
hmodel=list(
hmmCat(prob=c(0.9, 0.1, 0, 0)),
hmmCat(prob=c(0.1, 0.8, 0.1, 0)),
hmmCat(prob=c(0, 0.1, 0.9, 0)), hmmIdent(4))
)
expect_equal(miscnew.msm$minus2loglik, 4357.70908245511, tol=1e-06)
})
57 changes: 37 additions & 20 deletions tests/slow/test_outputs_boot.r → tests/testthat/test_boot.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
## Test bootstrap CIs.
## Make sure everything runs for the moment
## Make sure everything runs at least
## hard to check e.g. coverage
## e.g. width should be greater than asymp SE from Hessian.

context("bootstrapping")

set.seed(22061976)
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, covariates = ~ollwsdrt+hieffusn, constraint = list(hieffusn=c(1,1,1),ollwsdrt=c(1,1,2)), fixedpars=FALSE)
set.seed(1)
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q,
covariates = ~ollwsdrt+hieffusn,
constraint = list(hieffusn=c(1,1,1),ollwsdrt=c(1,1,2)), fixedpars=FALSE)

test_that("bootstrap CIs run for all functions that support them",{
q.list <- boot.msm(psor.msm, function(x)x$Qmatrices$baseline, file="~/work/msm/devel/psor.q.boot.rda", B=2)
skip_on_cran()
q.list <- boot.msm(psor.msm, function(x)x$Qmatrices$baseline, B=2)
expect_error(qmatrix.msm(psor.msm, ci="boot", B=2), NA)
expect_error(pmatrix.msm(psor.msm, ci="boot", B=2), NA)
totlos.msm(psor.msm, ci="boot", B=2, tot=1000)
qratio.msm(psor.msm, c(1,2), c(2,3), ci="boot", B=2)
pnext.msm(psor.msm, ci="boot", B=2)
efpt.msm(psor.msm, tostate=3, ci="boot", B=2)
ppass.msm(psor.msm, tot=10, ci="boot", B=2)
cavcens.msm <- msm(state ~ years, subject=PTNUM, data=cav.cens, qmatrix=twoway4.q, censor=99, fixedpars=c(3,6))
ppass.msm(psor.msm, tot=10, ci="boot", B=2)

cavcens.msm <- msm(state ~ years, subject=PTNUM, data=cav.cens[500:1000,],
qmatrix=twoway4.q, censor=99, fixedpars=c(2,5,7))
pmatrix.msm(cavcens.msm, ci="boot", B=2)
pci.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, covariates = ~ollwsdrt+hieffusn, constraint = list(hieffusn=c(1,1,1),ollwsdrt=c(1,1,2)), pci=5, fixedpars=FALSE)
pci.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q,
covariates = ~ollwsdrt+hieffusn,
constraint = list(hieffusn=c(1,1,1),ollwsdrt=c(1,1,2)), pci=5, fixedpars=FALSE)
expect_error(pmatrix.msm(pci.msm, ci="boot", B=2), NA)
})

Expand All @@ -33,27 +37,40 @@ test_that("normal CIs run for all functions that support them",{
expect_error(ppass.msm(psor.msm, tot=10, ci="normal", B=10) , NA)
})

test_that("bootstrap CIs with parallel processing",{
t.par <- system.time(pmatrix.msm(psor.msm, ci="boot", cores=4, B=20))
t.ser <- system.time(pmatrix.msm(psor.msm, ci="boot", cores=1, B=20))
# expect_true(t.par["elapsed"] < t.ser["elapsed"])
# todo needs more like B=100 for benefit on Windows
})

psor2 <- psor
psor2$ollwsdrt <- sample(c(0,1,2), size=nrow(psor), replace=TRUE)
psor2$ollwsdrt <- factor(ifelse(psor2$ollwsdrt==0, "foo", ifelse(psor2$ollwsdrt==1, "bar", "boing")))

test_that("bootstrap CIs with factor covariates",{
psor2.msm <- msm(state ~ months, subject=ptnum, data=psor2, qmatrix = psor.q, covariates = ~ollwsdrt + hieffusn, constraint = list(hieffusn=c(1,1,1)))
skip_on_cran()
psor2.msm <- msm(state ~ months, subject=ptnum, data=psor2,
qmatrix = psor.q, covariates = ~ollwsdrt + hieffusn,
constraint = list(hieffusn=c(1,1,1)))
expect_true(is.list(pmatrix.msm(psor2.msm, ci="boot", B=3)))
expect_true(is.list(pmatrix.msm(psor2.msm, ci="boot", B=2, covariates=list(hieffusn=0, ollwsdrt="foo"))))
expect_true(is.list(pmatrix.msm(psor2.msm, ci="boot", B=2,
covariates=list(hieffusn=0, ollwsdrt="foo"))))
})

psor2$ptnum <- factor(psor2$ptnum)

test_that("bootstrap CIs with factor subject IDs and factor covariates",{
psor2.msm <- msm(state ~ months, subject=ptnum, data=psor2, qmatrix = psor.q, covariates = ~ollwsdrt + hieffusn, constraint = list(hieffusn=c(1,1,1)))
skip_on_cran()
psor2.msm <- msm(state ~ months, subject=ptnum, data=psor2,

qmatrix = psor.q, covariates = ~ollwsdrt + hieffusn,
constraint = list(hieffusn=c(1,1,1)))
expect_true(is.list(pmatrix.msm(psor2.msm, ci="boot", B=3)))
expect_true(is.list(pmatrix.msm(psor2.msm, ci="boot", B=2, covariates=list(hieffusn=0, ollwsdrt="foo"))))
expect_true(is.list(pmatrix.msm(psor2.msm, ci="boot", B=2,

covariates=list(hieffusn=0, ollwsdrt="foo"))))
})

test_that("bootstrap CIs with parallel processing",{
skip_on_cran()
expect_error({
t.par <- system.time(pmatrix.msm(psor.msm, ci="boot", cores=4, B=8))
# t.ser <- system.time(pmatrix.msm(psor.msm, ci="boot", cores=1, B=8))
# expect_true(t.par["elapsed"] < t.ser["elapsed"]) # if B is bigger
}, NA)
})
34 changes: 34 additions & 0 deletions tests/testthat/test_draic.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q,
covariates = ~ollwsdrt+hieffusn,
constraint = list(hieffusn=c(1,1,1),ollwsdrt=c(1,1,2)),
control=list(fnscale=1))

## Merge states 2 and 3
psor$state3 <- ifelse(psor$state==3, 2,
ifelse(psor$state==4, 3, psor$state))
psor3.q <- psor.q[1:3,1:3]
psor3.msm <- msm(state3 ~ months, subject=ptnum, data=psor, qmatrix = psor3.q,
covariates = ~ollwsdrt+hieffusn,
constraint = list(hieffusn=c(1,1),ollwsdrt=c(1,1)),
control=list(fnscale=1))

test_that("DRAIC",{
d <- draic.msm(psor.msm, psor3.msm)
expect_true(is.numeric(d$draic))
dl <- draic.msm(psor.msm, psor3.msm, likelihood.only=TRUE)
expect_equal(d$lik.restricted["complex","-LL"], dl[["complex"]])
expect_equal(as.numeric(dl[["complex"]]), 415.032735368145, tolerance=1e-06)
expect_true(is.numeric(draic.msm(psor.msm, psor3.msm, tl=0.99)$ti["Lower"]))
})

test_that("DRAIC with observed information", {
skip_on_cran()
expect_error(draic.msm(psor.msm, psor3.msm, information="observed"), NA)
})

test_that("DRLCV",{
psor.msm <- msm(state ~ months, subject=ptnum, data=psor[1:29,], qmatrix = psor.q)
psor3.msm <- msm(state3 ~ months, subject=ptnum, data=psor[1:29,], qmatrix = psor3.q)
expect_error(drlcv.msm(psor.msm, psor3.msm, verbose=FALSE), NA)
})

14 changes: 14 additions & 0 deletions tests/testthat/test_pearson.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("pearson.msm help example",{
psor.q <- rbind(c(0,0.1,0,0),c(0,0,0.1,0),c(0,0,0,0.1),c(0,0,0,0))
psor.msm <- msm(state ~ months, subject=ptnum, data=psor,
qmatrix = psor.q, covariates = ~ollwsdrt+hieffusn,
constraint = list(hieffusn=c(1,1,1),ollwsdrt=c(1,1,2)))
p <- pearson.msm(psor.msm, timegroups=2, intervalgroups=2, covgroups=2)
expect_equal(p$test[["p"]],0)
})

#test_that("pearson with bootstrap",{
# psor.msm <- msm(state ~ months, subject=ptnum, data=psor[1:29,],
# qmatrix = psor.q)
# p <- pearson.msm(psor.msm, timegroups=2, intervalgroups=2, covgroups=1, boot=TRUE, B=10)
#})
102 changes: 102 additions & 0 deletions tests/testthat/test_phase.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
test_that("one phased state", {
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, fixedpars=TRUE,
phase.states=c(1))
expect_equal(psor.msm$minus2loglik, 1308.35590680014, tol=1e-06)
}
)

test_that("working example",{
skip_on_cran()
## SIMULATE
mst1 <- 5 # Short-stay mean
mst2 <- 30 # Long-stay mean
p2 <- 0.9 # Long-stay probability
q23 <- 1/5 # Transition rate between phases

l1 <- (p2/mst1)
mu1 <- (1-p2)/mst1
mu2 <- 1/(mst2-mst1)
Q <- rbind(c(0,l1,mu1*0.4,mu1*0.6),
c(0,0,mu2*0.4,mu2*0.6),
c(0,0,0,q23),
c(0,0,0,0))
# Given the hidden state, the observed state is deterministic
E <- rbind(c(1,0,0,0),
c(1,0,0,0),
c(0,1,0,0),
c(0,0,1,0))
nsubj <- 1000; nobspt <- 10
set.seed(1)
sim.df <- data.frame(subject = rep(1:nsubj, each=nobspt), time = seq(0, 100, length=nobspt))
sim2.df <- simmulti.msm(sim.df[,1:2], qmatrix=Q, ematrix=E)
statetable.msm(obs, subject, sim2.df)

## FIT
Q3 <- rbind(c(0,0.5,0.5),c(0,0,0.5),c(0,0,0))
s.msm <- msm(obs ~ time, subject=subject, data=sim2.df, phase.states=1, qmatrix=Q3, # default inits
phase.inits=list(list(trans=0.05,
exit=matrix(c(0.1,0.1,0.1,0.1),nrow=2))),
control=list(trace=1,REPORT=1,fnscale=50000,maxit=10000),method="BFGS")

# Parameter estimates should agree with the values used for simulation
s.msm
c(l1, mu1*0.4, mu1*0.6, mu2*0.4, mu2*0.6, q23)
means <- phasemeans.msm(s.msm)

expect_equal(means[[1,"Short stay mean"]], 5, tol=1)
expect_equal(means[[1,"Long stay mean"]], 30, tol=5)
expect_equal(means[[1,"Long stay probability"]], 0.9, tol=0.1)
})


test_that("two phased states", {
## note phase transition within state 2 appears hard to estimate.
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, fixedpars=TRUE,
phase.states=c(1,2))
expect_equal(psor.msm$minus2loglik, 1303.768173132893, tol=1e-06)
})


test_that("supplying initial values",{
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1,2),
phase.inits = list(
list(trans=c(0.42032), exit=c(0.17404,0.04809)),
list(trans=c(0.42032), exit=c(0.17404,0.04809))),
fixedpars=TRUE)
expect_equal(psor.msm$minus2loglik, 1280.788860486683, tol=1e-06)
})

test_that("errors in initial values",{
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1,2), phase.inits = "foo"), "phase.inits should be a list")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1,2), phase.inits = list(1,2,3)), "phase.inits of length 3, but there are 2 phased states")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1,2), phase.inits = list(1,2)), "phase.inits.+ list of length 1, should be 2")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1), phase.inits = list(list(trans=c(0.42032, 1), exit=c(0.17404,0.04809)))), "phase.inits.+trans of length 2, should be 1")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1), phase.inits = list(list(trans=c(0.42032), exit=c(0.17404,0.04809,1,1)))), "phase.inits.+exit has 4 columns, should be 2")
expect_error(psor2.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, phase.states=c(1), phase.inits = list(list(trans=c(0.42032), exit=matrix(c(0.17404,0.04809,1,1),ncol=2,byrow=TRUE)))), "phase.inits.+exit has 2 rows, but there are 1 exit states from this state")
})


test_that("with censoring", {
psor2 <- psor
psor2$state[c(16,18)] <- 99
psorc.msm <- msm(state ~ months, subject=ptnum, data=psor2, qmatrix = psor.q, fixedpars=TRUE,
censor = 99, censor.states=c(3,4))
expect_equal(psorc.msm$minus2loglik, 1290.189703452163, tol=1e-06)
})


test_that("HMM on top", {
### Note 1.6 clarifies obstrue not supported for hmmCat: so test result in 1.5 wrong
miscnew.msm <- msm(state ~ years, subject = PTNUM, data = cav,
qmatrix = oneway4.q, death = 5, #obstrue=firstobs,
fixedpars=TRUE,
# could try to fit, but weakly identifiable. fit seems improved though (-2LL 3864 vs 3951)
# control=list(trace=1,REPORT=1,fnscale=4000, maxit=10000),
phase.states = 1,
hmodel=list(
hmmCat(prob=c(0.9, 0.1, 0, 0)),
hmmCat(prob=c(0.1, 0.8, 0.1, 0)),
hmmCat(prob=c(0, 0.1, 0.9, 0)), hmmIdent(4))
)
expect_equal(miscnew.msm$minus2loglik, 4357.70908245511, tol=1e-06)
})

0 comments on commit 9399e1b

Please sign in to comment.