From 5b39d7d0d627061d3e1e07c30af61abea866129b Mon Sep 17 00:00:00 2001 From: Andreas Peterseil Date: Sun, 5 May 2019 14:55:37 +0200 Subject: [PATCH 1/9] Fix #1: Included replacement of extremely low values --- R/windsorize.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/windsorize.R b/R/windsorize.R index b4e15e6..2132c34 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -3,8 +3,9 @@ #' Do some windsorization. #' @export windsorize <- function(x, p = .90) { - q <- quantile(x, p) - x[x >= q] <- q + q <- quantile(x, 0.5 * ( 1 + p * c(-1,1) ) ) + x[x >= q[2] ] <- q[2] + x[x <= q[1] ] <- q[1] x } From 23a38fe7bbef15218105f5a556df4063c056b256 Mon Sep 17 00:00:00 2001 From: Andreas Peterseil Date: Sun, 5 May 2019 14:58:42 +0200 Subject: [PATCH 2/9] Fix #3: Error is thrown in input is emptry or contains NAs --- R/windsorize.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/windsorize.R b/R/windsorize.R index 2132c34..d6cbac1 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -3,6 +3,7 @@ #' Do some windsorization. #' @export windsorize <- function(x, p = .90) { + stopifnot(length(x)>0 && sum(is.na(x))==0) q <- quantile(x, 0.5 * ( 1 + p * c(-1,1) ) ) x[x >= q[2] ] <- q[2] x[x <= q[1] ] <- q[1] From fe77e6ac0f0e11f0e427b7da928216c18decd9a9 Mon Sep 17 00:00:00 2001 From: Andreas Peterseil Date: Sun, 5 May 2019 15:00:50 +0200 Subject: [PATCH 3/9] Fix #5: transform_log added --- R/transform_log.R | 1 + 1 file changed, 1 insertion(+) 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..8c6138d --- /dev/null +++ b/R/transform_log.R @@ -0,0 +1 @@ +transform_log<-function(x) log(x) \ No newline at end of file From d80308d183910bdce2357257ebca23a4964eda99 Mon Sep 17 00:00:00 2001 From: Andreas Peterseil Date: Sun, 5 May 2019 15:05:21 +0200 Subject: [PATCH 4/9] Fix #5: added check of input in transform_log --- R/transform_log.R | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/R/transform_log.R b/R/transform_log.R index 8c6138d..45ca6ef 100644 --- a/R/transform_log.R +++ b/R/transform_log.R @@ -1 +1,10 @@ -transform_log<-function(x) log(x) \ No newline at end of file +transform_log<-function(x){ + if(length(x)==0){ + stop("x must have positive length") + }else if(sum(x<=0)>0){ + stop("x must not contain non-positive values") + }else if(sum(is.na(x))>0){ + stop("x must not contain any NA") + } + log(x) +} \ No newline at end of file From 8352e552cb99e1093a064137478ada866ae4bcc0 Mon Sep 17 00:00:00 2001 From: Andreas Peterseil Date: Sun, 5 May 2019 15:06:55 +0200 Subject: [PATCH 5/9] Fix #3: Error is thrown in input is emptry or contains NAs --- R/windsorize.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/windsorize.R b/R/windsorize.R index d6cbac1..238f06a 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -3,7 +3,11 @@ #' Do some windsorization. #' @export windsorize <- function(x, p = .90) { - stopifnot(length(x)>0 && sum(is.na(x))==0) + if(length(x)==0){ + stop("x must be of positive length") + }else if(sum(is.na(x))>0){ + stop("x must not contain any NA") + } q <- quantile(x, 0.5 * ( 1 + p * c(-1,1) ) ) x[x >= q[2] ] <- q[2] x[x <= q[1] ] <- q[1] From 8a551e48f082745bed7a22c4e7ba29f9bc862cce Mon Sep 17 00:00:00 2001 From: Andreas Peterseil Date: Sun, 5 May 2019 15:19:55 +0200 Subject: [PATCH 6/9] Fix #3: Error is thrown in input is emptry or contains NAs git commit -m Fix --- NAMESPACE | 6 +++++- R/meanimpute.R | 5 +++++ R/transform_log.R | 7 +++++++ R/windsorize.R | 8 ++++++-- man/meanimpute.Rd | 9 +++++++++ man/transform_log.Rd | 20 ++++++++++++++++++++ man/windsorize.Rd | 15 +++++++++++++-- 7 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 man/transform_log.Rd diff --git a/NAMESPACE b/NAMESPACE index d75f824..934d9b3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,5 @@ -exportPattern("^[[:alpha:]]+") +# Generated by roxygen2: do not edit by hand + +export(meanimpute) +export(transform_log) +export(windsorize) diff --git a/R/meanimpute.R b/R/meanimpute.R index cc7cf5e..d09ca51 100644 --- a/R/meanimpute.R +++ b/R/meanimpute.R @@ -1,4 +1,9 @@ #' Meanimputation +#' +#' @param x A numeric vector containing the data +#' @return the input vector with its NA values replaced by the mean +#' @examples +#' log(1:5) #' @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 index 45ca6ef..db716d8 100644 --- a/R/transform_log.R +++ b/R/transform_log.R @@ -1,3 +1,10 @@ +#' Log transforms data. +#' +#' @param x A numeric vector containing the data +#' @return A numeric vector whose entries are the logarithms of the input. +#' @examples +#' log(1:5) +#' @export transform_log<-function(x){ if(length(x)==0){ stop("x must have positive length") diff --git a/R/windsorize.R b/R/windsorize.R index 238f06a..dba1fda 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -1,6 +1,10 @@ -#' Windsorize +#' Windsorizes data. #' -#' Do some windsorization. +#' @param x A numeric vector containing the data. +#' @param p A numeric value denoting the a probability +#' @return A numeric vector containing the windsorized data, i.e. extreme values are replaced by the quantiles derived from \code{p}. +#' @examples +#' windsorize(c(92 , 19 , 101 , 58 , 1053 , 91 , 26 , 78 , 10 , 13 , -40 , 101 , 86 , 85 , 15 , 89 , 89 , 28 , -5 , 41)) #' @export windsorize <- function(x, p = .90) { if(length(x)==0){ diff --git a/man/meanimpute.Rd b/man/meanimpute.Rd index 8139e8f..5984f37 100644 --- a/man/meanimpute.Rd +++ b/man/meanimpute.Rd @@ -6,6 +6,15 @@ \usage{ meanimpute(x) } +\arguments{ +\item{x}{A numeric vector containing the data} +} +\value{ +the input vector with its NA values replaced by the mean +} \description{ Meanimputation } +\examples{ +log(1:5) +} diff --git a/man/transform_log.Rd b/man/transform_log.Rd new file mode 100644 index 0000000..b7d9dce --- /dev/null +++ b/man/transform_log.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/transform_log.R +\name{transform_log} +\alias{transform_log} +\title{Log transforms data.} +\usage{ +transform_log(x) +} +\arguments{ +\item{x}{A numeric vector containing the data} +} +\value{ +A numeric vector whose entries are the logarithms of the input. +} +\description{ +Log transforms data. +} +\examples{ +log(1:5) +} diff --git a/man/windsorize.Rd b/man/windsorize.Rd index 832c3cb..ed90eb5 100644 --- a/man/windsorize.Rd +++ b/man/windsorize.Rd @@ -2,10 +2,21 @@ % Please edit documentation in R/windsorize.R \name{windsorize} \alias{windsorize} -\title{Windsorize} +\title{Windsorizes data.} \usage{ windsorize(x, p = 0.9) } +\arguments{ +\item{x}{A numeric vector containing the data.} + +\item{p}{A numeric value denoting the a probability} +} +\value{ +A numeric vector containing the windsorized data, i.e. extreme values are replaced by the quantiles derived from \code{p}. +} \description{ -Do some windsorization. +Windsorizes data. +} +\examples{ +windsorize(c(92 , 19 , 101 , 58 , 1053 , 91 , 26 , 78 , 10 , 13 , -40 , 101 , 86 , 85 , 15 , 89 , 89 , 28 , -5 , 41)) } From c9d0d26d701acdec53cd38099acb309e56661953 Mon Sep 17 00:00:00 2001 From: Andreas Peterseil Date: Sun, 5 May 2019 15:30:40 +0200 Subject: [PATCH 7/9] added test --- DESCRIPTION | 2 +- tests/testthat.R | 4 ++++ tests/testthat/test-transform_log.R | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-transform_log.R diff --git a/DESCRIPTION b/DESCRIPTION index 492ae33..cad592e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,7 @@ License: GPL-3 Encoding: UTF-8 LazyData: true Suggests: - testthat, + testthat (>= 2.1.0), covr, rmarkdown RoxygenNote: 6.1.1 diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..56f8508 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +dlibrary(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..42d45df --- /dev/null +++ b/tests/testthat/test-transform_log.R @@ -0,0 +1,3 @@ +test_that("windsorize works", { + expect_equal(windsorize(3:10), c(3.35,4.00,5.00,6.00,7.00,8.00,9.00,9.65)) +}) From 0723c6b2c743275ecc79de045919282ae8a03957 Mon Sep 17 00:00:00 2001 From: Andreas Peterseil Date: Sun, 5 May 2019 15:32:56 +0200 Subject: [PATCH 8/9] windsorize test: changed test --- tests/testthat/{test-transform_log.R => test-windsorize.R} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/testthat/{test-transform_log.R => test-windsorize.R} (100%) diff --git a/tests/testthat/test-transform_log.R b/tests/testthat/test-windsorize.R similarity index 100% rename from tests/testthat/test-transform_log.R rename to tests/testthat/test-windsorize.R From ed28c497c4e5a6ad61a60ff1818358014a7323db Mon Sep 17 00:00:00 2001 From: Andreas Peterseil Date: Sun, 5 May 2019 15:34:16 +0200 Subject: [PATCH 9/9] removed typo --- tests/testthat.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat.R b/tests/testthat.R index 56f8508..8adbfff 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,4 +1,4 @@ -dlibrary(testthat) +library(testthat) library(datacleaner) test_check("datacleaner")