Hi Apollo Team,
I'm encountering an issue where Apollo fails to recognize variables in the `database` object, despite them being present in the dataset. I consistently get the error:
`"object not found"` when attempting to reference variables inside the `apollo_probabilities` function.
Description of the Issue:
I am trying to run a multinomial logit model (MNL) for a race with multiple athletes. The dataset `database` contains variables such as `performance_last_race`, `athlete_id`, `won`, etc. While the dataset is successfully assigned to `database` and Apollo’s validation steps complete without issues, when I try to reference variables inside the `apollo_probabilities` function, I get the following error:
```r
Error in apollo_probabilities(apollo_beta_shifted, apollo_inputs) :
object 'performance_last_race' not found
```
I understand that Apollo automatically attaches the `database` object, so I’m not using the `database$` prefix, but the error persists.
Here is a simplified version of my dataset to reproduce the issue:
```r
database <- data.frame(
race_id = c(1, 1, 1, 2, 2, 2),
athlete_id = c(101, 102, 103, 201, 202, 203),
won = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE),
performance_last_race = c(60, 70, 50, 80, 90, 100),
speed_last_race = c(50, 60, 40, 70, 80, 90),
coach_avg_performance = c(45, 55, 35, 65, 75, 85),
is_female = c(1, 0, 1, 0, 1, 0)
)
```
Code Attempt:
Here is the code I’m using:
```r
# Load necessary libraries
library(apollo)
# Initialize Apollo
apollo_initialise()
# Define Apollo control settings
apollo_control <- list(
modelName = "AthleticsRaceMNL", # Name of the model
indivID = "race_id", # Individual ID, which is the race ID
panelData = TRUE # Set panelData to TRUE for in-race competition
)
# Define starting values for the model parameters
apollo_beta <- c(
b_performance_last_race = 0
)
# No fixed parameters
apollo_fixed <- c()
# Apollo input validation
apollo_inputs <- apollo_validateInputs()
# Define the utility functions
apollo_probabilities <- function(apollo_beta, apollo_inputs, functionality = "estimate") {
# Define the utility function using performance_last_race
V <- list()
V[['athlete']] <- apollo_beta['b_performance_last_race'] * performance_last_race
# Define the multinomial logit model (MNL) settings
mnl_settings <- list(
alternatives = athlete_id,
avail = 1,
choiceVar = won,
V = V
)
# Compute probabilities using the MNL model
P <- apollo_mnl(mnl_settings, functionality)
# Prepare probabilities for Apollo
P <- apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# Estimate the model
model <- apollo_estimate(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs
)
```
Error:
When I run the code, I get the following error:
```r
Error in apollo_probabilities(apollo_beta_shifted, apollo_inputs) :
object 'performance_last_race' not found
```
### Steps I Have Tried:
1. I ensured that the dataset is correctly assigned to `database`.
2. I confirmed that variables such as `performance_last_race` are accessible within the dataset.
3. I removed any `database$` prefixes as per Apollo's documentation, but the issue persists.
4. I tried attaching the dataset explicitly, but that didn’t solve the problem either.
Could someone help me understand why Apollo is not recognizing the variables in the `database` object within the `apollo_probabilities` function? Any guidance or suggestions would be much appreciated!
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.
Error in Apollo: “object not found” for variables in the database
-
stephanehess
- Site Admin
- Posts: 1358
- Joined: 24 Apr 2020, 16:29
Re: Error in Apollo: “object not found” for variables in the database
You're missing apollo_attach etc. Please have a look at the examples online
Re: Error in Apollo: “object not found” for variables in the database
Hi - I did look at the examples, have tried attach and detach and still throwing errors.
Before going any further, perhaps I should ask the more general question, is apollo suited to this problem?
As I understand it, apollo is designed to predict probabilities of an individual choosing from a fixed list of products (for example).
I was trying to use it to model which runners would be best suited to a race (so which runners would have highest probability of winning the race).
In this case the choice is between runners, each of whom have different attributes within the context of the race.
Can apollo be used for this type of problem? Is there any of the examples that corresponds to this?
Thanks in advance for any guidance.
Before going any further, perhaps I should ask the more general question, is apollo suited to this problem?
As I understand it, apollo is designed to predict probabilities of an individual choosing from a fixed list of products (for example).
I was trying to use it to model which runners would be best suited to a race (so which runners would have highest probability of winning the race).
In this case the choice is between runners, each of whom have different attributes within the context of the race.
Can apollo be used for this type of problem? Is there any of the examples that corresponds to this?
Thanks in advance for any guidance.
-
stephanehess
- Site Admin
- Posts: 1358
- Joined: 24 Apr 2020, 16:29
Re: Error in Apollo: “object not found” for variables in the database
Hi
I cannot see why it wouldn't work. Can you show us your code?
Stephane
I cannot see why it wouldn't work. Can you show us your code?
Stephane
Re: Error in Apollo: “object not found” for variables in the database
Hi Stephane,
The code is per the last example I posted.
So here is a simplified version of the data:
database <- data.frame(
race_id = c(1, 1, 1, 2, 2, 2),
athlete_id = c(101, 102, 103, 201, 202, 203),
won = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE),
performance_last_race = c(60, 70, 50, 80, 90, 100),
speed_last_race = c(50, 60, 40, 70, 80, 90),
coach_avg_performance = c(45, 55, 35, 65, 75, 85),
is_female = c(1, 0, 1, 0, 1, 0)
)
Therefore the races are different event, each with different athletes competing. In this simplified use case there are 2 races, each with 3 coompetitors, but in reality there could be anywhere from 3 to 20 athletes competing in a single race. They all have different ID numbers and are competing in different races. Each competitor has different attributes - in this case, performance last race which is percentage rivals beaten, a figure relating to speed, the coach performance, in mixed competitions the gender of the athlete etc etc. Again this is a toy exmaple just to try and get a prototype code working with apollo, in reality there are many more attributes.
The won vector shows us which athlete won, and we are trying to model and then predict on unseen races with unseen athletes the probability of winning.
Colin
The code is per the last example I posted.
So here is a simplified version of the data:
database <- data.frame(
race_id = c(1, 1, 1, 2, 2, 2),
athlete_id = c(101, 102, 103, 201, 202, 203),
won = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE),
performance_last_race = c(60, 70, 50, 80, 90, 100),
speed_last_race = c(50, 60, 40, 70, 80, 90),
coach_avg_performance = c(45, 55, 35, 65, 75, 85),
is_female = c(1, 0, 1, 0, 1, 0)
)
Therefore the races are different event, each with different athletes competing. In this simplified use case there are 2 races, each with 3 coompetitors, but in reality there could be anywhere from 3 to 20 athletes competing in a single race. They all have different ID numbers and are competing in different races. Each competitor has different attributes - in this case, performance last race which is percentage rivals beaten, a figure relating to speed, the coach performance, in mixed competitions the gender of the athlete etc etc. Again this is a toy exmaple just to try and get a prototype code working with apollo, in reality there are many more attributes.
The won vector shows us which athlete won, and we are trying to model and then predict on unseen races with unseen athletes the probability of winning.
Colin
-
stephanehess
- Site Admin
- Posts: 1358
- Joined: 24 Apr 2020, 16:29
Re: Error in Apollo: “object not found” for variables in the database
Hi
here are some of the issues:
1. your data is in the long format instead of wide format
2. you are missing apollo_attach
3. you are missing the definition of P as a list
4. your definition of the alternatives is not correct
5. you need to return the output of apollo_mnl into P[["model"]]
if you base your script on one of the examples online, these are all easy to fix
Hope this helps
Stephane
here are some of the issues:
1. your data is in the long format instead of wide format
2. you are missing apollo_attach
3. you are missing the definition of P as a list
4. your definition of the alternatives is not correct
5. you need to return the output of apollo_mnl into P[["model"]]
if you base your script on one of the examples online, these are all easy to fix
Hope this helps
Stephane