tests/testthat/test-estimate_expectation.R

if (require("lme4") && require("insight")) {
  test_that("estimate_expectation", {
    model <- lmer(mpg ~ wt + factor(am) + (1 | cyl), data = mtcars)
    estim <- estimate_expectation(model)
    expect_equal(dim(estim), c(32, 8))
    expect_equal(
      colnames(estim),
      c("wt", "am", "cyl", "Predicted", "SE", "CI_low", "CI_high", "Residuals")
    )

    m <- lm(mpg ~ 1, data = mtcars)
    estim <- estimate_expectation(m)
    expect_equal(dim(estim), c(32, 5))
    expect_equal(
      colnames(estim),
      c("Predicted", "SE", "CI_low", "CI_high", "Residuals")
    )

    m <- lm(mpg ~ am, data = mtcars)
    estim <- estimate_expectation(m)
    expect_equal(dim(estim), c(32, 6))
    expect_equal(
      colnames(estim),
      c("am", "Predicted", "SE", "CI_low", "CI_high", "Residuals")
    )
  })


  test_that("estimate_expectation - data-grid", {
    model <- lmer(mpg ~ wt + factor(am) + (1 | cyl), data = mtcars)
    estim <- estimate_expectation(model, data = "grid")
    expect_equal(dim(estim), c(12, 7))
    expect_equal(
      colnames(estim),
      c("wt", "am", "cyl", "Predicted", "SE", "CI_low", "CI_high")
    )

    m <- lm(mpg ~ 1, data = mtcars)
    estim <- estimate_expectation(m, data = "grid")
    expect_equal(dim(estim), c(10, 5))
    expect_equal(
      colnames(estim),
      c("Predicted", "SE", "CI_low", "CI_high", "Residuals")
    )

    m <- lm(mpg ~ cyl, data = mtcars)
    estim <- estimate_expectation(m, data = "grid")
    expect_equal(dim(estim), c(10, 5))
    expect_equal(
      colnames(estim),
      c("cyl", "Predicted", "SE", "CI_low", "CI_high")
    )

    m <- lm(mpg ~ factor(cyl), data = mtcars)
    estim <- estimate_expectation(m, data = "grid")
    expect_equal(dim(estim), c(3, 5))
    expect_equal(
      colnames(estim),
      c("cyl", "Predicted", "SE", "CI_low", "CI_high")
    )
  })
}

Try the modelbased package in your browser

Any scripts or data that you put into this service are public.

modelbased documentation built on Jan. 13, 2023, 9:12 a.m.