Nothing
library(testthat)
# Create test data
test_that("MFdiv requires at least 3 columns", {
single_col <- matrix(runif(4), ncol = 1)
expect_error(MFdiv(single_col), "The number of functions must be greater than 2!")
two_col <- matrix(runif(8), ncol = 2)
expect_error(MFdiv(two_col), "The number of functions must be greater than 2!")
})
test_that("MFdiv function basic functionality tests", {
# Create simple test dataset
set.seed(123)
test_data <- matrix(runif(20), nrow = 4, ncol = 5)
colnames(test_data) <- paste0("func", 1:5)
rownames(test_data) <- paste0("site", 1:4)
# Test default parameters
result_default <- MFdiv(test_data)
expect_s3_class(result_default, "data.frame")
expect_equal(nrow(result_default), 4)
expect_equal(ncol(result_default), 1)
expect_equal(colnames(result_default), "MFdiv")
expect_equal(rownames(result_default), rownames(test_data))
# Test uncorrected mode
result_uncor <- MFdiv(test_data, cor = FALSE)
expect_equal(result_default, result_uncor) # Default should be uncorrected
# Test corrected mode
result_cor <- MFdiv(test_data, cor = TRUE)
expect_s3_class(result_cor, "data.frame")
expect_equal(dim(result_cor), dim(result_default))
expect_equal(colnames(result_cor), colnames(result_default))
# Test weights parameter
weights <- c(0.5, 1, 1, 2, 0.8)
result_weighted <- MFdiv(test_data, weights = weights)
expect_s3_class(result_weighted, "data.frame")
# Test weight error handling
expect_error(MFdiv(test_data, weights = c(1, 2, 3)),
"The length of the weight vector must be equal to the number of columns in the data frame")
})
test_that("MFdiv handles special cases", {
# Test single row data
single_row <- matrix(runif(5), nrow = 1)
result_single <- MFdiv(single_row)
expect_equal(nrow(result_single), 1)
# Test single column data
single_col <- matrix(runif(4), ncol = 1)
expect_error(MFdiv(single_col), "The number of functions must be greater than 2!")
# Test data with zero values
test_data_zeros <- matrix(c(0.5, 0, 0.7, 0.3, 0, 0.6), nrow = 2)
result_zeros <- MFdiv(test_data_zeros)
expect_s3_class(result_zeros, "data.frame")
# Test data with negative values
test_data_neg <- matrix(c(0.5, -0.2, 0.7, 0.3, -0.5, 0.6), nrow = 2)
result_neg <- MFdiv(test_data_neg)
expect_s3_class(result_neg, "data.frame")
})
test_that("MFdiv consistency tests with example dataset", {
# Load example data
data(forestfunctions)
# Test uncorrected results
result_forest_uncor <- MFdiv(forestfunctions[,6:31], cor = FALSE)
expect_s3_class(result_forest_uncor, "data.frame")
expect_equal(nrow(result_forest_uncor), nrow(forestfunctions))
# Test corrected results
result_forest_cor <- MFdiv(forestfunctions[,6:31], cor = TRUE)
expect_s3_class(result_forest_cor, "data.frame")
expect_equal(nrow(result_forest_cor), nrow(forestfunctions))
# Note: corrected and uncorrected results should be different
expect_false(identical(result_forest_cor, result_forest_uncor))
})
# Test edge cases
test_that("MFdiv edge case handling", {
# All zeros data - may produce warnings about standard deviation
all_zeros <- matrix(0, nrow = 3, ncol = 4)
suppressWarnings({
result_all_zeros <- MFdiv(all_zeros)
})
expect_s3_class(result_all_zeros, "data.frame")
# All same values - may produce warnings about standard deviation
all_same <- matrix(0.5, nrow = 3, ncol = 4)
suppressWarnings({
result_all_same <- MFdiv(all_same)
})
expect_s3_class(result_all_same, "data.frame")
# Test perfectly correlated functions - uncorrected
perfect_corr <- matrix(c(1:6, 3*(1:6)), ncol = 3)
result_corr_uncor <- MFdiv(perfect_corr, cor = FALSE)
expect_s3_class(result_corr_uncor, "data.frame")
# Test perfectly correlated functions - corrected
result_corr_cor <- MFdiv(perfect_corr, cor = TRUE)
expect_s3_class(result_corr_cor, "data.frame")
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.