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.

Unconditionals

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Unconditionals

Post by stephanehess »

Mohammed

re the below, can you please send me your code and data outside the forum and I'll look into it for you

Stephane
malemu588@gmail.com wrote: 19 Oct 2023, 13:54 Hi Stephane,

Thanks for your time and support in this forum. I see the version I am using is 0.3.0, so not quite sure when you say the error is related to an old version of Apollo. Of course, I will try to update to the newest version (if available) and see if the error can be eliminated.

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

Re: Unconditionals

Post by stephanehess »

Anders

please also share your data and code with me outside the forum and I'll look into this for you

Stephane
anders wrote: 19 Oct 2023, 20:37 Thank you very much, Stephane.

I am now defining betas inside apollo_lcPars, it provides me with the same results as my previous code.

I run the following code:

Code: Select all

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

### Load Apollo library
#library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "LC-MMNL-ANA-8",
  modelDescr      = "",
  indivID         = "id",
  mixing          = TRUE, 
  nCores          =    10,
  outputDirectory = "output"
)

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

### Loading data from
database = read.csv("apollo_data_wind_main_full.csv",header=TRUE)

#Use subset
database = subset(database,database$utvalg==2)


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

### Vector of parameters
apollo_beta = c(mu_twh2_a                                   =              2.9828,
                mu_twh3_a                                   =              5.1948,
                mu_twh4_a                                   =              5.9565,
                b_twh2_b                                    =                   0,
                b_twh3_b                                    =                   0,
                b_twh4_b                                    =                   0,
                mu_turb7_a                                  =             -4.7124,
                mu_turb14_a                                 =             -6.9625,
                mu_turb21_a                                 =             -7.8751,
                b_turb7_b                                   =                   0,
                b_turb14_b                                  =                   0,
                b_turb21_b                                  =                   0,
                b_cost_a                                    =              0.1168,
                b_cost_b                                    =                   0,
                sigma_twh2_a                                =                   0,
                sigma_twh3_a                                =                   0,
                sigma_twh4_a                                =                   0,
                sigma_turb7_a                               =                   0,
                sigma_turb14_a                              =                   0,
                sigma_turb21_a                              =                   0,
                delta_a                                     =              0.6696,
                delta_b                                     =             -1.0402,
                delta_c                                     =              0.3473,
                delta_d                                     =             -2.0474,
                delta_e                                     =              0.6300,
                delta_f                                     =              1.9412,
                delta_g                                     =             -0.5162)

### 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("b_twh2_b","b_twh3_b","b_twh4_b","b_turb7_b","b_turb14_b","b_turb21_b","b_cost_b")


### Set parameters for generating draws
apollo_draws = list(
  interDrawsType="mlhs",           
  interNDraws=500,                   
  interNormDraws=c("xi_twh2","xi_twh3","xi_twh4","xi_turb7","xi_turb14","xi_turb21")
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_twh2_a"]] =mu_twh2_a + sigma_twh2_a * xi_twh2
  randcoeff[["b_twh3_a"]] = mu_twh3_a + sigma_twh3_a * xi_twh3
  randcoeff[["b_twh4_a"]] = mu_twh4_a + sigma_twh4_a * xi_twh4
  randcoeff[["b_turb7_a"]] = mu_turb7_a + sigma_turb7_a * xi_turb7
  randcoeff[["b_turb14_a"]] = mu_turb14_a + sigma_turb14_a * xi_turb14 
  randcoeff[["b_turb21_a"]] = mu_turb21_a + sigma_turb21_a * xi_turb21

  
  return(randcoeff)
}

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

