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.

Order of parameters in apollo_beta (LC model)

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
paul
Posts: 2
Joined: 27 Mar 2024, 09:22

Order of parameters in apollo_beta (LC model)

Post by paul »

Dear Prof. Hess,
I am running a simple LC model (without covariates for now). All attributes are dummy coded.
I am confused because I have run the same model twice with a different order of attributes in the function appolo_beta. In one case attribute by attribute (as in the example)

Code: Select all

apollo_beta = c(                
                beta_totb_keine_a  = 0,
                beta_totb_keine_b  = 0,
                beta_totb_keine_c  = 0,
                beta_totb_hoheStubben_a  = 0,
                beta_totb_hoheStubben_b  = 0,
                beta_totb_hoheStubben_c  = 0,
                beta_totb_gruppen_a = 0,
                beta_totb_gruppen_b = 0,
                beta_totb_gruppen_c = 0,
                beta_totb_ueberall_a =0,
                beta_totb_ueberall_b =0,
                beta_totb_ueberall_c =0,
                beta_boden_gemulcht_a = 0,
                beta_boden_gemulcht_b = 0,
                beta_boden_gemulcht_c = 0,
                beta_boden_ueberall_a = 0,
                beta_boden_ueberall_b = 0,
                beta_boden_ueberall_c = 0,
                beta_boden_keine_a = 0,
                beta_boden_keine_b = 0,
                beta_boden_keine_c = 0,
                beta_zaun_Flaechig_a   = 0,
                beta_zaun_Flaechig_b   = 0,
                beta_zaun_Flaechig_c   = 0,
                beta_zaun_kleingatter_a   = 0,
                beta_zaun_kleingatter_b   = 0,
                beta_zaun_kleingatter_c   = 0,
                beta_zaun_Teilflaechig_a   = 0,
                beta_zaun_Teilflaechig_b   = 0,
                beta_zaun_Teilflaechig_c   = 0,
                beta_zaun_zaun_a   = 0,
                beta_zaun_zaun_b   = 0,
                beta_zaun_zaun_c   = 0,
                delta_a = 0,
                delta_b = 0,
                delta_c = 0)]
and in the other case first all attributes of the same class, then the second class and so on (easier to copy paste when I add a class).

Code: Select all

apollo_beta = c(                
  beta_totb_keine_a  = 0,
  beta_totb_hoheStubben_a  = 0,
  beta_totb_gruppen_a = 0,
  beta_totb_ueberall_a =0,
  beta_boden_gemulcht_a = 0,
  beta_boden_ueberall_a = 0,
  beta_boden_keine_a = 0,
  beta_zaun_Flaechig_a   = 0,
  beta_zaun_kleingatter_a   = 0,
  beta_zaun_Teilflaechig_a   = 0,
  beta_zaun_zaun_a   = 0,
  delta_a = 0,
  beta_totb_keine_b  = 0,
  beta_totb_hoheStubben_b  = 0,
  beta_totb_gruppen_b = 0,
  beta_totb_ueberall_b =0,
  beta_boden_gemulcht_b = 0,
  beta_boden_ueberall_b = 0,
  beta_boden_keine_b = 0,
  beta_zaun_Flaechig_b   = 0,
  beta_zaun_kleingatter_b   = 0,
  beta_zaun_Teilflaechig_b   = 0,
  beta_zaun_zaun_b   = 0,
  delta_b = 0,
  beta_totb_keine_c  = 0,
  beta_totb_hoheStubben_c  = 0,
  beta_totb_gruppen_c = 0,
  beta_totb_ueberall_c =0,
  beta_boden_gemulcht_c = 0,
  beta_boden_ueberall_c = 0,
  beta_boden_keine_c = 0,
  beta_zaun_Flaechig_c   = 0,
  beta_zaun_kleingatter_c   = 0,
  beta_zaun_Teilflaechig_c   = 0,
  beta_zaun_zaun_c   = 0,
  delta_c = 0)
With two classes, both types of coding work and give the same results. But with three classes, coding like in the example produces the warning “Estimation failed. No covariance matrix to compute.” Whereas the other type of coding provides results. Isn’t the order of the arguments within apollo_beta normally irrelevant?

Here is my entire Code:

Code: Select all

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)
library(readxl)
setwd("~/R/Default speicherort")
### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "LC_no_covariates",
  modelDescr      = "Simple LC model, using dummy coding, no covariates in class allocation model",
  indivID         = "id",
  nCores          = 7,
  outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

longZ1komplett <- read_excel("longZ1komplett.xlsx")


database = longZ1komplett

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(                
  beta_totb_keine_a  = 0,
  beta_totb_hoheStubben_a  = 0,
  beta_totb_gruppen_a = 0,
  beta_totb_ueberall_a =0,
  beta_boden_gemulcht_a = 0,
  beta_boden_ueberall_a = 0,
  beta_boden_keine_a = 0,
  beta_zaun_Flaechig_a   = 0,
  beta_zaun_kleingatter_a   = 0,
  beta_zaun_Teilflaechig_a   = 0,
  beta_zaun_zaun_a   = 0,
  delta_a = 0,
  beta_totb_keine_b  = 0,
  beta_totb_hoheStubben_b  = 0,
  beta_totb_gruppen_b = 0,
  beta_totb_ueberall_b =0,
  beta_boden_gemulcht_b = 0,
  beta_boden_ueberall_b = 0,
  beta_boden_keine_b = 0,
  beta_zaun_Flaechig_b   = 0,
  beta_zaun_kleingatter_b   = 0,
  beta_zaun_Teilflaechig_b   = 0,
  beta_zaun_zaun_b   = 0,
  delta_b = 0,
  beta_totb_keine_c  = 0,
  beta_totb_hoheStubben_c  = 0,
  beta_totb_gruppen_c = 0,
  beta_totb_ueberall_c =0,
  beta_boden_gemulcht_c = 0,
  beta_boden_ueberall_c = 0,
  beta_boden_keine_c = 0,
  beta_zaun_Flaechig_c   = 0,
  beta_zaun_kleingatter_c   = 0,
  beta_zaun_Teilflaechig_c   = 0,
  beta_zaun_zaun_c   = 0,
  delta_c = 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("delta_a", "delta_b", 
                 "beta_totb_keine_a","beta_boden_gemulcht_a","beta_zaun_Flaechig_a",
                 "beta_totb_keine_b","beta_boden_gemulcht_b","beta_zaun_Flaechig_b",
                 "beta_totb_keine_c","beta_boden_gemulcht_c","beta_zaun_Flaechig_c"
                 )

# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS                              ####
# ################################################################# #

apollo_lcPars=function(apollo_beta, apollo_inputs){
  lcpars = list()
  
  lcpars[["beta_totb_keine"]] = list(beta_totb_keine_a, beta_totb_keine_b, beta_totb_keine_c)
  lcpars[["beta_totb_hoheStubben"]] = list(beta_totb_hoheStubben_a, beta_totb_hoheStubben_b, beta_totb_hoheStubben_c)
  lcpars[["beta_totb_gruppen"]] = list(beta_totb_gruppen_a, beta_totb_gruppen_b, beta_totb_gruppen_c)
  lcpars[["beta_totb_ueberall"]] = list(beta_totb_ueberall_a, beta_totb_ueberall_b, beta_totb_ueberall_c)
  lcpars[["beta_boden_gemulcht"]] = list(beta_boden_gemulcht_a, beta_boden_gemulcht_b, beta_boden_gemulcht_c)
  lcpars[["beta_boden_ueberall"]] = list(beta_boden_ueberall_a, beta_boden_ueberall_b, beta_boden_ueberall_c)
  lcpars[["beta_boden_keine"]] = list(beta_boden_keine_a, beta_boden_keine_b, beta_boden_keine_c)
  lcpars[["beta_zaun_Flaechig"]] = list(beta_zaun_Flaechig_a, beta_zaun_Flaechig_b, beta_zaun_Flaechig_c)
  lcpars[["beta_zaun_kleingatter"]] = list(beta_zaun_kleingatter_a, beta_zaun_kleingatter_b, beta_zaun_kleingatter_c)
  lcpars[["beta_zaun_Teilflaechig"]] = list(beta_zaun_Teilflaechig_a, beta_zaun_Teilflaechig_b, beta_zaun_Teilflaechig_c)
  lcpars[["beta_zaun_zaun"]] = list(beta_zaun_zaun_a, beta_zaun_zaun_b, beta_zaun_zaun_c)
  
  
  V=list()
  V[["class_a"]] = delta_a
  V[["class_b"]] = delta_b
  V[["class_c"]] = delta_c
  
  classAlloc_settings = list(
    classes      = c(class_a=1, class_b=2, class_c=3), 
    utilities    = V
  )
  
  lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)
  
  return(lcpars)
}

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #

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()
  
  
  
  ### Define settings for MNL model component that are generic across classes
  mnl_settings = list(
    alternatives = c(alt1=1, alt2=2, alt3=3),
    avail        = list(alt1=1, alt2=1, alt3=1),
    choiceVar    = choice
  )
  
  ### Loop over classes
  for(s in 1:3){
    
    ### Compute class-specific utilities
    V = list()
    V[['alt1']] = 
      beta_totb_keine[[s]] * (c1_totb==1) + beta_totb_hoheStubben[[s]] * (c1_totb==2) + beta_totb_gruppen[[s]] * (c1_totb==3) + beta_totb_ueberall[[s]] * (c1_totb==4) +
      beta_boden_gemulcht[[s]]* (c1_boden==1) + beta_boden_keine[[s]] * (c1_boden==2) + beta_boden_ueberall[[s]] * (c1_boden==3) +
      beta_zaun_Flaechig[[s]] * (c1_zaun==1) + beta_zaun_kleingatter[[s]] * (c1_zaun==2) + beta_zaun_Teilflaechig[[s]] * (c1_zaun==3) + beta_zaun_zaun[[s]] * (c1_zaun==4)
    
    V[['alt2']] = 
      beta_totb_keine[[s]] * (c2_totb==1) + beta_totb_hoheStubben[[s]] * (c2_totb==2) + beta_totb_gruppen[[s]] * (c2_totb==3) + beta_totb_ueberall[[s]] * (c2_totb==4) +
      beta_boden_gemulcht[[s]]* (c2_boden==1) + beta_boden_keine[[s]] * (c2_boden==2) + beta_boden_ueberall[[s]] * (c2_boden==3) +
      beta_zaun_Flaechig[[s]] * (c2_zaun==1) + beta_zaun_kleingatter[[s]] * (c2_zaun==2) + beta_zaun_Teilflaechig[[s]] * (c2_zaun==3) + beta_zaun_zaun[[s]] * (c2_zaun==4)
    
    V[['alt3']] = 
      beta_totb_keine[[s]] * (c3_totb==1) + beta_totb_hoheStubben[[s]] * (c3_totb==2) + beta_totb_gruppen[[s]] * (c3_totb==3) + beta_totb_ueberall[[s]] * (c3_totb==4) +
      beta_boden_gemulcht[[s]]* (c3_boden==1) + beta_boden_keine[[s]] * (c3_boden==2) + beta_boden_ueberall[[s]] * (c3_boden==3) +
      beta_zaun_Flaechig[[s]] * (c3_zaun==1) + beta_zaun_kleingatter[[s]] * (c3_zaun==2) + beta_zaun_Teilflaechig[[s]] * (c3_zaun==3) + beta_zaun_zaun[[s]] * (c3_zaun==4)
    
    
    mnl_settings$utilities = V
    #mnl_settings$componentName = paste0("Class_",s)
    
    ### Compute within-class choice probabilities using MNL model
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
    
    ### Take product across observation for same individual
    P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)
  }
  
  ### Compute latent class model probabilities
  lc_settings   = list(inClassProb = P, classProb=pi_values)
  P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #

