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

added test and error messages

parent fcbe60f9
No related branches found
No related tags found
No related merge requests found
...@@ -4,8 +4,9 @@ ...@@ -4,8 +4,9 @@
#' @param resps Number of respondents you want to simulate #' @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 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" #' @param designpath The path to the folder where the designs are stored. For example "c:/myfancydec/Designs"
#' @param u A list with utility functions. The list can incorporate as many decision rule groups as you want. However, each group must be in a list in this list. If you just use one group (the normal), this group still has to be in a list in the u list.
#' #'
#' @return a list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template. #' @return A list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template.
#' @export #' @export
#' #'
#' @examples #' @examples
...@@ -16,8 +17,14 @@ ...@@ -16,8 +17,14 @@
#' #'
sim_all <- function(nosim=2, resps, destype="ngene", designpath, u){ sim_all <- function(nosim=2, resps, destype="ngene", designpath, u){
if (missing(u) || !is.list(u)) {
stop("The 'u' must be provided and must be a list containing at least one list element.")
if (missing(u) || !(is.list(u) && any(sapply(u, is.list)))){
stop(" 'u' must be provided and must be a list containing at least one list element.")
}
if (missing(resps) || !(is.integer(resps) || (is.numeric(resps) && identical(trunc(resps), resps)))) {
stop(" 'resps' must be provided and must be an integer indicating the number of respondents per run.")
} }
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#' @examples \dontrun{ simchoice(designfile="somefile", no_sim=10, respondents=330, #' @examples \dontrun{ simchoice(designfile="somefile", no_sim=10, respondents=330,
#' mnl_U,utils=u[[1]] ,destype="ngene")} #' mnl_U,utils=u[[1]] ,destype="ngene")}
#' #'
sim_choice <- function(designfile, no_sim=10, respondents=330,utils=u ,destype=destype) { sim_choice <- function(designfile, no_sim=10, respondents=330,utils ,destype=destype) {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
\alias{sim_all} \alias{sim_all}
\title{Title Is a wrapper for sim_choice executing the simulation over all designs stored in a specific folder} \title{Title Is a wrapper for sim_choice executing the simulation over all designs stored in a specific folder}
\usage{ \usage{
sim_all(nosim = 2, resps, destype = "ngene", designpath) sim_all(nosim = 2, resps, destype = "ngene", designpath, u)
} }
\arguments{ \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{nosim}{Number of runs or simulations. For testing use 2 but once you go serious, use at least 200, for better results use 2000.}
...@@ -14,9 +14,11 @@ sim_all(nosim = 2, resps, destype = "ngene", designpath) ...@@ -14,9 +14,11 @@ sim_all(nosim = 2, resps, destype = "ngene", designpath)
\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{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"} \item{designpath}{The path to the folder where the designs are stored. For example "c:/myfancydec/Designs"}
\item{u}{A list with utility functions. The list can incorporate as many decision rule groups as you want. However, each group must be in a list in this list. If you just use one group (the normal), this group still has to be in a list in the u list.}
} }
\value{ \value{
a list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template. A list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template.
} }
\description{ \description{
Title Is a wrapper for sim_choice executing the simulation over all designs stored in a specific folder Title Is a wrapper for sim_choice executing the simulation over all designs stored in a specific folder
......
...@@ -8,8 +8,7 @@ sim_choice( ...@@ -8,8 +8,7 @@ sim_choice(
designfile, designfile,
no_sim = 10, no_sim = 10,
respondents = 330, respondents = 330,
mnl_U, utils,
utils = u[[1]],
destype = destype destype = destype
) )
} }
...@@ -20,8 +19,6 @@ sim_choice( ...@@ -20,8 +19,6 @@ sim_choice(
\item{respondents}{Number of respondents. How many respondents do you want to simulate in each run.} \item{respondents}{Number of respondents. How many respondents do you want to simulate in each run.}
\item{mnl_U}{a list containing utility functions as formulas}
\item{utils}{The first element of the utility function list} \item{utils}{The first element of the utility function list}
\item{destype}{Specify which type of design you use. Either ngene or spdesign} \item{destype}{Specify which type of design you use. Either ngene or spdesign}
......
...@@ -38,7 +38,7 @@ manipulations = list(alt1.x2= expr(alt1.x2/10), ...@@ -38,7 +38,7 @@ manipulations = list(alt1.x2= expr(alt1.x2/10),
#place your utility functions here #place your utility functions here
u<-list( u1 = ul<-list( u1 =
list( list(
v1 =V.1~ bpreis * alt1.x1 + blade*alt1.x2 + bwarte*alt1.x3 , v1 =V.1~ bpreis * alt1.x1 + blade*alt1.x2 + bwarte*alt1.x3 ,
...@@ -54,4 +54,5 @@ u<-list( u1 = ...@@ -54,4 +54,5 @@ u<-list( u1 =
destype="ngene" destype="ngene"
sedrive <- sim_all() sedrive <- sim_all(nosim = nosim, resps=resps, destype = destype,
designpath = designpath, u=ul)
designpath<- system.file("extdata","Rbook" ,package = "simulateDCE")
#notes <- "This design consists of different heuristics. One group did not attend the methan attribute, another group only decided based on the payment"
notes <- "No Heuristics"
resps =240 # number of respondents
nosim=2 # number of simulations to run (about 500 is minimum)
#betacoefficients should not include "-"
bsq=0.00
bredkite=-0.05
bdistance=0.50
bcost=-0.05
bfarm2=0.25
bfarm3=0.50
bheight2=0.25
bheight3=0.50
destype <- "spdesign"
#place your utility functions here
ul<- list(u1= list(
v1 =V.1 ~ bsq * alt1.sq,
v2 =V.2 ~ bfarm2 * alt2.farm2 + bfarm3 * alt2.farm3 + bheight2 * alt2.height2 + bheight3 * alt2.height3 + bredkite * alt2.redkite + bdistance * alt2.distance + bcost * alt2.cost,
v3 =V.3 ~ bfarm2 * alt3.farm2 + bfarm3 * alt3.farm3 + bheight2 * alt3.height2 + bheight3 * alt3.height3 + bredkite * alt3.redkite + bdistance * alt3.distance + bcost * alt3.cost
)
)
test_that(" u is not a list", {
expect_error(sim_all(nosim = nosim, resps=resps, destype = destype,
designpath = designpath, u=data.frame(u=" alp")),
"must be provided and must be a list containing ")
})
test_that("no value provided for utility", {
expect_error(sim_all(nosim = nosim, resps=resps, destype = destype,
designpath = designpath),
"must be provided and must be a list containing ")
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment