tests/testthat/test-get_modelmatrix.R

test_that("Issue #612: factor padding", {
  # stats::model.matrix() breaks on contrasts when a column of `data` has
  # only 1 factor level

  # no factor
  mod <- glm(vs ~ cyl, data = mtcars, family = binomial)
  mm <- get_modelmatrix(mod)
  expect_identical(nrow(mm), 32L)
  mm <- get_modelmatrix(mod, data = mtcars)
  expect_identical(nrow(mm), 32L)
  mm <- get_modelmatrix(mod, data = head(mtcars))
  expect_identical(nrow(mm), 6L)

  # one factor
  dat <- mtcars
  dat$cyl <- factor(dat$cyl)
  mod <- glm(vs ~ cyl, data = dat, family = binomial)

  # no data argument
  mm <- get_modelmatrix(mod)
  expect_identical(nrow(mm), 32L)

  # enough factor levels
  mm <- get_modelmatrix(mod, data = head(dat))
  expect_identical(nrow(mm), 6L)

  # not enough factor levels
  mm <- get_modelmatrix(mod, data = dat[3, ])
  expect_identical(nrow(mm), 1L)
})


# iv_robust --------------------------------------------------------------
# =========================================================================

test_that("get_modelmatrix - iv_robust", {
  skip_if_not_installed("ivreg")
  skip_if_not_installed("estimatr")
  data(Kmenta, package = "ivreg")

  x <- estimatr::iv_robust(Q ~ P + D | D + F + A, se_type = "stata", data = Kmenta)

  out1 <- get_modelmatrix(x)
  out2 <- model.matrix(terms(x), data = Kmenta)
  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)

  out1 <- get_modelmatrix(x, data = get_datagrid(x, by = "P"))
  out2 <- model.matrix(
    terms(x),
    data = get_datagrid(x, by = "P", include_response = TRUE)
  )
  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)
  expect_identical(nrow(get_datagrid(x, by = "P")), nrow(out2))
})


# ivreg --------------------------------------------------------------
# ====================================================================

test_that("get_modelmatrix - ivreg", {
  skip_if(getRversion() < "4.2.0")
  skip_if_not_installed("ivreg")
  data(Kmenta, package = "ivreg")
  d_kmenta <<- Kmenta

  set.seed(15)
  x <- ivreg::ivreg(Q ~ P + D | D + F + A, data = d_kmenta)

  out1 <- get_modelmatrix(x)
  out2 <- model.matrix(x, data = d_kmenta)
  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)

  out1 <- get_modelmatrix(x, data = get_datagrid(x, by = "P"))
  out2 <- model.matrix(
    terms(x),
    data = get_datagrid(x, by = "P", include_response = TRUE)
  )
  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)
  expect_identical(nrow(get_datagrid(x, by = "P")), nrow(out2))
})


# ivreg --------------------------------------------------------------
# ====================================================================

test_that("get_modelmatrix - lm_robust", {
  skip_if_not_installed("estimatr")

  set.seed(15)
  N <- 1:40
  dat <<- data.frame(
    N = N,
    y = rpois(N, lambda = 4),
    x = rnorm(N),
    z = rbinom(N, 1, prob = 0.4)
  )

  x <- estimatr::lm_robust(y ~ x + z, data = dat)

  out1 <- get_modelmatrix(x)
  out2 <- model.matrix(x, data = dat)
  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)

  out1 <- get_modelmatrix(x, data = get_datagrid(x, by = "x"))
  out2 <- model.matrix(x, data = get_datagrid(x, by = "x", include_response = TRUE))
  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)
  expect_identical(nrow(get_datagrid(x, by = "x")), nrow(out2))
})


test_that("Issue #693", {
  set.seed(12345)
  n <- 500
  x <- sample.int(3, n, replace = TRUE)
  w <- sample.int(4, n, replace = TRUE)
  y <- rnorm(n)
  z <- as.numeric(x + y + rlogis(n) > 1.5)
  dat <<- data.frame(x = factor(x), w = factor(w), y = y, z = z)
  m <- glm(z ~ x + w + y, family = binomial, data = dat)
  nd <- head(dat, 2)
  mm <- get_modelmatrix(m, data = head(dat, 1))
  expect_true(all(c("x2", "x3", "w2", "w3", "w4") %in% colnames(mm)))
})


test_that("get_modelmatrix - lme uses model contrasts, Issue #483", {
  skip_if_not_installed("nlme")
  d <- mtcars
  d$cyl <- factor(d$cyl)
  m <- nlme::lme(
    mpg ~ cyl,
    data = d,
    random = ~ 1 | gear,
    contrasts = list(cyl = contr.sum)
  )
  expected <- stats::model.matrix(
    ~cyl,
    data = d,
    contrasts.arg = list(cyl = contr.sum)
  )
  out <- get_modelmatrix(m)
  expect_identical(colnames(out), c("(Intercept)", "cyl1", "cyl2"))
  expect_equal(out, expected, ignore_attr = TRUE)
  expect_identical(colnames(out), names(nlme::fixef(m)))

  # new data keeps the model contrasts
  out <- get_modelmatrix(m, data = head(d, 5))
  expect_equal(out, head(expected, 5), ignore_attr = TRUE)

  # new data with only some of the factor levels
  nd <- data.frame(mpg = 0, cyl = c("6", "8"), gear = 4)
  out <- get_modelmatrix(m, data = nd)
  expect_equal(out, expected[c(1, 5), ], ignore_attr = TRUE)

  # user-provided contrasts, also NULL, replace the model contrasts
  out <- get_modelmatrix(m, contrasts.arg = NULL)
  expect_identical(colnames(out), c("(Intercept)", "cyl6", "cyl8"))

  # default contrasts are unchanged
  m <- nlme::lme(mpg ~ cyl, data = d, random = ~ 1 | gear)
  out <- get_modelmatrix(m)
  expect_identical(colnames(out), c("(Intercept)", "cyl6", "cyl8"))
})


test_that("get_modelmatrix - lme, new data with character predictor", {
  skip_if_not_installed("nlme")
  d <- mtcars
  d$cyl <- as.character(d$cyl)
  m <- nlme::lme(mpg ~ cyl, data = d, random = ~ 1 | gear)
  expected <- stats::model.matrix(~cyl, data = d)
  # new data with only some of the levels
  nd <- data.frame(mpg = 0, cyl = c("6", "8"), gear = 4)
  out <- get_modelmatrix(m, data = nd)
  expect_identical(colnames(out), c("(Intercept)", "cyl6", "cyl8"))
  expect_equal(out, expected[c(1, 5), ], ignore_attr = TRUE)
})


test_that("get_modelmatrix - gls uses model contrasts", {
  skip_if_not_installed("nlme")
  # not `d`: with the global `d` that test-coxme.R leaves, get_data() returns
  # the wrong data for this model
  d_gls <- mtcars
  d_gls$cyl <- factor(d_gls$cyl)
  m <- withr::with_options(
    list(contrasts = c("contr.sum", "contr.poly")),
    nlme::gls(mpg ~ cyl, data = d_gls)
  )
  expected <- stats::model.matrix(
    ~cyl,
    data = d_gls,
    contrasts.arg = list(cyl = contr.sum)
  )
  out <- get_modelmatrix(m)
  expect_identical(colnames(out), names(stats::coef(m)))
  expect_equal(out, expected, ignore_attr = TRUE)

  # new data with only some of the factor levels
  nd <- data.frame(mpg = 0, cyl = c("6", "8"))
  out <- get_modelmatrix(m, data = nd)
  expect_equal(out, expected[c(1, 5), ], ignore_attr = TRUE)
})


test_that("get_modelmatrix - lme ignores model.matrix() methods for lme objects", {
  skip_if_not_installed("nlme")
  # MuMIn registers a model.matrix() method for lme objects that ignores the
  # `data` and `contrasts.arg` arguments
  local_mocked_s3_method(
    "model.matrix",
    "lme",
    function(object, ...) stop("model.matrix.lme() was called", call. = FALSE)
  )
  d_lme <- mtcars
  d_lme$cyl <- factor(d_lme$cyl)
  m <- nlme::lme(
    mpg ~ cyl,
    data = d_lme,
    random = ~ 1 | gear,
    contrasts = list(cyl = contr.sum)
  )
  expected <- stats::model.matrix(
    ~cyl,
    data = d_lme,
    contrasts.arg = list(cyl = contr.sum)
  )
  out <- get_modelmatrix(m, data = head(d_lme, 5))
  expect_equal(out, head(expected, 5), ignore_attr = TRUE)
})


