Skip to content
Snippets Groups Projects
Commit 20cff6f9 authored by dj44vuri's avatar dj44vuri
Browse files

utility transformation works now

parent b77c6a1b
No related branches found
No related tags found
No related merge requests found
Version: 1.0
ProjectId: b4471ba3-d3d6-48a1-a26a-00c2bdd34cf7
RestoreWorkspace: No
SaveWorkspace: No
......
......@@ -108,15 +108,40 @@ designs_all <- list()
}
transform_util2 <- function() {
mnl_U <-paste(purrr::map_chr(ut[[1]],as.character,keep.source.attr = TRUE),collapse = "",";") %>%
stringr::str_replace_all(setNames(paste0("@", names(bcoefficients)), paste0("\\b", names(bcoefficients), "\\b"))) %>%
stringr::str_replace_all(setNames(paste0("$", names(datadet)), paste0("\\b", names(datadet), "\\b"))) %>%
stringr::str_replace_all( c( "priors\\[\"" = "" , "\"\\]" = "" , "~" = "=", "\\." = "_" , "V_"="U_"))
transform_util2 <- function() {
# Filter relevant database variables
relevant_database_vars <- setdiff(names(database), c("V_1", "V_2", "U_1", "U_2", "CHOICE"))
mnl_U <- paste(
purrr::map_chr(ut[[1]], as.character, keep.source.attr = TRUE),
collapse = "",
";"
) %>%
# Replace coefficients with exact matches
stringr::str_replace_all(setNames(
paste0("@", names(bcoefficients)),
paste0("(?<![._a-zA-Z0-9])", names(bcoefficients), "(?![._a-zA-Z0-9-])")
)) %>%
# General transformations
stringr::str_replace_all(c(
`priors\\["` = "",
`"\\]` = "",
`~` = "=",
`\\.` = "_",
`V_` = "U_"
)) %>%
# Replace only relevant database variables
stringr::str_replace_all(setNames(
paste0("$", relevant_database_vars),
paste0("(?<![._a-zA-Z0-9])", relevant_database_vars, "(?![._a-zA-Z0-9-])")
)) %>%
# Clean up duplicate symbols
stringr::str_replace_all(c(`@@` = "@", "\\$\\$" = "$"))
return(mnl_U)
}
}
......
......@@ -82,7 +82,7 @@ simulate_choices <- function(data, utility, setspp, destype, bcoefficients, deci
## add random component and calculate total utility
data<- data %>%
dplyr::rename_with(~ stringr::str_replace(.,pattern = "\\.","_"), tidyr::everything()) %>%
dplyr::rename_with(~ stringr::str_replace_all(.,pattern = "\\.","_"), tidyr::everything()) %>%
dplyr::mutate(dplyr::across(.cols=n,.fns = ~ evd::rgumbel(setspp,loc=0, scale=1), .names = "{'e'}_{n}" ),
dplyr::across(dplyr::starts_with("V_"), .names = "{'U'}_{n}") + dplyr::across(dplyr::starts_with("e_")) ) %>% dplyr::ungroup() %>%
dplyr::mutate(CHOICE=max.col(.[,grep("U_",names(.))])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment