Skip to content

Commit 82cd7ef

Browse files
committed
test Euclidean weights and weight standardisation for control_for_euc()
1 parent 4b9b9ee commit 82cd7ef

1 file changed

Lines changed: 96 additions & 2 deletions

File tree

tests/testthat/test-generate.R

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ testthat::test_that("no id_col given", {
188188
eg_df %>%
189189
split_by(a, -5:0 ~ 0:5) %>%
190190
control_for(b, -2.5:2.5) %>%
191-
generate(17) %>%
191+
generate(17, silent=TRUE) %>%
192192
nrow()
193193
),
194194
17
@@ -198,7 +198,7 @@ testthat::test_that("no id_col given", {
198198
eg_df %>%
199199
split_by(a, -5:0 ~ 0:5) %>%
200200
control_for(b, -2.5:2.5) %>%
201-
generate(17),
201+
generate(17, silent=TRUE),
202202
"No id_col detected; will use row numbers."
203203
)
204204
# since eg_df's id column is just the row numbers anyway, these should be identical
@@ -777,4 +777,98 @@ testthat::test_that("control_for_euc", {
777777
dplyr::filter(gen_euc_dist == man_euc_dist) %>%
778778
nrow()
779779
}, 20)
780+
# test weighted Euclidean distance is calculated as expected
781+
testthat::expect_equal({
782+
weights <- runif(2, 0.1, 100)
783+
weights_std <- weights / mean(weights)
784+
785+
wide_res <- eg_df %>%
786+
set_options(id_col = "id") %>%
787+
split_by(a, -5:0 ~ 0:5) %>%
788+
control_for_euc(
789+
c(b, e),
790+
0:1.5,
791+
name = "gen_euc_dist",
792+
weights = weights
793+
) %>%
794+
generate(20, silent=TRUE)
795+
796+
manual_euc_dist <- wide_res %>%
797+
dplyr::left_join(
798+
eg_df %>%
799+
dplyr::select(id, b, e) %>%
800+
dplyr::mutate(b = weights_std[1]*scale(b), e = weights_std[2]*scale(e)) %>%
801+
dplyr::rename(A1_b = b, A1_e = e),
802+
by = c("A1" = "id")
803+
) %>%
804+
dplyr::left_join(
805+
eg_df %>%
806+
dplyr::select(id, b, e) %>%
807+
dplyr::mutate(b = weights_std[1]*scale(b), e = weights_std[2]*scale(e)) %>%
808+
dplyr::rename(A2_b = b, A2_e = e),
809+
by = c("A2" = "id")
810+
) %>%
811+
dplyr::mutate(
812+
dist_b = A1_b - A2_b,
813+
dist_e = A1_e - A2_e,
814+
man_euc_dist = sqrt(dist_b**2 + dist_e**2)
815+
)
816+
817+
wide_res %>%
818+
long_format() %>%
819+
dplyr::filter(condition != match_null) %>%
820+
dplyr::left_join(
821+
dplyr::select(manual_euc_dist, item_nr, man_euc_dist),
822+
by = "item_nr"
823+
) %>%
824+
dplyr::filter(gen_euc_dist == man_euc_dist) %>%
825+
nrow()
826+
}, 20)
827+
# test that weight standardisation can be disabled
828+
testthat::expect_equal({
829+
weights <- runif(2, 0.1, 100)
830+
831+
wide_res <- eg_df %>%
832+
set_options(id_col = "id") %>%
833+
split_by(a, -5:0 ~ 0:5) %>%
834+
control_for_euc(
835+
c(b, e),
836+
0:10,
837+
name = "gen_euc_dist",
838+
weights = weights,
839+
standardise_weights = FALSE
840+
) %>%
841+
generate(20, silent=TRUE)
842+
843+
manual_euc_dist <- wide_res %>%
844+
dplyr::left_join(
845+
eg_df %>%
846+
dplyr::select(id, b, e) %>%
847+
dplyr::mutate(b = weights[1]*scale(b), e = weights[2]*scale(e)) %>%
848+
dplyr::rename(A1_b = b, A1_e = e),
849+
by = c("A1" = "id")
850+
) %>%
851+
dplyr::left_join(
852+
eg_df %>%
853+
dplyr::select(id, b, e) %>%
854+
dplyr::mutate(b = weights[1]*scale(b), e = weights[2]*scale(e)) %>%
855+
dplyr::rename(A2_b = b, A2_e = e),
856+
by = c("A2" = "id")
857+
) %>%
858+
dplyr::mutate(
859+
dist_b = A1_b - A2_b,
860+
dist_e = A1_e - A2_e,
861+
man_euc_dist = sqrt(dist_b**2 + dist_e**2)
862+
)
863+
864+
wide_res %>%
865+
long_format() %>%
866+
dplyr::filter(condition != match_null) %>%
867+
dplyr::left_join(
868+
dplyr::select(manual_euc_dist, item_nr, man_euc_dist),
869+
by = "item_nr"
870+
) %>%
871+
dplyr::filter(gen_euc_dist == man_euc_dist) %>%
872+
nrow()
873+
}, 20)
780874
})

0 commit comments

Comments
 (0)