test_that("get_modelmatrix - lme and gls, new data keeps the basis of poly()", {
  skip_if_not_installed("nlme")
  d_lme <- mtcars
  d_lme$cyl <- factor(d_lme$cyl)
  expected <- stats::model.matrix(stats::lm(mpg ~ poly(wt, 2) + cyl, data = d_lme))
  m <- nlme::lme(mpg ~ poly(wt, 2) + cyl, data = d_lme, random = ~ 1 | gear)
  out <- get_modelmatrix(m, data = head(d_lme, 5))
  expect_identical(colnames(out), colnames(expected))
  expect_equal(out, head(expected, 5), ignore_attr = TRUE)
  m <- nlme::gls(mpg ~ poly(wt, 2) + cyl, data = d_lme)
  out <- get_modelmatrix(m, data = head(d_lme, 5))
  expect_equal(out, head(expected, 5), ignore_attr = TRUE)
})


test_that("get_modelmatrix works with NA columns, Issue #1147", {
  skip_if(getRversion() < "4.5.0")
  data(penguins)
  mod <- lm(flipper_len ~ bill_dep * sex, data = penguins, weights = body_mass)
  nd <- get_datagrid(mod)
  nd$flipper_len <- 0
  out <- get_modelmatrix(mod, data = nd)

  expect_identical(
    colnames(out),
    c("(Intercept)", "bill_dep", "sexmale", "bill_dep:sexmale")
  )
  expect_identical(dim(out), c(18L, 4L))

  # these examples should work without error
  skip_if_not_installed("glmmTMB")
  skip_if_not_installed("modelbased")

  mod_tmb <- glmmTMB::glmmTMB(
    flipper_len ~ bill_dep * sex,
    data = penguins
  )
  expect_silent(modelbased::estimate_relation(mod_tmb, by = c("bill_dep", "sex")))

  mod_tmb <- glmmTMB::glmmTMB(
    flipper_len ~ bill_dep * sex,
    data = penguins,
    weights = body_mass
  )
  expect_silent(modelbased::estimate_relation(mod_tmb, by = c("bill_dep", "sex")))
})


# clmm and brmsfit: model contrasts, new data, user contrasts, Issue #1237 ----

# compare column names, then values of the plain matrices
expect_modelmatrix <- function(out, expected) {
  expect_identical(colnames(out), colnames(expected))
  plain <- function(x) matrix(as.vector(x), nrow = nrow(x), ncol = ncol(x))
  expect_equal(plain(out), plain(expected))
}

clmm_fixture <- function() {
  w2 <- ordinal::wine
  w2$ch <- as.character(w2$contact)
  m_c <- ordinal::clmm(
    rating ~ temp + ch + (1 | judge),
    data = w2,
    contrasts = list(temp = "contr.sum")
  )
  list(data = w2, model = m_c)
}

# not `d`: with the global `d` that test-coxme.R leaves, get_data() returns
# the wrong data for brmsfit models
brms_fixture <- function() {
  d_brms <- mtcars
  d_brms$cyl <- factor(d_brms$cyl)
  contrasts(d_brms$cyl) <- contr.sum(3)
  m_b <- suppressMessages(suppressWarnings(
    brms::brm(mpg ~ cyl + wt, data = d_brms, empty = TRUE)
  ))
  list(data = d_brms, model = m_b)
}

test_that("get_modelmatrix - clmm with sum contrasts, no data", {
  skip_if_not_installed("ordinal")
  fx <- clmm_fixture()
  expected <- stats::model.matrix(
    ~ temp + ch,
    fx$data,
    contrasts.arg = list(temp = "contr.sum")
  )
  expect_modelmatrix(get_modelmatrix(fx$model), expected)
})

test_that("get_variance - clmm var.fixed with sum contrasts equals default contrasts", {
  skip_if_not_installed("ordinal")
  fx <- clmm_fixture()
  m_default <- ordinal::clmm(rating ~ temp + ch + (1 | judge), data = fx$data)
  out <- get_variance(fx$model)
  ref <- get_variance(m_default)
  expect_type(out, "list")
  expect_equal(out$var.fixed, ref$var.fixed, tolerance = 1e-4)
})

