Skip to content

Commit

Permalink
Added tests for detectSingleOut (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
BartJanvanRossum committed May 21, 2021
1 parent 30d59b6 commit 4218c77
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 10 deletions.
13 changes: 8 additions & 5 deletions R/detectSingleOut.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
#' ## First select a subset of plants, for example here 9 plants
#' plantSel <- phenoTP[[1]]$plotId[1:9]
#' # Then run on the subset
#' resuVatorHTP <- detectSingleOut (TP = phenoTP,
#' trait = "EffpsII",
#' plotIds = plantSel,
#' confIntSize = 3,
#' nnLocfit = 0.1)
#' resuVatorHTP <- detectSingleOut(TP = phenoTP,
#' trait = "EffpsII",
#' plotIds = plantSel,
#' confIntSize = 3,
#' nnLocfit = 0.1)
#'
#' @family functions for detecting outliers for single observations
#'
Expand Down Expand Up @@ -151,6 +151,9 @@ detectSingleOut <- function(TP,
})
## Bind everything together in a single data.frame.
plotPred <- do.call(rbind, plotPreds)
if (is.null(plotPred)) {
stop("Not enough data points (<= 6) for any of the plots.\n")
}
## Rownames are confusing and redundant.
rownames(plotPred) <- NULL
## Add class and trait as attribute.
Expand Down
Binary file added inst/tinytest/singleOut
Binary file not shown.
141 changes: 141 additions & 0 deletions inst/tinytest/test_detectSingleOut.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
### Test detectSingleOut.

## Read test data from .csv
testDat <- read.csv("testDat.csv", stringsAsFactors = FALSE)

## Create TP object.
testTP <- createTimePoints(dat = testDat, experimentName = "testExp",
genotype = "Genotype", timePoint = "timepoints",
plotId = "pos")

## Check that general checks in detectSingleOut function correctly.
expect_error(detectSingleOut(1),
"TP should be an object of class TP")
expect_error(detectSingleOut(testTP, trait = 1),
"trait should be a character string of length 1")
expect_error(detectSingleOut(testTP, trait = c("t1", "t2")),
"trait should be a character string of length 1")
expect_error(detectSingleOut(testTP, trait = "t2"),
"TP should contain a column t2")
expect_error(detectSingleOut(testTP, trait = "t1", plotIds = "c1r1"),
"All plotIds should be in TP")

## Check that check for minimal number of time points functions correctly.
expect_warning(detectSingleOut(testTP, trait = "t1"),
"to fit a model for: c10r2")
expect_error(detectSingleOut(testTP, trait = "t1"),
"for any of the plots")

## testDat only contains 5 timepoints so cannot be used for actual testing.
## Using the data in the package for that instead.
phenoTP <- createTimePoints(dat = PhenovatorDat1,
experimentName = "Phenovator",
genotype = "Genotype",
timePoint = "timepoints",
plotId = "pos")

## Select some plots.
plantSel <- c("c14r32", "c13r17")

singleOut <- detectSingleOut(phenoTP, trait = "EffpsII",
plotIds = plantSel)

## Check that general structure of the output is correct.
expect_inherits(singleOut, c("singleOut", "data.frame"))
expect_equal(dim(singleOut), c(132, 8))
expect_equal(colnames(singleOut),
c("plotId", "timePoint", "EffpsII", "yPred", "sd_yPred",
"lwr", "upr", "outlier"))

## Check that full output contenct is correct.
expect_equal_to_reference(singleOut, file = "singleOut")

## Check that parameter confIntSize functions correctly.

# Setting a high value should lead to no outliers.
singleOut2 <- detectSingleOut(phenoTP, trait = "EffpsII",
plotIds = plantSel,
confIntSize = 15)
expect_equal(sum(singleOut2[["outlier"]]), 0)

# Setting confIntSize to 0 should cause (almost) all observations to be outliers.
singleOut3 <- detectSingleOut(phenoTP, trait = "EffpsII",
plotIds = plantSel,
confIntSize = 0)
expect_equal(sum(singleOut3[["outlier"]]), 132)

## Check that parameter nnLocFit functions correctly.

# A low value of nnLocFit should lead to a decrease of number of outliers.
singleOut4 <- detectSingleOut(phenoTP, trait = "EffpsII",
plotIds = plantSel,
nnLocfit = 0.1)
expect_equal(sum(singleOut4[["outlier"]]), 1)

## Check that parameter checkEdges functions correctly.

# Add an edge outlier.
phenoTP2 <- phenoTP
phenoTP2[[1]][phenoTP2[[1]][["plotId"]] == "c14r32", "EffpsII"] <- 1

# Not checking edges influences outliers detected elsewhere.
singleOut5 <- detectSingleOut(phenoTP2, trait = "EffpsII",
plotIds = "c14r32")
singleOut6 <- detectSingleOut(phenoTP2, trait = "EffpsII",
plotIds = "c14r32",
checkEdges = FALSE)
expect_equal(setdiff(singleOut5[singleOut5[["outlier"]] == 1, "timePoint"],
singleOut6[singleOut6[["outlier"]] == 1, "timePoint"]),
1528448820)
expect_equal(setdiff(singleOut6[singleOut6[["outlier"]] == 1, "timePoint"],
singleOut5[singleOut5[["outlier"]] == 1, "timePoint"]),
1527871020)

### Check plotting of detectSingleOut results.

## Check that general checks in plot function correctly.
expect_error(plot(singleOut, plotIds = "a"),
"All plotIds should be in x")

## Check that general output structure is correct.
expect_silent(p <- plot(singleOut))
expect_inherits(p, "list")
expect_equal(length(p), 1)
expect_inherits(p[[1]], "ggplot")

## Check that parameter outOnly functions correctly.
# singleOut2 has no ouliers.

expect_error(plot(singleOut2),
"No outliers present for selected plotIds")
expect_silent(plot(singleOut2, outOnly = FALSE))

### Check removal of outliers detected by detectSSinleOut

## Check that general checks in plot function correctly.
expect_error(removeSingleOut(1),
"TP should be an object of class TP")
expect_error(removeSingleOut(phenoTP, singleOut = 1),
"singleOut should be a data.frame")
expect_error(removeSingleOut(phenoTP, singleOut = data.frame()),
"singleOut should at least contain the columns plotId and timePoint")
expect_error(removeSingleOut(phenoTP[1], singleOut = singleOut),
"All time points in singleOut should be in TP")
expect_error(removeSingleOut(phenoTP, singleOut = singleOut2),
"There are no outlying points in TP")

## Check that outliers are removed.
phenoTPOut <- removeSingleOut(phenoTP, singleOut = singleOut)

expect_inherits(phenoTPOut, "TP")

expect_true(is.na(phenoTPOut[[11]][phenoTPOut[[11]][["plotId"]] == "c13r17",
"EffpsII"]))

## Check that paramater trait functions correctly.
# Data contains only one trait, so set Sowing_Position to NA.
phenoTPOut2 <- removeSingleOut(phenoTP, singleOut = singleOut,
trait = "Sowing_Position")
expect_true(is.na(phenoTPOut2[[11]][phenoTPOut2[[11]][["plotId"]] == "c13r17",
"Sowing_Position"]))

10 changes: 5 additions & 5 deletions man/detectSingleOut.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4218c77

Please sign in to comment.