tests/testthat/test-myLM.R

test_that("myLM works", {
  data("iris")
  rand = runif(nrow(iris))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width, data = iris)$coefficients),6), c(1.083558,2.229940))
  expect_error(myLM(1))
  expect_error(myLM(Petal.Length~Petal.Width))
  expect_warning(myLM(Petal.Length~Petal.Width, data = iris, method = "ayyy"))
  expect_error(myLM(Petal.Length~Petal.Width, data = as.matrix(iris)))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width,data = iris, subset = "Petal.Width<2")$coefficients),4), c(0.9043, 2.4955))
  expect_error(myLM(Petal.Length~Petal.Width,data = iris, subset = Petal.Width<2))
  expect_error(myLM(Petal.Length~Petal.Width,data = iris, subset = 123))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width,data = iris, weights = rand)$coefficients),6), round(as.numeric(lm(Petal.Length~Petal.Width,data = iris, weights = rand)$coefficients),6))
  expect_error(myLM(Petal.Length~Petal.Width,data = iris, weights = 1))
  expect_error(myLM(Petal.Length~Petal.Width,data = iris, qr = 123))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width, data = iris, qr = F)$coefficients),6), c(1.083558,2.229940))
  expect_error(myLM(Petal.Length~Petal.Width,data = iris, model = 123))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width, data = iris, model = F)$coefficients),6), c(1.083558,2.229940))
  expect_error(myLM(Petal.Length~Petal.Width,data = iris, x = 123))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width, data = iris, x = T)$coefficients),6), c(1.083558,2.229940))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width, data = iris, x = F)$coefficients),6), c(1.083558,2.229940))
  expect_error(myLM(Petal.Length~Petal.Width,data = iris, y = 123))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width, data = iris, y = T)$coefficients),6), c(1.083558,2.229940))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width, data = iris, y = F)$coefficients),6), c(1.083558,2.229940))
  expect_equal(round(as.numeric(myLM(Petal.Length~Petal.Width, data = iris,na.actions = na.exclude)$coefficients),6), c(1.083558,2.229940))
  expect_equal(round(as.numeric(myLM(iris$Petal.Length~iris$Petal.Width)$coefficients),6), c(1.083558,2.229940))
  expect_equal(as.matrix(myLM(iris$Petal.Length~iris$Petal.Width, method = "model.frame")), as.matrix(lm(iris$Petal.Length~iris$Petal.Width, method = "model.frame")))
  expect_equal(as.matrix(myLM(Petal.Length~Petal.Width,data = iris, method = "model.frame")), as.matrix(lm(Petal.Length~Petal.Width,data = iris, method = "model.frame")))
})
wjhlang/myLM documentation built on Dec. 23, 2021, 5:16 p.m.