apollo_lcPars=function(apollo_beta, apollo_inputs){
  lcpars = list()
  lcpars[["b_twh2"  ]] = list(b_twh2_a, b_twh2_b)
  lcpars[["b_twh3"  ]] = list(b_twh3_a, b_twh3_b)
  lcpars[["b_twh4"  ]] = list(b_twh4_a, b_twh4_b)
  lcpars[["b_turb7"  ]] = list(b_turb7_a, b_turb7_b)
  lcpars[["b_turb14"  ]] = list(b_turb14_a, b_turb14_b)
  lcpars[["b_turb21"  ]] = list(b_turb21_a, b_turb21_b)
  lcpars[["b_cost"  ]] = list(b_cost_a, b_cost_b)
  
  V=list()
  V[["class_a"]] = delta_a
  V[["class_b"]] = delta_b
  V[["class_c"]] = delta_c
  V[["class_d"]] = delta_d
  V[["class_e"]] = delta_e
  V[["class_f"]] = delta_f
  V[["class_g"]] = delta_g
  V[["class_h"]] = 0
  
  ### Settings for class allocation models
  classAlloc_settings = list(
    classes      = c(class_a=1, class_b=2, class_c=3, class_d=4, class_e=5, class_f=6, class_g=7, class_h=8), 
    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:8){
    
    V = list()
    
    if(s==1){
      V[['alt1']]  = ((b_cost_a*(feeneg_1/100) + b_twh2_a*(twh_1==20) + b_twh3_a*(twh_1==30) + b_twh4_a*(twh_1==40)))
      V[['alt2']]  = ((b_cost_a*(feeneg_2/100) + b_twh2_a*(twh_2==20) + b_twh3_a*(twh_2==30) + b_twh4_a*(twh_2==40) + b_turb7_a*(turbines_2==700) + b_turb14_a*(turbines_2==1400) + b_turb21_a*(turbines_2==2100)))
      
      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)
      
      ### Average across inter-individual draws within classes
      P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
      }
    
    if(s==2){
      V[['alt1']]  = ((b_cost_b*(feeneg_1/100) + b_twh2_a*(twh_1==20) + b_twh3_a*(twh_1==30) + b_twh4_a*(twh_1==40)))
      V[['alt2']]  = ((b_cost_b*(feeneg_2/100) + b_twh2_a*(twh_2==20) + b_twh3_a*(twh_2==30) + b_twh4_a*(twh_2==40) + b_turb7_a*(turbines_2==700) + b_turb14_a*(turbines_2==1400) + b_turb21_a*(turbines_2==2100)))
      
      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)
      
      ### Average across inter-individual draws within classes
      P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
      }
    
    if(s==3){
      V[['alt1']]  = ((b_cost_a*(feeneg_1/100) + b_twh2_a*(twh_1==20) + b_twh3_a*(twh_1==30) + b_twh4_a*(twh_1==40)))
      V[['alt2']]  = ((b_cost_a*(feeneg_2/100) + b_twh2_a*(twh_2==20) + b_twh3_a*(twh_2==30) + b_twh4_a*(twh_2==40) + b_turb7_b*(turbines_2==700) + b_turb14_b*(turbines_2==1400) + b_turb21_b*(turbines_2==2100)))
      
      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)
      
      ### Average across inter-individual draws within classes
      P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
      }
    
    if(s==4){
      V[['alt1']]  = ((b_cost_a*(feeneg_1/100) + b_twh2_b*(twh_1==20) + b_twh3_b*(twh_1==30) + b_twh4_b*(twh_1==40)))
      V[['alt2']]  = ((b_cost_a*(feeneg_2/100) + b_twh2_b*(twh_2==20) + b_twh3_b*(twh_2==30) + b_twh4_b*(twh_2==40) + b_turb7_a*(turbines_2==700) + b_turb14_a*(turbines_2==1400) + b_turb21_a*(turbines_2==2100)))
    
      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)
      
      ### Average across inter-individual draws within classes
      P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
      }
    
    if(s==5){
      V[['alt1']]  = ((b_cost_a*(feeneg_1/100) + b_twh2_b*(twh_1==20) + b_twh3_b*(twh_1==30) + b_twh4_b*(twh_1==40)))
      V[['alt2']]  = ((b_cost_a*(feeneg_2/100) + b_twh2_b*(twh_2==20) + b_twh3_b*(twh_2==30) + b_twh4_b*(twh_2==40) + b_turb7_b*(turbines_2==700) + b_turb14_b*(turbines_2==1400) + b_turb21_b*(turbines_2==2100)))
      
      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)
      
      ### Average across inter-individual draws within classes
      #P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
      }
    
    if(s==6){
      V[['alt1']]  = ((b_cost_b*(feeneg_1/100) + b_twh2_b*(twh_1==20) + b_twh3_b*(twh_1==30) + b_twh4_b*(twh_1==40)))
      V[['alt2']]  = ((b_cost_b*(feeneg_2/100) + b_twh2_b*(twh_2==20) + b_twh3_b*(twh_2==30) + b_twh4_b*(twh_2==40) + b_turb7_a*(turbines_2==700) + b_turb14_a*(turbines_2==1400) + b_turb21_a*(turbines_2==2100)))
      
      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)
      
      ### Average across inter-individual draws within classes
      P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
      }
    
    if(s==7){
      V[['alt1']]  = ((b_cost_b*(feeneg_1/100) + b_twh2_a*(twh_1==20) + b_twh3_a*(twh_1==30) + b_twh4_a*(twh_1==40)))
      V[['alt2']]  = ((b_cost_b*(feeneg_2/100) + b_twh2_a*(twh_2==20) + b_twh3_a*(twh_2==30) + b_twh4_a*(twh_2==40) + b_turb7_b*(turbines_2==700) + b_turb14_b*(turbines_2==1400) + b_turb21_b*(turbines_2==2100)))
      
      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)
      
      ### Average across inter-individual draws within classes
      P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
      }
    
    if(s==8){
      V[['alt1']]  = ((b_cost_b*(feeneg_1/100) + b_twh2_b*(twh_1==20) + b_twh3_b*(twh_1==30) + b_twh4_b*(twh_1==40)))
      V[['alt2']]  = ((b_cost_b*(feeneg_2/100) + b_twh2_b*(twh_2==20) + b_twh3_b*(twh_2==30) + b_twh4_b*(twh_2==40) + b_turb7_b*(turbines_2==700) + b_turb14_b*(turbines_2==1400) + b_turb21_b*(turbines_2==2100)))
     
       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)
      
      ### Average across inter-individual draws within classes
      #P[[paste0("Class_",s)]] = apollo_avgInterDraws(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_saveOutput(model)

unconditionals = apollo_unconditionals(model,apollo_probabilities,apollo_inputs)
It runs smoothly and I get consistent results, but when I run the unconditionals part, I get the following error message:
Error in !is.na(apollo_draws) && apollo_draws$intraNDraws == 0 :
'length = 5' in coercion to 'logical(1)'
I have also tried to run an alternative specification of the ANA model that provides the same result, but also the same error message when I run the apollo_unconditionals command. The alternative specification is:

Code: Select all

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

### Load Apollo library
#library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "LC-MMNL-ANA-8",
  modelDescr      = "",
  indivID         = "id",
  mixing          = TRUE, 
  nCores          =    10,
  outputDirectory = "output"
)

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

### Loading data from
database = read.csv("apollo_data_wind_main_full.csv",header=TRUE)

#Use subset
database = subset(database,database$utvalg==2)


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

### Vector of parameters
apollo_beta = c(mu_twh2_a                                   =              2.9828,
                mu_twh3_a                                   =              5.1948,
                mu_twh4_a                                   =              5.9565,
                mu_twh2_b                                    =                   0,
                mu_twh3_b                                    =                   0,
                mu_twh4_b                                    =                   0,
                mu_turb7_a                                  =             -4.7124,
                mu_turb14_a                                 =             -6.9625,
                mu_turb21_a                                 =             -7.8751,
                mu_turb7_b                                  =                   0,
                mu_turb14_b                                 =                   0,
                mu_turb21_b                                 =                   0,
                mu_cost_a                                   =              0.1168,
                mu_cost_b                                   =                   0,
                sigma_twh2_a                                =                   0,
                sigma_twh3_a                                =                   0,
                sigma_twh4_a                                =                   0,
                sigma_turb7_a                               =                   0,
                sigma_turb14_a                              =                   0,
                sigma_turb21_a                              =                   0,
                delta_a                                     =              0.6696,
                delta_b                                     =             -1.0402,
                delta_c                                     =              0.3473,
                delta_d                                     =             -2.0474,
                delta_e                                     =              0.6300,
                delta_f                                     =              1.9412,
                delta_g                                     =             -0.5162)

### 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("mu_twh2_b","mu_twh3_b","mu_twh4_b","mu_turb7_b","mu_turb14_b","mu_turb21_b","mu_cost_b")


