Page 1 of 1

Error in Apollo: “object not found” for variables in the database

Posted: 02 Oct 2024, 19:15
by colin77
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!

Re: Error in Apollo: “object not found” for variables in the database

Posted: 02 Oct 2024, 22:05
by stephanehess
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

Posted: 03 Oct 2024, 10:19
by colin77
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.

Re: Error in Apollo: “object not found” for variables in the database

Posted: 03 Oct 2024, 13:59
by stephanehess
Hi

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

Posted: 03 Oct 2024, 22:06
by colin77
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

Re: Error in Apollo: “object not found” for variables in the database

Posted: 04 Oct 2024, 21:59
by stephanehess
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