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.

Scripting for a DCE

Ask questions about data format and processing of data, including the use of pre-estimation functions in Apollo. If your question relates to a specific error you are getting, please provide some of the output.
Post Reply
edwinicq
Posts: 1
Joined: 28 Nov 2022, 17:13

Scripting for a DCE

Post by edwinicq »

Hi,

I have a DCE with 4 attributes (info, alert, person, freq), 3 levels each, 1 block, 3 choices (2 treatment and ASC), 20 observations. I'm trying to do MNL effects coding.

I've been trying to follow apollo's manual and example, but not sure if I'm doing it correctly. Can anyone kindly shed some light on the parts in red?

Thanks in advance!

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

database=longfinal

### Set core controls
apollo_control = list(
modelName = "MNL_SP_effects",
modelDescr = "Simple MNL model on mode choice SP data using effects coding",
indivID = "ID",
outputDirectory = "output"
)


#?it syas apollo_beta and database have to have different variable names, I simply changed the var names in apollo beta, is that ok?

apollo_beta=c(asc_info_fin = 0,
asc_info_legal = 0,
asc_info_fam = 0,
asc_alert_nodetails = 0,
asc_alert_concerns = 0,
asc_alert_reasons = 0,
asc_person_patient = 0,
asc_person_dr = 0,
asc_person_emergency = 0,
asc_freq_once = 0,
asc_freq_year = 0,
asc_freq_each = 0)
apollo_fixed = c()
apollo_beta_fixed = c()
apollo_inputs = apollo_validateInputs()

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

### Effects coding constraint
info_fin_e = info_fin - info_fam
info_legal_e = info_legal - info_fam
alert_nodetails_e = alert_nodetails - alert_reasons
alert_concerns_e = alert_concerns - alert_reasons
person_patient_e = person_patient - person_emergency
person_dr_e = person_dr - person_emergency
freq_once_e = freq_once - freq_each
freq_year_e = freq_year - freq_each

### List of utilities: these must use the same names as in mnl_settings, order is irrelevant

#?I'm not sure how to define the V elements, is it asc + attribute_level_1_effect + attribute_level_2_effect + attribute_2?

V = list()
V[["info"]] = asc + info_fin_e + info_fam + info_legal_e
V[["alert"]] = asc + alert_nodetails_e + alert_concerns_e + alert_reasons
V[["person"]] = asc + person_patient_e+person_dr_e+person_emergency
V[["freq"]] = asc + freq_once_e +freq_year_e+freq_each

mnl_settings = list(
alternatives = c(info=1, alert=2, person=3, freq=4),
choiceVar = Choice,
utilities = V
)
### Compute probabilities using MNL model
P[["model"]] = apollo_mnl(mnl_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)
}

#this returns "Error in if (!test) { : missing value where TRUE/FALSE needed"
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Scripting for a DCE

Post by stephanehess »

Hi

first a point on language. You have already conducted the DCE - DCE is nothing to do with modelling, it's simply the data collection. Now you're doing the modelling use a discrete choice model.

In your code, you seem to get a bit mixed up between attributes and parameters, those are two different things again.

We can help you, but can you explain all the attributes with names and levels

Also, you seem to imply that you have 3 alternatives and 4 attributes, but in your code, you specify the utilities for 4 alternatives, which seem to be your attributes

Thanks

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