### Set parameters for generating draws
apollo_draws = list(
  interDrawsType="mlhs",           
  interNDraws=500,                   
  interNormDraws=c("xi_twh2","xi_twh3","xi_twh4","xi_turb7","xi_turb14","xi_turb21","xi_cost","xi_twh2_b","xi_twh3_b","xi_twh4_b","xi_turb7_b","xi_turb14_b","xi_turb21_b","xi_cost_b")    
  
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_twh2_a"]] =mu_twh2_a + sigma_twh2_a * xi_twh2
  randcoeff[["b_twh3_a"]] = mu_twh3_a + sigma_twh3_a * xi_twh3
  randcoeff[["b_twh4_a"]] = mu_twh4_a + sigma_twh4_a * xi_twh4
  randcoeff[["b_turb7_a"]] = mu_turb7_a + sigma_turb7_a * xi_turb7
  randcoeff[["b_turb14_a"]] = mu_turb14_a + sigma_turb14_a * xi_turb14 
  randcoeff[["b_turb21_a"]] = mu_turb21_a + sigma_turb21_a * xi_turb21
  randcoeff[["b_cost_a"]] =mu_cost_a + 0 * xi_cost
  
  randcoeff[["b_twh2_b"]] =mu_twh2_b + 0 * xi_twh2_b
  randcoeff[["b_twh3_b"]] = mu_twh3_b + 0 * xi_twh3_b
  randcoeff[["b_twh4_b"]] = mu_twh4_b + 0 * xi_twh4_b
  randcoeff[["b_turb7_b"]] = mu_turb7_b + 0 * xi_turb7_b
  randcoeff[["b_turb14_b"]] = mu_turb14_b + 0 * xi_turb14_b 
  randcoeff[["b_turb21_b"]] = mu_turb21_b + 0 * xi_turb21_b
  randcoeff[["b_cost_b"]] =mu_cost_b + 0 * xi_cost_b
  
  
  return(randcoeff)
}

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

