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.

From most likely value to characteristics across classes

Ask questions about post-estimation functions (e.g. prediction, conditionals, etc) or other processing of results.
Post Reply
LianEnaLiu
Posts: 3
Joined: 02 Jan 2023, 11:19

From most likely value to characteristics across classes

Post by LianEnaLiu »

Hi,

I have a question about calculating the characteristics across classes. For example, I have the covariate age in my model, this is divided into three categories: youth, young-adults and middle-aged adults. I've managed to find the most likely value per class:

X1 X2 X3
2.107134 2.281334 1.961140

However, I would like to find how many respondents (in %) of class 1 fall within category 1 (youth), how many in category 2 (young-adults) and how many in category 3 (middle-aged adults). Do you have some advice on how to calculate this?

Kind regards

This is my code:

rm(list = ls())

### Load Apollo library

library(apollo)

### Initialise code

apollo_initialise()


### Set core controls
apollo_control = list(
modelName = "LCCM_covariates",
modelDescr = "LCCM_covariates",
indivID = "ID"
)


### Load data
database = read.delim("LCCMcovariates.dat",sep=',',header=TRUE)

###
apollo_beta = c(BETA_env_a = 0,
BETA_env_b = 0,
BETA_env_c = 0,
BETA_nutri_a = 0,
BETA_nutri_b = 0,
BETA_nutri_c = 0,
BETA_price_a = 0,
BETA_price_b = 0,
BETA_price_c = 0,
BETA_taste_a = 0,
BETA_taste_b = 0,
BETA_taste_c = 0,
BETA_tex_a = 0,
BETA_tex_b = 0,
BETA_tex_c = 0,
BETA_app_a = 0,
BETA_app_b = 0,
BETA_app_c = 0,
BETA_AW_a = 0,
BETA_AW_b = 0,
BETA_AW_c = 0,
delta_a = 0,
delta_b = 0,
delta_c = 0,
gamma_diet_a = 0,
gamma_diet_b = 0,
gamma_diet_c = 0,
gamma_age_a = 0,
gamma_age_b = 0,
gamma_age_c = 0,
gamma_prep_a = 0,
gamma_prep_b = 0,
gamma_prep_c = 0,
gamma_promeat_a = 0,
gamma_promeat_b = 0,
gamma_promeat_c = 0,
gamma_sustainablebehaviour_a = 0,
gamma_sustainablebehaviour_b = 0,
gamma_sustainablebehaviour_c = 0,
gamma_EnvConcern_a = 0,
gamma_EnvConcern_b = 0,
gamma_EnvConcern_c = 0,
gamma_HealthConscious_a = 0,
gamma_HealthConscious_b = 0,
gamma_HealthConscious_c = 0,
gamma_objknow_a = 0,
gamma_objknow_b = 0,
gamma_objknow_c = 0)

apollo_fixed = c("delta_c","gamma_diet_c","gamma_age_c",
"gamma_prep_c","gamma_promeat_c" ,
"gamma_sustainablebehaviour_c","gamma_EnvConcern_c", "gamma_HealthConscious_c",
"gamma_objknow_c")



### Defining Latent Class Parameters
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["BETA_env"]] = list(BETA_env_a, BETA_env_b, BETA_env_c)
lcpars[["BETA_nutri"]] = list(BETA_nutri_a, BETA_nutri_b, BETA_nutri_c)
lcpars[["BETA_price"]] = list(BETA_price_a, BETA_price_b, BETA_price_c)
lcpars[["BETA_taste"]] = list(BETA_taste_a, BETA_taste_b, BETA_taste_c)
lcpars[["BETA_tex"]] = list(BETA_tex_a, BETA_tex_b, BETA_tex_c)
lcpars[["BETA_app"]] = list(BETA_app_a, BETA_app_b, BETA_app_c)
lcpars[["BETA_AW"]] = list(BETA_AW_a, BETA_AW_b, BETA_AW_c)

V=list()
V[["class_a"]] = delta_a + gamma_diet_a*diet + gamma_age_a * AgeCategory + gamma_prep_a * prep1 +
gamma_promeat_a * promeat +
gamma_sustainablebehaviour_a * sustainablebehaviour + gamma_EnvConcern_a * EnvConcern +
gamma_HealthConscious_a * HealthConscious + gamma_objknow_a * objknow

V[["class_b"]] = delta_b + gamma_diet_b*diet + gamma_age_b * AgeCategory + gamma_prep_b * prep1 +
gamma_promeat_b * promeat +
gamma_sustainablebehaviour_b * sustainablebehaviour + gamma_EnvConcern_b * EnvConcern +
gamma_HealthConscious_b * HealthConscious + gamma_objknow_b * objknow

V[["class_c"]] = delta_c + gamma_diet_c*diet + gamma_age_c * AgeCategory + gamma_prep_c * prep1 +
gamma_promeat_c * promeat +
gamma_sustainablebehaviour_c * sustainablebehaviour + gamma_EnvConcern_c * EnvConcern +
gamma_HealthConscious_c * HealthConscious + gamma_objknow_c * objknow




mnl_settings = list(
alternatives = c(class_a=1, class_b=2, class_c=3),
avail = 1,
choiceVar = NA,
V = V
)


lcpars[["pi_values"]] = apollo_mnl(mnl_settings, functionality = "raw")
lcpars[["pi_values"]] = apollo_firstRow(lcpars[["pi_values"]], apollo_inputs)
return(lcpars)
}



### VALIDATING AND PREPARING INPUTS
apollo_inputs = apollo_validateInputs()



### Model definition
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 = value
)


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

### Compute class-specific utilities
V = list()
V[['alt1']] = env1 * BETA_env[[s]] + nutri1 * BETA_nutri[[s]] + price1 * BETA_price[[s]] +
taste1 * BETA_taste[[s]] + tex1 * BETA_tex[[s]] + app1 * BETA_app[[s]] + AW1 * BETA_AW[[s]]
V[['alt2']] = env2 * BETA_env[[s]] + nutri2 * BETA_nutri[[s]] + price2 * BETA_price[[s]] +
taste2 * BETA_taste[[s]] + tex2 * BETA_tex[[s]] + app2 * BETA_app[[s]] + AW2 * BETA_AW[[s]]
V[['alt3']] = env3 * BETA_env[[s]] + nutri3 * BETA_nutri[[s]] + price3 * BETA_price[[s]] +
taste3 * BETA_taste[[s]] + tex3 * BETA_tex[[s]] + app3 * BETA_app[[s]] + AW3 * BETA_AW[[s]]

mnl_settings$V = 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)
}

#### MODEL ESTIMATION
model = apollo_estimate(apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs)


#### MODEL OUTPUTS
apollo_modelOutput(model,modelOutput_settings=list(printPVal=TRUE))

###apollo_saveOutput(model)


unconditionals = apollo_unconditionals(model,apollo_probabilities,apollo_inputs)

conditionals = apollo_conditionals(model,apollo_probabilities, apollo_inputs)

summary(conditionals)

summary(as.data.frame(unconditionals[["pi_values"]]))

diet_n = apollo_firstRow(database$diet, apollo_inputs)
age_n = apollo_firstRow(database$AgeCategory, apollo_inputs)

post_diet=colSums(diet_n*conditionals)/colSums(conditionals)
post_age=colSums(age_n*conditionals)/colSums(conditionals)

post_diet

post_age

post_diet[2:4]

post_age[2:4]
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: From most likely value to characteristics across classes

Post by stephanehess »

The command

Code: Select all

colSums((age_n==1)*conditionals)/colSums(conditionals)
would tell you the posterior probability of someone in class 1 being in age group 1. Then you could do the same for other age groups and other classes

Then multiplying the posterior probability for people in group 1 being in age group 1 by the size of class 1 would tell you how many of the age group 1 people you would expect to fall into class 1. Just remember that this is all probabilistic, so it's an expectation, rather than an observation

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