Skip to content

Commit

Permalink
Added function to standardize a variable
Browse files Browse the repository at this point in the history
  • Loading branch information
edsandorf committed Mar 28, 2022
1 parent daf516a commit 693532d
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: cmdlr
Type: Package
Title: Choice Modeling in R
Version: 0.0.4
Version: 0.0.4.9001
Authors@R: person("Erlend", "Dancke Sandorf", email = "erlend.dancke.sandorf@nmbu.no", role = c("aut", "cre"))
Maintainer: Erlend Dancke Sandorf <erlend.dancke.sandorf@nmbu.no>
Description: The problem of choice is fundamental to economics. Choice models are used to understand how people make choices in markets. cmdlR is a set of wrapper functions around a user written log-likelihood function. The package also contain several functions to check for local-optima, calculate welfare measures, compare results and make predictions. To get started, the package contains several examples with pre-programmed log-likelihood functions that can be easily tweaked by the user.
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export(rep_rows)
export(reshape_panel)
export(save)
export(search_start_values)
export(standardize)
export(subset_data)
export(summary_poe_test)
export(tidy)
Expand Down Expand Up @@ -94,4 +95,5 @@ importFrom(stats,coef)
importFrom(stats,nobs)
importFrom(stats,qnorm)
importFrom(stats,runif)
importFrom(stats,sd)
importFrom(stats,vcov)
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## cmdlr v0.0.5
* Added a function to standardize variables
* General bug fixes


## cmdlr v0.0.4
* Major structural changes to the code with multiple changes breaking existing code.
* Code refactoring
Expand Down
2 changes: 1 addition & 1 deletion R/cmdlR.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' @docType package
#' @name cmldr
#'
#' @importFrom stats runif qnorm
#' @importFrom stats runif qnorm sd
#' @importFrom rlang sym := .data
#' @importFrom dplyr bind_cols bind_rows n group_by starts_with
#' @importFrom magrittr %>%
Expand Down
25 changes: 25 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,28 @@ load_packages <- function(pkgs) {

return(NULL)
}

#' Standardize a variable
#'
#' Standardize a variable by subtracting the mean and dividing by the standard
#' deviation
#'
#' @param x A numeric vector
#' @param na.rm A boolean equal to TRUE if ignore NA when calculating the mean
#' and standard deviations. See \code{\link{mean}} and \code{\link{sd}}
#'
#' @examples
#' x <- runif(10)
#' standardize(x)
#'
#' x[1] <- NA
#' standardize(x, na.rm = TRUE)
#'
#' @return A vector of standardized numbers
#'
#' @export
standardize <- function(x, na.rm = FALSE) {
return(
(x - mean(x, na.rm = na.rm)) / sd(x, na.rm = na.rm)
)
}
29 changes: 29 additions & 0 deletions man/standardize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ test_that("Repeat columns correctly repeats the columns of a matrix", {
)
)
})

x <- runif(50)
y <- c(runif(49), NA)

test_that("Standardize works correctly", {
expect_equal(
standardize(x), standardize(x, na.rm = TRUE)
)

expect_equal(
mean(standardize(x)), 0
)

expect_equal(
sd(standardize(x)), 1
)

expect_equal(
mean(standardize(y)), mean(rep(NA, length(y)))
)

expect_true(all(is.numeric(standardize(y, na.rm = TRUE))))

})

0 comments on commit 693532d

Please sign in to comment.