test_that("get_modelmatrix - clmm, new data with a subset of levels", {
  skip_if_not_installed("ordinal")
  fx <- clmm_fixture()
  s <- fx$data$temp == "cold" & fx$data$ch == "no"
  expected <- get_modelmatrix(fx$model)[which(s), , drop = FALSE]
  # (a) rows of the model data, all factor levels kept
  nd <- fx$data[s, ]
  expect_modelmatrix(get_modelmatrix(fx$model, data = nd), expected)
  # (b) unused factor levels dropped
  nd <- droplevels(fx$data[s, ])
  expect_modelmatrix(get_modelmatrix(fx$model, data = nd), expected)
  # (c) as (b), without the response column
  nd$rating <- NULL
  expect_modelmatrix(get_modelmatrix(fx$model, data = nd), expected)
})

test_that("get_modelmatrix - clmm, user contrasts replace model contrasts", {
  skip_if_not_installed("ordinal")
  fx <- clmm_fixture()
  expected <- stats::model.matrix(
    ~ temp + ch,
    fx$data,
    contrasts.arg = list(temp = "contr.helmert")
  )
  expect_no_warning({
    out <- get_modelmatrix(fx$model, contrasts.arg = list(temp = "contr.helmert"))
  })
  expect_modelmatrix(out, expected)
})

test_that("get_modelmatrix - brmsfit with sum contrasts, new data with a subset of levels", {
  skip_if_not_installed("brms")
  fx <- brms_fixture()
  # the full model matrix uses the sum contrasts of the factor
  expect_modelmatrix(
    get_modelmatrix(fx$model),
    stats::model.matrix(~ cyl + wt, fx$data, contrasts.arg = list(cyl = "contr.sum"))
  )
  s <- fx$data$cyl %in% c("4", "6")
  expected <- get_modelmatrix(fx$model)[which(s), , drop = FALSE]
  # (a) rows of the model data, all factor levels and the contrasts kept
  nd <- fx$data[s, ]
  expect_modelmatrix(get_modelmatrix(fx$model, data = nd), expected)
  # (b) unused factor levels dropped
  nd <- droplevels(fx$data[s, ])
  expect_modelmatrix(get_modelmatrix(fx$model, data = nd), expected)
  # (c) as (b), factor re-made with all levels and no contrasts attribute
  nd$cyl <- factor(as.character(nd$cyl), levels = levels(fx$data$cyl))
  expect_modelmatrix(get_modelmatrix(fx$model, data = nd), expected)
})

test_that("get_modelmatrix - brmsfit, user contrasts replace model contrasts", {
  skip_if_not_installed("brms")
  fx <- brms_fixture()
  expected <- stats::model.matrix(
    ~ cyl + wt,
    fx$data,
    contrasts.arg = list(cyl = "contr.helmert")
  )
  expect_no_warning({
    out <- get_modelmatrix(fx$model, contrasts.arg = list(cyl = "contr.helmert"))
  })
  expect_modelmatrix(out, expected)
})

test_that("get_modelmatrix - brmsfit, no warning for contrasts of a grouping factor", {
  skip_if_not_installed("brms")
  d_brms <- mtcars
  d_brms$cyl <- factor(d_brms$cyl)
  contrasts(d_brms$cyl) <- contr.sum(3)
  m <- suppressMessages(suppressWarnings(
    brms::brm(mpg ~ wt + (1 | cyl), data = d_brms, empty = TRUE)
  ))
  expect_no_warning({
    out <- get_modelmatrix(m)
  })
  expect_identical(colnames(out), c("(Intercept)", "wt"))
})

test_that("get_modelmatrix - clmm fitted with model = FALSE", {
  skip_if_not_installed("ordinal")
  fx <- clmm_fixture()
  w3 <- fx$data
  m <- ordinal::clmm(
    rating ~ temp + ch + (1 | judge),
    data = w3,
    contrasts = list(temp = "contr.sum"),
    model = FALSE
  )
  expect_modelmatrix(get_modelmatrix(m), get_modelmatrix(fx$model))
})


# new data with NA values or unknown factor levels keeps its rows ----

# data names that no other test file uses
ndrows_fixture <- function() {
  set.seed(5)
  d_ndrows <- data.frame(
    x = rnorm(120),
    f = factor(sample(c("a", "b", "c"), 120, replace = TRUE)),
    grp = factor(rep(1:12, each = 10))
  )
  d_ndrows$y <- 1 + d_ndrows$x + as.numeric(d_ndrows$f) + rnorm(120)
  d_ndrows$yo <- cut(d_ndrows$y, 4, labels = c("q1", "q2", "q3", "q4"), ordered_result = TRUE)
  d_ndrows
}

# 5 rows: rows 1 and 5 complete, row 2 NA in `x`, row 3 NA in `f`,
# row 4 a level of `f` ("d") that the model data does not have
ndrows_newdata <- function(d_ndrows, character = FALSE) {
  f <- c("a", "b", NA, "d", "c")
  nd_ndrows <- data.frame(
    x = c(0.5, NA, 1, -0.2, 0.8),
    f = if (character) f else factor(f, levels = c("a", "b", "c", "d")),
    grp = factor(rep(1, 5), levels = levels(d_ndrows$grp)),
    y = 0,
    yo = d_ndrows$yo[rep(1, 5)],
    stringsAsFactors = FALSE
  )
  nd_ndrows
}

expect_ndrows_modelmatrix <- function(m, nd_ndrows) {
  out <- get_modelmatrix(m, data = nd_ndrows)
  expect_identical(nrow(out), 5L)
  expect_modelmatrix(
    out[c(1, 5), , drop = FALSE],
    get_modelmatrix(m, data = nd_ndrows[c(1, 5), ])
  )
  expect_true(all(apply(out[2:4, , drop = FALSE], 1, anyNA)))
}

test_that("get_modelmatrix - lme, gls and clmm, new data rows with NA or unknown levels kept", {
  skip_if_not_installed("nlme")
  skip_if_not_installed("ordinal")
  d_ndrows <- ndrows_fixture()
  models <- list(
    lme = nlme::lme(y ~ x + f, random = ~ 1 | grp, data = d_ndrows),
    gls = nlme::gls(y ~ x + f, data = d_ndrows),
    clmm = ordinal::clmm(yo ~ x + f + (1 | grp), data = d_ndrows)
  )
  for (m in models) {
    # `f` as factor with the extra level "d", and as character vector
    expect_ndrows_modelmatrix(m, ndrows_newdata(d_ndrows))
    expect_ndrows_modelmatrix(m, ndrows_newdata(d_ndrows, character = TRUE))
  }
})

test_that("get_modelmatrix - brmsfit, new data rows with NA or unknown levels kept", {
  skip_if_not_installed("brms")
  d_ndrows <- ndrows_fixture()
  m <- suppressMessages(suppressWarnings(
    brms::brm(y ~ x + f, data = d_ndrows, empty = TRUE)
  ))
  expect_ndrows_modelmatrix(m, ndrows_newdata(d_ndrows))
  expect_ndrows_modelmatrix(m, ndrows_newdata(d_ndrows, character = TRUE))
})


# ordered predictor given as character vector in new data ----

ndord_fixture <- function() {
  set.seed(6)
  d_ndord <- data.frame(
    x = rnorm(90),
    g = factor(sample(c("lo", "mid", "hi"), 90, replace = TRUE), levels = c("lo", "mid", "hi"), ordered = TRUE),
    grp = factor(rep(1:9, each = 10))
  )
  d_ndord$y <- d_ndord$x + as.numeric(d_ndord$g) + rnorm(90)
  d_ndord$yo <- cut(d_ndord$y, 3, labels = c("q1", "q2", "q3"), ordered_result = TRUE)
  d_ndord
}

expect_ndord_modelmatrix <- function(m, d_ndord) {
  nd_ndord <- data.frame(
    x = c(0, 1, 2),
    g = c("mid", "hi", "lo"),
    grp = factor(1),
    y = 0,
    stringsAsFactors = FALSE
  )
  ndo_ndord <- nd_ndord
  ndo_ndord$g <- factor(ndo_ndord$g, levels = levels(d_ndord$g), ordered = TRUE)
  expected <- stats::model.matrix(~g, ndo_ndord)[, c("g.L", "g.Q")]
  out <- get_modelmatrix(m, data = nd_ndord)
  expect_true(all(c("g.L", "g.Q") %in% colnames(out)))
  expect_modelmatrix(out[, c("g.L", "g.Q")], expected)
}

