relative_difference {fritools2}R Documentation

Compute Relative Differences Between the Values of Two Vectors

Description

We often try to compare vectors on near equality. This is a wrapper to all.equal for our convenience. It also implements relative difference and change as discussed in https://en.wikipedia.org/wiki/Relative_change_and_difference.

Usage

relative_difference(
  current,
  reference,
  type = c("all.equal", "difference", "change")
)

Arguments

current

One vector.

reference

Another vector, for type = all.equal, this is passed as target, for type = all.equal this can be thought of as the "correct" value or the state "before".

type

The method to be used. See Details.

Details

The default method (type = all.equal) applies all.equal onto the two vectors. Method type = difference is somewhat the same as the default, method type = change takes account of the sign of the differences.

Value

A vector of relative differences.

See Also

Other statistics: column_sums(), count_groups(), round_half_away_from_zero(), weighted_variance()

Other vector comparing functions: compare_vectors()

Examples

n <- 500
x <- rnorm(n)
y <- x + rnorm(n, sd = 0.0001)
plot(relative_difference(x, y), x)
plot(relative_difference(x, y, "difference"), x)
# They do approximately the same:
max(relative_difference(relative_difference(x, y),
                            relative_difference(x, y, "difference")))
# Takes sign into account:
plot(relative_difference(x, y, "change"), x)
max(relative_difference(relative_difference(x, y),
                        abs(relative_difference(x, y, "change"))))

[Package fritools2 version 4.1.0 Index]