Important: Read this before posting to this forum

  1. This forum is for questions related to the use of Apollo. We will answer some general choice modelling questions too, where appropriate, and time permitting. We cannot answer questions about how to estimate choice models with other software packages.
  2. There is a very detailed manual for Apollo available at http://www.ApolloChoiceModelling.com/manual.html. This contains detailed descriptions of the various Apollo functions, and numerous examples are available at http://www.ApolloChoiceModelling.com/examples.html. In addition, help files are available for all functions, using e.g. ?apollo_mnl
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. Ensure that the correct syntax has been used. For any function, detailed instructions are available directly in Apollo, e.g. by using ?apollo_mnl for apollo_mnl
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. posts will not immediately appear on the forum, but will be checked by a moderator first. This may take a day or two at busy times. There is no need to submit the post multiple times.

MDCEV with outside good

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
maxbirdsong
Posts: 1
Joined: 21 Jan 2023, 14:53

MDCEV with outside good

Post by maxbirdsong »

Hi all,

I know this is a very basic question, but this is my first time using Apollo, and I am having some troubles.

I am trying to set up a choice model with RP data. In my dataset, we have 36 mountain biking destinations (with destination attributes), the total number of times each individual (n=500) visited each of the 36 destinations during the last year, and also the travel distance for each trip. I would like to know the influence of attributes (i.e., total mileage or facilities) on destination choice.

Am I correct in assuming that a MDCEV model, with time as an outside good is the appropriate way to set this up?

Below is the code that I have created so far, with the help of a colleague. But I feel that I should define the model parameters differently if my goal is to have an estimate for each attribute across all 36 destinations, rather than an estimate for each attribute at each of the 36 destinations.

Thanks,
Max

{r results}

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="MDCEV_with_outside_good",
modelDescr ="MDCEV model hunting",
indivID = "AC",
outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)
database = dat_wma %>%
dplyr::select(AC, ends_with("TRIPS"), ends_with("DIS")) %>% na.omit()
### for data dictionary, use ?apollo_timeUseData

### Create consumption variables for combined activities
database$t_outside = 365-rowSums(database %>% dplyr::select(starts_with("WMA") & ends_with("_TRIPS"))) # outside good: time spent at home and travelling
#database$t_leisure = rowSums(database[,c("t_a07", "t_a08", "t_a09")])

# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation

apollo_beta = c(0, rep(1, 36), rep(0, 144), 1)
names(apollo_beta) = c("alpha_base",
paste0("gamma_", 1:36), paste0("delta_", 1:36),
paste0("delta_dist_", 1:36),
paste0("delta_size_", 1:36),
paste0("delta_walkin_", 1:36),
"sig")



### 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("sig")

# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### 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()

### Define individual alternatives

alternatives = c("outside", paste0("site_", 1:36))


### Define availabilities
avail = list()

### Define continuous consumption for individual alternatives
continuousChoice = list()
V = list()
alpha = list()
gamma = list()
cost = list()

avail[["outside"]] = 1
V[["outside"]] = 0
alpha[["outside"]] = 1 /(1 + exp(-apollo_beta["alpha_base"]))
continuousChoice[["outside"]] = t_outside

for(i in 1:36){
avail[[paste0("site_", i)]] = 1
continuousChoice[[paste0("site_", i)]] = as.vector(eval(parse(text = paste0("WMA_", i, "_TRIPS"))))
V[[paste0("site_", i)]] = apollo_beta[paste0("delta_", i)] +
apollo_beta[paste0("delta_dist_", i)] * eval(parse(text = paste0("WMA_", i, "_DIS"))) ### Define utilities for individual alternatives
alpha[[paste0("site_", i)]] = 1 /(1 + exp(-apollo_beta["alpha_base"])) ### Define alpha parameters
gamma[[paste0("site_", i)]] = apollo_beta[paste0("gamma_", i)] ### Define gamma parameters
cost[[paste0("site_", i)]] = 1
}


### Define settings for MDCEV model
mdcev_settings <- list(alternatives = alternatives,
avail = avail,
continuousChoice = continuousChoice,
utilities = V,
alpha = alpha,
gamma = gamma,
sigma = sig,
cost = cost,
budget = 365)

### Compute probabilities using MDCEV model
P[["model"]] = apollo_mdcev(mdcev_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)
}

# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: MDCEV with outside good

Post by stephanehess »

Max

your data is not really discrete-continuous, but discrete-discrete, but you could try an MDCEV model indeed.

What the outside good relates to is unspent budget. So you would need to think about the budget first, and I guess in your case, one option would be the maximum number of trips possible per year.

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply