Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
simulateDCE
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dj44vuri
simulateDCE
Commits
aa033dcd
Commit
aa033dcd
authored
1 year ago
by
dj44vuri
Browse files
Options
Downloads
Plain Diff
WIP on main:
11286e16
added some arguments to sim_all function
parents
11286e16
05fcebc5
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
R/sim_all.R
+6
-3
6 additions, 3 deletions
R/sim_all.R
R/sim_choice.R
+4
-6
4 additions, 6 deletions
R/sim_choice.R
R/simulate_choices.R
+3
-3
3 additions, 3 deletions
R/simulate_choices.R
tests/manual-tests/Rbookfull.R
+2
-2
2 additions, 2 deletions
tests/manual-tests/Rbookfull.R
with
15 additions
and
14 deletions
R/sim_all.R
+
6
−
3
View file @
aa033dcd
...
@@ -14,10 +14,13 @@
...
@@ -14,10 +14,13 @@
#' 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)
#'
#'
sim_all
<-
function
(
nosim
=
2
,
resps
,
destype
=
"ngene"
,
designpath
){
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."
)
}
#browser()
designfile
<-
list.files
(
designpath
,
full.names
=
T
)
designfile
<-
list.files
(
designpath
,
full.names
=
T
)
designname
<-
stringr
::
str_remove_all
(
list.files
(
designpath
,
full.names
=
F
),
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
"(.ngd|_|.RDS)"
)
## Make sure designnames to not contain file ending and "_", as the may cause issues when replace
...
@@ -26,7 +29,7 @@ sim_all <- function(nosim=2, resps, destype="ngene", designpath){
...
@@ -26,7 +29,7 @@ sim_all <- function(nosim=2, resps, destype="ngene", designpath){
tictoc
::
tic
()
tictoc
::
tic
()
all_designs
<-
purrr
::
map
(
designfile
,
sim_choice
,
all_designs
<-
purrr
::
map
(
designfile
,
sim_choice
,
no_sim
=
nosim
,
respondents
=
resps
,
mnl_U
=
mnl_U
,
destype
=
destype
)
%>%
## iterate simulation over all designs
no_sim
=
nosim
,
respondents
=
resps
,
destype
=
destype
,
utils
=
u
)
%>%
## iterate simulation over all designs
stats
::
setNames
(
designname
)
stats
::
setNames
(
designname
)
...
...
This diff is collapsed.
Click to expand it.
R/sim_choice.R
+
4
−
6
View file @
aa033dcd
...
@@ -11,7 +11,6 @@
...
@@ -11,7 +11,6 @@
#' @param designfile path to a file containing a design.
#' @param designfile path to a file containing a design.
#' @param no_sim Number of runs i.e. how often do you want the simulation to be repeated
#' @param no_sim Number of runs i.e. how often do you want the simulation to be repeated
#' @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 mnl_U a list containing utility functions as formulas
#' @param utils The first element of the utility function list
#' @param utils 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
#'
#'
...
@@ -21,18 +20,18 @@
...
@@ -21,18 +20,18 @@
#' @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
,
mnl_U
,
utils
=
u
[[
1
]]
,
destype
=
destype
)
{
sim_choice
<-
function
(
designfile
,
no_sim
=
10
,
respondents
=
330
,
utils
=
u
,
destype
=
destype
)
{
#### Function that transforms user written utility for simulation into utility function for mixl.
#### Function that transforms user written utility for simulation into utility function for mixl.
transform_util
<-
function
()
{
transform_util
<-
function
()
{
mnl_U
<-
paste
(
purrr
::
map_chr
(
utils
,
as.character
,
keep.source.attr
=
TRUE
),
collapse
=
""
,
";"
)
%>%
mnl_U
<-
paste
(
purrr
::
map_chr
(
utils
[[
1
]]
,
as.character
,
keep.source.attr
=
TRUE
),
collapse
=
""
,
";"
)
%>%
stringr
::
str_replace_all
(
c
(
"priors\\[\""
=
""
,
"\"\\]"
=
""
,
"~"
=
"="
,
"\\."
=
"_"
,
" b"
=
" @b"
,
"V_"
=
"U_"
,
" alt"
=
"$alt"
))
stringr
::
str_replace_all
(
c
(
"priors\\[\""
=
""
,
"\"\\]"
=
""
,
"~"
=
"="
,
"\\."
=
"_"
,
" b"
=
" @b"
,
"V_"
=
"U_"
,
" alt"
=
"$alt"
))
}
}
#### Function to simulate and estimate
#### Function to simulate and estimate
####
estimate_sim
<-
function
(
run
=
1
)
{
#start loop
estimate_sim
<-
function
(
run
=
1
)
{
#start loop
...
@@ -50,7 +49,6 @@ sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]
...
@@ -50,7 +49,6 @@ sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]
}
}
# transform utility function to mixl format
# transform utility function to mixl format
mnl_U
<-
transform_util
()
mnl_U
<-
transform_util
()
...
@@ -61,7 +59,7 @@ designs_all <- list()
...
@@ -61,7 +59,7 @@ designs_all <- list()
cat
(
"Utility function used in simulation, ie the true utility: \n\n"
)
cat
(
"Utility function used in simulation, ie the true utility: \n\n"
)
print
(
u
)
print
(
u
tils
)
cat
(
"Utility function used for Logit estimation with mixl: \n\n"
)
cat
(
"Utility function used for Logit estimation with mixl: \n\n"
)
...
...
This diff is collapsed.
Click to expand it.
R/simulate_choices.R
+
3
−
3
View file @
aa033dcd
...
@@ -57,12 +57,12 @@ simulate_choices <- function(data=datadet, utility =utils, setspp, destype) { #
...
@@ -57,12 +57,12 @@ simulate_choices <- function(data=datadet, utility =utils, setspp, destype) { #
dplyr
::
group_by
(
ID
)
%>%
dplyr
::
group_by
(
ID
)
%>%
dplyr
::
mutate
(
!!!
manipulations
)
dplyr
::
mutate
(
!!!
manipulations
)
browser
()
subsets
<-
split
(
data
,
data
$
group
)
subsets
<-
split
(
data
,
data
$
group
)
subsets
<-
purrr
::
map2
(
.x
=
seq_along
(
u
),
.y
=
subsets
,
subsets
<-
purrr
::
map2
(
.x
=
seq_along
(
u
tility
),
.y
=
subsets
,
~
dplyr
::
mutate
(
.y
,
purrr
::
map_dfc
(
u
[[
.x
]],
by_formula
)))
~
dplyr
::
mutate
(
.y
,
purrr
::
map_dfc
(
u
tility
[[
.x
]],
by_formula
)))
data
<-
dplyr
::
bind_rows
(
subsets
)
data
<-
dplyr
::
bind_rows
(
subsets
)
...
...
This diff is collapsed.
Click to expand it.
tests/manual-tests/Rbookfull.R
+
2
−
2
View file @
aa033dcd
...
@@ -29,7 +29,7 @@ destype <- "spdesign"
...
@@ -29,7 +29,7 @@ destype <- "spdesign"
#place your utility functions here
#place your utility functions here
u
<-
list
(
u1
=
list
(
u
l
<-
list
(
u1
=
list
(
v1
=
V.1
~
bsq
*
alt1.sq
,
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
,
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
v3
=
V.3
~
bfarm2
*
alt3.farm2
+
bfarm3
*
alt3.farm3
+
bheight2
*
alt3.height2
+
bheight3
*
alt3.height3
+
bredkite
*
alt3.redkite
+
bdistance
*
alt3.distance
+
bcost
*
alt3.cost
...
@@ -39,7 +39,7 @@ u<- list(u1= list(
...
@@ -39,7 +39,7 @@ u<- list(u1= list(
rbook
<-
sim_all
(
nosim
=
nosim
,
resps
=
resps
,
destype
=
destype
,
rbook
<-
sim_all
(
nosim
=
nosim
,
resps
=
resps
,
destype
=
destype
,
designpath
=
designpath
)
designpath
=
designpath
,
u
=
ul
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment