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.

ANA_EM has no covariance matrix

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
kiki
Posts: 3
Joined: 25 Mar 2024, 19:11

ANA_EM has no covariance matrix

Post by kiki »

Hi,

I use the EM algorithm to model the LC_ANA model.

I try the following ways to adjust the model. All of them present the covariance matrix is not available. So maybe it is not the problem about the model.

1) Consider the parameters in class membership model according to attributes
2) Consider the parameters in class membership model according to class
3) Consider the parameters in class membership model according to attributes and class
4) Remove one ASC (i.e., J-1).
5) Not remove one ASC.
6) Remove one delta_d (constant in class membership)
7) Not remove delta_d

The codes and the results are as follows. Thank you for your check in advance!

Best regards,
kiki
-------------------------------------------------------------------------------------------------------------------------------------------------
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "EM_LC_with_covariates_ANA",
modelDescr = "LC model with class allocation model on Swiss route choice data, EM algorithm, ANA",
indivID = "ID",
noValidation = TRUE,
noDiagnostics = TRUE,
outputDirectory = "output"
)

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

### 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 = apollo_swissRouteChoiceData
### for data dictionary, use ?apollo_swissRouteChoiceData

### create weights column for use in EM algorithm
database$weights = 1

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


# Create the matrix
Q <- matrix(c(0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L), nrow = 2, byrow = TRUE)

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_1_a = 0,
asc_1_b = 0,
asc_1_c = 0,
asc_1_d = 0,
asc_2_a = 0,
asc_2_b = 0,
asc_2_c = 0,
asc_2_d = 0,
beta_tt_a = 0,
beta_tt_b = 0,
beta_tt_c = 0,
beta_tt_d = 0,
beta_tc_a = 0,
beta_tc_b = 0,
beta_tc_c = 0,
beta_tc_d = 0,
# beta_hw_a =-0.0396,
# beta_hw_b =-0.0479,
# beta_ch_a =-0.7624,
# beta_ch_b =-2.1725,
delta_a_k1 = 0, # 0.0329,
delta_b_k1 = 0, # 0.0329,
delta_c_k1 = 0, # 0.0329,
delta_d_k1 =0, # 0.0329,
gamma_commute_a_k1 = 0,
gamma_commute_b_k1 = 0,
gamma_commute_c_k1 = 0,
gamma_commute_d_k1 = 0,
gamma_car_av_a_k1 = 0,
gamma_car_av_b_k1 = 0,
gamma_car_av_c_k1 = 0,
gamma_car_av_d_k1 = 0,
delta_a_k2 = 0,
delta_b_k2 = 0,
delta_c_k2 = 0,
delta_d_k2 = 0,
gamma_commute_a_k2 = 0,
gamma_commute_b_k2 = 0,
gamma_commute_c_k2 = 0,
gamma_commute_d_k2 = 0,
gamma_car_av_a_k2 = 0,
gamma_car_av_b_k2 = 0,
gamma_car_av_c_k2 = 0,
gamma_car_av_d_k2 = 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_2_a", "asc_2_b","delta_b","gamma_commute_b","gamma_car_av_b")

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

apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["asc_1" ]] = list( asc_1_a, asc_1_b,asc_1_c,asc_1_d)
lcpars[["asc_2" ]] = list( asc_2_a, asc_2_b,asc_2_c,asc_2_d)
lcpars[["beta_tt"]] = list(beta_tt_a, beta_tt_b,beta_tt_c,beta_tt_d)
lcpars[["beta_tc"]] = list(beta_tc_a, beta_tc_b,beta_tc_c,beta_tc_d)
# lcpars[["beta_hw"]] = list(beta_hw_a, beta_hw_b)
# lcpars[["beta_ch"]] = list(beta_ch_a, beta_ch_b)

V=list()
# V[["class_a"]] = delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability
# V[["class_b"]] = delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability

# V[["class_a"]] = (exp((delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability)*Q[1,1])*exp((delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability)*Q[2,1]))/((exp(delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability)+1)*(exp(delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability)+1))
# V[["class_b"]] = (exp((delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability)*Q[1,2])*exp((delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability)*Q[2,2]))/((exp(delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability)+1)*(exp(delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability)+1))
# V[["class_c"]] =(exp((delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability)*Q[1,3])*exp((delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability)*Q[2,3]))/((exp(delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability)+1)*(exp(delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability)+1))
# V[["class_d"]] =(exp((delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability)*Q[1,4])*exp((delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability)*Q[2,4]))/((exp(delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability)+1)*(exp(delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability)+1))

V[["class_a"]] = (exp((delta_a_k1 + gamma_commute_a_k1*commute + gamma_car_av_a_k1*car_availability)*Q[1,1])*exp((delta_a_k2 + gamma_commute_a_k2*commute + gamma_car_av_a_k2*car_availability)*Q[2,1]))/((exp(delta_a_k1 + gamma_commute_a_k1*commute + gamma_car_av_a_k1*car_availability)+1)*(exp(delta_a_k1 + gamma_commute_a_k1*commute + gamma_car_av_a_k1*car_availability)+1))
V[["class_b"]] = (exp((delta_b_k1 + gamma_commute_b_k1*commute + gamma_car_av_b_k1*car_availability)*Q[1,2])*exp((delta_b_k2 + gamma_commute_b_k2*commute + gamma_car_av_b_k2*car_availability)*Q[2,2]))/((exp(delta_b_k1 + gamma_commute_b_k1*commute + gamma_car_av_b_k1*car_availability)+1)*(exp(delta_b_k1 + gamma_commute_b_k1*commute + gamma_car_av_b_k1*car_availability)+1))
V[["class_c"]] =(exp((delta_c_k1 + gamma_commute_c_k1*commute + gamma_car_av_c_k1*car_availability)*Q[1,3])*exp((delta_c_k2 + gamma_commute_c_k2*commute + gamma_car_av_c_k2*car_availability)*Q[2,3]))/((exp(delta_c_k1 + gamma_commute_c_k1*commute + gamma_car_av_c_k1*car_availability)+1)*(exp(delta_c_k1 + gamma_commute_c_k1*commute + gamma_car_av_c_k1*car_availability)+1))
V[["class_d"]] =(exp((delta_d_k1+gamma_commute_d_k1*commute + gamma_car_av_d_k1*car_availability)*Q[1,4])*exp((delta_d_k2+gamma_commute_d_k2*commute + gamma_car_av_d_k2*car_availability)*Q[2,4]))/((exp(delta_d_k1+gamma_commute_d_k1*commute + gamma_car_av_d_k1*car_availability)+1)*(exp(delta_d_k2 + gamma_commute_d_k2*commute + gamma_car_av_d_k2*car_availability)+1))


### Settings for class allocation models
classAlloc_settings = list(
classes = c(class_a=1, class_b=2,class_c=3,class_d=4),
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),
avail = list(alt1=1, alt2=1),
choiceVar = choice
)

### Loop over classes
for(s in 1:length(pi_values)){

### Compute class-specific utilities
V=list()
V[["alt1"]] = asc_1[[s]] + beta_tc[[s]]*tc1*Q[1,s] + beta_tt[[s]]*tt1*Q[2,s] #+ beta_hw[[s]]*hw1 + beta_ch[[s]]*ch1
V[["alt2"]] = asc_2[[s]] + beta_tc[[s]]*tc2*Q[1,s] + beta_tt[[s]]*tt2*Q[2,s] #+ beta_hw[[s]]*hw2 + beta_ch[[s]]*ch2

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

# ################################################################# #
#### EM ESTIMATION FOR COVARIANCE MATRIX ####
# ################################################################# #

model=apollo_lcEM(apollo_beta, apollo_fixed, apollo_probabilities,
apollo_inputs, lcEM_settings = list(EMmaxIterations=100))

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

apollo_modelOutput(model)

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)

-------------------------------------------------------------------------------------------------------------------------------------------------
Model name : EM_LC_with_covariates_ANA
Model description : LC model with class allocation model on Swiss route choice data, EM algorithm, ANA
Model run at : 2024-04-05 19:00:00
Estimation method : EM algorithm (bfgs) -> Maximum likelihood (bfgs)
Model diagnosis : successful convergence
Optimisation diagnosis : Saddle point found
hessian properties : Some eigenvalues are positive and others negative
maximum eigenvalue : 0.778227
reciprocal of condition number : not calculated (Hessian is not negative definite)
Number of individuals : 388
Number of rows in database : 3492
Number of modelled outcomes : 3492

Number of cores used : 1
Model without mixing

LL(start) : -2420.47
LL (whole model) at equal shares, LL(0) : -2420.47
LL (whole model) at observed shares, LL(C) : -2420.39
LL(final, whole model) : -2295.6
Rho-squared vs equal shares : 0.0516
Adj.Rho-squared vs equal shares : 0.0351
Rho-squared vs observed shares : 0.0516
Adj.Rho-squared vs observed shares : 0.0392
AIC : 4671.19
BIC : 4917.52

LL(0,Class_1) : -2420.47
LL(final,Class_1) : -2428.88
LL(0,Class_2) : -2420.47
LL(final,Class_2) : -2574.11
LL(0,Class_3) : -2420.47
LL(final,Class_3) : -2621.55
LL(0,Class_4) : -2420.47
LL(final,Class_4) : -3487.33

Estimated parameters : 40
Time taken (hh:mm:ss) : 00:01:58.77
pre-estimation : 00:00:9.59
estimation : 00:00:58.6
post-estimation : 00:00:50.57
Iterations : 3 (EM) & 82 (bfgs)

Unconstrained optimisation.

