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.

Apollo modeloutput cannot present the result

Report bugs or highlight issues with Apollo functions. At a minimum, please include the part of the output where you believe the bug is manifested. Ideally, please share your model file.
Post Reply
Finley
Posts: 4
Joined: 26 Jul 2021, 07:21

Apollo modeloutput cannot present the result

Post by Finley »

Hi,

I would like to run a mixed logit model in a remote server, since the dataset is quite large. When I run this model with small test sample in my own PC (like 1% of the total sample), it works well. However, after updating to the latest version, if I run the mixed logit model in a remote server and then save the model as RData file for further analysis, an error will pop up in the 'Apollo_modelOutput' function, it writes:

Code: Select all

Model run by Finley using Apollo 0.2.8 on R 4.2.0 for Windows.
www.ApolloChoiceModelling.com

Model name                                  : Mixed logit model: Preference sapce
Model description                           : Mixed logit model to determine Adults' preferences for a vaccine product
Model run at                                : 2022-07-18 20:48:21
Estimation method                           : bfgs
Model diagnosis                             : successful convergence 
Number of individuals                       : 12000
Number of rows in database                  : 144000
Number of modelled outcomes                 : 144000

Number of cores used                        :  6 
Number of inter-individual draws            : 500 (mlhs)

LL(start)                                   : -158200.2
LL at equal shares, LL(0)                   : -158200.2
LL at observed shares, LL(C)                : -129597.2
LL(final)                                   : -101452
Error in round(model$rho2_0, 4) : 
  non-numeric argument to mathematical function
But the model downloaded from the server seems to have no problem. This is a screenshot from Rstudio view:
Image



It really confuses me since this way worked well before I update the apollo package. I am wondering if there is something wrong with the updating or the remote server.

The original model (run in the remote server) is present below:

Code: Select all

# use apollo package to analyze
# ####################################################### #
#### 1. Definition of core settings
# ####################################################### #
### Clear memory
rm(list = ls())

### Load library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control <- list(
  modelName = "Mixed logit model",
  modelDescr = "Mixed logit model to determine Adults' preferences for a vaccine product",
  indivID = "ID",
  mixing = TRUE, # mixed logit model for random coefficients, FALSE for MNL model
  nCores = 6
  #,weights = "weight"
)
# ####################################################### #
#### 2. Data loading                                  
# ####################################################### #
database <- read.csv(file = "Data/database.csv")
database <- database[1:1440, ]
database <- subset(database, select = c("ID","price.1","risk2.1","risk3.1","duration2.1",
                                     "duration3.1","efficacy2.1","efficacy3.1",
                                     "admin.1","doses2.1","doses3.1","origin.1",
                                     "price.2","risk2.2","risk3.2",
                                     "duration2.2","duration3.2","efficacy2.2",
                                     "efficacy3.2","admin.2","doses2.2",
                                     "doses3.2","origin.2","choice","weight"))

database <- as.data.frame(database)

# ####################################################### #
#### 3. Parameter definition                           
# ####################################################### #

### Vector of parameters, including any that are kept fixed
### during estimation

apollo_beta <- c(
  asc = 0,
  b_price_mu = 0,
  b_risk2_mu = 0,
  b_risk3_mu = 0,
  b_duration2_mu = 0,
  b_duration3_mu = 0,
  b_efficacy2_mu = 0,
  b_efficacy3_mu = 0,
  b_oral_mu = 0,
  b_dose2_mu = 0,
  b_dose3_mu = 0,
  b_imported_mu = 0,
  b_price_sigma = 0,
  b_risk2_sigma = 0,
  b_risk3_sigma = 0,
  b_duration2_sigma = 0,
  b_duration3_sigma = 0,
  b_efficacy2_sigma = 0,
  b_efficacy3_sigma = 0,
  b_oral_sigma = 0,
  b_dose2_sigma = 0,
  b_dose3_sigma = 0,
  b_imported_sigma = 0
)

### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_beta_fixed = c() if none
apollo_fixed <- c()

# ################################################################# #
#### 4. Define Random Components                                    
# ################################################################# #

