Skip to content

Commit

Permalink
Merge pull request #74 from venpopov/45-robust-check-response-variabl…
Browse files Browse the repository at this point in the history
…e-for-CRT

Update data check and add deg2rad & rad2deg
  • Loading branch information
venpopov authored Feb 8, 2024
2 parents 898aedb + b13f5c2 commit 8a99014
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 13 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(calc_error_relative_to_nontargets)
export(check_data)
export(configure_model)
export(dIMM)
export(deg2rad)
export(dmixture2p)
export(dmixture3p)
export(dsdm)
Expand All @@ -46,6 +47,7 @@ export(qmixture2p)
export(qmixture3p)
export(qsdm)
export(rIMM)
export(rad2deg)
export(rmixture2p)
export(rmixture3p)
export(rsdm)
Expand Down
45 changes: 33 additions & 12 deletions R/helpers-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ check_data.default <- function(model, data, formula) {
#' @export
check_data.vwm <- function(model, data, formula) {
resp_name <- get_response(formula$formula)
if (max(abs(data[[resp_name]]), na.rm=T) > 10) {
data[[resp_name]] <- data[[resp_name]]*pi/180
warning('It appears your response variable is in degrees. We will transform it to radians.')
if (max(abs(data[[resp_name]]), na.rm = T) > 2*pi) {
warning('It appears your response variable is in degrees.\n
The model requires the response variable to be in radians.\n
The model will continue to run, but the results may be compromised.')
}

# wrap recoded responses around the circle (range = -pi to pi)
data[[resp_name]] <- wrap(data[[resp_name]])

data = NextMethod("check_data")

return(data)
Expand All @@ -72,9 +70,10 @@ check_data.vwm <- function(model, data, formula) {
#' @export
check_data.nontargets <- function(model, data, formula) {
non_targets <- model$vars$non_targets
if (max(abs(data[,non_targets]), na.rm=T) > 10) {
data[,non_targets] <- data[,non_targets]*pi/180
warning('It appears your non_target variables are in degrees. We will transform it to radians.')
if (max(abs(data[,non_targets]), na.rm = T) > 2*pi) {
warning('It appears at least one of your non_target variables are in degrees.\n
The model requires these variable to be in radians.\n
The model will continue to run, but the results may be compromised.')
}

setsize <- model$vars$setsize
Expand All @@ -98,9 +97,6 @@ check_data.nontargets <- function(model, data, formula) {
'`non_targets` is more than max(setsize)-1')
}

# wrap non_target variables around the circle (range = -pi to pi)
data[,non_targets] <- wrap(data[,non_targets])

# create index variables for non_targets and correction variable for theta due to setsize
lure_idx_vars <- paste0('LureIdx',1:(max_setsize - 1))
for (i in 1:(max_setsize - 1)) {
Expand Down Expand Up @@ -180,3 +176,28 @@ wrap <- function(x, radians=TRUE) {
return(((x+180) %% (2*180)) - 180)
}
}

#' @title Convert degrees to radians or radians to degrees.
#' @description
#' The helper functions `deg2rad` and `rad2deg` should add convenience in transforming
#' data from degrees to radians and from radians to degrees.
#'
#' @name circle_transform
#' @param deg A numeric vector of values in degrees.
#' @param rad A numeric vector of values in radians.
#' @return A numeric vector of the same length as `deg` or `rad`.
#' @keywords transform
#' @export
#' @examples
#' degrees <- runif(100, min = 0, max = 360)
#' radians <- deg2rad(degrees)
#' degrees_again <- rad2deg(radians)
deg2rad <- function(deg){
deg * pi / 180
}

#' @rdname circle_transform
#' @export
rad2deg <- function(rad){
rad * 180 / pi
}
30 changes: 30 additions & 0 deletions man/circle_transform.Rd

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

13 changes: 12 additions & 1 deletion tests/testthat/test-helpers-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("check_data() produces expected errors and warnings", {
expect_warning(check_data(ml(non_targets = 'x', setsize=2, spaPos = 'z'),
data.frame(y = 12, x = 1, z = 2),
brms::bf(y ~ 1)),
"It appears your response variable is in degrees. We will transform it to radians.")
"It appears your response variable is in degrees.\n")
expect_silent(check_data(ml(non_targets = 'x', setsize=2, spaPos = 'z'),
data.frame(y = 1, x = 1, z = 2), brms::bf(y ~ 1)))
}
Expand Down Expand Up @@ -71,3 +71,14 @@ test_that("wrap(x) returns the correct value for values between (3*pi,4*pi)", {
x <- 3*pi+1
expect_equal(wrap(x), -(pi-1))
})


test_that("deg2rad returns the correct values for 0, 180, 360", {
x <- c(0,90,180)
expect_equal(round(deg2rad(x),2),c(0.00,1.57,3.14))
})

test_that("rad2deg returns the correct values for 0, pi/2, 2*pi", {
x <- c(0, pi/2, 2*pi)
expect_equal(round(rad2deg(x),2),c(0,90,360))
})

0 comments on commit 8a99014

Please sign in to comment.