test_that("get_modelmatrix - lme, gls and clmm, ordered predictor given as character in new data", {
  skip_if_not_installed("nlme")
  skip_if_not_installed("ordinal")
  d_ndord <- ndord_fixture()
  expect_ndord_modelmatrix(nlme::lme(y ~ x + g, random = ~ 1 | grp, data = d_ndord), d_ndord)
  expect_ndord_modelmatrix(nlme::gls(y ~ x + g, data = d_ndord), d_ndord)
  expect_ndord_modelmatrix(ordinal::clmm(yo ~ x + g + (1 | grp), data = d_ndord), d_ndord)
})

test_that("get_modelmatrix - brmsfit, ordered predictor given as character in new data", {
  skip_if_not_installed("brms")
  d_ndord <- ndord_fixture()
  m <- suppressMessages(suppressWarnings(
    brms::brm(y ~ x + g, data = d_ndord, empty = TRUE)
  ))
  expect_ndord_modelmatrix(m, d_ndord)
})


# Arima --------------------------------------------------------------
# ====================================================================

# names of `coef()` without the AR, MA, SAR and SMA coefficients
arima_regression_names <- function(m) {
  n_arma <- sum(m$arma[1:4])
  names(stats::coef(m))[setdiff(seq_along(stats::coef(m)), seq_len(n_arma))]
}

test_that("get_modelmatrix - Arima with xreg", {
  arima_m1 <- stats::arima(LakeHuron, order = c(2, 0, 0), xreg = time(LakeHuron))
  out <- get_modelmatrix(arima_m1)
  # a plain matrix, not a time series ("mts") matrix
  expect_true(is.matrix(out))
  expect_null(oldClass(out))
  expect_identical(nrow(out), 98L)
  expect_identical(colnames(out), c("intercept", "time(LakeHuron)"))
  expect_identical(
    colnames(out),
    setdiff(get_parameters(arima_m1)$Parameter, c("ar1", "ar2"))
  )
  expect_identical(unname(out[, 1]), rep(1, 98))
  expect_identical(unname(out[, 2]), as.numeric(time(LakeHuron)))
})

test_that("get_modelmatrix - Arima, columns follow coef()", {
  # differenced series: arima fits no intercept
  arima_d1 <- stats::arima(LakeHuron, order = c(1, 1, 0), xreg = time(LakeHuron))
  # no mean, two named regressors
  arima_nomean <- stats::arima(
    LakeHuron,
    order = c(1, 0, 0),
    include.mean = FALSE,
    xreg = cbind(a = time(LakeHuron), b = seq_along(LakeHuron))
  )
  # seasonal MA terms
  arima_seas <- stats::arima(
    USAccDeaths,
    order = c(0, 1, 1),
    seasonal = list(order = c(0, 1, 1)),
    xreg = sin(seq_along(USAccDeaths))
  )
  # regressors given as a data frame
  arima_xreg_df <- data.frame(
    t = as.numeric(time(LakeHuron)),
    s = sin(seq_along(LakeHuron))
  )
  arima_df <- stats::arima(LakeHuron, order = c(1, 0, 0), xreg = arima_xreg_df)
  # one missing value in the series
  arima_lh_na <- LakeHuron
  arima_lh_na[10] <- NA
  arima_na <- stats::arima(arima_lh_na, order = c(1, 0, 0), xreg = time(LakeHuron))

  for (m in list(arima_d1, arima_nomean, arima_seas, arima_df, arima_na)) {
    out <- get_modelmatrix(m)
    expect_identical(colnames(out), arima_regression_names(m))
    expect_identical(nrow(out), length(stats::residuals(m)))
  }
  expect_identical(colnames(get_modelmatrix(arima_d1)), "time(LakeHuron)")
  expect_identical(colnames(get_modelmatrix(arima_seas)), "sin(seq_along(USAccDeaths))")
  expect_identical(colnames(get_modelmatrix(arima_df)), c("intercept", "t", "s"))
  expect_identical(unname(get_modelmatrix(arima_df)[, "s"]), sin(seq_along(LakeHuron)))
  expect_identical(nrow(get_modelmatrix(arima_na)), 98L)
})

test_that("get_modelmatrix - Arima without xreg", {
  arima_ar1 <- stats::arima(LakeHuron, order = c(1, 0, 0))
  out <- get_modelmatrix(arima_ar1)
  expect_identical(dim(out), c(98L, 1L))
  expect_identical(colnames(out), "intercept")
  expect_identical(unname(out[, 1]), rep(1, 98))

  # no regression coefficient at all
  arima_ari <- stats::arima(LakeHuron, order = c(1, 1, 0))
  out <- get_modelmatrix(arima_ari)
  expect_identical(dim(out), c(98L, 0L))
})

