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.

Latent class model with covariates, using categorical data

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
TheChainsOfMarkov
Posts: 3
Joined: 28 Apr 2022, 20:09

Latent class model with covariates, using categorical data

Post by TheChainsOfMarkov »

Hi, Prof. Hess

Apologies if this is in the manual, but I've been searching all day and I can't find it.

I want to run an LC model on choice data I have. In both the demographic data and the alternative attribute data, some columns are continuous, some are binary, and some are categorical.

I'm at a loss for how to code this.

For example, I have the following in the attributes:
* price (continuous)
* side effect risk (categorical: low, medium, high)
* over-the-counter (binary: yes / no)

In the demographics, similarly, I have:
* age (continuous)
* education (categorical: primary, secondary, university, more)
* prior doctor's visit (categorical: yes, no, unknown)

Here's what I've got so far, for a 3-class model, with 3 alternatives at each choice task:

Code: Select all

apollo_beta = c(
  asc_1              = 0,
  asc_2              = 0,
  asc_3              = 0,
  price_a            = 0,
  price_b              = 0,
  price_c              = 0,
  sfx_a           = 0,
  sfx_b           = 0,
  sfx_c           = 0,
  otc_a         = 0,
  otc_b         = 0,
  otc_c         = 0,
  delta_a            = 0,
  educ_a           = 0,
  prior_a             = 0,
  age_a             = 0,
  delta_b           = 0,
  educ_b           = 0,
  prior_b             = 0,
  age_b             = 0,
  delta_c            = 0,
  educ_c           = 0,
  prior_c             = 0,
  age_c             = 0,
)
apollo_fixed = c("asc_3", "delta_c","educ_c","prior_c", "age_c") 

#Define latent class components
apollo_lcPars=function(apollo_beta, apollo_inputs){
  lcpars = list()
  lcpars[["price"]]  = list(price_a, price_b, price_c) 
  lcpars[["sfx"]]    = list(sfx_a, sfx_b, sfx_c) 
  lcpars[["otc"]]  = list(otc_a, otc_b, otc_c) 

  
  ### Utilities of class allocation model
  V=list()
  V[["class_a"]] = delta_a + educ_a*educ + prior_a*prior
  V[["class_b"]] = delta_b + educ_b*educ + prior_b*prior
  V[["class_c"]] = delta_c + educ_c*educ + prior_c*prior
  
  ### Settings for class allocation models
  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)
}
Unfortunately, I get the following error:

Code: Select all

Error in educ_a * educ : non-numeric argument to binary operator
In addition: Warning message:
In Ops.factor(prior_a, prior) : ‘*’ not meaningful for factors
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Latent class model with covariates, using categorical data

Post by stephanehess »

Hi

it seems like your data is coded as factors, which is not comptaible with Apollo. First convert your data to numeric, and then you can see the examples on the apollo website that use categorical variables and you can then dummy code the attribute

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
TheChainsOfMarkov
Posts: 3
Joined: 28 Apr 2022, 20:09

Re: Latent class model with covariates, using categorical data

Post by TheChainsOfMarkov »

You're right, the data are coded as factors. Is this, then how you'd recommend it:

If I follow the manual's description of an EL model, I'd code the following in the apollo_probabilities() function

Code: Select all

V[["alt1"]] = ( b_price*price_1 + b_OTC*otc_1 +
			bSFX_low*(sfx_1=="low") + bSFX_med*(sfx_1=="med") + bSFX_hi*(sfx_1=="hi")) 
And this is what the relevant portion of the apollo_lcpars() function would look like

Code: Select all

V[["class_a"]] = delta_a + age_a*age + prior_a*prior + educPrimary_a*(educ=="primary") + educSec_a*(educ=="secondary") + ...
Thank you!
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Latent class model with covariates, using categorical data

Post by stephanehess »

Yes, with an appropriate normalisation
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply