diff --git a/NAMESPACE b/NAMESPACE index d75f824..c1a7cea 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,2 @@ exportPattern("^[[:alpha:]]+") +importFrom("stats", "quantile") diff --git a/R/meanimpute.R b/R/meanimpute.R index cc7cf5e..93c9166 100644 --- a/R/meanimpute.R +++ b/R/meanimpute.R @@ -1,4 +1,6 @@ #' Meanimputation +#' @param x A vector. +#' #' @export meanimpute <- function(x) { x[is.na(x)] <- mean(x, na.rm = TRUE) diff --git a/R/transform_log.R b/R/transform_log.R new file mode 100644 index 0000000..ff38f41 --- /dev/null +++ b/R/transform_log.R @@ -0,0 +1,16 @@ +#' Transform_log +#' +#' Log transformation. +#' +#' @param x A vector. +#' +#' @examples +#' transform_log(exp(rnorm(2))) +#' +#' @export + +transform_log <- function(x) { + if (any(x<0)) {stop("log values must be positive")} + x <- log(x) + x +} \ No newline at end of file diff --git a/R/windsorize.R b/R/windsorize.R index b4e15e6..64c8116 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -1,10 +1,25 @@ #' Windsorize #' -#' Do some windsorization. +#' Winsorizing or winsorization is the transformation of statistics by limiting +#' extreme values to reduce the effect of +#' spurious outliers in statistical data. +#' +#' @param x A vector. +#' @param p A quantile. +#' +#' @examples +#' windsorize(rnorm(5)) +#' #' @export windsorize <- function(x, p = .90) { - q <- quantile(x, p) - x[x >= q] <- q + if (is.null(x)) { + stop("nul vector")} + if(is.na(x)){ + stop("NA vector")} + q_low <- quantile(x, 0.5-p/2) + q_high <- quantile(x, 0.5+p/2) + x[x >= q_high] <- q_high + x[x <= q_low] <- q_low x } diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..8a664b5 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,6 @@ +library(testthat) +library(datacleaner) + +test_check("datacleaner") + + diff --git a/tests/testthat/test_transform_log.R b/tests/testthat/test_transform_log.R new file mode 100644 index 0000000..d679c45 --- /dev/null +++ b/tests/testthat/test_transform_log.R @@ -0,0 +1,5 @@ +context("No negative values") +library(datacleaner) +test_that("There are no negative values in input", { + expect_error(transform_log(c(1,2,-1)), "input can't be negative") +}) \ No newline at end of file diff --git a/tests/testthat/test_windsorize.R b/tests/testthat/test_windsorize.R new file mode 100644 index 0000000..ee8f8bd --- /dev/null +++ b/tests/testthat/test_windsorize.R @@ -0,0 +1,6 @@ +context("NA and null values") +library(datacleaner) +test_that("NA and null values produce error message", { + expect_error(windsorize(NA), "argument should not be a vector containing only NA-s or NULL-s") + expect_error(windsorize(NULL), "argument should not be a vector containing only NA-s or NULL-s") +}) \ No newline at end of file