Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dce_simulation_tool
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
dce_simulation_tool
Commits
43f6772f
Commit
43f6772f
authored
1 year ago
by
dj44vuri
Browse files
Options
Downloads
Patches
Plain Diff
small change in code structure
parent
51c31bb8
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
functions.R
+29
-50
29 additions, 50 deletions
functions.R
with
29 additions
and
50 deletions
functions.R
+
29
−
50
View file @
43f6772f
...
@@ -123,16 +123,12 @@ simulate_choices <- function(data=datadet, utility =utils, setspp) { #the part
...
@@ -123,16 +123,12 @@ simulate_choices <- function(data=datadet, utility =utils, setspp) { #the part
cat
(
"\n source of gis has been done \n"
)
cat
(
"\n source of gis has been done \n"
)
}
}
# source("Projects/ValuGaps/code/GIS_data.R")
if
(
!
exists
(
"manipulations"
))
manipulations
=
list
()
## If no user input on further data manipulations
if
(
!
exists
(
"manipulations"
))
manipulations
=
list
()
## If no user input on further data manipulations
n
=
seq_along
(
1
:
length
(
utility
))
# number of utility functions
n
=
seq_along
(
1
:
length
(
utility
))
# number of utility functions
cat
(
"\n dataset final_set exists: "
,
exists
(
"final_set"
),
"\n"
)
cat
(
"\n dataset final_set exists: "
,
exists
(
"final_set"
),
"\n"
)
if
(
exists
(
"final_set"
))
data
=
left_join
(
data
,
final_set
,
by
=
"ID"
)
if
(
exists
(
"final_set"
))
data
=
left_join
(
data
,
final_set
,
by
=
"ID"
)
...
@@ -207,28 +203,6 @@ sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]
...
@@ -207,28 +203,6 @@ sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]
require
(
"rlang"
)
require
(
"rlang"
)
mnl_U
<-
paste
(
map_chr
(
utils
,
as.character
,
keep.source.attr
=
TRUE
),
collapse
=
""
,
";"
)
%>%
str_replace_all
(
c
(
"priors\\[\""
=
""
,
"\"\\]"
=
""
,
"~"
=
"="
,
"\\."
=
"_"
,
" b"
=
" @b"
,
"V_"
=
"U_"
,
" alt"
=
"$alt"
))
cat
(
"mixl \n"
)
cat
(
mnl_U
)
cat
(
"\n Simulation \n"
)
print
(
u
)
estimate_sim
<-
function
(
run
=
1
)
{
#start loop
estimate_sim
<-
function
(
run
=
1
)
{
#start loop
cat
(
"This is Run number "
,
run
)
cat
(
"This is Run number "
,
run
)
...
@@ -236,40 +210,45 @@ sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]
...
@@ -236,40 +210,45 @@ sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]
database
<-
simulate_choices
(
datadet
,
utility
=
utils
,
setspp
=
setpp
)
database
<-
simulate_choices
(
datadet
,
utility
=
utils
,
setspp
=
setpp
)
cat
(
"This is the utility functions \n"
,
mnl_U
)
cat
(
"This is the utility functions \n"
,
mnl_U
)
model
<-
mixl
::
estimate
(
model_spec
,
start_values
=
est
,
availabilities
=
availabilities
,
data
=
database
,)
model
<-
mixl
::
estimate
(
model_spec
,
start_values
=
est
,
availabilities
=
availabilities
,
data
=
database
,)
return
(
model
)
return
(
model
)
}
}
mnl_U
<-
paste
(
map_chr
(
utils
,
as.character
,
keep.source.attr
=
TRUE
),
collapse
=
""
,
";"
)
%>%
str_replace_all
(
c
(
"priors\\[\""
=
""
,
"\"\\]"
=
""
,
"~"
=
"="
,
"\\."
=
"_"
,
" b"
=
" @b"
,
"V_"
=
"U_"
,
" alt"
=
"$alt"
))
cat
(
"mixl \n"
)
cat
(
mnl_U
)
cat
(
"\n Simulation \n"
)
print
(
u
)
designs_all
<-
list
()
designs_all
<-
list
()
design
<-
readdesign
(
design
=
designfile
)
design
<-
readdesign
(
design
=
designfile
)
if
(
!
exists
(
"design$Block"
))
design
$
Block
=
1
if
(
!
exists
(
"design$Block"
))
design
$
Block
=
1
nsets
<-
nrow
(
design
)
nblocks
<-
max
(
design
$
Block
)
setpp
<-
nsets
/
nblocks
# Choice Sets per respondent; in this 'no blocks' design everyone sees all 24 sets
replications
<-
respondents
/
nblocks
nsets
<-
nrow
(
design
)
nblocks
<-
max
(
design
$
Block
)
setpp
<-
nsets
/
nblocks
# Choice Sets per respondent; in this 'no blocks' design everyone sees all 24 sets
replications
<-
respondents
/
nblocks
datadet
<-
design
%>%
datadet
<-
design
%>%
arrange
(
Block
,
Choice.situation
)
%>%
arrange
(
Block
,
Choice.situation
)
%>%
slice
(
rep
(
row_number
(),
replications
))
%>%
## replicate design according to number of replications
slice
(
rep
(
row_number
(),
replications
))
%>%
## replicate design according to number of replications
mutate
(
ID
=
rep
(
1
:
respondents
,
each
=
setpp
))
%>%
# create Respondent ID.
mutate
(
ID
=
rep
(
1
:
respondents
,
each
=
setpp
))
%>%
# create Respondent ID.
relocate
(
ID
,
`Choice.situation`
)
%>%
relocate
(
ID
,
`Choice.situation`
)
%>%
as.data.frame
()
as.data.frame
()
database
<-
simulate_choices
(
data
=
datadet
,
utility
=
utils
,
setspp
=
setpp
)
database
<-
simulate_choices
(
data
=
datadet
,
utility
=
utils
,
setspp
=
setpp
)
model_spec
<-
mixl
::
specify_model
(
mnl_U
,
database
,
disable_multicore
=
F
)
model_spec
<-
mixl
::
specify_model
(
mnl_U
,
database
,
disable_multicore
=
F
)
...
@@ -291,22 +270,22 @@ coefs<-map(1:length(output),~summary(output[[.]])[["coefTable"]][c(1,8)] %>%
...
@@ -291,22 +270,22 @@ coefs<-map(1:length(output),~summary(output[[.]])[["coefTable"]][c(1,8)] %>%
pivot_wider
(
names_from
=
rowname
,
values_from
=
c
(
est
,
rob_pval0
))
)
%>%
pivot_wider
(
names_from
=
rowname
,
values_from
=
c
(
est
,
rob_pval0
))
)
%>%
bind_rows
(
.id
=
"run"
)
bind_rows
(
.id
=
"run"
)
output
[[
"summary"
]]
<-
psych
::
describe
(
coefs
[,
-1
],
fast
=
TRUE
)
output
[[
"summary"
]]
<-
psych
::
describe
(
coefs
[,
-1
],
fast
=
TRUE
)
output
[[
"coefs"
]]
<-
coefs
output
[[
"coefs"
]]
<-
coefs
pvals
<-
output
[[
"coefs"
]]
%>%
dplyr
::
select
(
starts_with
(
"rob_pval0"
))
pvals
<-
output
[[
"coefs"
]]
%>%
dplyr
::
select
(
starts_with
(
"rob_pval0"
))
output
[[
"power"
]]
<-
100
*
table
(
apply
(
pvals
,
1
,
function
(
x
)
all
(
x
<
0.05
)))
/
nrow
(
pvals
)
output
[[
"power"
]]
<-
100
*
table
(
apply
(
pvals
,
1
,
function
(
x
)
all
(
x
<
0.05
)))
/
nrow
(
pvals
)
output
[[
"metainfo"
]]
<-
c
(
Path
=
designfile
,
NoSim
=
no_sim
,
NoResp
=
respondents
)
output
[[
"metainfo"
]]
<-
c
(
Path
=
designfile
,
NoSim
=
no_sim
,
NoResp
=
respondents
)
print
(
kable
(
output
[[
"summary"
]],
digits
=
2
,
format
=
"rst"
))
print
(
kable
(
output
[[
"summary"
]],
digits
=
2
,
format
=
"rst"
))
print
(
output
[[
"power"
]])
print
(
output
[[
"power"
]])
return
(
output
)
return
(
output
)
...
...
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