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. We check the forum at least twice a week. It may thus take a couple of days for your post to appear and before we reply. There is no need to submit the post multiple times.

Individual weights error with LC model

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
cdelosrios
Posts: 1
Joined: 20 Nov 2025, 15:41

Individual weights error with LC model

Post by cdelosrios »

Hi,

I am running a basic LC model without any covariates, just to build up towards more complex classes. I am using individual weights in my estimation. I know for sure that the weights are correctly specified since I have already estimated RPL models using those weights without issues. However, when I turn to the LC estimation, I encounter the following error:
Error en apollo_weighting(P, apollo_inputs, functionality):INTERNAL ISSUE - When applying weights at the individual level, weights should be the same for all observations of each individual
Below my full code:

Code: Select all


apollo_initialise()

# core controls
apollo_control = list(
  modelDescr      = "Latent class with continuous random parameters on Swiss route choice data",
  indivID         = "individual_id",
  nCores          = 4, 
  outputDirectory = "RESULTS/LC",
  weights    = "weights" # These weights are at the individual level
)


# PARAMETERS                                     

apollo_beta = c(
  mu_b_optout_a        = -1.095,
  sigma_b_optout_a        = 0.5,
  mu_b_optout_b        = -1.095,
  sigma_b_optout_b        = 0.5,
  mu_b_grass_low_a    = 0.227,
  sigma_b_grass_low_a    = 0.09,
  mu_b_grass_low_b    = 0.227,
  sigma_b_grass_low_b    = 0.09,
  mu_b_grass_high_a   = 0.082,
  sigma_b_grass_high_a   = 0.3,
  mu_b_grass_high_b   = -0.082,
  sigma_b_grass_high_b   = 0.3,
  mu_b_species_a      = -0.037,
  sigma_b_species_a      = 0.01,
  mu_b_species_b      = 0.037,
  sigma_b_species_b      = 0.01,
  mu_b_no_seeds_a     = -0.867,
  sigma_b_no_seeds_a     = 0.4,
  mu_b_no_seeds_b     = -0.867,
  sigma_b_no_seeds_b     = 0.4,
  mu_b_no_fert_seeds_a= 0.879, 
  sigma_b_no_fert_seeds_a= 0.4, 
  mu_b_no_fert_seeds_b= -0.879, 
  sigma_b_no_fert_seeds_b= 0.4,
  mu_b_percent_out_a  = -0.008,
  sigma_b_percent_out_a  = 0.002,
  mu_b_percent_out_b  = 0.008,
  sigma_b_percent_out_b  = 0.002,
  mu_b_payment_a      =  -4.13,
  sigma_b_payment_a     = 2,
  mu_b_payment_b      =  -4.13,
  sigma_b_payment_b      = 2,
  delta_a         =  0,
  delta_b         =  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_b")

# ################################################################# #
# RANDOM COMPONENTS                                    ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType="halton",           
  interNDraws=500,                   
  interUnifDraws=c(),                
  interNormDraws=c("draws_tt"),    
  
  intraDrawsType="mlhs",
  intraNDraws=0,
  intraUnifDraws=c(),
  intraNormDraws=c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["tt_a"]] = -exp(log_tt_a_mu + log_tt_a_sig*draws_tt)
  randcoeff[["tt_b"]] = -exp(log_tt_b_mu + log_tt_b_sig*draws_tt)
  
  return(randcoeff)
}

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton",
  interNDraws    = 100,
  interNormDraws = c("draws_payment","draws_optout","draws_g_low","draws_g_high","draws_species","draws_seeds","draws_seeds_fert","draws_percentout","draws_ascB")
)

### Create random parameters
# only downpay and fbound are negative log-normal, the rest are positive
apollo_randCoeff <- function(apollo_beta, apollo_inputs){
  randcoeff <- list()
  
  
  randcoeff[["b_optout_a"]]             = mu_b_optout_a + sigma_b_optout_a * draws_optout
  randcoeff[["b_grass_low_a"]]             = mu_b_grass_low_a + sigma_b_grass_low_a * draws_g_low
  randcoeff[["b_grass_high_a"]]             = mu_b_grass_high_a + sigma_b_grass_high_a * draws_g_high
  randcoeff[["b_species_a"]]             = mu_b_species_a + sigma_b_species_a * draws_species
  randcoeff[["b_no_seeds_a"]]             = mu_b_no_seeds_a + sigma_b_no_seeds_a* draws_seeds
  randcoeff[["b_no_fert_seeds_a"]]        = mu_b_no_fert_seeds_a + sigma_b_no_fert_seeds_a * draws_seeds_fert
  randcoeff[["b_percent_out_a"]]        = mu_b_percent_out_a + sigma_b_percent_out_a * draws_percentout
  
  randcoeff[["b_payment_a"]]             = exp(mu_b_payment_a + sigma_b_payment_a * draws_payment)  # positive log-normal,
  
  randcoeff[["b_optout_b"]]             = mu_b_optout_b + sigma_b_optout_b * draws_optout
  randcoeff[["b_grass_low_b"]]             = mu_b_grass_low_b + sigma_b_grass_low_b * draws_g_low
  randcoeff[["b_grass_high_b"]]             = mu_b_grass_high_b + sigma_b_grass_high_b * draws_g_high
  randcoeff[["b_species_b"]]             = mu_b_species_b + sigma_b_species_b * draws_species
  randcoeff[["b_no_seeds_b"]]             = mu_b_no_seeds_b + sigma_b_no_seeds_b * draws_seeds
  randcoeff[["b_no_fert_seeds_b"]]        = mu_b_no_fert_seeds_b + sigma_b_no_fert_seeds_b * draws_seeds_fert
  randcoeff[["b_percent_out_b"]]        = mu_b_percent_out_b + sigma_b_percent_out_b * draws_percentout
  
  randcoeff[["b_payment_b"]]             = exp(mu_b_payment_b + sigma_b_payment_b * draws_payment)  # positive log-normal,
  
  
  
  return(randcoeff)
}


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

apollo_lcPars = function(apollo_beta, apollo_inputs){
  lcpars = list()
  
  # Class-specific coefficients
  lcpars[["b_optout"]]        <- list(b_optout_a,        b_optout_b)
  lcpars[["b_grass_low"]]     <- list(b_grass_low_a,     b_grass_low_b)
  lcpars[["b_grass_high"]]    <- list(b_grass_high_a,    b_grass_high_b)
  lcpars[["b_species"]]       <- list(b_species_a,       b_species_b)
  lcpars[["b_no_seeds"]]      <- list(b_no_seeds_a,      b_no_seeds_b)
  lcpars[["b_no_fert_seeds"]] <- list(b_no_fert_seeds_a, b_no_fert_seeds_b)
  lcpars[["b_percent_out"]]   <- list(b_percent_out_a,   b_percent_out_b)
  lcpars[["b_payment"]]       <- list(b_payment_a,       b_payment_b)
  
  # Class membership probabilities (no covariates)
  #lcpars[["pi_values"]]       <- list(1, exp(delta_a + delta_b*0))
  
  V=list()
  V[["class_a"]] = delta_a 
  V[["class_b"]] = delta_b
  
  classAlloc_settings = list(
    classes      = c(class_a=1, class_b=2), 
    utilities    = V
  )
  
  lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)
  
  return(lcpars)
}



# ################################################################# #
#### 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:2){
    ### Compute class-specific utilities
    
    
    ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
    V <- list(
      alt1 = 
        b_grass_low[[s]]     * grass_low_1     +
        b_grass_high[[s]]    * grass_high_1    +
        b_species[[s]]       * Species_1       +
        b_no_seeds[[s]]      * no_seeds_1      +
        b_no_fert_seeds[[s]] * no_fert_seeds_1 +
        b_percent_out[[s]]   * percent_out_1   +
        b_payment[[s]]       * payment_1,
      alt2 = 
        b_grass_low[[s]]     * grass_low_2     +
        b_grass_high[[s]]    * grass_high_2    +
        b_species[[s]]       * Species_2       +
        b_no_seeds[[s]]      * no_seeds_2      +
        b_no_fert_seeds[[s]] * no_fert_seeds_2 +
        b_percent_out[[s]]   * percent_out_2   +
        b_payment[[s]]       * payment_2,
      alt3 = b_optout[[s]]
    )
    
    
    
    mnl_settings$utilities = V
    
    ### Compute within-class choice probabilities using MNL model
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
    
    ### Apply weights before taking the product of all observations of each indiv
    P = apollo_weighting(P, apollo_inputs, 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)
}


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


D <- read_dta("DATA/analysis/full_data_estimate.dta") %>% as.data.frame()
D <- subset(D, select = -c(choice_1, choice_2, choice_3))


# Only with the ones that completed all tasks
database <- D %>%
  group_by(individual_id) %>%
  filter(n_distinct(choice_set) == 6) 


apollo_control$modelName <- "LC_RPL_basic_w"
apollo_inputs = apollo_validateInputs()
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
apollo_modelOutput(model)
apollo_saveOutput(model)

After running that code I get the following output

Code: Select all

> model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
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. 
Apollo found a model component of type MNL without a componentName. The name was set to "Class_1" by default.

Overview of choices for MNL model component Class_1:
                                    alt1   alt2    alt3
Times available                  2010.00 2010.0 2010.00
Times chosen                      879.00  786.0  345.00
Percentage chosen overall          43.73   39.1   17.16
Percentage chosen when available   43.73   39.1   17.16

Apollo found a model component of type MNL without a componentName. The name was set to "Class_2" by default.

Overview of choices for MNL model component Class_2:
                                    alt1   alt2    alt3
Times available                  2010.00 2010.0 2010.00
Times chosen                      879.00  786.0  345.00
Percentage chosen overall          43.73   39.1   17.16
Percentage chosen when available   43.73   39.1   17.16

Error en apollo_weighting(P, apollo_inputs, functionality): 
  INTERNAL ISSUE - When applying weights at the individual level, weights should be the same for all observations of each individual.
I have estimated the model without any weights and it works, so I know the only issue is with the weighting part. I am guessing I am not placing the weighting in the right part of the code, I followed what I was doing and worked for the RPL here.

I would really appreciate any help.
stephanehess
Site Admin
Posts: 1341
Joined: 24 Apr 2020, 16:29

Re: Individual weights error with LC model

Post by stephanehess »

Hi

yes, you're doing it at the wrong place, I believe. I woiuld do the weigthing after the call to apollo_lc

Also, you really should not use Halton draws with so many random terms, and you need many more draws too

Hope this helps

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