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.

Choice sets with large numbers of alternatives

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
Malte
Posts: 1
Joined: 11 May 2020, 15:10

Choice sets with large numbers of alternatives

Post by Malte »

Hi,

I have a question on choice sets with large numbers of alternatives:
I have a RP dataset with each respondent making one choice. The number of alternatives varies over respondents. For instance, respondent 1 has only 2 alternatives whereas respondent 2 has 100.
The data is organized such that all attributes are stored in the columns, i.e. atr1_1, atr2_2,… As a consequence, I have a very wide data structure with more than 3.000.000 columns.
Is there an efficient way to specify a model with varying and large numbers of alternatives in Apollo?
I was thinking of a loop for the utilities, but what to do with the Apollo-specifications for avail and alternatives?

Thanks a lot,

Malte
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Choice sets with large numbers of alternatives

Post by dpalma »

Hi Malte,

In your case, I would recommend creating 100 alternatives for all individuals, and then set their availability to reflect the actual number of alternatives that each individual faces. You can use a "for" loop to avoid writing 100 utilities.

Below is an example. That piece of code should go inside apollo_probabilities. It assumes that you have columns called "av_j" indicating the availability of alternative j in your database. It also assumes that "b1" and "b2" are the only parameters to estimate, and that each alternative is described by two attributes in the database called "atr1_j" and "atr2_j".

Code: Select all

V = list()
A = list()
J = 100 # number of alternatives
for(j in 1:J){
  altName = paste0("alt",j)
  V[[altName]] = b1*get( paste0("atr1_", j) ) + b2*get( paste0("atr1_", j) )
  A[[altName]] = get( paste0("av_", j) )
}
mnl_settings = list(
  alternatives  = setNames(1:J, names(V)),
  avail         = A,
  choiceVar     = choice,
  V             = V
)
P[['model']] = apollo_mnl(mnl_settings, functionality)
If you don't have columns indicating the availability of each alternative, and instead have a single column called "nAvail" indicating the number of alternatives for each individual (i.e. they have alternatives from 1 to nAvail available), then the code would change as follows.

Code: Select all

V = list()
A = list()
J = 100 # number of alternatives
for(j in 1:J){
  altName = paste0("alt",j)
  V[[altName]] = b1*get( paste0("atr1_", j) ) + b2*get( paste0("atr1_", j) )
  A[[altName]] = ( nAvail >= j )
}
mnl_settings = list(
  alternatives  = setNames(1:J, names(V)),
  avail         = A,
  choiceVar     = choice,
  V             = V
)
P[['model']] = apollo_mnl(mnl_settings, functionality)
I hope this helps with your question.

Best wishes
David
roussanoff
Posts: 7
Joined: 29 Aug 2021, 22:44

Re: Choice sets with large numbers of alternatives

Post by roussanoff »

This solution by David assumes that altName object can be built as altName = paste0("alt",j). This requires that all alternatives have simple names (in this example, alt1, alt2 etc.)

I am working with a large choice model with many choices with complicated names. My choice is cities, like "Paris", "Beijing", "Caracas" and so on. The choice set is, of course, extractable from the database. However, I am not allowed to refer to the database object in the code. I also work with different subsamples of the data, I would prefer not to use altNames <- c("Paris", "Beijing", "Caracas, ... 500 other cities) because it is not robust to changes in the data.

Of course, it is possible to build the altNames object outside of the model definition. However, this will create problems for estimation on several nodes. Is it true that Apollo "fetches" the database object for the estimation but may have issues if other objects are called inside apollo_probabilities? What is the recommended way for dynamically building the list of alternatives inside apollo_probabilities()?
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Choice sets with large numbers of alternatives

Post by dpalma »

Hi,

You can access the database from within apollo_probabilities by referencing it through apollo_inputs. So let's imagine you want to read the name of all variables in the database. Instead of writing:

Code: Select all

names(database)
You should write

Code: Select all

names(apollo_inputs$database)
I don't know how your database is shaped, or what are the name of your variables. But let's imagine you have a variable called "choice", whose content is the name (as text) of the chosen destination. You could extract the name of all chosen alternatives as follows:

Code: Select all

altNames <- sort(unique(apollo_inputs$database$choice))
Note that I am sorting the values, because I can't be sure in what order the function "unique" will return them. You could then write a loop that goes over the elements inside altNames and define their deterministic utility ("V"), and availability ("avail").

Hope this helps
David
aellyson
Posts: 5
Joined: 20 Oct 2021, 19:26

Re: Choice sets with large numbers of alternatives

Post by aellyson »

Hi, I am having a similar but not identical problem. I also have a discrete choice model with a large number of alternatives, but it is a stated preference discrete choice experiment with an efficient design. In the data, there are 1096 possible alternatives where each choice task has two options (alternative 1 and alternative 2) as well as an opt-out. I also have a column in my database indicating which of the 1096 alternatives were provided in the choice task, but each participant only receives two alternatives and the opt-out (a third alternative) at a time. I don't think I should be implementing the above solution, but I'm also not sure what I should be doing here to account for the very large number of alternatives. Do I need 1096 columns in my database to indicate which of the two alternatives were available in each choice task?
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Choice sets with large numbers of alternatives

Post by stephanehess »

Hi

what you're describing is a standard stated choice dataset, where in each task, a set of the possible options from the candidate set are shown. The easiest solution is to just code the data in the standard way, with the values for the two options (plus optout) that are shown in a given task being used in that row in the data. The code only needs to know that there is a choice set of 3 alternatives in each task

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
aellyson
Posts: 5
Joined: 20 Oct 2021, 19:26

Re: Choice sets with large numbers of alternatives

Post by aellyson »

Stephane, Yes, Thank you! This is what I thought, but was probably confusing myself by reading about some of the more complex set ups. I'm new to Apollo so thanks for making it easy. - Alice
lolabdn
Posts: 4
Joined: 05 Apr 2023, 09:45
Location: France

Re: Choice sets with large numbers of alternatives

Post by lolabdn »

Hi,

I also have a question on choice sets with large numbers of alternatives. I am currently working on a residential location choice model. I have a RP dataset with each respondent declaring his residential location (a municipality). In total, I have more than 1200 municipalities. I would like to reduce the set of alternative for each respondent, and define a set of 6 random alternatives (including the actual choice). Each respondent would therefore be associated to this set and the set would be different for each.

I was thinking on making a loop to define the 6 utility functions associated to each respondent, but seeing this topic, I wonder if it would be better to define all 1200 utility functions and use the availability specification in order to define the set of each individual.
Any suggestion on this issue ?

Thanks for your help

Lola
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Choice sets with large numbers of alternatives

Post by stephanehess »

Lola

computationally, it will be much easier to just calculate utilities for the available alternatives. So I would preprocess the data in such a way that each row just contains the attributes for those alternatives that you sample

I assume you are familiar with the work on sampling of alternatives, and that you potentially introduce bias by sampling?

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