Error in Apollo: “object not found” for variables in the database
Posted: 02 Oct 2024, 19:15
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!
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!