Estimates:
Estimate s.e.
asc_1_a 0.06289 NA
asc_1_b -0.29744 NA
asc_1_c 0.03818 NA
asc_1_d 0.09051 NA
asc_2_a -0.06289 NA
asc_2_b 0.29744 NA
asc_2_c -0.03818 NA
asc_2_d -0.09050 NA
beta_tt_a 0.00000 NA
beta_tt_b 0.00000 NA
beta_tt_c -0.06053 NA
beta_tt_d -0.16335 NA
beta_tc_a 0.00000 NA
beta_tc_b -0.02635 NA
beta_tc_c 0.00000 NA
beta_tc_d -0.83472 NA
delta_a_k1 -8.77122 NA
delta_b_k1 0.00000 NA
delta_c_k1 -1.48338 NA
delta_d_k1 7.75759 NA
gamma_commute_a_k1 12.69659 NA
gamma_commute_b_k1 0.00000 NA
gamma_commute_c_k1 2.86563 NA
gamma_commute_d_k1 15.99644 NA
gamma_car_av_a_k1 24.42493 NA
gamma_car_av_b_k1 0.00000 NA
gamma_car_av_c_k1 -1.65684 NA
gamma_car_av_d_k1 -12.35994 NA
delta_a_k2 0.00000 NA
delta_b_k2 0.00000 NA
delta_c_k2 -0.71514 NA
delta_d_k2 7.75758 NA
gamma_commute_a_k2 0.00000 NA
gamma_commute_b_k2 0.00000 NA
gamma_commute_c_k2 2.25553 NA
gamma_commute_d_k2 15.99646 NA
gamma_car_av_a_k2 0.00000 NA
gamma_car_av_b_k2 0.00000 NA
gamma_car_av_c_k2 -0.20253 NA
gamma_car_av_d_k2 -12.35996 NA
t.rat.(0) Rob.s.e.
asc_1_a NA NA
asc_1_b NA NA
asc_1_c NA NA
asc_1_d NA NA
asc_2_a NA NA
asc_2_b NA NA
asc_2_c NA NA
asc_2_d NA NA
beta_tt_a NA NA
beta_tt_b NA NA
beta_tt_c NA NA
beta_tt_d NA NA
beta_tc_a NA NA
beta_tc_b NA NA
beta_tc_c NA NA
beta_tc_d NA NA
delta_a_k1 NA NA
delta_b_k1 NA NA
delta_c_k1 NA NA
delta_d_k1 NA NA
gamma_commute_a_k1 NA NA
gamma_commute_b_k1 NA NA
gamma_commute_c_k1 NA NA
gamma_commute_d_k1 NA NA
gamma_car_av_a_k1 NA NA
gamma_car_av_b_k1 NA NA
gamma_car_av_c_k1 NA NA
gamma_car_av_d_k1 NA NA
delta_a_k2 NA NA
delta_b_k2 NA NA
delta_c_k2 NA NA
delta_d_k2 NA NA
gamma_commute_a_k2 NA NA
gamma_commute_b_k2 NA NA
gamma_commute_c_k2 NA NA
gamma_commute_d_k2 NA NA
gamma_car_av_a_k2 NA NA
gamma_car_av_b_k2 NA NA
gamma_car_av_c_k2 NA NA
gamma_car_av_d_k2 NA NA
Rob.t.rat.(0)
asc_1_a NA
asc_1_b NA
asc_1_c NA
asc_1_d NA
asc_2_a NA
asc_2_b NA
asc_2_c NA
asc_2_d NA
beta_tt_a NA
beta_tt_b NA
beta_tt_c NA
beta_tt_d NA
beta_tc_a NA
beta_tc_b NA
beta_tc_c NA
beta_tc_d NA
delta_a_k1 NA
delta_b_k1 NA
delta_c_k1 NA
delta_d_k1 NA
gamma_commute_a_k1 NA
gamma_commute_b_k1 NA
gamma_commute_c_k1 NA
gamma_commute_d_k1 NA
gamma_car_av_a_k1 NA
gamma_car_av_b_k1 NA
gamma_car_av_c_k1 NA
gamma_car_av_d_k1 NA
delta_a_k2 NA
delta_b_k2 NA
delta_c_k2 NA
delta_d_k2 NA
gamma_commute_a_k2 NA
gamma_commute_b_k2 NA
gamma_commute_c_k2 NA
gamma_commute_d_k2 NA
gamma_car_av_a_k2 NA
gamma_car_av_b_k2 NA
gamma_car_av_c_k2 NA
gamma_car_av_d_k2 NA


Summary of class allocation for model
component :
Mean prob.
Class_1 0.2474
Class_2 0.1970
Class_3 0.2369
Class_4 0.3186
stephanehess
Site Admin
Posts: 1054
Joined: 24 Apr 2020, 16:29

Re: ANA_EM has no covariance matrix

Post by stephanehess »

Hi

it's difficult to diagnose the issue here, but the large number of post-EM iterations is suspicious. Did you try estimation via apollo_estimate instead of EM first? That might better highlight any identification issues

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
kiki
Posts: 3
Joined: 25 Mar 2024, 19:11

Re: ANA_EM has no covariance matrix

Post by kiki »

Hi Stephane,

I try the apollo-estimate, it shows no covariates matrix, or s.e., t.rat., Rob.s.e. either. I attached the codes and results below.

Thank you in advance!

Best regards,
kiki


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "LC_with_covariates_ANA",
modelDescr = "LC model with class allocation model on Swiss route choice data, ANA",
indivID = "ID",
noValidation = TRUE,
noDiagnostics = TRUE,
outputDirectory = "output"
)

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

### 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 = apollo_swissRouteChoiceData
### for data dictionary, use ?apollo_swissRouteChoiceData

### create weights column for use in EM algorithm
database$weights = 1

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


# Create the matrix
Q <- matrix(c(0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L), nrow = 2, byrow = TRUE)

# Assign row and column names
#rownames(Q) <- c("Att.1", "Att.2")
#colnames(Q) <- paste0("Class", 1:4)

# Print the matrix
#print(Q)


### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_1_a = 0,
asc_1_b = 0,
asc_1_c = 0,
asc_1_d = 0,
asc_2_a = 0,
asc_2_b = 0,
asc_2_c = 0,
asc_2_d = 0,
beta_tt_a = 0,
beta_tt_b = 0,
beta_tt_c = 0,
beta_tt_d = 0,
beta_tc_a = 0,
beta_tc_b = 0,
beta_tc_c = 0,
beta_tc_d = 0,
# beta_hw_a =-0.0396,
# beta_hw_b =-0.0479,
# beta_ch_a =-0.7624,
# beta_ch_b =-2.1725,
delta_a_k1 = 0, # 0.0329,
delta_b_k1 = 0, # 0.0329,
delta_c_k1 = 0, # 0.0329,
delta_d_k1 =0, # 0.0329,
gamma_commute_a_k1 = 0,
gamma_commute_b_k1 = 0,
gamma_commute_c_k1 = 0,
gamma_commute_d_k1 = 0,
gamma_car_av_a_k1 = 0,
gamma_car_av_b_k1 = 0,
gamma_car_av_c_k1 = 0,
gamma_car_av_d_k1 = 0,
delta_a_k2 = 0,
delta_b_k2 = 0,
delta_c_k2 = 0,
delta_d_k2 = 0,
gamma_commute_a_k2 = 0,
gamma_commute_b_k2 = 0,
gamma_commute_c_k2 = 0,
gamma_commute_d_k2 = 0,
gamma_car_av_a_k2 = 0,
gamma_car_av_b_k2 = 0,
gamma_car_av_c_k2 = 0,
gamma_car_av_d_k2 = 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_2_a", "asc_2_b","delta_b","gamma_commute_b","gamma_car_av_b")

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

apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["asc_1" ]] = list( asc_1_a, asc_1_b,asc_1_c,asc_1_d)
lcpars[["asc_2" ]] = list( asc_2_a, asc_2_b,asc_2_c,asc_2_d)
lcpars[["beta_tt"]] = list(beta_tt_a, beta_tt_b,beta_tt_c,beta_tt_d)
lcpars[["beta_tc"]] = list(beta_tc_a, beta_tc_b,beta_tc_c,beta_tc_d)
# lcpars[["beta_hw"]] = list(beta_hw_a, beta_hw_b)
# lcpars[["beta_ch"]] = list(beta_ch_a, beta_ch_b)

V=list()

V[["class_a"]] = (exp((delta_a_k1 + gamma_commute_a_k1*commute + gamma_car_av_a_k1*car_availability)*Q[1,1])*exp((delta_a_k2 + gamma_commute_a_k2*commute + gamma_car_av_a_k2*car_availability)*Q[2,1]))/((exp(delta_a_k1 + gamma_commute_a_k1*commute + gamma_car_av_a_k1*car_availability)+1)*(exp(delta_a_k1 + gamma_commute_a_k1*commute + gamma_car_av_a_k1*car_availability)+1))
V[["class_b"]] = (exp((delta_b_k1 + gamma_commute_b_k1*commute + gamma_car_av_b_k1*car_availability)*Q[1,2])*exp((delta_b_k2 + gamma_commute_b_k2*commute + gamma_car_av_b_k2*car_availability)*Q[2,2]))/((exp(delta_b_k1 + gamma_commute_b_k1*commute + gamma_car_av_b_k1*car_availability)+1)*(exp(delta_b_k1 + gamma_commute_b_k1*commute + gamma_car_av_b_k1*car_availability)+1))
V[["class_c"]] =(exp((delta_c_k1 + gamma_commute_c_k1*commute + gamma_car_av_c_k1*car_availability)*Q[1,3])*exp((delta_c_k2 + gamma_commute_c_k2*commute + gamma_car_av_c_k2*car_availability)*Q[2,3]))/((exp(delta_c_k1 + gamma_commute_c_k1*commute + gamma_car_av_c_k1*car_availability)+1)*(exp(delta_c_k1 + gamma_commute_c_k1*commute + gamma_car_av_c_k1*car_availability)+1))
V[["class_d"]] =(exp((delta_d_k1+gamma_commute_d_k1*commute + gamma_car_av_d_k1*car_availability)*Q[1,4])*exp((delta_d_k2+gamma_commute_d_k2*commute + gamma_car_av_d_k2*car_availability)*Q[2,4]))/((exp(delta_d_k1+gamma_commute_d_k1*commute + gamma_car_av_d_k1*car_availability)+1)*(exp(delta_d_k2 + gamma_commute_d_k2*commute + gamma_car_av_d_k2*car_availability)+1))

# Calculate utility function for each latent class (Both are ok)
# V <- list()
#for (s in 1:S) {
# V[[paste0("class_", s)]] <- (exp(delta_k1 * Q[1, s]) * exp(delta_k2 * Q[2, s])) / ((exp(delta_k1) + 1) * (exp(delta_k2) + 1))
# }

### Settings for class allocation models
classAlloc_settings = list(
classes = c(class_a=1, class_b=2,class_c=3,class_d=4),
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),
avail = list(alt1=1, alt2=1),
choiceVar = choice
)

