Page 1 of 1

Latent class model with covariates, using categorical data

Posted: 28 Apr 2022, 20:23
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

Re: Latent class model with covariates, using categorical data

Posted: 28 Apr 2022, 22:38
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

Re: Latent class model with covariates, using categorical data

Posted: 28 Apr 2022, 23:37
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!

Re: Latent class model with covariates, using categorical data

Posted: 29 Apr 2022, 00:24
by stephanehess
Yes, with an appropriate normalisation