apollo_lcPars=function(apollo_beta, apollo_inputs){
  lcpars = list()
  lcpars[["b_twh2"  ]] = list(b_twh2_a, b_twh2_b)
  lcpars[["b_twh3"  ]] = list(b_twh3_a, b_twh3_b)
  lcpars[["b_twh4"  ]] = list(b_twh4_a, b_twh4_b)
  lcpars[["b_turb7"  ]] = list(b_turb7_a, b_turb7_b)
  lcpars[["b_turb14"  ]] = list(b_turb14_a, b_turb14_b)
  lcpars[["b_turb21"  ]] = list(b_turb21_a, b_turb21_b)
  lcpars[["b_cost"  ]] = list(b_cost_a, b_cost_b)
  
  V=list()
  V[["class_a"]] = delta_a
  V[["class_b"]] = delta_b
  V[["class_c"]] = delta_c
  V[["class_d"]] = delta_d
  V[["class_e"]] = delta_e
  V[["class_f"]] = delta_f
  V[["class_g"]] = delta_g
  V[["class_h"]] = 0
  
  ### Settings for class allocation models
  classAlloc_settings = list(
    classes      = c(class_a=1, class_b=2, class_c=3, class_d=4, class_e=5, class_f=6, class_g=7, class_h=8), 
    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:8){
    
    V = list()
    
    if(s==1){
      V[['alt1']]  = ((b_cost_a*(feeneg_1/100) + b_twh2_a*(twh_1==20) + b_twh3_a*(twh_1==30) + b_twh4_a*(twh_1==40)))
      V[['alt2']]  = ((b_cost_a*(feeneg_2/100) + b_twh2_a*(twh_2==20) + b_twh3_a*(twh_2==30) + b_twh4_a*(twh_2==40) + b_turb7_a*(turbines_2==700) + b_turb14_a*(turbines_2==1400) + b_turb21_a*(turbines_2==2100)))
    
    }
    
    if(s==2){
      V[['alt1']]  = ((b_cost_b*(feeneg_1/100) + b_twh2_a*(twh_1==20) + b_twh3_a*(twh_1==30) + b_twh4_a*(twh_1==40)))
      V[['alt2']]  = ((b_cost_b*(feeneg_2/100) + b_twh2_a*(twh_2==20) + b_twh3_a*(twh_2==30) + b_twh4_a*(twh_2==40) + b_turb7_a*(turbines_2==700) + b_turb14_a*(turbines_2==1400) + b_turb21_a*(turbines_2==2100)))
      
    }
    
    if(s==3){
      V[['alt1']]  = ((b_cost_a*(feeneg_1/100) + b_twh2_a*(twh_1==20) + b_twh3_a*(twh_1==30) + b_twh4_a*(twh_1==40)))
      V[['alt2']]  = ((b_cost_a*(feeneg_2/100) + b_twh2_a*(twh_2==20) + b_twh3_a*(twh_2==30) + b_twh4_a*(twh_2==40) + b_turb7_b*(turbines_2==700) + b_turb14_b*(turbines_2==1400) + b_turb21_b*(turbines_2==2100)))
      
    }
    
    if(s==4){
      V[['alt1']]  = ((b_cost_a*(feeneg_1/100) + b_twh2_b*(twh_1==20) + b_twh3_b*(twh_1==30) + b_twh4_b*(twh_1==40)))
      V[['alt2']]  = ((b_cost_a*(feeneg_2/100) + b_twh2_b*(twh_2==20) + b_twh3_b*(twh_2==30) + b_twh4_b*(twh_2==40) + b_turb7_a*(turbines_2==700) + b_turb14_a*(turbines_2==1400) + b_turb21_a*(turbines_2==2100)))
      
    }
    
    if(s==5){
      V[['alt1']]  = ((b_cost_a*(feeneg_1/100) + b_twh2_b*(twh_1==20) + b_twh3_b*(twh_1==30) + b_twh4_b*(twh_1==40)))
      V[['alt2']]  = ((b_cost_a*(feeneg_2/100) + b_twh2_b*(twh_2==20) + b_twh3_b*(twh_2==30) + b_twh4_b*(twh_2==40) + b_turb7_b*(turbines_2==700) + b_turb14_b*(turbines_2==1400) + b_turb21_b*(turbines_2==2100)))
      
    }
    
    if(s==6){
      V[['alt1']]  = ((b_cost_b*(feeneg_1/100) + b_twh2_b*(twh_1==20) + b_twh3_b*(twh_1==30) + b_twh4_b*(twh_1==40)))
      V[['alt2']]  = ((b_cost_b*(feeneg_2/100) + b_twh2_b*(twh_2==20) + b_twh3_b*(twh_2==30) + b_twh4_b*(twh_2==40) + b_turb7_a*(turbines_2==700) + b_turb14_a*(turbines_2==1400) + b_turb21_a*(turbines_2==2100)))
      
    }
    
    if(s==7){
      V[['alt1']]  = ((b_cost_b*(feeneg_1/100) + b_twh2_a*(twh_1==20) + b_twh3_a*(twh_1==30) + b_twh4_a*(twh_1==40)))
      V[['alt2']]  = ((b_cost_b*(feeneg_2/100) + b_twh2_a*(twh_2==20) + b_twh3_a*(twh_2==30) + b_twh4_a*(twh_2==40) + b_turb7_b*(turbines_2==700) + b_turb14_b*(turbines_2==1400) + b_turb21_b*(turbines_2==2100)))
      
    }
    
    if(s==8){
      V[['alt1']]  = ((b_cost_b*(feeneg_1/100) + b_twh2_b*(twh_1==20) + b_twh3_b*(twh_1==30) + b_twh4_b*(twh_1==40)))
      V[['alt2']]  = ((b_cost_b*(feeneg_2/100) + b_twh2_b*(twh_2==20) + b_twh3_b*(twh_2==30) + b_twh4_b*(twh_2==40) + b_turb7_b*(turbines_2==700) + b_turb14_b*(turbines_2==1400) + b_turb21_b*(turbines_2==2100)))
      
    }
    
    
    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)
    
    ### Average across inter-individual draws within classes
    P[[paste0("Class_",s)]] = apollo_avgInterDraws(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_saveOutput(model)

unconditionals = apollo_unconditionals(model,apollo_probabilities,apollo_inputs)
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply