Skip to content
Snippets Groups Projects
Commit 7f9e9a1c authored by samuelsmock's avatar samuelsmock
Browse files

added decisiongroups to all functions

parent 0ce5c7a1
No related branches found
No related tags found
No related merge requests found
Showing
with 64 additions and 1802 deletions
Package: simulateDCE Package: simulateDCE
Title: Simulate data for discrete choice experiments Title: Simulate data for discrete choice experiments
Version: 0.1.0 Version: 0.1.1
Authors@R: Authors@R:
person("Julian", "Sagebiel", , "julian.sagebiel@idiv.de", role = c("aut", "cre"), person("Julian", "Sagebiel", , "julian.sagebiel@idiv.de", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-0253-6875")) comment = c(ORCID = "0000-0002-0253-6875"))
...@@ -20,6 +20,7 @@ Imports: ...@@ -20,6 +20,7 @@ Imports:
psych, psych,
purrr, purrr,
readr, readr,
rlang,
rmarkdown, rmarkdown,
stringr, stringr,
tibble, tibble,
......
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
#' @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. As a convention name beta coefficients starting with a lower case "b" #' @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. As a convention name beta coefficients starting with a lower case "b"
#' @param bcoefficients List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions #' @param bcoeff List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions
#' @param decisiongroups A vector showing how decision groups are numerically distributed
#' #'
#' @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
...@@ -16,7 +17,8 @@ ...@@ -16,7 +17,8 @@
#' resps =240 # number of respondents #' resps =240 # number of respondents
#' nosim=2 # number of simulations to run (about 500 is minimum) #' nosim=2 # number of simulations to run (about 500 is minimum)
#' #'
#' bcoeff <-list(bsq=0.00, # hypothesized beta coefficients for individual terms of the utility function #'
#' bcoeff <-list(bsq=0.00,
#' bredkite=-0.05, #' bredkite=-0.05,
#' bdistance=0.50, #' bdistance=0.50,
#' bcost=-0.05, #' bcost=-0.05,
...@@ -25,7 +27,7 @@ ...@@ -25,7 +27,7 @@
#' bheight2=0.25, #' bheight2=0.25,
#' bheight3=0.50) #' bheight3=0.50)
#' #'
sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff){ sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff, decisiongroups = c(0,1)){
################################################# #################################################
########## Input Validation Test ############### ########## Input Validation Test ###############
...@@ -47,6 +49,9 @@ sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff){ ...@@ -47,6 +49,9 @@ sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff){
stop("Argument 'bcoeff' must be a list.") stop("Argument 'bcoeff' must be a list.")
} }
if (length(u) != length(decisiongroups) -1){
stop("Number of decision groups must equal number of utility functions!")
}
# Check if values in bcoeff are numeric # Check if values in bcoeff are numeric
if (!all(sapply(bcoeff, is.numeric))) { if (!all(sapply(bcoeff, is.numeric))) {
stop("Values in 'bcoeff' must be numeric.") stop("Values in 'bcoeff' must be numeric.")
...@@ -91,7 +96,7 @@ sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff){ ...@@ -91,7 +96,7 @@ sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff){
tictoc::tic() tictoc::tic()
all_designs<- purrr::map(designfile, sim_choice, all_designs<- purrr::map(designfile, sim_choice,
no_sim= nosim,respondents = resps, destype=destype, ut=u, bcoefficients = bcoeff) %>% ## iterate simulation over all designs no_sim= nosim,respondents = resps, destype=destype, ut=u, bcoefficients = bcoeff, decisiongroups = decisiongroups) %>% ## iterate simulation over all designs
stats::setNames(designname) stats::setNames(designname)
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#' @param respondents Number of respondents. How many respondents do you want to simulate in each run. #' @param respondents Number of respondents. How many respondents do you want to simulate in each run.
#' @param ut The first element of the utility function list #' @param ut The first element of the utility function list
#' @param destype Specify which type of design you use. Either ngene or spdesign #' @param destype Specify which type of design you use. Either ngene or spdesign
#' @param bcoefficients List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions
#' @param decisiongroups A vector showing how decision groups are numerically distributed
#' #'
#' @return a list with all information on the run #' @return a list with all information on the run
#' @export #' @export
...@@ -20,7 +22,7 @@ ...@@ -20,7 +22,7 @@
#' @examples \dontrun{ simchoice(designfile="somefile", no_sim=10, respondents=330, #' @examples \dontrun{ simchoice(designfile="somefile", no_sim=10, respondents=330,
#' mnl_U,ut=u[[1]] ,destype="ngene")} #' mnl_U,ut=u[[1]] ,destype="ngene")}
#' #'
sim_choice <- function(designfile, no_sim=10, respondents=330,ut ,destype=destype, bcoefficients) { sim_choice <- function(designfile, no_sim=10, respondents=330,ut ,destype=destype, bcoefficients, decisiongroups=c(0,1)) {
...@@ -38,7 +40,7 @@ sim_choice <- function(designfile, no_sim=10, respondents=330,ut ,destype=destyp ...@@ -38,7 +40,7 @@ sim_choice <- function(designfile, no_sim=10, respondents=330,ut ,destype=destyp
cat("This is Run number ", run) cat("This is Run number ", run)
database <- simulate_choices(datadet, utility = ut, setspp=setpp, bcoefficients = bcoefficients) database <- simulate_choices(datadet, utility = ut, setspp=setpp, bcoefficients = bcoefficients, decisiongroups = decisiongroups)
cat("This is the utility functions \n" , mnl_U) cat("This is the utility functions \n" , mnl_U)
...@@ -85,6 +87,8 @@ designs_all <- list() ...@@ -85,6 +87,8 @@ designs_all <- list()
replications <- respondents/nblocks replications <- respondents/nblocks
## if replications is non int, assign unevenly
##browser() ##browser()
datadet<- design %>% datadet<- design %>%
dplyr::arrange(Block,Choice.situation) %>% dplyr::arrange(Block,Choice.situation) %>%
...@@ -95,7 +99,7 @@ designs_all <- list() ...@@ -95,7 +99,7 @@ designs_all <- list()
as.data.frame() as.data.frame()
database <- simulate_choices(data=datadet, utility = ut, setspp = setpp, bcoefficients = bcoefficients) database <- simulate_choices(data=datadet, utility = ut, setspp = setpp, bcoefficients = bcoefficients, decisiongroups = decisiongroups)
......
...@@ -5,10 +5,13 @@ ...@@ -5,10 +5,13 @@
#' @param setspp an integer, 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 #' @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 #' @return a dataframe that includes simulated choices and a design
#' @param bcoefficients List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions
#' @param decisiongroups A vector showing how decision groups are numerically distributed
#'
#' @export #' @export
#' #'
#' @examples \dontrun{simulate_choices(datadet, ut,setspp)} #' @examples \dontrun{simulate_choices(datadet, ut,setspp)}
simulate_choices <- function(data, utility, setspp, destype, bcoefficients) { #the part in dataset that needs to be repeated in each run simulate_choices <- function(data, utility, setspp, destype, bcoefficients, decisiongroups = c(0,1)) { #the part in dataset that needs to be repeated in each run
......
Source diff could not be displayed: it is too large. Options to address this: view the blob.
No preview for this file type
Design Choice situation alt1.cow alt1.adv alt1.vet alt1.far alt1.met alt1.bon alt2.cow alt2.adv alt2.vet alt2.far alt2.met alt2.bon Block
1 1 0 0 0 0 0 1 1 0 1 0 3 0 8
1 2 0 0 0 0 1 6 0 1 1 1 0 5 1
1 3 0 0 0 0 2 1 0 1 0 0 0 0 4
1 4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+ i1[0] * COW.dummy[0] * VET.dummy[1]
/
U(alt2) = b1 * COW
+ b2 * ADV
+ b3 * VET
+ b4 * FAR
+ b5 * MET
+ b6 * BON
+ i1 * COW.dummy[0] * VET.dummy[1]
/
U(alt3) = asc3[0.2]
;
$
\ No newline at end of file
...@@ -5,7 +5,15 @@ ...@@ -5,7 +5,15 @@
\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
update} update}
\usage{ \usage{
sim_all(nosim = 2, resps, destype = "ngene", designpath, u, bcoeff) sim_all(
nosim = 2,
resps,
destype = "ngene",
designpath,
u,
bcoeff,
decisiongroups = c(0, 1)
)
} }
\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.}
...@@ -18,7 +26,9 @@ sim_all(nosim = 2, resps, destype = "ngene", designpath, u, bcoeff) ...@@ -18,7 +26,9 @@ sim_all(nosim = 2, resps, destype = "ngene", designpath, u, bcoeff)
\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. As a convention name beta coefficients starting with a lower case "b"} \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. As a convention name beta coefficients starting with a lower case "b"}
\item{bcoefficients}{List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions} \item{bcoeff}{List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions}
\item{decisiongroups}{A vector showing how decision groups are numerically distributed}
} }
\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.
...@@ -33,7 +43,8 @@ update ...@@ -33,7 +43,8 @@ update
resps =240 # number of respondents resps =240 # number of respondents
nosim=2 # number of simulations to run (about 500 is minimum) nosim=2 # number of simulations to run (about 500 is minimum)
bcoeff <-list(bsq=0.00, # hypothesized beta coefficients for individual terms of the utility function
bcoeff <-list(bsq=0.00,
bredkite=-0.05, bredkite=-0.05,
bdistance=0.50, bdistance=0.50,
bcost=-0.05, bcost=-0.05,
......
...@@ -10,7 +10,8 @@ sim_choice( ...@@ -10,7 +10,8 @@ sim_choice(
respondents = 330, respondents = 330,
ut, ut,
destype = destype, destype = destype,
bcoefficients bcoefficients,
decisiongroups = c(0, 1)
) )
} }
\arguments{ \arguments{
...@@ -23,6 +24,10 @@ sim_choice( ...@@ -23,6 +24,10 @@ sim_choice(
\item{ut}{The first element of the utility function list} \item{ut}{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}
\item{bcoefficients}{List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions}
\item{decisiongroups}{A vector showing how decision groups are numerically distributed}
} }
\value{ \value{
a list with all information on the run a list with all information on the run
......
...@@ -4,7 +4,14 @@ ...@@ -4,7 +4,14 @@
\alias{simulate_choices} \alias{simulate_choices}
\title{Simulate choices based on a dataframe with a design} \title{Simulate choices based on a dataframe with a design}
\usage{ \usage{
simulate_choices(data, utility, setspp, destype, bcoefficients) simulate_choices(
data,
utility,
setspp,
destype,
bcoefficients,
decisiongroups = c(0, 1)
)
} }
\arguments{ \arguments{
\item{data}{a dataframe that includes a design repeated for the number of observations} \item{data}{a dataframe that includes a design repeated for the number of observations}
...@@ -14,6 +21,10 @@ simulate_choices(data, utility, setspp, destype, bcoefficients) ...@@ -14,6 +21,10 @@ simulate_choices(data, utility, setspp, destype, bcoefficients)
\item{setspp}{an integer, 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} \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{bcoefficients}{List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions}
\item{decisiongroups}{A vector showing how decision groups are numerically distributed}
} }
\value{ \value{
a dataframe that includes simulated choices and a design a dataframe that includes simulated choices and a design
......
rm(list=ls())
devtools::load_all()
library(rlang)
designpath<- system.file("extdata","SE_AGRI", package = "simulateDCE")
destype = 'ngene'
resps =360 # number of respondents
nosim=2 # number of simulations to run (about 500 is minimum)
#betacoefficients should not include "-"
bcoeff <- list(
basc = 4.2, ## very high asc
bprof = 0.3,
bexp = 0.3,
bdomestic = 0.3,
bforeign = 0.3,
bdamage = 0.6,
bprice = 0.2)
manipulations = list(alt1.professional= expr(alt1.initiator==1),
alt2.professional= expr(alt2.initiator==1),
alt1.expert = expr(alt1.initiator==2),
alt2.expert = expr(alt2.initiator==2),
alt1.domestic = expr(alt1.funding==1),
alt2.domestic = expr(alt2.funding==1),
alt1.foreign = expr(alt1.funding==2),
alt2.foreign = expr(alt2.funding==2))
#place your utility functions here
ul<- list(u1=
list(
v1 =V.1 ~ bprof*alt1.professional+ bexp * alt1.expert + bdomestic * alt1.domestic + bforeign * alt1.foreign + bdamage*alt1.damage + bprice * alt1.compensation,
v2 =V.2 ~ bprof*alt2.professional + bexp * alt2.expert + bdomestic * alt2.domestic + bforeign * alt2.foreign + bdamage*alt2.damage + bprice * alt2.compensation,
v3 =V.3 ~ basc)
)
seagri <- sim_all(nosim = nosim, resps=resps, destype = destype,
designpath = designpath, u=ul, bcoeff = bcoeff)
...@@ -51,4 +51,4 @@ ul<-list( u1 = ...@@ -51,4 +51,4 @@ ul<-list( u1 =
destype="ngene" destype="ngene"
sedrive <- sim_all(nosim = nosim, resps=resps, destype = destype, sedrive <- sim_all(nosim = nosim, resps=resps, destype = destype,
designpath = designpath, u=ul, bcoeff = bcoeff) designpath = designpath, u=ul, bcoeff = bcoeff, decisiongroups = decisiongroups)
No preview for this file type
...@@ -45,5 +45,5 @@ ul<- list(u1= list( ...@@ -45,5 +45,5 @@ ul<- list(u1= list(
v3 =V.3 ~ basc) v3 =V.3 ~ basc)
) )
sedrive <- sim_all(nosim = nosim, resps=resps, destype = destype, feedadditives <- sim_all(nosim = nosim, resps=resps, destype = destype,
designpath = designpath, u=ul, bcoeff = bcoeff) designpath = designpath, u=ul, bcoeff = bcoeff, decisiongroups = decisiongroups)
No preview for this file type
library(rlang)
library(formula.tools)
designpath<- system.file("extdata","Rbook" ,package = "simulateDCE") designpath<- system.file("extdata","Rbook" ,package = "simulateDCE")
...@@ -195,8 +197,7 @@ test_that("Simulation results are reasonable", { ...@@ -195,8 +197,7 @@ test_that("Simulation results are reasonable", {
sprintf("Variable est_%s does not exist in coeffNestedOutput", variable)) sprintf("Variable est_%s does not exist in coeffNestedOutput", variable))
# Check if variable is numeric # Check if variable is numeric
expect_type(coeffNestedOutput[[paste0("est_", variable)]], "numeric", ## expect_is(coeffNestedOutput[[paste0("est_", variable)]], "numeric", sprintf("Variable est_%s in coeffNestedOutput is not numeric", variable))
sprintf("Variable est_%s in coeffNestedOutput is not numeric", variable))
# Check if each entry in the variable column is numeric # Check if each entry in the variable column is numeric
expect_true(all(sapply(coeffNestedOutput[[paste0("est_", variable)]], is.numeric)), expect_true(all(sapply(coeffNestedOutput[[paste0("est_", variable)]], is.numeric)),
......
---
title: "SE_Agri-vignette"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{SE_Agri-vignette}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
<!-- There is an issue when creating these vignettes using usethis::use_vignette and
devtools::build_vignettes() where the compilied vignette HTML files are placed in /doc
rather than inst/doc
Best Practice is to follow these steps
1. Create vignette using usethis::use_vignette("my-vignette")
2. After making changes, then run devtools::build_vignettes()
3. Rebuild using devtools::install(build_vignettes = TRUE)
4. Check that it is in the vignette environment using browseVigettes()
If vignette does not appear in gitHub, it is possibly due to a file heirarchy problem where rendered files
appear in /doc instead of /inst/doc
To avoid this run:
tools::buildVignettes(dir = ".", tangle=TRUE)
dir.create("inst/doc")
file.copy(dir("vignettes", full.names=TRUE), "inst/doc", overwrite=TRUE)
More info here: https://community.rstudio.com/t/browsevignettes-mypackage-saying-no-vignettes-found/68656/7
-->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
# Introduction
This vignette demonstrates how to use simulate situations in which different utility functions apply to diffferent subsets of respondents. We will use a sample dataset and utility functions to generate simulated data and analyze the results. First off it is good practice remove all objects (variables, functions, etc.) from the current workspace and load all R files in the package directory into the current R session.
```{r setup}
library(simulateDCE)
rm(list=ls())
devtools::load_all()
library(rlang)
```
# Inititalize Variables
sim_all is the highest level function in the package and will run simulations for all designs contained in the specified design folder. Accordingly, this is generally the function the user will want to call. To prepare for using this function, a hypothesized utility function with corresponding beta coefficients representing the weight of each term must be declared in R.
The manipulation variable allows the user to assign different terms based on the values of columns in the experimental design file
```{r initialize}
bcoeff <- list(
basc = 4.2, ## very high asc
bprof = 0.3,
bexp = 0.3,
bdomestic = 0.3,
bforeign = 0.3,
bdamage = 0.6,
bprice = 0.2)
manipulations = list(alt1.professional= expr(alt1.initiator==1),
alt2.professional= expr(alt2.initiator==1),
alt1.expert = expr(alt1.initiator==2),
alt2.expert = expr(alt2.initiator==2),
alt1.domestic = expr(alt1.funding==1),
alt2.domestic = expr(alt2.funding==1),
alt1.foreign = expr(alt1.funding==2),
alt2.foreign = expr(alt2.funding==2))
#place your utility functions here
ul<- list(u1=
list(
v1 =V.1 ~ bprof*alt1.professional+ bexp * alt1.expert + bdomestic * alt1.domestic + bforeign * alt1.foreign + bdamage*alt1.damage + bprice * alt1.compensation,
v2 =V.2 ~ bprof*alt2.professional + bexp * alt2.expert + bdomestic * alt2.domestic + bforeign * alt2.foreign + bdamage*alt2.damage + bprice * alt2.compensation,
v3 =V.3 ~ basc)
)
```
# Other parameters
Besides these arguments the user must also specify the number of respondents in the simulated survey and the number of times to run the model. The number of respondents (resps) should be selected based on experimental design parameters, while the number of simulations (nosim) should be large enough to glean statistically significant data. It is best to use a small number for this while learning to use the package and a large number (at least 500) once the other parameters have been settled.
The simulation can be ran using spdesign or NGENE design files which will be contained in the design path. The design path and design type, must also be specified as strings:
```{r other}
designpath<- system.file("extdata","se_AGRI" ,package = "simulateDCE")
## can also be specified using relative path eg. designpath<- "Projects/CSA/Designs/"
#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)
## design type must be either 'spdesign' or 'ngene'
destype <- "spdesign"
```
# Randomness
As several part of the simulation rely on random values within experimentally defined bounds, the output of a given simulation call using sim_all will vary each time it is called. Unless the seed for R's randome number generator is set like so:
```{r random}
set.seed(3393)
```
# Output
The sim_all function returns a multidimensional R list containing graphs, simulated observations and a dataframe containing sumaries of estimated b coefficients. In general these will be printed to the console, but the entire results can also be assigned to an r list object.
```{r output}
csa <- simulateDCE::sim_all(nosim = nosim, resps=resps, destype = destype,
designpath = designpath, u= ul, bcoeff = bcoeff)
```
# Accessing the Output in R
The beta cofficients for each simulation are contained in a dataframe called coeffs located within {result}->olddesign->coefs. and a summary table, which displays
statistics of these b coefficients across all simulations is contained in ->olddesign->summary.
You can also save the results to disk using saveRDS(csa,file = "tests/manual-tests/csa.RDS")
```{r accessOutput}
## nested results are hard coded, if the design changes this must aswell
simulationCoeff <- csa$olddesign$coefs
coeffSummary <- csa$olddesign$summary
print(simulationCoeff)
print(coeffSummary)
## saveRDS(csa,file = "tests/manual-tests/csa.RDS")
```
...@@ -40,12 +40,8 @@ This vignette demonstrates how to use the `simulateDCE` package to simulate disc ...@@ -40,12 +40,8 @@ This vignette demonstrates how to use the `simulateDCE` package to simulate disc
```{r setup} ```{r setup}
library(simulateDCE) library(simulateDCE)
rm(list=ls())
devtools::load_all()
library(rlang) library(rlang)
library(formula.tools)
``` ```
# Inititalize Variables # Inititalize Variables
...@@ -108,7 +104,7 @@ The sim_all function returns a multidimensional R list containing graphs, simula ...@@ -108,7 +104,7 @@ The sim_all function returns a multidimensional R list containing graphs, simula
```{r output} ```{r output}
sedrive <- sim_all(nosim = nosim, resps=resps, destype = destype, sedrive <- sim_all(nosim = nosim, resps=resps, destype = destype,
designpath = designpath, u=ul, bcoeff = bcoeff) designpath = designpath, u=ul, bcoeff = bcoeff, decisiongroups = decisiongroups)
......
...@@ -15,7 +15,7 @@ Best Practice is to follow these steps ...@@ -15,7 +15,7 @@ Best Practice is to follow these steps
1. Create vignette using usethis::use_vignette("my-vignette") 1. Create vignette using usethis::use_vignette("my-vignette")
2. After making changes, then run devtools::build_vignettes() 2. After making changes, then run devtools::build_vignettes()
3. Rebuild using devtools::install(build_vignettes = TRUE) 3. Rebuild using devtools::install(build_vignettes = TRUE)
4. Check that it is in the vignette environment using browseVigettes() 4. Check that it is in the vignette environment using browseVignettes()
If vignette does not appear in gitHub, it is possibly due to a file heirarchy problem where rendered files If vignette does not appear in gitHub, it is possibly due to a file heirarchy problem where rendered files
appear in /doc instead of /inst/doc appear in /doc instead of /inst/doc
...@@ -40,12 +40,8 @@ This vignette demonstrates how to use the `simulateDCE` package to simulate disc ...@@ -40,12 +40,8 @@ This vignette demonstrates how to use the `simulateDCE` package to simulate disc
```{r setup} ```{r setup}
library(simulateDCE) library(simulateDCE)
rm(list=ls())
devtools::load_all()
library(rlang) library(rlang)
library(formula.tools)
``` ```
# Inititalize Variables # Inititalize Variables
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment