diff --git a/NAMESPACE b/NAMESPACE index d75f824..256f26a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,6 @@ -exportPattern("^[[:alpha:]]+") +# Generated by roxygen2: do not edit by hand + +export(meanimpute) +export(transform_log) +export(windsorize) +import(stats) diff --git a/R/meanimpute.R b/R/meanimpute.R index cc7cf5e..64dce2e 100644 --- a/R/meanimpute.R +++ b/R/meanimpute.R @@ -1,6 +1,18 @@ -#' Meanimputation +#' Meanimpute +#' +#' Replace NA's with mean value +#' @param x a numeric vector +#' @return new vector, where NA's are replace by mean \code{x} +#' @examples +#' example_vector=c(1,5,NA,NA) +#' meanimpute(example_vector) #' @export meanimpute <- function(x) { + + if(is.null(x)) {stop("Input vector cannot be NULL.")} + if(all(is.na(x))) {stop("Input vector should contain at least one numeric element.")} + if(any(is.numeric(x)==FALSE)) {stop("Input vector should contain at least one numeric element.")} + x[is.na(x)] <- mean(x, na.rm = TRUE) x } diff --git a/R/transform_log.R b/R/transform_log.R new file mode 100644 index 0000000..ba11c66 --- /dev/null +++ b/R/transform_log.R @@ -0,0 +1,20 @@ +#' transform_log +#' +#' log-transformation of a numeric vector. For details about log-transformation please see you basic school math textbook. +#' @param x a numeric vector +#' @return log-transformed vector \code{x} +#' @examples +#' example_vector=c(1,2,3,4,5,6,7,8,9,10) +#' transform_log(example_vector) +#' @export + +transform_log <- function(x){ + + if( is.null(x) ) stop("Input vector is not allowed to be NULL.") + if( any(is.na(x)) ) stop("There is at least one NA value in input vector.") + if( any(x <= 0) ) stop("There is at least one negative value.") + if( any(is.numeric(x) == FALSE) ) stop("There is at least one non-numeric value.") + y<-log(x) + return(y) + +} \ No newline at end of file diff --git a/R/windsorize.R b/R/windsorize.R index b4e15e6..6bc3d98 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -1,10 +1,36 @@ #' Windsorize #' -#' Do some windsorization. +#' Its purposes is to eliminate outliers in a following way. Values of (0.5 +- p/2)th quantiles are calculated and all +#' values above(below) those quantiles are replaced by the quantiles. +#' @param x a numeric vector +#' @param p quantile +#' @return Windsorized vector \code{x} +#' @examples +#' example_vector=c(-1000,1,2,3,4,5,6,7,8,9,1000) +#' windsorize(example_vector, 0.9) +#' +#' example_vector=rnorm(100) +#' windsorize(example_vector, 0.9) #' @export +#' @import stats + windsorize <- function(x, p = .90) { - q <- quantile(x, p) - x[x >= q] <- q - x + + if(is.null(x)) {stop("Input vector cannot be NULL.")} + if(any(is.na(x))) {stop("There should be no NA's in input vector.")} + if(all(is.numeric(x)==FALSE)) {stop("There should only numeric values in the input vector.")} + + if(is.na(p)==TRUE) {stop("Input quantile should be a number between 0 and 1 ")} + if(is.numeric(p)==FALSE) {stop("Input quantile should be a number between 0 and 1 ")} + if(p > 1) {stop("Input quantile should be a number between 0 and 1 ")} + if(p < 0) {stop("Input quantile should be a number between 0 and 1 ")} + + + q_u <- quantile(x, 0.5 + p/2) + x[x >= q_u] <- q_u + + q_l <- quantile(x, 0.5 - p/2) + x[x <= q_l] <- q_l + + return(x) } - diff --git a/man/meanimpute.Rd b/man/meanimpute.Rd index 8139e8f..f9edcb4 100644 --- a/man/meanimpute.Rd +++ b/man/meanimpute.Rd @@ -2,10 +2,20 @@ % Please edit documentation in R/meanimpute.R \name{meanimpute} \alias{meanimpute} -\title{Meanimputation} +\title{Meanimpute} \usage{ meanimpute(x) } +\arguments{ +\item{x}{a numeric vector} +} +\value{ +new vector, where NA's are replace by mean \code{x} +} \description{ -Meanimputation +Replace NA's with mean value +} +\examples{ +example_vector=c(1,5,NA,NA) +meanimpute(example_vector) } diff --git a/man/transform_log.Rd b/man/transform_log.Rd new file mode 100644 index 0000000..3da4d60 --- /dev/null +++ b/man/transform_log.Rd @@ -0,0 +1,21 @@ +% 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 numeric vector} +} +\value{ +log-transformed vector \code{x} +} +\description{ +log-transformation of a numeric vector. For details about log-transformation please see you basic school math textbook. +} +\examples{ +example_vector=c(1,2,3,4,5,6,7,8,9,10) +transform_log(example_vector) +} diff --git a/man/windsorize.Rd b/man/windsorize.Rd index 832c3cb..ce06bce 100644 --- a/man/windsorize.Rd +++ b/man/windsorize.Rd @@ -6,6 +6,22 @@ \usage{ windsorize(x, p = 0.9) } +\arguments{ +\item{x}{a numeric vector} + +\item{p}{quantile} +} +\value{ +Windsorized vector \code{x} +} \description{ -Do some windsorization. +Its purposes is to eliminate outliers in a following way. Values of (0.5 +- p/2)th quantiles are calculated and all +values above(below) those quantiles are replaced by the quantiles. +} +\examples{ +example_vector=c(-1000,1,2,3,4,5,6,7,8,9,1000) +windsorize(example_vector, 0.9) + +example_vector=rnorm(100) +windsorize(example_vector, 0.9) } 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_correct_input_meanimpute.R b/tests/testthat/test_correct_input_meanimpute.R new file mode 100644 index 0000000..5b61d16 --- /dev/null +++ b/tests/testthat/test_correct_input_meanimpute.R @@ -0,0 +1,7 @@ +test_that("Incorrect input of meanimpute", { + #tests related to input vector + expect_error(meanimpute(NULL),"Input vector cannot be NULL.") + expect_error(meanimpute(c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)),"Input vector should contain at least one numeric element.") + expect_error(meanimpute(c(1,2,3,4,"Dracula"), "Input vector should contain at least one numeric element.")) + +}) diff --git a/tests/testthat/test_correct_input_transform_log.R b/tests/testthat/test_correct_input_transform_log.R new file mode 100644 index 0000000..606574b --- /dev/null +++ b/tests/testthat/test_correct_input_transform_log.R @@ -0,0 +1,8 @@ +test_that("Input of transform_log() is correct.", { + #tests related to input vector + expect_error(transform_log(NULL), "Input vector is not allowed to be NULL.") + expect_error(transform_log(c(NA,NA,NA,6,NA,5,NA,NA,7,NA,NA)), "There is at least one NA value in input vector.") + expect_error(transform_log(c(1,2,3,4,5,6,7,8,"string",1000)), "There is at least one non-numeric value.") + expect_error(transform_log(c(1,2,3,4,5,6,7,8,-5)), "There is at least one negative value.") + +}) diff --git a/tests/testthat/test_correct_input_windsorization.R b/tests/testthat/test_correct_input_windsorization.R new file mode 100644 index 0000000..5007b4c --- /dev/null +++ b/tests/testthat/test_correct_input_windsorization.R @@ -0,0 +1,10 @@ +test_that("Incorrect input", { + #tests related to input vector + expect_error(windsorize(NULL, .9), "Input vector cannot be NULL.") + expect_error(windsorize(c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA), .9), "There should be no NA's in input vector.") + expect_error(windsorize(c(-1000,1,2,3,4,5,6,7,8,"string",1000), .9), "There should only numeric values in the input vector.") + expect_error(windsorize(c(-1000,1,2,3,4,6,7,8,9,1000), "string"), "Input quantile should be a number between 0 and 1.") + expect_error(windsorize(c(-1000,1,2,3,4,6,7,8,9,1000), 2), "Input quantile should be a number between 0 and 1") + expect_error(windsorize(c(-1000,1,2,3,4,6,7,8,9,1000), -2), "Input quantile should be a number between 0 and 1") + +}) diff --git a/tests/testthat/test_correct_result_meanimpute.R b/tests/testthat/test_correct_result_meanimpute.R new file mode 100644 index 0000000..85d1afc --- /dev/null +++ b/tests/testthat/test_correct_result_meanimpute.R @@ -0,0 +1,3 @@ +test_that("NA's in vector are correctly replace by mean", { + expect_equal(meanimpute(c(2,4,6,NA)), c(2,4,6,4)) +}) diff --git a/tests/testthat/test_correct_result_transform_log.R b/tests/testthat/test_correct_result_transform_log.R new file mode 100644 index 0000000..d8ca231 --- /dev/null +++ b/tests/testthat/test_correct_result_transform_log.R @@ -0,0 +1,3 @@ +test_that("Vector is correctly log-transformed", { + expect_equal(transform_log(c(1,1,1)), c(0,0,0)) +}) diff --git a/tests/testthat/test_correct_result_windsorization.R b/tests/testthat/test_correct_result_windsorization.R new file mode 100644 index 0000000..2cc607b --- /dev/null +++ b/tests/testthat/test_correct_result_windsorization.R @@ -0,0 +1,3 @@ +test_that("vector is correctly windsorized", { + expect_equal(windsorize(c(-1000,1,2,3,4,5,6,7,8,9,1000),0.9), c(-499.5,1,2,3,4,5,6,7,8,9,504.5) ) +})