-
Notifications
You must be signed in to change notification settings - Fork 14
Solution for the Preparation phase #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
9431e33
3fae72a
5e0d2a1
e458028
84bf9e5
1a15e0f
b7b0d20
0034c46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| exportPattern("^[[:alpha:]]+") | ||
| importFrom("stats", "quantile") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #' Transform_log | ||
| #' | ||
| #' Transforms values to natural logarithm. | ||
| #' | ||
| #' @param x A numeric vector containing positive real numbers | ||
| #' @return Natural logarithm of the values in \code{x}. | ||
| #' @examples | ||
| #' transform_log(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) | ||
| #' transform_log(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) | ||
| #' @export | ||
|
|
||
| transform_log <- function(x){ | ||
|
|
||
| if( sum(is.na(x)) ){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do |
||
| stop("Input x contains value NA") | ||
| } | ||
| else if( length(x) == 0 ){ | ||
| stop("Input x is NULL") | ||
| } | ||
| else if( sum(x <= 0) ){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, more explicit to use |
||
| stop("Input x contains non-positive values") | ||
| } | ||
|
|
||
| log(x) | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,29 @@ | ||
| #' Windsorize | ||
| #' | ||
| #' Do some windsorization. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Description should be more explicit. |
||
| #' | ||
| #' @param x A numeric vector containing real numbers | ||
| #' @param p A number between 0 and 1, representing the mass of probability. Values are winsorized outside \code{p}. Default value is 0.9. | ||
| #' @return Windsorized vector of \code{x}. | ||
| #' @examples | ||
| #' windsorize(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) | ||
| #' windsorize(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), p = 0.8) | ||
| #' windsorize(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 0.8) | ||
| #' @export | ||
|
|
||
| windsorize <- function(x, p = .90) { | ||
| q <- quantile(x, p) | ||
| x[x >= q] <- q | ||
|
|
||
| if( sum(is.na(x))){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, |
||
| stop("Input x contains value NA") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to throw an error here? What could be the workaround for |
||
| } | ||
| else if( length(x) == 0 ){ | ||
| stop("Input x is NULL") | ||
| } | ||
|
|
||
| q_lower <- quantile(x, (1-p)/2) | ||
| q_upper <- quantile(x, p + (1-p)/2) | ||
| x[x <= q_lower] <- q_lower | ||
| x[x >= q_upper] <- q_upper | ||
| x | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| library(testthat) | ||
| library(datacleaner) | ||
|
|
||
| test_check("datacleaner") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| library(datacleaner) | ||
|
|
||
| test_that("windsorize the input vector", { | ||
| expect_error(windsorize(c(NULL)), "Input x is NULL") | ||
| expect_error(windsorize(c(1, 2, NA)), "Input x contains value NA") | ||
| expect_equal(windsorize(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 0.8), c(1.9, 2, 3, 4, 5, 6, 7, 8, 9, 9.1)) | ||
| }) | ||
|
|
||
| test_that("Takes natural logarithm", { | ||
| expect_error(transform_log(c(NULL)), "Input x is NULL") | ||
| expect_error(transform_log(c(1, 2, NA)), "Input x contains value NA") | ||
| expect_error(transform_log(c(-1, 2, 3)), "Input x contains non-positive values") | ||
| expect_equal(transform_log(c(1, exp(1))), c(0, 1)) | ||
| }) | ||
|
|
||
| test_that("Takes natural logarithm", { | ||
| expect_equal(meanimpute(c(1, NA, 3)), c(1, 2, 3)) | ||
| expect_equal(meanimpute(c(1, 2 , 3)), c(1, 2, 3)) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you used roxygen2 to generate the file here?