tests/testthat/test-D3.R

context("D3")

# The following test was contributed by jawitte
# https://github.com/amices/mice/issues/226
set.seed(1)
A <- rnorm(100)
B <- 0.1 * A + rnorm(100)
fit1 <- lapply(1:5, function(m) {
  lm(B ~ A)
})
fit0 <- lapply(1:5, function(m) {
  lm(B ~ 1)
})
# outcomment to evade dependency of lmtest
# x1 <- lmtest::lrtest(fit1[[1]], fit0[[1]])
x1 <- structure(list(`#Df` = c(3, 2),
                     LogLik = c(-137.087912980007, -137.516434459951),
                     Df = c(NA, -1), Chisq = c(NA, 0.857042959888474),
                     `Pr(>Chisq)` = c(NA, 0.354567523408569)),
                class = c("anova",  "data.frame"),
                row.names = c("1", "2"),
                heading = c("Likelihood ratio test\n",  "Model 1: B ~ A\nModel 2: B ~ 1"))
x2 <- D3(fit1 = fit1, fit0 = fit0)
x3 <- mitml::testModels(fit1, fit0, method = "D3")

# tests for complete data
test_that("lm, complete data: D3() and lrtest() calculate same test statistic", {
  expect_equal(x1$Chisq[2], x2$result[1])
})
test_that("lm, complete data: testModels() and lrtest() calculate same test statistic", {
  expect_equal(x1$Chisq[2], x3$test[1])
})


# FIXME:
# for imputed data, there are discrepancies between mitml and mice
# the tests below compare mitml and mice, but none of these seem to work
# so I have a outcommented the critical lines

imp <- mice(nhanes, print = FALSE, m = 10, seed = 219)

fit1 <- with(data = imp, expr = lm(hyp ~ age + chl))
fit0 <- with(data = imp, expr = lm(hyp ~ 1))
empty <- with(data = imp, expr = lm(hyp ~ 0))

# stat1 <- pool.compare(fit1, fit0, method = "likelihood")

z1 <- D3(fit1, fit0)
z2 <- mitml::testModels(as.mitml.result(fit1), as.mitml.result(fit0), method = "D3")

# This test fails
# FIXME
# test_that("lm: mice and mitml calculate same F", {
# expect_equal(z1$result[1], z2$test[1])
# })


# using lmer
suppressPackageStartupMessages(library(mitml, quietly = TRUE))
library(lme4, quietly = TRUE)
# library(broom.mixed, quietly = TRUE)
data(studentratings)
fml <- ReadDis + SES ~ ReadAchiev + (1 | ID)
set.seed(26262)
imp <- mitml::panImpute(studentratings,
  formula = fml, n.burn = 1000, n.iter = 100, m = 5,
  silent = TRUE
)
implist <- mitml::mitmlComplete(imp, print = 1:5)
fit0 <- with(implist, lmer(ReadAchiev ~ (1 | ID), REML = FALSE))
fit1 <- with(implist, lmer(ReadAchiev ~ ReadDis + SES + (1 | ID), REML = FALSE))

# outcommented to evade dependency on broom.mixed under the _R_CHECK_DEPENDS_ONLY=true flag
# # likelihood test
# z3 <- D3(fit1, fit0)
# z4 <- mitml::testModels(fit1, fit0, method = "D3")

# This test fails.
# FIXME
# test_that("lmer: mice and mitml calculate same F", {
# expect_equal(z3$result[1], z4$test[1])
# })


# glm
# imp <- mice(nhanes2, print = FALSE, m = 10, seed = 219)
#
# fit1 <- with(data = imp, expr = glm(hyp == "yes" ~ age + chl, family = binomial))
# fit0 <- with(data = imp, expr = glm(hyp == "yes" ~ 1, family = binomial))
# empty <- with(data = imp, expr = glm(hyp == "yes" ~ 0, family = binomial))
#
# model dev1.L does not look right, negative Dm, convergence problems
# FIXME
# z5 <- D3(fit1, fit0)

# mitml can't do this case
# z6 <- mitml::testModels(as.mitml.result(fit1), as.mitml.result(fit0), method = "D3")

# crashes on terms
# FIXME
# z5a <- D3(fit1, empty)

# This test fails.
# FIXME
# test_that("glm: mice and mitml calculate same F", {
#   expect_equal(z5$result[1], z6$test[1])
# })


# data with factors
imp <- mice(nhanes2, print = FALSE, m = 10, seed = 219)
fit1 <- with(data = imp, expr = lm(bmi ~ age + chl + hyp))
fit0 <- with(data = imp, expr = lm(bmi ~ age))
empty <- with(data = imp, expr = lm(bmi ~ 0))

z7 <- D3(fit1, fit0)
z8 <- mitml::testModels(as.mitml.result(fit1), as.mitml.result(fit0), method = "D3")

# This test fails.
# FIXME
# test_that("factors: mice and mitml calculate same F", {
#  expect_equal(z7$result[1], z8$test[1])
# })
stefvanbuuren/mice documentation built on Dec. 3, 2023, 5:38 a.m.