Switch to unified view

a b/tests/testthat/test-getNcomp.R
1
context("getNcomp")
2
3
demo <- suppressWarnings(get_demo_cluster())
4
demo$pca <- mixOmics::pca(X = demo$X, ncomp = 5)
5
demo$pls <- mixOmics::pls(X = demo$X, Y=demo$Y, ncomp =5, mode = "canonical")
6
demo$block.pls = suppressWarnings(mixOmics::block.pls(X=list(X=demo$X, Y=demo$Y, Z=demo$Z), indY=1, ncomp = 5, mode = "canonical"))
7
8
9
test_that("getNcomp failed on invalid input - object", {
10
    # test for "object"
11
    lapply(list("",1,demo$X, NA), function(i) expect_error(getNcomp(i), "invalid object, should be one of c(pca, mixo_pls, block.pls)", fixed = TRUE))
12
})
13
14
test_that("getNcomp failed on invalid input - max.ncomp", {
15
    expect_error(getNcomp(object=demo$pca, max.ncomp = 0), "'max.ncomp' should be greater than 1")
16
    expect_error(getNcomp(object=demo$block.pls, max.ncomp = 0), "'max.ncomp' should be greater than 1")
17
    expect_error(getNcomp(object=demo$block.pls, max.ncomp = 11), "use smaller 'max.ncomp'")
18
    expect_error(getNcomp(object=demo$pca, max.ncomp = 11), "use smaller 'max.ncomp'")
19
})
20
21
test_that("getNcomp failed on invalid input - X", {
22
    # pca
23
    expect_error(getNcomp(object=demo$pca, X = NA), "X must be a numeric matrix/data.frame", fixed = TRUE)
24
    expect_error(getNcomp(object=demo$pca, X = NULL), "X must be a numeric matrix/data.frame", fixed = TRUE)
25
    expect_error(getNcomp(object=demo$pca, X = "ah!"), "X must be a numeric matrix/data.frame", fixed = TRUE)
26
    expect_error(getNcomp(object=demo$pca, X = matrix(LETTERS[1:9])), "X must be a numeric matrix/data.frame", fixed = TRUE)
27
    expect_error(getNcomp(object=demo$pca, X = list()), "X must be a numeric matrix/data.frame", fixed = TRUE)
28
    
29
    # block.pls
30
    expect_error(getNcomp(object=demo$block.pls, X = demo$X), "X must be a list of matrix/data.frame", fixed = TRUE)
31
    expect_error(getNcomp(object=demo$block.pls, X = NA), "X must be a list of matrix/data.frame", fixed = TRUE)
32
    expect_error(getNcomp(object=demo$block.pls, X = NULL), "X must be a list of matrix/data.frame", fixed = TRUE)
33
    expect_error(getNcomp(object=demo$block.pls, X = "ah!"), "X must be a list of matrix/data.frame", fixed = TRUE)
34
    expect_error(getNcomp(object=demo$block.pls, X = list(matrix(LETTERS[1:9]))), "X must be a numeric matrix/data.frame", fixed = TRUE)
35
    expect_error(getNcomp(object=demo$block.pls, X = list("a" = matrix(1:9), "b"= matrix(LETTERS[1:9]))), "X must be a numeric matrix/data.frame", fixed = TRUE)
36
})
37
     
38
test_that("getNcomp failed on invalid input - Y", {
39
    #-- Y  // NULL or matrix
40
    expect_error(getNcomp(object=demo$block.pls, X = list(X =demo$X, Z=demo$Z), Y = ""), "Y must be a numeric matrix/data.frame", fixed = TRUE)
41
    expect_error(getNcomp(object=demo$block.pls, X = list(X =demo$X, Z=demo$Z), Y = NA), "Y must be a numeric matrix/data.frame", fixed = TRUE)
42
})
43
44
test_that("getNcomp failed on invalid input - indY", {   
45
    #-- indY  // if Y is NULL, numeric
46
    expect_error(getNcomp(object=demo$block.pls, X = list(X =demo$X, Z=demo$Z), Y = NULL), "'indY' must be a numeric value lower or equal to 2, the number of blocks in X.", fixed = TRUE)
47
    expect_error(getNcomp(object=demo$block.pls, X = list(X =demo$X, Z=demo$Z), indY = 3), "'indY' must be a numeric value lower or equal to 2, the number of blocks in X.", fixed = TRUE)
48
    expect_error(getNcomp(object=demo$block.pls, X = list(X =demo$X, Z=demo$Z), indY = ""), "'indY' must be a numeric value lower or equal to 2, the number of blocks in X.", fixed = TRUE)
49
    expect_error(getNcomp(object=demo$block.pls, X = list(X =demo$X, Z=demo$Z), indY = NULL), "'indY' must be a numeric value lower or equal to 2, the number of blocks in X.", fixed = TRUE)
50
}) 
51
52
test_that("getNcomp works", {  
53
    expect_is(getNcomp(demo$pca, max.ncomp = 4, X = demo$X), "ncomp.tune.silhouette")
54
    expect_is(getNcomp(demo$pca, max.ncomp = 4.9, X = demo$X), "ncomp.tune.silhouette")
55
    expect_is(getNcomp(demo$pca, max.ncomp = 4, X = demo$X, scale = TRUE, center = TRUE), "ncomp.tune.silhouette")
56
    expect_is(getNcomp(demo$pls, max.ncomp = 4, X = demo$X, Y=demo$Y, scale = TRUE), "ncomp.tune.silhouette")
57
    expect_is(suppressWarnings(getNcomp(demo$block.pls, max.ncomp = 4, X=list(X=demo$X, Z=demo$Z), Y=demo$Y)), "ncomp.tune.silhouette")
58
    expect_is(suppressWarnings(getNcomp(demo$block.pls, max.ncomp = 4, X=list(X=demo$X, Z=demo$Z, Y = demo$Y), indY=3, scale = TRUE)), "ncomp.tune.silhouette")
59
})
60
    
61
pdf(NULL)
62
test_that("getNcomp plot works",{
63
    res <- getNcomp(object = demo$pca, X=demo$X)
64
    expect_is(plot(res), "ggplot")
65
66
    res <- getNcomp(demo$pls, max.ncomp = 4, X = demo$X, Y=demo$Y)
67
    expect_is(plot(res), "ggplot")
68
    
69
    res <- suppressWarnings(getNcomp(demo$block.pls, max.ncomp = 4, X=list(X=demo$X, Z=demo$Z), Y=demo$Y))
70
    expect_is(plot(res), "ggplot")
71
})
72
dev.off()