From 5782aa0b23c1e46cbc372de6cf6b22467f808d9a Mon Sep 17 00:00:00 2001 From: Zoltan Szebenyi Date: Wed, 1 May 2019 20:23:42 +0200 Subject: [PATCH 1/8] fix issue #1 and #3 in windsorize() --- R/windsorize.R | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/R/windsorize.R b/R/windsorize.R index b4e15e6..fb3558f 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -3,8 +3,12 @@ #' Do some windsorization. #' @export windsorize <- function(x, p = .90) { - q <- quantile(x, p) - x[x >= q] <- q + if(all(is.na(x))) {stop("argument should not be a vector containing only NA-s")} + if(all(is.null(x))) {stop("argument should not be a null vector")} + q_up <- quantile(x, 0.5 + p / 2 ) + q_down <- quantile(x, 0.5 - p / 2 ) + x[x >= q_up] <- q_up + x[x <= q_down] <- q_down x } From 92dc53882e2fa1787473df021115ab32fd5f8996 Mon Sep 17 00:00:00 2001 From: Zoltan Szebenyi Date: Wed, 1 May 2019 20:39:55 +0200 Subject: [PATCH 2/8] fix issue #5: added function transform_log() --- R/transform_log.R | 5 +++++ 1 file changed, 5 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..aa9cfaf --- /dev/null +++ b/R/transform_log.R @@ -0,0 +1,5 @@ +transform_log <- function(x) { + if (x<0) {stop("input can't be negative")} + x <- log(x) + x +} \ No newline at end of file From b560f90cca944234906a29d5b513fd57d84c5daf Mon Sep 17 00:00:00 2001 From: Zoltan Szebenyi Date: Wed, 1 May 2019 20:56:53 +0200 Subject: [PATCH 3/8] fix issue #7: updated documentation of meanimpute() --- R/meanimpute.R | 2 ++ man/meanimpute.Rd | 3 +++ 2 files changed, 5 insertions(+) diff --git a/R/meanimpute.R b/R/meanimpute.R index cc7cf5e..c3fe624 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/man/meanimpute.Rd b/man/meanimpute.Rd index 8139e8f..7773d13 100644 --- a/man/meanimpute.Rd +++ b/man/meanimpute.Rd @@ -6,6 +6,9 @@ \usage{ meanimpute(x) } +\arguments{ +\item{x}{A vector.} +} \description{ Meanimputation } From d8121082c0c40fc5260a35d6f3db906478ff3e0e Mon Sep 17 00:00:00 2001 From: Zoltan Szebenyi Date: Wed, 1 May 2019 20:57:25 +0200 Subject: [PATCH 4/8] fix issue #6: updated documentation for windsorize() --- R/windsorize.R | 13 +++++++++++-- man/windsorize.Rd | 13 ++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/R/windsorize.R b/R/windsorize.R index fb3558f..2923636 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -1,6 +1,15 @@ #' Windsorize -#' -#' Do some windsorization. +#' +#' Winsorizing or winsorization is the transformation of statistics by limiting +#' extreme values in the statistical data to reduce the effect of possibly +#' spurious outliers. +#' +#' @param x A vector. +#' @param p A quantile. +#' +#' @examples +#' windsorize(rnorm(5)) +#' #' @export windsorize <- function(x, p = .90) { if(all(is.na(x))) {stop("argument should not be a vector containing only NA-s")} diff --git a/man/windsorize.Rd b/man/windsorize.Rd index 832c3cb..249fecc 100644 --- a/man/windsorize.Rd +++ b/man/windsorize.Rd @@ -6,6 +6,17 @@ \usage{ windsorize(x, p = 0.9) } +\arguments{ +\item{x}{A vector.} + +\item{p}{A quantile.} +} \description{ -Do some windsorization. +Winsorizing or winsorization is the transformation of statistics by limiting +extreme values in the statistical data to reduce the effect of possibly +spurious outliers. +} +\examples{ +windsorize(rnorm(5)) + } From 8f2c7b1e66bf31972a1896c9e452c25c3ad68027 Mon Sep 17 00:00:00 2001 From: Zoltan Szebenyi Date: Wed, 1 May 2019 20:57:57 +0200 Subject: [PATCH 5/8] issue #5: updated documentation for transform_log() --- R/transform_log.R | 13 ++++++++++++- man/transform_log.Rd | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 man/transform_log.Rd diff --git a/R/transform_log.R b/R/transform_log.R index aa9cfaf..157bd49 100644 --- a/R/transform_log.R +++ b/R/transform_log.R @@ -1,5 +1,16 @@ +#' Transform_log +#' +#' Log transformation. +#' +#' @param x A vector. +#' +#' @examples +#' transform_log(exp(rnorm(2))) +#' +#' @export + transform_log <- function(x) { - if (x<0) {stop("input can't be negative")} + if (any(x<0)) {stop("input can't be negative")} x <- log(x) x } \ No newline at end of file diff --git a/man/transform_log.Rd b/man/transform_log.Rd new file mode 100644 index 0000000..e77f2c3 --- /dev/null +++ b/man/transform_log.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/transform_log.R +\name{transform_log} +\alias{transform_log} +\title{Transform_log} +\usage{ +transform_log(x) +} +\arguments{ +\item{x}{A vector.} +} +\description{ +Log transformation. +} +\examples{ +transform_log(exp(rnorm(2))) + +} From 04a8d03515c364373954f2ed1dd24344ce1c0589 Mon Sep 17 00:00:00 2001 From: Zoltan Szebenyi Date: Wed, 1 May 2019 21:10:35 +0200 Subject: [PATCH 6/8] functionality tests implemented for transform_log() and windsorize() --- tests/testthat.R | 4 ++++ tests/testthat/test_transform_log.R | 5 +++++ tests/testthat/test_windsorize.R | 6 ++++++ 3 files changed, 15 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..8adbfff --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +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..9b96c99 --- /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..4e19ba5 --- /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 From de64e384e5ff49fe6168111b8760ea06e9ef3df0 Mon Sep 17 00:00:00 2001 From: Zoltan Szebenyi Date: Wed, 1 May 2019 21:11:21 +0200 Subject: [PATCH 7/8] update windsorize(): spurious check for null values removed --- R/windsorize.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/windsorize.R b/R/windsorize.R index 2923636..9438ac7 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -12,8 +12,7 @@ #' #' @export windsorize <- function(x, p = .90) { - if(all(is.na(x))) {stop("argument should not be a vector containing only NA-s")} - if(all(is.null(x))) {stop("argument should not be a null vector")} + if(all(is.na(x))) {stop("argument should not be a vector containing only NA-s or NULL-s")} q_up <- quantile(x, 0.5 + p / 2 ) q_down <- quantile(x, 0.5 - p / 2 ) x[x >= q_up] <- q_up From ed0e383809b4c1d1b182489ebcd36e0d9a87f7e1 Mon Sep 17 00:00:00 2001 From: Zoltan Szebenyi Date: Wed, 1 May 2019 21:18:42 +0200 Subject: [PATCH 8/8] adding importFrom("stats", "quantile") to NAMESPACE --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index d75f824..1015c5a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,2 @@ exportPattern("^[[:alpha:]]+") +importFrom("stats", "quantile") \ No newline at end of file