Thanks for your reply.
Code: Select all
```{r Init, include=FALSE}
print("To strive, to seek, to find, and not to yield.")
```
```{r Libraries, include=FALSE}
suppressWarnings(suppressMessages(library(apollo)))
suppressWarnings(suppressMessages(library(dplyr)))
```
```{r, include=FALSE}
### Clear memory
rm(list = ls())
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="NL_1",
modelDescr ="Preliminary two-level Nested Logit",
indivID ="ID"
)
```
```{r}
database <- read.csv("./data_reshaped.csv")
# Data treatment
database$av_car <- 1
database$av_bike <- 1
database$av_walk <- 1
database$av_other <- 1
```
```{r}
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_car = 0,
asc_bike = 0,
asc_walk = 0,
asc_other = 0,
#b_travel_time = 0,
b_travel_time_nm = 0,
b_travel_time_car = 0,
#b_travel_time_bike = 0,
#b_travel_time_walk = 0,
b_travel_cost_car = 0,
#b_parking_cost_car = 0,
b_LTS_bike = 0,
b_LTS_walk = 0,
# #b_purpose_ws = 0,
#b_purpose_sh = 0,
#b_purpose_so = 0,
#b_purpose_o = 0,
#b_homebased = 0,
#b_length = 0
lambda_nomotor = 0.95 # One lambda for each nest (and subnest) that contains more than one alternatives
)
### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_beta_fixed = c() if none
apollo_fixed = c("asc_car")
### Read in starting values for at least some parameters from existing model output file
#apollo_beta=apollo_readBeta(apollo_beta,apollo_fixed,"Apollo_example_3",overwriteFixed=FALSE)
```
```{r}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
```
```{r}
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
### Attach inputs and detach after function exit
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
### Create list of probabilities P
P = list()
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['car']] = asc_car +
#b_travel_time * CARTT +
b_travel_time_car * CARTT +
b_travel_cost_car * CARCOST +
b_travel_cost_car * CARPCOST
V[['bike']] = asc_bike +
#b_travel_time * BIKETT +
b_travel_time_nm * BIKETT +
#b_travel_time_bike * BIKETT +
b_LTS_bike * BIKELTS
V[['walk']] = asc_walk +
#b_travel_time * WALKTT +
b_travel_time_nm * WALKTT +
#b_travel_time_walk * WALKTT +
b_LTS_walk * WALKLTS
V[['other']] = asc_other
### Specify nests for NL model
nlNests = list(root=1, NM=lambda_nomotor)
### Specify tree structure for NL model
nlStructure = list()
nlStructure[["root"]] = c("car","NM")
nlStructure[["NM"]] = c("bike","walk","other")
### Define settings for NL model
nl_settings <- list(
alternatives = c(car=1, bike=2, walk=3, other=4),
avail = list(car=av_car, bike=av_bike, walk=av_walk, other=av_other),
choiceVar = CHOICE,
V = V,
nlNests = nlNests,
nlStructure = nlStructure
)
### Compute probabilities using MNL model
P[['model']] = apollo_nl(nl_settings, functionality)
### Take product across observation for same individual
P = apollo_panelProd(P, apollo_inputs, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
```
```{r}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
```
```{r}
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
```
Thanks.