From 34ebd459300009c8633e8ae4ef1f46643d7450b4 Mon Sep 17 00:00:00 2001 From: Steve Martin Date: Tue, 13 Aug 2024 00:26:42 -0400 Subject: [PATCH] Fixed a bug with transmute_weights() --- NEWS.md | 2 ++ R/weights.R | 2 +- tests/testthat/test-weights.R | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index cabe717..56d5542 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ - `splice_index()` now keeps names. +- Fixed a bug with `transmute_weights()` where the weights could be negative. + ## Version 0.6.1 - Updated maintainer email. diff --git a/R/weights.R b/R/weights.R index 51ca00b..3b9dc7d 100644 --- a/R/weights.R +++ b/R/weights.R @@ -6,7 +6,7 @@ rdiff <- function(a, b, r) { } else if (r == 1) { a - b } else { - a^r - b^r + (a^r - b^r) / r } } diff --git a/tests/testthat/test-weights.R b/tests/testthat/test-weights.R index f0c31e5..cce59f2 100644 --- a/tests/testthat/test-weights.R +++ b/tests/testthat/test-weights.R @@ -14,6 +14,8 @@ test_that("weights transmute correctly", { expect_equal(transmute_weights(2, 1)(c(1, NA)), c(1, NA)) expect_equal(transmute_weights(-1, 1)(x, w), scale_weights(w / x)) expect_equal(transmute_weights(1, -1)(xna, w), scale_weights(w * xna)) + # Used to give negative weights. + expect_equal(transmute_weights(-1, 1)(1:3 / 2, 1:3 / 6), rep(1 / 3, 3)) expect_equal( transmute_weights(7, -3)(x, transmute_weights(-3, 7)(x, w)), scale_weights(w)