From 86cfdad2e93a52a1f1741a538970214fd693d85d Mon Sep 17 00:00:00 2001 From: marinapopovic11 <50150624+marinapopovic11@users.noreply.github.com> Date: Tue, 7 May 2019 00:58:59 +0200 Subject: [PATCH 1/5] Update meanimpute.R --- R/meanimpute.R | 2 ++ 1 file changed, 2 insertions(+) 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) From d2ed4bac7950a61fb8c1de73caaf55e7aa3dfbdb Mon Sep 17 00:00:00 2001 From: marinapopovic11 <50150624+marinapopovic11@users.noreply.github.com> Date: Tue, 7 May 2019 00:59:26 +0200 Subject: [PATCH 2/5] Update windsorize.R --- R/windsorize.R | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) 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 } From 52779612fc9e49c27e85e3f607974c9ea27448bc Mon Sep 17 00:00:00 2001 From: marinapopovic11 <50150624+marinapopovic11@users.noreply.github.com> Date: Tue, 7 May 2019 00:59:55 +0200 Subject: [PATCH 3/5] Add files via upload --- R/transform_log.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 R/transform_log.R 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 From f2ed16c22251dc7376f3f5e3606613f9aecc6ae4 Mon Sep 17 00:00:00 2001 From: marinapopovic11 <50150624+marinapopovic11@users.noreply.github.com> Date: Tue, 7 May 2019 01:00:43 +0200 Subject: [PATCH 4/5] Update NAMESPACE --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index d75f824..c1a7cea 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,2 @@ exportPattern("^[[:alpha:]]+") +importFrom("stats", "quantile") From afedd090a0b365b9b23ed0b8fbd19a709345dd37 Mon Sep 17 00:00:00 2001 From: marinapopovic11 <50150624+marinapopovic11@users.noreply.github.com> Date: Tue, 7 May 2019 01:05:30 +0200 Subject: [PATCH 5/5] Add files via upload --- tests/testthat.R | 6 ++++++ tests/testthat/test_transform_log.R | 5 +++++ tests/testthat/test_windsorize.R | 6 ++++++ 3 files changed, 17 insertions(+) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test_transform_log.R create mode 100644 tests/testthat/test_windsorize.R 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