### Set parameters for generating draws
apollo_draws <- list(
  interDrawsType = "mlhs",
  interNDraws = 500,
  interUnifDraws = c(),
  interNormDraws = paste0("draws_", c(
    "price", "risk2",
    "risk3", "duration2",
    "duration3", "efficacy2",
    "efficacy3", "oral",
    "dose2", "dose3",
    "imported"
  )),
  intraDrawsType = "",
  intraNDraws = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff <- function(apollo_beta, apollo_inputs) {
  randcoeff <- list()

  randcoeff[["b_price"]] <- b_price_mu + b_price_sigma * draws_price
  randcoeff[["b_risk2"]] <- b_risk2_mu + b_risk2_sigma * draws_risk2
  randcoeff[["b_risk3"]] <- b_risk3_mu + b_risk3_sigma * draws_risk3
  randcoeff[["b_duration2"]] <- b_duration2_mu + b_duration2_sigma * draws_duration2
  randcoeff[["b_duration3"]] <- b_duration3_mu + b_duration3_sigma * draws_duration3
  randcoeff[["b_efficacy2"]] <- b_efficacy2_mu + b_efficacy2_sigma * draws_efficacy2
  randcoeff[["b_efficacy3"]] <- b_efficacy3_mu + b_efficacy3_sigma * draws_efficacy3
  randcoeff[["b_oral"]] <- b_oral_mu + b_oral_sigma * draws_oral
  randcoeff[["b_dose2"]] <- b_dose2_mu + b_dose2_sigma * draws_dose2
  randcoeff[["b_dose3"]] <- b_dose3_mu + b_dose3_sigma * draws_dose3
  randcoeff[["b_imported"]] <- b_imported_mu + b_imported_sigma * draws_imported

  return(randcoeff)
}

# ####################################################### #
#### 5. Input validation                              
# ####################################################### #

apollo_inputs <- apollo_validateInputs()

# ####################################################### #
#### 6. Likelihood definition                          
# ####################################################### #

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()

  V <- list()

  ### List of utilities: these must use the same names as
  ### in mnl_settings, order is irrelevant.
  V[["alt1"]] <- asc + b_price * price.1 + b_risk2 * risk2.1 + b_risk3 * risk3.1 +
    b_duration2 * duration2.1 + b_duration3 * duration3.1 + b_efficacy2 * efficacy2.1 +
    b_efficacy3 * efficacy3.1 + b_oral * admin.1 + b_dose2 * doses2.1 +
    b_dose3 * doses3.1 + b_imported * origin.1
  V[["alt2"]] <- asc + b_price * price.2 + b_risk2 * risk2.2 + b_risk3 * risk3.2 +
    b_duration2 * duration2.2 + b_duration3 * duration3.2 + b_efficacy2 * efficacy2.2 +
    b_efficacy3 * efficacy3.2 + b_oral * admin.2 + b_dose2 * doses2.2 +
    b_dose3 * doses3.2 + b_imported * origin.2
  V[["alt3"]] <- 0

  ### Define settings for MNL model component
  mnl_settings <- list(
    alternatives = c(alt1 = 1, alt2 = 2, alt3 = 3),
    avail        = list(alt1 = 1, alt2 = 1, alt3 = 1),
    choiceVar    = choice,
    V            = 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)

  ### Average across inter-individual draws
  P = apollo_avgInterDraws(P, apollo_inputs, functionality)

  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  
  ### Using sampling weights in estimation and prediction
  # P = apollo_weighting(P, apollo_inputs, functionality)

  return(P)
}

# ####################################################### #
#### 7. Model estimation                
# ####################################################### #

fit <- apollo_estimate(
  apollo_beta, apollo_fixed,
  apollo_probabilities, apollo_inputs
)

save(fit, file = paste0(getwd(), "/Model/ML_preference_space_test.RData"))
R session info:

Code: Select all

> sessionInfo()
R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.utf8  LC_CTYPE=Chinese (Simplified)_China.utf8   
[3] LC_MONETARY=Chinese (Simplified)_China.utf8 LC_NUMERIC=C                               
[5] LC_TIME=Chinese (Simplified)_China.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] xfun_0.30           apollo_0.2.8        zoo_1.8-10          tidyselect_1.2.0   
 [5] lattice_0.20-45     generics_0.1.2      vctrs_0.4.1         htmltools_0.5.2    
 [9] yaml_2.3.5          MCMCpack_1.6-3      utf8_1.2.2          rlang_1.0.6        
[13] pillar_1.7.0        glue_1.6.2          DBI_1.1.2           matrixStats_0.62.0 
[17] lifecycle_1.0.3     stringr_1.4.0       MatrixModels_0.5-0  mvtnorm_1.1-3      
[21] coda_0.19-4         evaluate_0.15       knitr_1.38          miscTools_0.6-26   
[25] fastmap_1.1.0       SparseM_1.81        RSGHB_1.2.2         quantreg_5.88      
[29] parallel_4.2.0      fansi_1.0.3         Rcpp_1.0.8.3        tmvnsim_1.0-2      
[33] mcmc_0.9-7          maxLik_1.5-2        mnormt_2.0.2        digest_0.6.29      
[37] stringi_1.7.6       dplyr_1.0.8         numDeriv_2016.8-1.1 grid_4.2.0         
[41] cli_3.3.0           tools_4.2.0         sandwich_3.0-1      magrittr_2.0.3     
[45] tibble_3.1.6        pacman_0.5.1        crayon_1.5.1        pkgconfig_2.0.3    
[49] MASS_7.3-56         ellipsis_0.3.2      Matrix_1.4-1        randtoolbox_1.31.1 
[53] assertthat_0.2.1    rmarkdown_2.13      rstudioapi_0.13     R6_2.5.1           
[57] rngWELL_0.10-7      compiler_4.2.0     
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Apollo modeloutput cannot present the result

Post by stephanehess »

Hi

many apologies for the slow reply. To help me fix this, would you be able at all to share the rds file with me?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Finley
Posts: 4
Joined: 26 Jul 2021, 07:21

Re: Apollo modeloutput cannot present the result

Post by Finley »

Hi,

Thank you for your reply. I am surprised that we don't have s.e. or rob. s.e in the rds file. Previously, I only saved the output of `apollo_estimate` as an RDATA file and used it to gather results.

The RDS file shows in the following figure:
Image

I have sent the rds file via email ('contact@apollochoicemodelling.com').

I have returned to the previous apollo package (version 0.2.6) to analyse my data. I hope my problem can provide some insights for later updates.

Best regards,
Finley
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Apollo modeloutput cannot present the result

Post by stephanehess »

Hi

please send it to me at stephane.hess@gmail.com

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