### Optional starting values search
# apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model, modelOutput_settings = list(printPVal=1))
Thank you for your help!
Paul
stephanehess
Site Admin
Posts: 1046
Joined: 24 Apr 2020, 16:29

Re: Order of parameters in apollo_beta (LC model)

Post by stephanehess »

Paul

the order does not matter as they are indexed by name. So there must be another issue.

Did you compare the starting LL?

As an aside, I strongly suggesting using different starting values in the different classes

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
paul
Posts: 2
Joined: 27 Mar 2024, 09:22

Re: Order of parameters in apollo_beta (LC model)

Post by paul »

Dear Prof. Hess,

the LL starting values are the same. I don't see any other difference either, but once the model converges and once it doesn't. I copied the entire code and only changed apollo_beta as specified, so there should be no difference in the code.

Here is the entire output of code one (apollo_beta sorted "attribute by attribute")

Code: Select all

Model name                                  : LC_no_covariates
Model description                           : Simple LC model, using dummy coding, no covariates in class allocation model
Model run at                                : 2024-05-02 09:45:11.437305
Estimation method                           : bgw
Model diagnosis                             : Singular convergence
Number of individuals                       : 2213
Number of rows in database                  : 26556
Number of modelled outcomes                 : 26556

Number of cores used                        :  7 
Model without mixing

