Skip to content

Commit

Permalink
Fixed bug where positions shifted with multiple dummies. This is avoi…
Browse files Browse the repository at this point in the history
…ded by now matching by name of the dummy parameter.
  • Loading branch information
edsandorf committed Jun 30, 2023
1 parent cfe9754 commit 10337c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ extract_unparsed_values <- function(string) {
# This is the expression that handles the prior distributions
expr <- "-?(\\d+\\.?\\d*|(normal|uniform|lognormal|triangular)_p\\(.*?\\))"

for (i in which(str_detect(string_elements, "b_.*_dummy\\["))){
expanded <- as.list(unlist(str_extract_all(values[i], expr)))
for (dummy in names(values)[str_detect(names(values), "b_.*_dummy")]) {
expanded <- as.list(unlist(str_extract_all(values[dummy], expr)))

# Get the corresponding attribute by remove "b_" and "_dummy"
# grep(str_extract(names(values[i]), "(?<=b_).*(?=_dummy)")

names(expanded) <- paste0(str_extract(names(values[i]), "^.*(?=_dummy$)"), seq_along(expanded) + 1)
names(expanded) <- paste0(str_extract(dummy, "^.*(?=_dummy$)"), seq_along(expanded) + 1)

# Order doesn´t matter, so we can do
values <- c(values[-i], expanded)
values <- c(values[names(values) != dummy], expanded)
# values <- c(values[-i], expanded)
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ test_that("Normal dummy priors are extracted", {

})

test_that("Normal dummy priors are extracted", {
utility <- list(
alt1 = "b_x1_dummy[c(-0.1, 0.2)] * x_1[c(1, 3, 5)] + b_x2_dummy[c(0.3, 0.4)] * x_2[c(0, 1, 2)] + b_x3[-0.2] * x_3[seq(0, 1, 0.25)]",
alt2 = "b_x1_dummy * x_1 + b_x2 * x_2 + b_x3 * x_3"
)

expect_equal(
prepare_priors(utility, draws, R),
list(c(b_x3 = -0.2, b_x12 = -0.1, b_x13 = 0.2, b_x22 = 0.3, b_x23 = 0.4))
)

})

test_that("Normal dummy priors are extracted", {
utility <- list(
alt1 = "b_x1_dummy[c(-0.1, --0.2)] * x_1[c(1, 3, 5)] + b_x2[0.4] * x_2[c(0, 1)] + b_x3[-0.2] * x_3[seq(0, 1, 0.25)]",
Expand Down

0 comments on commit 10337c9

Please sign in to comment.