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.

Splitting into subgroups and

Ask general questions about model specification and estimation that are not Apollo specific but relevant to Apollo users.
Post Reply
Anna321
Posts: 1
Joined: 16 Jun 2023, 12:48

Splitting into subgroups and

Post by Anna321 »

Hi,

I am analysing a discrete choice model for which one of the covariates is sex. See below.
I have started with running a simple MNL and explored interactions between the other covariates with sex in turn.
I have discovered that pretty much everything interacts with sex in the model!

Could you advise regarding the best way to proceed in terms of interaction term selection?

(I also tried to split the dataset into only males and only females but this did not converge?)

Any advice would be greatly appreciated.

Code utilised:
```{r setup, include=FALSE}
### Clear memory
rm(list = ls())
### Load Apollo library
library(tidyverse) # for data science
library(apollo)
### Initialise code: this ‘detaches’ variables5 and makes sure that output is directed to the console rather than a fil
apollo_initialise()
### Set core controls - see list of options page 22 of manual (name of the model (where any output files will use this name too), provide a brief description of the model (for use in the output) and indicate the name (in quotes) of the column in the data which contains the identifier variable for individual decision makers)
apollo_control = list(
modelName = "MNL_AAADCE_covariates_OpvsEVARvsnot",
modelDescr = "MNL model with socio-demographics on mode choice AAADCE data",
indivID = "ID", #Must name ID column, without this the code will not run
outputDirectory = "DCEMNL",
seed = 723
)
```
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
```{r}
### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)

database = DCEdata_3choices
### for data dictionary, use scenarioattributes file

#Remove all NA values for choice
database <- database %>%
filter(!is.na(database$choice))

str(database$choice)
```

MNL PLAIN
# ################################################################# #
#### MNL PLAIN ####
# ################################################################# #
MNL CODE
Model Parameters - define parameters, indicate any that are kept to be xied, for continuous mixture models define apollo_draws settings and creat appolo-randCoeff function, for latent class models define apollo_lcPars function
```{r}

### Vector of parameters, including
apollo_beta=c(asc_noRepair = 0,
asc_Open =0,
asc_EVAR =0,
#Coefficients open
beta_sex_female_open =0,
beta_age_open =0,
beta_anaes_mild_open =0,
beta_anaes_mod_open =0,
beta_anaes_severe_open =0,
beta_aaaSize_open =0,
beta_anatomy_neck_open =0,
beta_anatomy_access_open =0,
beta_anatomy_neckANDaccess_open =0,
beta_anxiety_yes_open =0,
#coefficients EVAR
beta_sex_female_evar =0,
beta_age_evar =0,
beta_anaes_mild_evar =0,
beta_anaes_mod_evar =0,
beta_anaes_severe_evar =0,
beta_aaaSize_evar =0,
beta_anatomy_neck_evar =0,
beta_anatomy_access_evar =0,
beta_anatomy_neckANDaccess_evar =0,
beta_anxiety_yes_evar =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("asc_noRepair")

```
Run apollo_validateInputs function
```{r}
# --------------------------------------------------------------------------------
### Step 4 - Validate data
apollo_inputs = apollo_validateInputs()

```
Model Definition - define apollo_probabilities function, create likelihood functions for individual mdoel components, combine into overal model likelihood if multiple components exist, depending on the model average over darws, latent classes and take procudes across choices, return output with one lieklihood value per individual in estimation
```{r}
### Step 5 - Define apollo probabilities

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

### Create a list of alternative specific constants and coefficients using interactions with sociodemographics.
###asc_Operation_value = asc_Operation + asc_Operation_shift_female*(surgeonGender==2) not working

### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['noRepair']] = 0
V[['Open']] = asc_Open + beta_sex_female_open * (sex == 1) + beta_age_open * age + beta_anaes_mild_open * (anaesRisk == 1) + beta_anaes_mod_open * (anaesRisk == 2) + beta_anaes_severe_open * (anaesRisk == 3) + beta_aaaSize_open * aaaSize + beta_anatomy_neck_open * (anatomy == 1) + beta_anatomy_access_open * (anatomy ==2) + beta_anatomy_neckANDaccess_open * (anatomy==3) + beta_anxiety_yes_open * (anxiety == 1)

V[['EVAR']] = asc_EVAR + beta_sex_female_evar * (sex == 1) + beta_age_evar * age + beta_anaes_mild_evar * (anaesRisk == 1) + beta_anaes_mod_evar * (anaesRisk == 2) + beta_anaes_severe_evar * (anaesRisk == 3) + beta_aaaSize_evar * aaaSize + beta_anatomy_neck_evar * (anatomy == 1) + beta_anatomy_access_evar * (anatomy ==2) + beta_anatomy_neckANDaccess_evar * (anatomy==3) + beta_anxiety_yes_evar * (anxiety == 1)

### Define settings for MNL model component
mnl_settings = list(
alternatives = c(noRepair = 0, Open = 2, EVAR = 1), #bear in mind this is from the Qualtrics output
avail = 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)

### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)

return(P)
}

```

Estimation and model output Run apollo_estimate function, run apollo_modelOutput for on screen output, run apollo_saveOutput for on output to file
```{r}
#Run model
model1 = apollo_estimate(apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs)

```
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Splitting into subgroups and

Post by stephanehess »

Hi

this seems a more general question rather than Apollo specific. What you could do is to perform statistical tests to see which parameters differ between men and women and keep the others generic. And you could compare that to two separate models. We can't really help in relation to you saying that the gender-specific model doesn't converge without seeing details of the output

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