LL(start)                                   : -29174.75
LL (whole model) at equal shares, LL(0)     : -29174.75
LL (whole model) at observed shares, LL(C)  : -29135.97
LL(final, whole model)                      : -29174.75
Rho-squared vs equal shares                  :  0 
Adj.Rho-squared vs equal shares              :  -9e-04 
Rho-squared vs observed shares               :  -0.0013 
Adj.Rho-squared vs observed shares           :  -0.002 
AIC                                         :  58399.5 
BIC                                         :  58604.17 

LL(0,Class_1)                    : -29174.75
LL(final,Class_1)                : -29174.75
LL(0,Class_2)                    : -29174.75
LL(final,Class_2)                : -29174.75
LL(0,Class_3)                    : -29174.75
LL(final,Class_3)                : -29174.75

Estimated parameters                        : 25
Time taken (hh:mm:ss)                       :  00:00:23.12 
     pre-estimation                         :  00:00:19.43 
     estimation                             :  00:00:3.31 
     post-estimation                        :  00:00:0.37 
Iterations                                  :  1 (Singular convergence)
and of code two (apollo_beta sorted "class by class").

Code: Select all

Model name                                  : LC_no_covariates
Model description                           : Simple LC model, using dummy coding, no covariates in class allocation model
Model run at                                : 2024-05-02 09:45:49.028038
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -69.075097
     reciprocal of condition number         : 0.0313654
Number of individuals                       : 2213
Number of rows in database                  : 26556
Number of modelled outcomes                 : 26556

Number of cores used                        :  7 
Model without mixing

LL(start)                                   : -29174.75
LL (whole model) at equal shares, LL(0)     : -29174.75
LL (whole model) at observed shares, LL(C)  : -29135.97
LL(final, whole model)                      : -23768.06
Rho-squared vs equal shares                  :  0.1853 
Adj.Rho-squared vs equal shares              :  0.1845 
Rho-squared vs observed shares               :  0.1842 
Adj.Rho-squared vs observed shares           :  0.1836 
AIC                                         :  47586.11 
BIC                                         :  47790.79 

LL(0,Class_1)                    : -29174.75
LL(final,Class_1)                : -36296.76
LL(0,Class_2)                    : -29174.75
LL(final,Class_2)                : -32857.33
LL(0,Class_3)                    : -29174.75
LL(final,Class_3)                : -32362.96

Estimated parameters                        : 25
Time taken (hh:mm:ss)                       :  00:00:43.53 
     pre-estimation                         :  00:00:17.95 
     estimation                             :  00:00:8.66 
          initial estimation                :  00:00:8.3 
          estimation after rescaling        :  00:00:0.37 
     post-estimation                        :  00:00:16.91 
Iterations                                  :  62  
     initial estimation                     :  61 
     estimation after rescaling             :  1 
I really have no clue what could be the reason for the difference... I would therefore be very grateful for any further hints as to possible causes.

Paul
stephanehess
Site Admin
Posts: 1046
Joined: 24 Apr 2020, 16:29

Re: Order of parameters in apollo_beta (LC model)

Post by stephanehess »

hi

your attribute by attribute model basically failed immediately, i.e. did not estimate.

I noticed you had an odd ] at the end of apollo_beta in one of your codes

If you want me to debug this, then please send me your data and code and I'll have a look as it shouldn't afffect things at all

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
stephanehess
Site Admin
Posts: 1046
Joined: 24 Apr 2020, 16:29

Re: Order of parameters in apollo_beta (LC model)

Post by stephanehess »

Essentially, you started the estimation at a degenerate solution, where two classes had exactly the same parameters, and you fixed the weight for the two classes to be the same. once you fix that issue, things work. The reason for the order of starting values messing things up in this case is that the initial gradients already run into trouble
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply