tests/testthat/test-lm2.R

test_that("lm2 works", {
  mtcars$cyl <- factor(mtcars$cyl)
  m <- lm(mpg ~ hp * cyl, data = mtcars)

  x <- model.matrix(m)
  y <- mtcars$mpg
  m2 <- JWileymisc:::lm2(mpg ~ 1 + cyl + hp:cyl, data = mtcars,
                         designMatrix = x[, -2, drop = FALSE],
                         yObserved = y)

  expect_true(inherits(m2, "lm"))
  expect_null(m2[["x"]])
  expect_null(m2[["y"]])
  expect_false(is.null(m2[["qr"]]))

  m2 <- JWileymisc:::lm2(mpg ~ 1 + cyl + hp:cyl, data = mtcars,
                         designMatrix = x[, -2, drop = FALSE],
                         yObserved = y,
                         x = TRUE, y = TRUE, qr = FALSE)

  expect_true(inherits(m2, "lm"))
  expect_true(inherits(m2[["x"]], "matrix"))
  expect_type(m2[["y"]], "double")
  expect_null(m2[["qr"]])

  m2 <- JWileymisc:::lm2(mpg ~ 0, data = mtcars,
                         designMatrix = x[, -2, drop = FALSE],
                         yObserved = y,
                         offset = mtcars$hp)

  expect_true(inherits(m2, "lm"))
})

Try the JWileymisc package in your browser

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

JWileymisc documentation built on Oct. 5, 2023, 5:06 p.m.