Page 1 of 1

MDCEV with outside good

Posted: 21 Jan 2023, 15:23
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)

Re: MDCEV with outside good

Posted: 02 Feb 2023, 11:12
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