-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
140c562
commit 5b55336
Showing
8 changed files
with
121 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
YEAR: 2024 | ||
COPYRIGHT HOLDER: fastmed authors | ||
COPYRIGHT HOLDER: Daehwan Kim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(mediation_analysis) | ||
import(Rcpp) | ||
import(data.table) | ||
importFrom(Rcpp,evalCpp) | ||
importFrom(RcppParallel,setThreadOptions) | ||
useDynLib(fastmed, .registration = TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,123 @@ | ||
# tests/testthat/test-mediation_analysis.R | ||
test_that("mediation_analysis works correctly with default prefixes", { | ||
# Create example data | ||
set.seed(123) | ||
test_data_default <- data.table( | ||
test_data_default <- data.table::data.table( | ||
CECUM1 = rnorm(50), | ||
CECUM2 = rnorm(50), | ||
SERUM1 = rnorm(50), | ||
SERUM2 = rnorm(50), | ||
CORTEX1 = rnorm(50), | ||
CORTEX2 = rnorm(50) | ||
) | ||
|
||
# Temporary output CSV file | ||
output_csv <- tempfile(fileext = ".csv") | ||
|
||
# Use local_tempfile to create a temporary output CSV file | ||
output_csv <- withr::local_tempfile(fileext = ".csv") | ||
# Run mediation_analysis function | ||
mediation_analysis( | ||
data = test_data_default, | ||
columns = list(exposure = c("CECUM"), mediator = c("SERUM"), outcome = c("CORTEX")), | ||
nrep = 100, # Reduced for faster testing | ||
output_file = output_csv, | ||
num_threads = 2 # Adjust thread count for testing environment | ||
output_file = output_csv, # Use the temporary file name | ||
num_threads = 1 # Adjust thread count for testing environment | ||
) | ||
|
||
# Read results | ||
results <- fread(output_csv) | ||
|
||
results <- data.table::fread(output_csv) | ||
# Expected result: 2 (CECUM) * 2 (SERUM) * 2 (CORTEX) = 8 combinations | ||
expect_equal(nrow(results), 8) | ||
|
||
# Remove temporary file | ||
file.remove(output_csv) | ||
}) | ||
|
||
test_that("mediation_analysis works correctly with custom prefixes", { | ||
# Create example data | ||
set.seed(123) | ||
test_data_custom <- data.table( | ||
test_data_custom <- data.table::data.table( | ||
EXP1 = rnorm(50), | ||
EXP2 = rnorm(50), | ||
MED1 = rnorm(50), | ||
MED2 = rnorm(50), | ||
OUT1 = rnorm(50), | ||
OUT2 = rnorm(50) | ||
) | ||
|
||
# Temporary output CSV file | ||
output_csv <- tempfile(fileext = ".csv") | ||
|
||
# Use local_tempfile to create a temporary output CSV file | ||
output_csv <- withr::local_tempfile(fileext = ".csv") | ||
# Run mediation_analysis function | ||
mediation_analysis( | ||
data = test_data_custom, | ||
columns = list(exposure = c("EXP"), mediator = c("MED"), outcome = c("OUT")), | ||
nrep = 100, # Reduced for faster testing | ||
output_file = output_csv, | ||
num_threads = 2 # Adjust thread count for testing environment | ||
output_file = output_csv, # Use the temporary file name | ||
num_threads = 1 # Adjust thread count for testing environment | ||
) | ||
|
||
# Read results | ||
results <- fread(output_csv) | ||
|
||
results <- data.table::fread(output_csv) | ||
# Expected result: 2 (EXP) * 2 (MED) * 2 (OUT) = 8 combinations | ||
expect_equal(nrow(results), 8) | ||
|
||
# Remove temporary file | ||
file.remove(output_csv) | ||
}) | ||
|
||
test_that("mediation_analysis handles large datasets correctly", { | ||
# Create large example data | ||
set.seed(123) | ||
test_data_large <- data.table( | ||
test_data_large <- data.table::data.table( | ||
CECUM1 = rnorm(1000), | ||
CECUM2 = rnorm(1000), | ||
SERUM1 = rnorm(1000), | ||
SERUM2 = rnorm(1000), | ||
CORTEX1 = rnorm(1000), | ||
CORTEX2 = rnorm(1000) | ||
) | ||
|
||
# Temporary output CSV file | ||
output_csv <- tempfile(fileext = ".csv") | ||
|
||
# Use local_tempfile to create a temporary output CSV file | ||
output_csv <- withr::local_tempfile(fileext = ".csv") | ||
# Run mediation_analysis function | ||
mediation_analysis( | ||
data = test_data_large, | ||
columns = list(exposure = c("CECUM"), mediator = c("SERUM"), outcome = c("CORTEX")), | ||
nrep = 100, # Reduced for faster testing | ||
output_file = output_csv, | ||
num_threads = 2 # Adjust thread count for testing environment | ||
output_file = output_csv, # Use the temporary file name | ||
num_threads = 1 # Adjust thread count for testing environment | ||
) | ||
|
||
# Read results | ||
results <- fread(output_csv) | ||
|
||
results <- data.table::fread(output_csv) | ||
# Expected result: 2 (CECUM) * 2 (SERUM) * 2 (CORTEX) = 8 combinations | ||
expect_equal(nrow(results), 8) | ||
|
||
# Remove temporary file | ||
file.remove(output_csv) | ||
}) | ||
|
||
test_that("mediation_analysis works correctly with multiple threads", { | ||
# Create large example data | ||
set.seed(123) | ||
test_data_large <- data.table::data.table( | ||
CECUM1 = rnorm(1000), | ||
CECUM2 = rnorm(1000), | ||
SERUM1 = rnorm(1000), | ||
SERUM2 = rnorm(1000), | ||
CORTEX1 = rnorm(1000), | ||
CORTEX2 = rnorm(1000) | ||
) | ||
|
||
# Use local_tempfile to create a temporary output CSV file | ||
output_csv <- withr::local_tempfile(fileext = ".csv") | ||
|
||
# Run mediation_analysis function | ||
mediation_analysis( | ||
data = test_data_large, | ||
columns = list(exposure = c("CECUM"), mediator = c("SERUM"), outcome = c("CORTEX")), | ||
nrep = 100, # Reduced for faster testing | ||
output_file = output_csv, # Use the temporary file name | ||
num_threads = 4 # Adjust thread count for testing environment | ||
) | ||
|
||
# Read results | ||
results <- data.table::fread(output_csv) | ||
|
||
# Expected result: 2 (CECUM) * 2 (SERUM) * 2 (CORTEX) = 8 combinations | ||
expect_equal(nrow(results), 8) | ||
}) |