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)
Important: Read this before posting to this forum
- 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.
- 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
- Before asking a question on the forum, users are kindly requested to follow these steps:
- Check that the same issue has not already been addressed in the forum - there is a search tool.
- 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
- Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
- Make sure that R is using the latest official release of Apollo.
- Users can check which version they are running by entering packageVersion("apollo").
- Then check what is the latest full release (not development version) at http://www.ApolloChoiceModelling.com/code.html.
- To update to the latest official version, just enter install.packages("apollo"). To update to a development version, download the appropriate binary file from http://www.ApolloChoiceModelling.com/code.html, and install the package from file
- If the above steps do not resolve the issue, then users should follow these steps when posting a question:
- provide full details on the issue, including the entire code and output, including any error messages
- posts will not immediately appear on the forum, but will be checked by a moderator first. We check the forum at least twice a week. It may thus take a couple of days for your post to appear and before we reply. There is no need to submit the post multiple times.
Scripting for a DCE
-
- Site Admin
- Posts: 1264
- Joined: 24 Apr 2020, 16:29
Re: Scripting for a DCE
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
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