Skip to content

Commit

Permalink
Added unit tests for #28
Browse files Browse the repository at this point in the history
  • Loading branch information
wleoncio committed Mar 1, 2024
1 parent 0a92a27 commit 122566f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/testthat/test-reg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Original function ============================================================
reg_R <- function(r, Z) {
K <- ncol(Z)

Check warning on line 3 in tests/testthat/test-reg.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-reg.R,line=3,col=3,[object_usage_linter] local variable 'K' assigned but may not be used
N <- nrow(Z)

Check warning on line 4 in tests/testthat/test-reg.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-reg.R,line=4,col=3,[object_usage_linter] local variable 'N' assigned but may not be used
beta01 <- matrix(0, 1, ncol(r))
theta01 <- matrix(0, ncol(Z), ncol(r))
for (e in 1:ncol(r)) {
new1 <- lm(r[, e] ~ Z, singular.ok = TRUE)
beta01[e] <- matrix(new1$coefficients[1])
theta01[, e] <- as.vector(new1$coefficients[-1])
}
return(list(beta0 = beta01, theta0 = theta01))
}

# Testing ======================================================================
reps <- 10L
n_obs <- rpois(reps, lambda = 10L)
n_vars <- sample(2:10, reps, replace = TRUE)
test_that("reg() produces the correct output", {
for (rp in seq_len(reps)) {
r <- matrix(rnorm(n_obs[rp] * n_vars[rp]), n_obs[rp], n_vars[rp])
z <- as.matrix(sample(0:1, n_obs[rp], replace = TRUE))
expect_equal(reg(r, z), reg_R(r, z))

Check warning on line 23 in tests/testthat/test-reg.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-reg.R,line=23,col=5,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
}
})

0 comments on commit 122566f

Please sign in to comment.