Page 1 of 1

Scripting for a DCE

Posted: 29 Nov 2022, 14:40
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)

Re: Scripting for a DCE

Posted: 30 Nov 2022, 15:37
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