Skip to content
Snippets Groups Projects
Commit 11286e16 authored by dj44vuri's avatar dj44vuri
Browse files

added some arguments to sim_all function

parent 80317791
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ Title: Simulate data for discrete choice experiments
Version: 0.0.1.0000
Authors@R:
person("Julian", "Sagebiel", , "julian.sagebiel@idiv.de", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
comment = c(ORCID = "0000-0002-0253-6875"))
Description: A package that supports simulating choice experiment data for given designs. It helps to quickly test different designs against each other.
License: MIT + file LICENSE
Encoding: UTF-8
......
#' Title Is a wrapper for sim_choice executing the simulation over all designs stored in a specific folder
#'
#' @param nosim Number of runs or simulations. For testing use 2 but once you go serious, use at least 200, for better results use 2000.
#' @param resps Number of respondents you want to simulate
#' @param destype Is it a design created with ngene or with spdesign. Ngene desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design
#' @param designpath The path to the folder where the designs are stored. For example "c:/myfancydec/Designs"
#'
#' @return a list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template.
#' @export
#'
......@@ -9,15 +14,14 @@
#' resps =240 # number of respondents
#' nosim=2 # number of simulations to run (about 500 is minimum)
#'
sim_all <- function(){
sim_all <- function(nosim=2, resps, destype="ngene", designpath){
#browser()
designfile<-list.files(designpath,full.names = T)
designname <- stringr::str_remove_all(list.files(designpath,full.names = F),
"(.ngd|_|.RDS)") ## Make sure designnames to not contain file ending and "_", as the may cause issues when replace
if (!exists("destype")) destype="ngene"
tictoc::tic()
......
......@@ -21,17 +21,19 @@
#' @examples \dontrun{ simchoice(designfile="somefile", no_sim=10, respondents=330,
#' mnl_U,utils=u[[1]] ,destype="ngene")}
#'
sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]] ,destype) {
sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]] ,destype=destype) {
## Function that transforms user written utiliy for simulation into utility function for mixl.
#### Function that transforms user written utility for simulation into utility function for mixl.
transform_util <- function() {
mnl_U <-paste(purrr::map_chr(utils,as.character,keep.source.attr = TRUE),collapse = "",";") %>%
stringr::str_replace_all( c( "priors\\[\"" = "" , "\"\\]" = "" , "~" = "=", "\\." = "_" , " b" = " @b" , "V_"="U_", " alt"="$alt"))
}
#### Function to simulate and estimate
estimate_sim <- function(run=1) { #start loop
cat("This is Run number ", run)
......@@ -49,8 +51,13 @@ sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]
# transform utility function to mixl format
mnl_U <- transform_util()
# Empty list where to store all designs later on
designs_all <- list()
#### Print some messages ####
cat("Utility function used in simulation, ie the true utility: \n\n")
......@@ -62,16 +69,20 @@ mnl_U <- transform_util()
designs_all <- list() ## Empty list where to store all designs later on
design<- readdesign(design = designfile) # Read in the design file
#### Read in the design file and set core variables ####
design<- readdesign(design = designfile, designtype = destype)
if (!("Block" %in% colnames(design))) design$Block=1 # If no Blocks exist, create a variable Blocks to indicate it is only one block
nsets<-nrow(design)
nblocks<-max(design$Block)
setpp <- nsets/nblocks # Choice Sets per respondent; in this 'no blocks' design everyone sees all 24 sets
setpp <- nsets/nblocks # Choice Sets per respondent
replications <- respondents/nblocks
......
......@@ -2,13 +2,13 @@
#'
#' @param data a dataframe that includes a design repeated for the number of observations
#' @param utility a list with the utility functions, one utility function for each alternatives
#' @param setspp an integeger, the number of choice sets per person
#'
#' @param setspp an integer, the number of choice sets per person
#' @param destype Is it a design created with ngene or with spdesign. Ngene desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design
#' @return a dataframe that includes simulated choices and a design
#' @export
#'
#' @examples \dontrun{simulate_choices(datadet, utils,setspp)}
simulate_choices <- function(data=datadet, utility =utils, setspp) { #the part in dataset that needs to be repeated in each run
simulate_choices <- function(data=datadet, utility =utils, setspp, destype) { #the part in dataset that needs to be repeated in each run
......
......@@ -4,7 +4,16 @@
\alias{sim_all}
\title{Title Is a wrapper for sim_choice executing the simulation over all designs stored in a specific folder}
\usage{
sim_all()
sim_all(nosim = 2, resps, destype = "ngene", designpath)
}
\arguments{
\item{nosim}{Number of runs or simulations. For testing use 2 but once you go serious, use at least 200, for better results use 2000.}
\item{resps}{Number of respondents you want to simulate}
\item{destype}{Is it a design created with ngene or with spdesign. Ngene desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design}
\item{designpath}{The path to the folder where the designs are stored. For example "c:/myfancydec/Designs"}
}
\value{
a list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template.
......
......@@ -10,7 +10,7 @@ sim_choice(
respondents = 330,
mnl_U,
utils = u[[1]],
destype
destype = destype
)
}
\arguments{
......
......@@ -4,14 +4,16 @@
\alias{simulate_choices}
\title{Simulate choices based on a dataframe with a design}
\usage{
simulate_choices(data = datadet, utility = utils, setspp)
simulate_choices(data = datadet, utility = utils, setspp, destype)
}
\arguments{
\item{data}{a dataframe that includes a design repeated for the number of observations}
\item{utility}{a list with the utility functions, one utility function for each alternatives}
\item{setspp}{an integeger, the number of choice sets per person}
\item{setspp}{an integer, the number of choice sets per person}
\item{destype}{Is it a design created with ngene or with spdesign. Ngene desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design}
}
\value{
a dataframe that includes simulated choices and a design
......
......@@ -38,7 +38,8 @@ u<- list(u1= list(
rbook <- sim_all()
rbook <- sim_all(nosim = nosim, resps=resps, destype = destype,
designpath = designpath)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment