summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--DESCRIPTION6
-rw-r--r--NAMESPACE2
-rw-r--r--R/simulate_data.R16
-rw-r--r--man/simulate_data.Rd5
-rw-r--r--tests/testthat.R12
-rw-r--r--tests/testthat/test-simulate_data.R4
6 files changed, 32 insertions, 13 deletions
diff --git a/DESCRIPTION b/DESCRIPTION
index 34d6b7b..2e67bd8 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -8,3 +8,9 @@ License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 8.0.0
+Imports:
+ MASS,
+ stats
+Suggests:
+ testthat (>= 3.0.0)
+Config/testthat/edition: 3
diff --git a/NAMESPACE b/NAMESPACE
index 5045b57..59ce237 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -2,3 +2,5 @@
export(simulate_data)
importFrom(MASS,mvrnorm)
+importFrom(stats,rchisq)
+importFrom(stats,runif)
diff --git a/R/simulate_data.R b/R/simulate_data.R
index 2338805..297b76f 100644
--- a/R/simulate_data.R
+++ b/R/simulate_data.R
@@ -15,11 +15,12 @@
#' @return A list with X, S and a vector identifying the contaminated rows.
#'
#' @examples
-#' X_obj <- generate_data(n = 10, p = 10)
+#' X_obj <- simulate_data(n = 10, p = 10)
#' X <- X_obj$X
#' S_true <- X_obj$S
#'
#' @importFrom MASS mvrnorm
+#' @importFrom stats rchisq runif
#'
#' @export
simulate_data <- function(
@@ -28,8 +29,7 @@ simulate_data <- function(
rho = 0.8,
contamination = 0,
block_fraction = 0.5,
- t_df = Inf,
- exact_contamination = FALSE
+ t_df = Inf
){
if(any(
rho < 0,
@@ -56,7 +56,7 @@ simulate_data <- function(
# Regular clean observations
Z <- MASS::mvrnorm(n = n, mu = numeric(p), Sigma = Sigma)
if(is.finite(t_df)){
- w <- rchisq(n, df = t_df)
+ w <- stats::rchisq(n, df = t_df)
X <- Z / sqrt(w / t_df)
}else{
X <- Z
@@ -65,15 +65,11 @@ simulate_data <- function(
# Contamination - by row
outlier_rows <- integer(0)
if(contamination > 0){
- if(exact_contamination){
- outlier_rows <- sample.int(n, ceiling(contamination * n))
- }else{
- outlier_rows <- which(runif(n) < contamination)
- }
+ outlier_rows <- which(stats::runif(n) < contamination)
if(length(outlier_rows) > 0){
X[outlier_rows, ] <- matrix(
- rchisq(length(outlier_rows) * p, df = 1),
+ stats::rchisq(length(outlier_rows) * p, df = 1),
nrow = length(outlier_rows),
ncol = p
)
diff --git a/man/simulate_data.Rd b/man/simulate_data.Rd
index b362518..09ae881 100644
--- a/man/simulate_data.Rd
+++ b/man/simulate_data.Rd
@@ -10,8 +10,7 @@ simulate_data(
rho = 0.8,
contamination = 0,
block_fraction = 0.5,
- t_df = Inf,
- exact_contamination = FALSE
+ t_df = Inf
)
}
\arguments{
@@ -37,7 +36,7 @@ with the intention of evaluating the performance
of covariance or correlation matrix estimators.
}
\examples{
-X_obj <- generate_data(n = 10, p = 10)
+X_obj <- simulate_data(n = 10, p = 10)
X <- X_obj$X
S_true <- X_obj$S
diff --git a/tests/testthat.R b/tests/testthat.R
new file mode 100644
index 0000000..7b29596
--- /dev/null
+++ b/tests/testthat.R
@@ -0,0 +1,12 @@
+# This file is part of the standard setup for testthat.
+# It is recommended that you do not modify it.
+#
+# Where should you do additional test configuration?
+# Learn more about the roles of various files in:
+# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
+# * https://testthat.r-lib.org/articles/special-files.html
+
+library(testthat)
+library(RSC.analysis)
+
+test_check("RSC.analysis")
diff --git a/tests/testthat/test-simulate_data.R b/tests/testthat/test-simulate_data.R
new file mode 100644
index 0000000..9d0cba3
--- /dev/null
+++ b/tests/testthat/test-simulate_data.R
@@ -0,0 +1,4 @@
+test_that("Returns a true sparse correlation matrix", {
+ X_obj <- simulate_data(n = 10, p = 10, rho = 0)
+ expect_equal(X_obj$S, diag(nrow = 10))
+})