### Loop over classes
for(s in 1:length(pi_values)){

### Compute class-specific utilities
V=list()
V[["alt1"]] = asc_1[[s]] + beta_tc[[s]]*tc1*Q[1,s] + beta_tt[[s]]*tt1*Q[2,s] #+ beta_hw[[s]]*hw1 + beta_ch[[s]]*ch1
V[["alt2"]] = asc_2[[s]] + beta_tc[[s]]*tc2*Q[1,s] + beta_tt[[s]]*tt2*Q[2,s] #+ beta_hw[[s]]*hw2 + beta_ch[[s]]*ch2
# V[["alt2"]] = beta_tc[[s]]*tc2*Q[1,s] + beta_tt[[s]]*tt2*Q[2,s] #+ beta_hw[[s]]*hw2 + beta_ch[[s]]*ch2

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

# ################################################################# #
#### EM ESTIMATION FOR COVARIANCE MATRIX ####
# ################################################################# #

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

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

apollo_modelOutput(model)

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Preparing user-defined functions.

Testing likelihood function...
Apollo found a model component of type
classAlloc without a componentName. The name
was set to "classAlloc" by default.
INFORMATION: Setting "avail" is missing, so full
availability is assumed.
The class allocation probabilities for model
component "model" are calculated at the
observation level in 'apollo_lcPars', but
are used in 'apollo_probabilities' to
multiply within class probabilities that are
at the individual level. Apollo will average
the class allocation probabilities across
observations for the same individual level
before using them to multiply the
within-class probabilities. If your class
allocation probabilities are constant across
choice situations for the same individual,
then this is of no concern. If your class
allocation probabilities however vary across
choice tasks, then you should change your
model specification in
'apollo_probabilities' to only call
'apollo_panelProd' after calling
'apollo_lc'.

Pre-processing likelihood function...
Analytical gradient is different to numerical
one. Numerical gradients will be used.
Analytical gradients could not be calculated
for all components, numerical gradients will
be used.

Starting main estimation

BGW is using FD derivatives for model Jacobian. (Caller did not provide derivatives.)


Iterates will be written to:
output/LC_with_covariates_ANA_iterations.csv it nf F RELDF PRELDF RELDX MODEL stppar
0 1 2.420469955e+03
1 4 2.346118850e+03 3.072e-02 1.844e-02 1.00e+00 G -5.32e-07
2 6 2.334602217e+03 4.909e-03 4.901e-03 1.07e-01 G 7.85e-01
3 7 2.330034764e+03 1.956e-03 2.683e-03 7.38e-02 G 5.19e-01
4 8 2.324259272e+03 2.479e-03 2.166e-03 6.15e-02 S 2.52e-01
5 10 2.321930037e+03 1.002e-03 1.090e-03 5.53e-02 S 2.92e-01
6 12 2.319822805e+03 9.075e-04 7.206e-04 7.08e-02 S-G 3.18e-02
7 13 2.318888158e+03 4.029e-04 6.192e-04 8.49e-02 G 6.06e-02
8 14 2.316729125e+03 9.311e-04 5.890e-04 7.56e-02 G 3.34e-02
9 16 2.314123589e+03 1.125e-03 7.492e-04 9.30e-02 G 2.68e-02
10 18 2.312481974e+03 7.094e-04 5.280e-04 5.17e-02 G 1.83e-02
11 19 2.310220849e+03 9.778e-04 1.803e-03 8.12e-02 G 2.49e-02
12 22 2.308417031e+03 7.808e-04 1.647e-03 1.97e-02 G 4.91e-01
13 24 2.308275896e+03 6.114e-05 7.454e-05 3.34e-03 S 4.07e+00
14 27 2.307604555e+03 2.908e-04 6.843e-04 5.15e-02 S 2.58e-01
15 28 2.306271066e+03 5.779e-04 7.182e-04 4.34e-02 G 8.50e-02
16 29 2.305484215e+03 3.412e-04 2.679e-04 4.62e-02 G 3.74e-02
17 31 2.304247671e+03 5.363e-04 4.498e-04 8.96e-02 G-S -3.98e-03
18 33 2.304022942e+03 9.753e-05 2.506e-04 4.23e-02 S 1.16e-02
19 36 2.302035949e+03 8.624e-04 5.247e-04 1.73e-02 S 1.69e-02
20 37 2.299665780e+03 1.030e-03 1.635e-03 3.86e-02 S 9.95e-02
21 39 2.298627982e+03 4.513e-04 5.925e-04 1.63e-02 S 3.64e-01
22 41 2.298572941e+03 2.395e-05 2.069e-05 1.83e-03 S 1.44e+00
23 42 2.298503318e+03 3.029e-05 2.705e-05 4.29e-03 G 3.97e-01
24 43 2.298416219e+03 3.789e-05 3.708e-05 1.19e-02 S 7.77e-02
25 45 2.298390780e+03 1.107e-05 1.196e-05 7.01e-03 S 6.61e-02
26 48 2.298379028e+03 5.113e-06 5.491e-06 6.73e-03 S 1.67e-02
27 49 2.298374439e+03 1.996e-06 2.310e-06 7.83e-03 G 1.27e-02
28 50 2.298369693e+03 2.065e-06 2.918e-06 1.46e-02 G 5.10e-03
29 51 2.298364806e+03 2.126e-06 1.809e-06 1.52e-02 G 3.18e-03
30 53 2.298347513e+03 7.524e-06 5.159e-06 2.84e-02 G 2.25e-03
31 57 2.298323240e+03 1.056e-05 2.610e-06 2.76e-03 G-S 8.25e-02
32 59 2.298282568e+03 1.770e-05 1.036e-05 7.46e-04 G 1.30e+01
33 60 2.298087332e+03 8.495e-05 5.762e-05 2.39e-03 G 3.00e+00
34 62 2.298029341e+03 2.523e-05 2.535e-05 6.80e-03 G 2.44e-02
35 63 2.298026665e+03 1.164e-06 4.288e-06 6.81e-03 G 2.29e-02
36 65 2.298018547e+03 3.533e-06 8.839e-05 1.07e-03 G 1.83e+01
37 66 2.298014686e+03 1.680e-06 1.601e-06 2.31e-03 S 8.33e-02
38 74 2.298014547e+03 6.051e-08 5.929e-08 7.16e-05 S 3.02e+00
39 76 2.298014502e+03 1.976e-08 1.976e-08 4.69e-05 S 2.05e+00
40 77 2.298014429e+03 3.154e-08 3.154e-08 8.05e-05 S 7.79e-01
41 80 2.298014369e+03 2.633e-08 2.632e-08 9.01e-05 S 6.41e-01
42 82 2.298014317e+03 2.240e-08 2.238e-08 9.21e-05 S 6.36e-01
43 86 2.298014305e+03 5.342e-09 5.334e-09 2.26e-05 S 2.57e+00
44 87 2.298014281e+03 1.045e-08 1.044e-08 4.52e-05 S 1.28e+00
45 92 2.298014275e+03 2.557e-09 2.555e-09 1.09e-05 S 5.31e+00
46 95 2.298014272e+03 1.287e-09 1.287e-09 5.33e-06 S 1.08e+01
47 96 2.298014266e+03 2.710e-09 2.710e-09 9.26e-06 S 5.64e+00
48 97 2.298014239e+03 1.187e-08 1.186e-08 3.51e-05 S 1.41e+00
49 100 2.298014212e+03 1.181e-08 1.181e-08 3.09e-05 S 1.56e+00
50 101 2.298014157e+03 2.373e-08 2.373e-08 6.12e-05 S 7.93e-01
51 104 2.298014102e+03 2.413e-08 2.413e-08 6.16e-05 S 7.97e-01
52 108 2.298014088e+03 6.013e-09 6.013e-09 1.55e-05 S 3.19e+00
53 109 2.298014061e+03 1.173e-08 1.173e-08 3.11e-05 S 1.56e+00
54 113 2.298014047e+03 5.867e-09 5.867e-09 1.82e-05 S 2.52e+00
55 114 2.298014021e+03 1.134e-08 1.134e-08 3.83e-05 S 1.22e+00
56 115 2.298013917e+03 4.534e-08 4.535e-08 2.21e-04 S 2.27e-01
57 120 2.298013893e+03 1.060e-08 1.059e-08 5.74e-05 S 8.56e-01
58 121 2.298013845e+03 2.087e-08 2.086e-08 1.15e-04 S 4.22e-01
59 126 2.298013833e+03 5.221e-09 5.219e-09 3.03e-05 S 1.39e+00
60 128 2.298013821e+03 4.929e-09 4.928e-09 2.86e-05 S 1.32e+00
61 129 2.298013801e+03 8.898e-09 8.899e-09 5.13e-05 S 5.94e-01
62 133 2.298013791e+03 4.449e-09 4.448e-09 2.55e-05 S 1.12e+00
63 134 2.298013228e+03 2.449e-07 2.448e-07 1.41e-04 S 2.03e-02
64 135 2.298011058e+03 9.444e-07 9.426e-07 5.62e-04 S 4.91e-03
65 136 2.298002898e+03 3.551e-06 3.527e-06 2.17e-03 S 1.23e-03
66 137 2.297967306e+03 1.549e-05 1.458e-05 1.68e-02 S 8.50e-05
67 142 2.297967105e+03 8.718e-08 7.500e-08 6.31e-06 S 3.36e+00
68 143 2.297966675e+03 1.872e-07 1.862e-07 8.04e-06 G 2.01e+00
69 144 2.297962808e+03 1.683e-06 1.740e-06 1.03e-04 G 1.27e-01
70 145 2.297956773e+03 2.626e-06 2.686e-06 2.43e-04 G 4.54e-02
71 146 2.297952333e+03 1.932e-06 1.831e-06 2.57e-04 G 3.74e-02
72 147 2.297235166e+03 3.121e-04 4.576e-06 5.84e-04 S 2.30e-02
73 149 2.296101340e+03 4.936e-04 4.892e-04 3.34e-04 S 2.14e+00
74 150 2.295729929e+03 1.618e-04 1.830e-04 4.82e-04 S 4.01e-01
75 152 2.295707257e+03 9.876e-06 1.023e-05 7.17e-05 S 2.54e+00
76 153 2.295692018e+03 6.638e-06 6.929e-06 7.62e-05 S 1.86e+00
77 154 2.295675847e+03 7.044e-06 6.819e-06 1.92e-04 G 2.97e-01
78 155 2.295657090e+03 8.171e-06 7.678e-06 3.60e-04 G 1.01e-01
79 157 2.295648651e+03 3.676e-06 3.094e-06 1.47e-04 G 2.30e-01
80 158 2.295631713e+03 7.378e-06 7.189e-06 2.67e-04 S 1.54e-01
81 159 2.295605839e+03 1.127e-05 1.707e-05 1.23e-03 S 1.30e-02
82 160 2.295596771e+03 3.950e-06 3.289e-06 9.22e-04 S -1.22e-05
83 162 2.295596110e+03 2.879e-07 2.551e-07 1.03e-04 G 3.95e-04
84 163 2.295596002e+03 4.680e-08 3.009e-08 1.82e-04 G 3.95e-04
85 164 2.295595976e+03 1.166e-08 2.110e-08 3.27e-04 G 9.16e-05
86 165 2.295595953e+03 9.998e-09 7.674e-06 3.05e-04 G 3.24e-01
87 167 2.295595815e+03 5.985e-08 5.182e-08 1.31e-05 G 1.45e-02
88 168 2.295595769e+03 2.018e-08 1.162e-08 1.04e-05 G 1.41e-02
89 171 2.295595749e+03 8.830e-09 6.776e-09 2.64e-06 G 1.40e+00
90 172 2.295595714e+03 1.526e-08 1.564e-08 5.36e-06 S 1.23e+00
91 173 2.295595628e+03 3.752e-08 4.032e-08 2.26e-05 S 1.34e-01
92 174 2.295595586e+03 1.815e-08 1.730e-08 2.23e-05 S 5.25e-02
93 176 2.295595578e+03 3.499e-09 3.341e-09 4.70e-06 S 2.39e-01
94 177 2.295595567e+03 4.639e-09 5.737e-09 8.90e-06 S 1.19e-01
95 178 2.295595558e+03 4.231e-09 4.223e-09 8.55e-06 S 7.37e-02
96 179 2.295595539e+03 8.246e-09 8.027e-09 3.49e-05 S 1.00e-02
97 180 2.295595527e+03 5.131e-09 6.117e-09 6.73e-05 S 7.14e-05
98 181 2.295595525e+03 6.785e-10 7.069e-10 5.41e-05 S 4.07e-06
99 182 2.295595525e+03 7.714e-11 8.701e-11 5.08e-05 S 5.56e-08
100 185 2.295595525e+03 2.423e-12 2.901e-11 3.48e-05 S 2.74e-05
101 186 2.295595525e+03 2.177e-12 7.042e-12 1.57e-05 S -1.23e-07

***** Singular convergence *****

Estimated parameters:
Estimate
asc_1_a 0.08380
asc_1_b -0.28648
asc_1_c 0.03440
asc_1_d 0.08298
asc_2_a -0.04181
asc_2_b 0.30840
asc_2_c -0.04191
asc_2_d -0.09828
beta_tt_a 0.00000
beta_tt_b 0.00000
beta_tt_c -0.06054
beta_tt_d -0.16334
beta_tc_a 0.00000
beta_tc_b -0.02636
beta_tc_c 0.00000
beta_tc_d -0.83432
delta_a_k1 -30.50983
delta_b_k1 0.00000
delta_c_k1 -0.55380
delta_d_k1 43.22028
gamma_commute_a_k1 312.92480
gamma_commute_b_k1 0.00000
gamma_commute_c_k1 -284.90097
gamma_commute_d_k1 352.42953
gamma_car_av_a_k1 204.43461
gamma_car_av_b_k1 0.00000
gamma_car_av_c_k1 1.59284
gamma_car_av_d_k1 -127.17496
delta_a_k2 0.00000
delta_b_k2 0.00000
delta_c_k2 -0.21800
delta_d_k2 27.93247
gamma_commute_a_k2 0.00000
gamma_commute_b_k2 0.00000
gamma_commute_c_k2 -1.47317
gamma_commute_d_k2 136.35338
gamma_car_av_a_k2 0.00000
gamma_car_av_b_k2 0.00000
gamma_car_av_c_k2 1.89988
gamma_car_av_d_k2 -28.19648

Final LL: -2295.5955

WARNING: Estimation failed. No covariance matrix to
compute.

Current process will resume in 3 seconds
unless interrupted by the user...


Summary of class allocation for model
component :
Mean prob.
Class_1 0.2474
Class_2 0.1970
Class_3 0.2368
Class_4 0.3188

Calculating log-likelihood at equal shares
(LL(0)) for applicable models...
Calculating log-likelihood at observed shares
from estimation data (LL(c)) for applicable
models...
Calculating LL of each model component...
Calculating other model fit measures

Your model was estimated using the BGW
algorithm. Please acknowledge this by citing
Bunch et al. (1993) - DOI
10.1145/151271.151279
>
> # ----------------------------------------------------------------- #
> #---- FORMATTED OUTPUT (TO SCREEN) ----
> # ----------------------------------------------------------------- #
>
> apollo_modelOutput(model)
Model run by romeechow using Apollo 0.3.1 on R 4.2.3 for Darwin.
Please acknowledge the use of Apollo by citing Hess & Palma (2019)
DOI 10.1016/j.jocm.2019.100170
www.ApolloChoiceModelling.com

Model name : LC_with_covariates_ANA
Model description : LC model with class allocation model on Swiss route choice data, ANA
Model run at : 2024-05-14 12:59:52
Estimation method : bgw
Model diagnosis : Singular convergence
Number of individuals : 388
Number of rows in database : 3492
Number of modelled outcomes : 3492

Number of cores used : 1
Model without mixing

LL(start) : -2420.47
LL (whole model) at equal shares, LL(0) : -2420.47
LL (whole model) at observed shares, LL(C) : -2420.39
LL(final, whole model) : -2295.6
Rho-squared vs equal shares : 0.0516
Adj.Rho-squared vs equal shares : 0.0351
Rho-squared vs observed shares : 0.0516
Adj.Rho-squared vs observed shares : 0.0392
AIC : 4671.19
BIC : 4917.52

LL(0,Class_1) : -2420.47
LL(final,Class_1) : -2428.86
LL(0,Class_2) : -2420.47
LL(final,Class_2) : -2574.12
LL(0,Class_3) : -2420.47
LL(final,Class_3) : -2621.63
LL(0,Class_4) : -2420.47
LL(final,Class_4) : -3486.17

Estimated parameters : 40
Time taken (hh:mm:ss) : 00:00:42.26
pre-estimation : 00:00:4.85
estimation : 00:00:37.35
post-estimation : 00:00:0.06
Iterations : 101 (Singular convergence)

Unconstrained optimisation.

Estimates:
Estimate s.e.
asc_1_a 0.08380 NA
asc_1_b -0.28648 NA
asc_1_c 0.03440 NA
asc_1_d 0.08298 NA
asc_2_a -0.04181 NA
asc_2_b 0.30840 NA
asc_2_c -0.04191 NA
asc_2_d -0.09828 NA
beta_tt_a 0.00000 NA
beta_tt_b 0.00000 NA
beta_tt_c -0.06054 NA
beta_tt_d -0.16334 NA
beta_tc_a 0.00000 NA
beta_tc_b -0.02636 NA
beta_tc_c 0.00000 NA
beta_tc_d -0.83432 NA
delta_a_k1 -30.50983 NA
delta_b_k1 0.00000 NA
delta_c_k1 -0.55380 NA
delta_d_k1 43.22028 NA
gamma_commute_a_k1 312.92480 NA
gamma_commute_b_k1 0.00000 NA
gamma_commute_c_k1 -284.90097 NA
gamma_commute_d_k1 352.42953 NA
gamma_car_av_a_k1 204.43461 NA
gamma_car_av_b_k1 0.00000 NA
gamma_car_av_c_k1 1.59284 NA
gamma_car_av_d_k1 -127.17496 NA
delta_a_k2 0.00000 NA
delta_b_k2 0.00000 NA
delta_c_k2 -0.21800 NA
delta_d_k2 27.93247 NA
gamma_commute_a_k2 0.00000 NA
gamma_commute_b_k2 0.00000 NA
gamma_commute_c_k2 -1.47317 NA
gamma_commute_d_k2 136.35338 NA
gamma_car_av_a_k2 0.00000 NA
gamma_car_av_b_k2 0.00000 NA
gamma_car_av_c_k2 1.89988 NA
gamma_car_av_d_k2 -28.19648 NA
t.rat.(0) Rob.s.e.
asc_1_a NA NA
asc_1_b NA NA
asc_1_c NA NA
asc_1_d NA NA
asc_2_a NA NA
asc_2_b NA NA
asc_2_c NA NA
asc_2_d NA NA
beta_tt_a NA NA
beta_tt_b NA NA
beta_tt_c NA NA
beta_tt_d NA NA
beta_tc_a NA NA
beta_tc_b NA NA
beta_tc_c NA NA
beta_tc_d NA NA
delta_a_k1 NA NA
delta_b_k1 NA NA
delta_c_k1 NA NA
delta_d_k1 NA NA
gamma_commute_a_k1 NA NA
gamma_commute_b_k1 NA NA
gamma_commute_c_k1 NA NA
gamma_commute_d_k1 NA NA
gamma_car_av_a_k1 NA NA
gamma_car_av_b_k1 NA NA
gamma_car_av_c_k1 NA NA
gamma_car_av_d_k1 NA NA
delta_a_k2 NA NA
delta_b_k2 NA NA
delta_c_k2 NA NA
delta_d_k2 NA NA
gamma_commute_a_k2 NA NA
gamma_commute_b_k2 NA NA
gamma_commute_c_k2 NA NA
gamma_commute_d_k2 NA NA
gamma_car_av_a_k2 NA NA
gamma_car_av_b_k2 NA NA
gamma_car_av_c_k2 NA NA
gamma_car_av_d_k2 NA NA
Rob.t.rat.(0)
asc_1_a NA
asc_1_b NA
asc_1_c NA
asc_1_d NA
asc_2_a NA
asc_2_b NA
asc_2_c NA
asc_2_d NA
beta_tt_a NA
beta_tt_b NA
beta_tt_c NA
beta_tt_d NA
beta_tc_a NA
beta_tc_b NA
beta_tc_c NA
beta_tc_d NA
delta_a_k1 NA
delta_b_k1 NA
delta_c_k1 NA
delta_d_k1 NA
gamma_commute_a_k1 NA
gamma_commute_b_k1 NA
gamma_commute_c_k1 NA
gamma_commute_d_k1 NA
gamma_car_av_a_k1 NA
gamma_car_av_b_k1 NA
gamma_car_av_c_k1 NA
gamma_car_av_d_k1 NA
delta_a_k2 NA
delta_b_k2 NA
delta_c_k2 NA
delta_d_k2 NA
gamma_commute_a_k2 NA
gamma_commute_b_k2 NA
gamma_commute_c_k2 NA
gamma_commute_d_k2 NA
gamma_car_av_a_k2 NA
gamma_car_av_b_k2 NA
gamma_car_av_c_k2 NA
gamma_car_av_d_k2 NA


Summary of class allocation for model
component :
Mean prob.
Class_1 0.2474
Class_2 0.1970
Class_3 0.2368
Class_4 0.3188
stephanehess
Site Admin
Posts: 1054
Joined: 24 Apr 2020, 16:29

Re: ANA_EM has no covariance matrix

Post by stephanehess »

Hi

why are you using:

Code: Select all

V[["class_a"]] = (exp((delta_a_k1 + gamma_commute_a_k1*commute + gamma_car_av_a_k1*car_availability)*Q[1,1])*exp((delta_a_k2 + gamma_commute_a_k2*commute + gamma_car_av_a_k2*car_availability)*Q[2,1]))/((exp(delta_a_k1 + gamma_commute_a_k1*commute + gamma_car_av_a_k1*car_availability)+1)*(exp(delta_a_k1 + gamma_commute_a_k1*commute + gamma_car_av_a_k1*car_availability)+1))
it looks like you are manually calculating the class allocation probabilities, rather than giving the Vs to Apollo. Have a look at the examples online, should be easy to fix

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