test_that("get_modelmatrix - Arima, regressors that cannot be used", {
  # the test creates and removes a global `z`
  skip_if(exists("z", envir = globalenv(), inherits = FALSE), "a global `z` exists")
  arima_local_fit <- function() {
    z <- time(LakeHuron)
    stats::arima(LakeHuron, order = c(1, 0, 0), xreg = z)
  }
  arima_local <- arima_local_fit()
  # `z` exists only inside the fitting function
  expect_error(
    get_modelmatrix(arima_local),
    "Could not find the regressors `z`",
    fixed = TRUE
  )

  # a local regressor with the name of a base function (`q()`)
  arima_local_q_fit <- function() {
    q <- time(LakeHuron) # nolint: the test needs the name of a base function
    stats::arima(LakeHuron, order = c(1, 0, 0), xreg = q)
  }
  expect_error(
    get_modelmatrix(arima_local_q_fit()),
    "Could not find the regressors `q`",
    fixed = TRUE
  )

  # a global `z` with another number of rows is not used
  assign("z", 1:3, envir = globalenv())
  withr::defer(rm("z", envir = globalenv()))
  expect_error(
    get_modelmatrix(arima_local),
    "The regressors `z` have 3 rows, but the model has 98 observations",
    fixed = TRUE
  )

  # new data
  arima_m1 <- stats::arima(LakeHuron, order = c(2, 0, 0), xreg = time(LakeHuron))
  expect_error(
    get_modelmatrix(arima_m1, data = data.frame(x = 1)),
    "New data is not supported for arima models",
    fixed = TRUE
  )
  # new data given by position
  expect_error(
    get_modelmatrix(arima_m1, data.frame(x = 1)),
    "New data is not supported for arima models",
    fixed = TRUE
  )
  # an argument whose name starts with "data" is not new data
  expect_identical(dim(get_modelmatrix(arima_m1, datagrid = 1)), c(98L, 2L))
})

test_that("get_modelmatrix - Arima, regressors found by name", {
  # a regressor object that holds NULL means no regressors
  arima_xr_null <- NULL
  arima_null <- stats::arima(LakeHuron, order = c(1, 0, 0), xreg = arima_xr_null)
  out <- get_modelmatrix(arima_null)
  expect_identical(dim(out), c(98L, 1L))
  expect_identical(colnames(out), "intercept")

  # a function that calls get_modelmatrix() has a local variable with the
  # name of the regressors, but another size; the global object is used
  skip_if(
    exists("arima_xr_global", envir = globalenv(), inherits = FALSE),
    "a global `arima_xr_global` exists"
  )
  assign("arima_xr_global", time(LakeHuron), envir = globalenv())
  withr::defer(rm("arima_xr_global", envir = globalenv()))
  arima_global <- stats::arima(LakeHuron, order = c(1, 0, 0), xreg = arima_xr_global)
  arima_wrapper <- function(model) {
    arima_xr_global <- 1:14
    get_modelmatrix(model)
  }
  out <- arima_wrapper(arima_global)
  expect_identical(dim(out), c(98L, 2L))
  expect_identical(unname(out[, 2]), as.numeric(time(LakeHuron)))
})

test_that("get_modelmatrix - Arima with stored xreg set by hand, as in forecast::Arima() fits", {
  arima_m1 <- stats::arima(LakeHuron, order = c(2, 0, 0), xreg = time(LakeHuron))
  arima_m1$xreg <- cbind(xreg = 2 * as.numeric(time(LakeHuron)))
  arima_m1$call$xreg <- as.name("arima_no_such_object")
  out <- get_modelmatrix(arima_m1)
  expect_identical(unname(out[, 2]), 2 * as.numeric(time(LakeHuron)))
  expect_identical(colnames(out), c("intercept", "time(LakeHuron)"))

  # stored regressors with more columns than regression coefficients
  arima_m1$xreg <- cbind(a = 1:98, b = 1:98, c = 1:98)
  expect_error(
    get_modelmatrix(arima_m1),
    "The regressors `xreg` have 3 columns, but the model has 2",
    fixed = TRUE
  )
})

Try the insight package in your browser

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

insight documentation built on Oct. 28, 2026, 5:11 p.m.