From 30cbcf924110e1b2da405ddf72df4b111d33536a Mon Sep 17 00:00:00 2001 From: dj44vuri <julian.sagebiel@idiv.de> Date: Wed, 27 Dec 2023 18:49:32 +0100 Subject: [PATCH] added examples in tests and readme file --- .Rbuildignore | 1 + .Rhistory | 77 ------------------- .gitignore | 2 + R/globals.R | 2 +- R/sim_all.R | 12 +-- R/sim_choice.R | 5 +- R/simulate_choices.R | 4 +- README.md | 51 ++++++++++++ .../Rbookfull.R} | 0 9 files changed, 65 insertions(+), 89 deletions(-) delete mode 100644 .Rhistory create mode 100644 README.md rename tests/{manualtests.R => manual-tests/Rbookfull.R} (100%) diff --git a/.Rbuildignore b/.Rbuildignore index 350519f..fc17ab1 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -2,3 +2,4 @@ ^\.Rproj\.user$ ^LICENSE\.md$ ^data-raw$ +^README\.Rmd$ diff --git a/.Rhistory b/.Rhistory deleted file mode 100644 index e3e3d24..0000000 --- a/.Rhistory +++ /dev/null @@ -1,77 +0,0 @@ -options(prompt = prompt::prompt_git() ) -install.packages("prompt") -options(prompt = prompt::prompt_git() ) -usethis::edit_r_profile() -options(prompt = prompt::prompt_git() ) -options(prompt = "hello" ) -asdf -options(prompt = prompt::prompt_git() ) -fasjh -prompt::prompt_git() -git_branch() -prompt::prompt_git(git_branch()) -prompt::git_branch() -usethis::edit_r_profile() -options(prompt = prompt::git_branch() ) -options(prompt = paste0(prompt::git_branch(), "> ") ) -options(prompt = paste0(prompt::git_branch(), " > ") ) -if (interactive()) prompt::set_prompt(prompt::prompt_git)) -if (interactive()) prompt::set_prompt(prompt::prompt_git) -if (interactive()) prompt::set_prompt(prompt::prompt_git) -devtools::check() -devtools::load_all -devtools::load_all() -devtools::document() -devtools::document() -devtools::check() -usethis::use_package("formula.tools") -usethis::use_package("formula.tools") -devtools::check() -usethis::use_package("purrr") -devtools::check() -devtools::document() -devtools::check() -usethis::use_package(c(‘mixl’, ‘psych’, ‘rmarkdown’, ‘tibble’, ‘tictoc’)) -usethis::use_package(c("mixl", "psych", "rmarkdown", "tibble", "tictoc")) -usethis::use_package(c("mixl")) -usethis::use_package(c( "tictoc")) -usethis::use_package(c(tibble",)) -l -) -, -} -q -. -ajsdflakjdf -" -usethis::use_package(c("tibble")) -usethis::use_package(c("mixl", "psych", "rmarkdown", "tibble", "tictoc")) -usethis::use_package(c( "rmarkdown")) -usethis::use_package(c("psych")) -devtools::check() -load_all() -devtools::load_all() -system.file("extdata","Rbook" ,package = "simulateDCE") -source("~/share/groups/bioecon/Julian/simulateDCE/tests/manualtests.R") -source("~/share/groups/bioecon/Julian/simulateDCE/tests/manualtests.R") -source("~/share/groups/bioecon/Julian/simulateDCE/tests/manualtests.R") -source("~/share/groups/bioecon/Julian/simulateDCE/R/simulate_choices.R") -source("~/share/groups/bioecon/Julian/simulateDCE/tests/manualtests.R") -devtools::document() -devtools::load_all() -?basename -devtools::document() -usethis::use_package("evd") -source("~/share/groups/bioecon/Julian/simulateDCE/tests/manualtests.R") -source("~/share/groups/bioecon/Julian/simulateDCE/tests/manualtests.R") -package_version(dplyr) -package_version("dplyr") -packageVersion("dplyr") -usethis::use_package("dplyr, 1.1.4) -"" -" -usethis::use_package("dplyr",min_version = "1.1.4") -devtools::document() -packageVersion("dplyr") -packageVersion("dplyr") -install.packages("dplyr") diff --git a/.gitignore b/.gitignore index cd67eac..a331156 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .Rproj.user +.Rhistory +README.rmd diff --git a/R/globals.R b/R/globals.R index 7acdf7a..eaa2250 100644 --- a/R/globals.R +++ b/R/globals.R @@ -1 +1 @@ -utils::globalVariables(c("designfile", "destype", "Choice.situation", "Design", "." ,"ID", ":=", "..density..")) +utils::globalVariables(c("designfile", "destype", "Choice.situation", "Design", "." ,"ID", ":=", "..density..", "sou_gis", "block", "Block")) diff --git a/R/sim_all.R b/R/sim_all.R index f051369..39e90c4 100644 --- a/R/sim_all.R +++ b/R/sim_all.R @@ -23,7 +23,7 @@ sim_all <- function(){ all_designs<- purrr::map(designfile, sim_choice, no_sim= nosim,respondents = resps, mnl_U = mnl_U, destype=destype) %>% ## iterate simulation over all designs - setNames(designname) + stats::setNames(designname) time <- tictoc::toc() @@ -38,19 +38,19 @@ sim_all <- function(){ summaryall <- as.data.frame(purrr::map(all_designs, ~.x$summary)) %>% - dplyr::select(!ends_with("vars")) %>% - dplyr::relocate(ends_with(c(".n", "mean","sd", "min" ,"max", "range" , "se" ))) + dplyr::select(!dplyr::ends_with("vars")) %>% + dplyr::relocate(dplyr::ends_with(c(".n", "mean","sd", "min" ,"max", "range" , "se" ))) coefall <- purrr::map(all_designs, ~ .x$coefs) pat<-paste0("(",paste(designname,collapse = "|"),").") # needed to identify pattern to be replaced s<-as.data.frame(coefall) %>% - dplyr::select(!matches("pval|run")) %>% + dplyr::select(!dplyr::matches("pval|run")) %>% dplyr::rename_with(~ sub("est_b", "", .x), dplyr::everything()) %>% dplyr::rename_with( ~ paste0(.,"_",stringr::str_extract(.,pat )), dplyr::everything() ) %>% # rename attributes for reshape part 1 - dplyr::rename_with( ~ stringr::str_replace(.,pattern = pat,replacement=""), everything() ) %>% - reshape(varying =1:ncol(.), sep = "_" , direction = "long" ,timevar = "design", idvar = "run" ) + dplyr::rename_with( ~ stringr::str_replace(.,pattern = pat,replacement=""), dplyr::everything() ) %>% + stats::reshape(varying =1:ncol(.), sep = "_" , direction = "long" ,timevar = "design", idvar = "run" ) p=list() diff --git a/R/sim_choice.R b/R/sim_choice.R index de9f961..4cc3dc0 100644 --- a/R/sim_choice.R +++ b/R/sim_choice.R @@ -24,7 +24,6 @@ sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]] ,destype) { - require("rlang") ## Function that transforms user written utiliy for simulation into utility function for mixl. transform_util <- function() { @@ -90,7 +89,7 @@ mnl_U <- transform_util() model_spec <- mixl::specify_model(mnl_U, database, disable_multicore=F) - est=setNames(rep(0,length(model_spec$beta_names)), model_spec$beta_names) + est=stats::setNames(rep(0,length(model_spec$beta_names)), model_spec$beta_names) availabilities <- mixl::generate_default_availabilities( @@ -111,7 +110,7 @@ mnl_U <- transform_util() output[["coefs"]] <-coefs - pvals <- output[["coefs"]] %>% dplyr::select(starts_with("rob_pval0")) + pvals <- output[["coefs"]] %>% dplyr::select(dplyr::starts_with("rob_pval0")) output[["power"]] <- 100*table(apply(pvals,1, function(x) all(x<0.05)))/nrow(pvals) diff --git a/R/simulate_choices.R b/R/simulate_choices.R index 4aa5d2d..def513d 100644 --- a/R/simulate_choices.R +++ b/R/simulate_choices.R @@ -14,7 +14,7 @@ simulate_choices <- function(data=datadet, utility =utils, setspp) { #the part by_formula <- function(equation){ #used to take formulas as inputs in simulation utility function # //! cur_data_all may get deprecated in favor of pick - dplyr::pick(everything()) |> + dplyr::pick(dplyr::everything()) |> #cur_data_all() |> dplyr::transmute(!!formula.tools::lhs(equation) := !!formula.tools::rhs(equation) ) } @@ -41,7 +41,7 @@ simulate_choices <- function(data=datadet, utility =utils, setspp) { #the part if(exists("decisiongroups")) { ### create a new variable to classify decision groups. - data = dplyr::mutate(data,group = as.numeric(cut(row_number(), + data = dplyr::mutate(data,group = as.numeric(cut(dplyr::row_number(), breaks = decisiongroups * n(), labels = seq_along(decisiongroups[-length(decisiongroups)]), include.lowest = TRUE))) diff --git a/README.md b/README.md new file mode 100644 index 0000000..c60ae4a --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ + +<!-- README.md is generated from README.Rmd. Please edit that file --> + +# simulateDCE + +<!-- badges: start --> +<!-- badges: end --> + +The goal of simulateDCE is to make it easy to simulate choice experiment +datasets using designs from NGENE or `spdesign`. You have to store the +design file in a subfolder and need to specify certain parameters and +the utility functions for the data generating process. The package is +useful for + +1. Test different designs in terms of statistical power, efficiency and + unbiasedness + +2. To test the effects of deviations from RUM, e.g. heuristics, on + model performance for different designs. + +3. In teaching, using simulated data is useful, if you want to know the + data generating process. It helps to demonstrate Maximum likelihood + and choice models, knowing exactly what you should expect. + +4. You can use simulation in pre-registration to justify your sample + size and design choice. + +5. Before data collection, you can use simulated data to estimate the + models you plan to use in the actual analysis. You can thus make + sure, you can estimate all effects for given sample sizes. + +## Installation + +You can install the development version of simulateDCE from gitlab. You +need to install the `remotes` package first. The current version is +alpha and there is no version on cran: + +``` r +# FILL THIS IN! HOW CAN PEOPLE INSTALL YOUR DEV PACKAGE? +install.packages("remotes") +remotes::install_gitlab(repo = "dj44vuri/simulateDCE" , host = "https://git.idiv.de") +``` + +## Example + +This is a basic example which shows you how to solve a common problem: + +``` r +library(simulateDCE) +## basic example code +``` diff --git a/tests/manualtests.R b/tests/manual-tests/Rbookfull.R similarity index 100% rename from tests/manualtests.R rename to tests/manual-tests/Rbookfull.R -- GitLab