Page 1 of 2

OL with latent classes

Posted: 16 Nov 2020, 18:32
by MAF
Thank you for Apollo and its nice features !

I have been looking at latent class models. Following examples 18 and 20 I was able to successfully implement a MNL with latent classes.

Now I am trying to implement an OL model with latent classes. According to the Apollo manual Tau parameters can vary across classes. Though, I get an error message “Error in V – tau[j-1] : non-numeric argument to binary operator”.

I looked for an example of OL with latent classes but I was unable to find it. If there is no example in the manual, is it possible to get an example of OL with latent classes? Or paste an example of the code that should be included in the “apollo_lcPars” and “apollo_probabilities” functions? Or highlight the differences between MNL and OL regarding latent classes?

Thank you, Miguel

Re: OL with latent classes

Posted: 16 Nov 2020, 20:25
by stephanehess
Hi

please post your code so we can diagnose what's wrong

Thanks

Re: OL with latent classes

Posted: 16 Nov 2020, 21:55
by MAF
Thanks, code pasted below.


---
title: "Basic LC - MNL + OL "
author: "MAF"
date: "11/12/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

### Load Apollo library
library(apollo)

options(digits = 3)
```

#### LOAD LIBRARY AND DEFINE CORE SETTINGS
```{r, echo=TRUE, eval = TRUE}
### Clear memory
rm(list = ls())

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="LCM.v2",
modelDescr ="Combined MNL - Latent Class model",
indivID ="ID",
nCores = 5,
noDiagnostics = TRUE
)
```

#### LOAD DATA AND APPLY ANY TRANSFORMATIONS
```{r, echo=TRUE, eval = TRUE}
database = read.csv("database.csv",header=TRUE)
ncol(database)
nrow(database)
head(database)

### Add one to prepare for OL analysis
database$atti_tdel <- database$atti_tdel + 1

```

#### DEFINE MODEL PARAMETER
```{r, echo=TRUE, eval = TRUE}
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(

# Betas
asc_1 = 0, asc_2 = 0,
beta_Subs_a = 0,
beta_Inc1_a = 0, beta_Inc2_a = 0, beta_Inc4_a = 0, beta_Inc5_a = 0,

beta_Subs_b = 0,
beta_Inc1_b = 0, beta_Inc2_b = 0, beta_Inc4_b = 0, beta_Inc5_b = 0,

delta_b = 0,


# OL Thresholds
tau_tdel_1_a =1.0, tau_tdel_2_a =2.0, tau_tdel_3_a =3.0,tau_tdel_4_a =4.0, tau_tdel_5_a =5.0,
tau_tdel_1_b =1.0, tau_tdel_2_b =2.0, tau_tdel_3_b =3.0,tau_tdel_4_b =4.0, tau_tdel_5_b =5.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("asc_1", "beta_Subs_a")
```

#### DEFINE LATENT CLASS COMPONENTS
```{r, echo=TRUE, eval = TRUE}

apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()

lcpars[["beta_Subs"]] = list(beta_Subs_a, beta_Subs_b)
lcpars[["beta_Inc1"]] = list(beta_Inc1_a, beta_Inc1_b)
lcpars[["beta_Inc2"]] = list(beta_Inc2_a, beta_Inc2_b)
lcpars[["beta_Inc4"]] = list(beta_Inc4_a, beta_Inc4_b)
lcpars[["beta_Inc5"]] = list(beta_Inc5_a, beta_Inc5_b)

lcpars[["tau_tdel_1"]] = list(tau_tdel_1_a, tau_tdel_1_b)
lcpars[["tau_tdel_2"]] = list(tau_tdel_2_a, tau_tdel_2_b)
lcpars[["tau_tdel_3"]] = list(tau_tdel_3_a, tau_tdel_3_b)
lcpars[["tau_tdel_4"]] = list(tau_tdel_4_a, tau_tdel_4_b)
lcpars[["tau_tdel_5"]] = list(tau_tdel_5_a, tau_tdel_5_b)


V=list()

V[["class_a"]] = beta_Subs_a * Subs + beta_Inc1_a * Inc1 + beta_Inc2_a * Inc2 + beta_Inc4_a * Inc4 + beta_Inc5_a * Inc5

V[["class_b"]] = delta_b + beta_Subs_b * Subs + beta_Inc1_b * Inc1 + beta_Inc2_b * Inc2 + beta_Inc4_b * Inc4 + beta_Inc5_b * Inc5

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


V_ol = 1

tau_ol = list()
tau_ol[["class_a"]] = c(tau_tdel_1_a,tau_tdel_2_a, tau_tdel_3_a, tau_tdel_4_a,tau_tdel_5_a)
tau_ol[["class_b"]] = c(tau_tdel_1_b,tau_tdel_2_b, tau_tdel_3_b, tau_tdel_4_b,tau_tdel_5_b)

ol_settings_1 = list(
outcomeOrdered = NA,
V = V_ol,
tau = tau_ol
)

lcpars[["pi_values"]] = apollo_mnl(mnl_settings, functionality="raw")

lcpars[["pi_values"]] = apollo_ol(ol_settings_1, functionality="raw")

lcpars[["pi_values"]] = apollo_firstRow(lcpars[["pi_values"]], apollo_inputs)

return(lcpars)
}

```

#### GROUP AND VALIDATE INPUTS
```{r, echo=TRUE, eval = TRUE}
apollo_inputs = apollo_validateInputs()
```

#### DEFINE MODEL AND LIKELIHOOD FUNCTION
```{r, echo=TRUE, eval = TRUE}
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()

#########################################################################

### Compute probabilities for the OL model

ol_settings_1 = list(outcomeOrdered = atti_tdel,
V = 1,
# tau = tau_ol,
rows = (question==7),
componentName = "indic_tdel"
)

### Loop over classes

s=1
while(s<=2){

tau_ol = list()
tau_ol[[s]] = c(tau_tdel_1[[s]],tau_tdel_2[[s]], tau_tdel_3[[s]], tau_tdel_4[[s]],tau_tdel_5[[s]])

ol_settings_1$tau[[s]] = tau_ol[[s]]
ol_settings_1$componentName = paste0("Class_",s)

### Compute within-class choice probabilities using OL model
P[[paste0("Class_",s)]] = apollo_ol(ol_settings_1, functionality)

### Take product across observation for same individual
P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)

s=s+1
}

#########################################################################

### Define settings for MNL model
mnl_settings = list(
alternatives = c(alt1=1, alt2=2),
avail = list(alt1=1, alt2=1),
choiceVar = choice
)

### Loop over classes
s=1
while(s<=2){

### Compute class-specific utilities
V=list()
V[['alt1']] = asc_1
V[['alt2']] = asc_2 + beta_Subs[[s]] * Subs +
beta_Inc1[[s]] * Inc1 + beta_Inc2[[s]] * Inc2 + beta_Inc4[[s]] * Inc4 + beta_Inc5[[s]] * Inc5


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)

s=s+1
}

### Compute latent class model probabilities
lc_settings = list(inClassProb = P, classProb=pi_values)

P[["indic_tdel"]] = apollo_lc(lc_settings, apollo_inputs, functionality)

P[["choice"]] = apollo_lc(lc_settings, apollo_inputs, functionality)


### Likelihood of the whole model
P = apollo_combineModels(P, apollo_inputs, functionality)



### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)

return(P)
}
```

#### MODEL ESTIMATION
```{r, echo=TRUE, eval = TRUE}

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
estimate_settings=list(writeIter=FALSE) # add to debug or remove

```

#### MODEL OUTPUTS
```{r, echo=TRUE, eval = TRUE}
modelOutput_settings <- list(printPVal=TRUE, printT1=FALSE, printClassical=FALSE)
ctable <- apollo_modelOutput(model,modelOutput_settings)

```

Re: OL with latent classes

Posted: 16 Nov 2020, 22:13
by MAF
Just to clarify, I used the simplest possible function (1 in V) for OL to troubleshoot the problem with Tau. Also, I will link the utilities later so that the models are jointly estimated but for now just trying to solve the problem with Tau and trying to keep it as simple as possible in a combined model with latent classes and MNL+OL.

Re: OL with latent classes

Posted: 17 Nov 2020, 12:41
by dpalma
Hi Miguel,

Thank you for using Apollo.

Stephane and I were looking at your code, but there are some aspects of your model we don't fully understand. So we wanted to ask you for some additional details.
  • Could you provide some details on your data? what is/are your dependant variable(s)? what is the "main choice" you want model? What are your explanatory variables for the class allocation and the "main choice"?
  • About the model structure: is the class allocation function a binary logit? Is the "main choice" inside each class an ordered logit?
Thank you for the detail
David

Re: OL with latent classes

Posted: 17 Nov 2020, 17:03
by MAF
Hi David, Stephane,

Long-term goal, I would like to estimate a HLCM mode and this paper was my inspiration:

Hoyos, David, Petr Mariel, and Stephane Hess. "Incorporating environmental attitudes in discrete choice models: An exploration of the utility of the awareness of consequences scale." Science of the Total Environment 505 (2015): 1100-1111.

I think this paper was “pre-Apollo” because the package was not referenced in the paper. I assume Apollo can be used to estimate a closely related model.

We have attitudes in a Likert scale, I am trying to estimate an ordered logit model for the latent variable, where parameters τ (by class) are thresholds that need to be estimated and sociodemographic variables (like income) are used for the latent part.

Answering your questions
• Could you provide some details on your data? what is/are your dependant variable(s)? what is the "main choice" you want model? What are your explanatory variables for the class allocation and the "main choice"?

Answer: dependent variable could be binary (e.g. whether received home deliveries) or if possible OL (for increasing ranges of home deliveries). I am not sure if OL is possible (maybe not, do you know?)

• About the model structure: is the class allocation function a binary logit? Is the "main choice" inside each class an ordered logit?

Answer: Ideally similar to the Hoyos et al. (2015) paper. Using sociodemographic variables and LVs to estimate class probabilities.

Right now, I am trying to estimate the simplest possible OL model with two classes. But, I am stuck with the error message “Error in V – tau[j-1] : non-numeric argument to binary operator”.

See attached code with just one OL model with 2 classes (not with two models jointly estimated as in the code I sent before, my apologies if this was confusing).

I hope this makes sense. THANKS ! Miguel


---
title: "Basic LC + OL "
author: "MAF"
date: "11/17/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

### Load Apollo library
library(apollo)

options(digits = 3)
```

#### LOAD LIBRARY AND DEFINE CORE SETTINGS
```{r, echo=TRUE, eval = TRUE}
### Clear memory
rm(list = ls())

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="LCM.v3",
modelDescr ="OL - Latent Class model",
indivID ="ID",
nCores = 5,
noDiagnostics = TRUE
)
```

#### LOAD DATA AND APPLY ANY TRANSFORMATIONS
```{r, echo=TRUE, eval = TRUE}
database = read.csv("database.csv",header=TRUE)
ncol(database)
nrow(database)
head(database)

### Add one to prepare for OL analysis
database$atti_tdel <- database$atti_tdel + 1

```

#### DEFINE MODEL PARAMETER
```{r, echo=TRUE, eval = TRUE}
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(

# Betas

beta_Subs_a = 0,
beta_Inc1_a = 0, beta_Inc2_a = 0, beta_Inc4_a = 0, beta_Inc5_a = 0,

beta_Subs_b = 0,
beta_Inc1_b = 0, beta_Inc2_b = 0, beta_Inc4_b = 0, beta_Inc5_b = 0,



# OL Thresholds
tau_tdel_1_a =1.0, tau_tdel_2_a =2.0, tau_tdel_3_a =3.0,tau_tdel_4_a =4.0, tau_tdel_5_a =5.0,
tau_tdel_1_b =1.0, tau_tdel_2_b =2.0, tau_tdel_3_b =3.0,tau_tdel_4_b =4.0, tau_tdel_5_b =5.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("beta_Subs_a")
```

#### DEFINE LATENT CLASS COMPONENTS
```{r, echo=TRUE, eval = TRUE}

apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()

lcpars[["beta_Subs"]] = list(beta_Subs_a, beta_Subs_b)
lcpars[["beta_Inc1"]] = list(beta_Inc1_a, beta_Inc1_b)
lcpars[["beta_Inc2"]] = list(beta_Inc2_a, beta_Inc2_b)
lcpars[["beta_Inc4"]] = list(beta_Inc4_a, beta_Inc4_b)
lcpars[["beta_Inc5"]] = list(beta_Inc5_a, beta_Inc5_b)

lcpars[["tau_tdel_1"]] = list(tau_tdel_1_a, tau_tdel_1_b)
lcpars[["tau_tdel_2"]] = list(tau_tdel_2_a, tau_tdel_2_b)
lcpars[["tau_tdel_3"]] = list(tau_tdel_3_a, tau_tdel_3_b)
lcpars[["tau_tdel_4"]] = list(tau_tdel_4_a, tau_tdel_4_b)
lcpars[["tau_tdel_5"]] = list(tau_tdel_5_a, tau_tdel_5_b)

V_ol = 1

tau_ol = list()
tau_ol[["class_a"]] = c(tau_tdel_1_a,tau_tdel_2_a, tau_tdel_3_a, tau_tdel_4_a,tau_tdel_5_a)
tau_ol[["class_b"]] = c(tau_tdel_1_b,tau_tdel_2_b, tau_tdel_3_b, tau_tdel_4_b,tau_tdel_5_b)

ol_settings_1 = list(
outcomeOrdered = NA,
V = V_ol,
tau = tau_ol
)


lcpars[["pi_values"]] = apollo_ol(ol_settings_1, functionality="raw")

lcpars[["pi_values"]] = apollo_firstRow(lcpars[["pi_values"]], apollo_inputs)

return(lcpars)
}

```

#### GROUP AND VALIDATE INPUTS
```{r, echo=TRUE, eval = TRUE}
apollo_inputs = apollo_validateInputs()
```

#### DEFINE MODEL AND LIKELIHOOD FUNCTION
```{r, echo=TRUE, eval = TRUE}
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()

#########################################################################

### Compute probabilities for the OL model

ol_settings_1 = list(outcomeOrdered = atti_tdel,
rows = (question==7),
componentName = "model"
)

### Loop over classes

s=1
while(s<=2){

V_ol = list()

V_ol[["class_a"]] = beta_Subs_a * Subs + beta_Inc1_a * Inc1 + beta_Inc2_a * Inc2 + beta_Inc4_a * Inc4 + beta_Inc5_a * Inc5
V_ol[["class_b"]] = beta_Subs_b * Subs + beta_Inc1_b * Inc1 + beta_Inc2_b * Inc2 + beta_Inc4_b * Inc4 + beta_Inc5_b * Inc5

ol_settings_1$V[[s]] = V_ol[[s]]

tau_ol = vector()
tau_ol[[s]] = c(tau_tdel_1[[s]],tau_tdel_2[[s]], tau_tdel_3[[s]], tau_tdel_4[[s]],tau_tdel_5[[s]])

ol_settings_1$tau[[s]] = tau_ol[[s]]
ol_settings_1$componentName = paste0("Class_",s)

### Compute within-class choice probabilities using OL model
P[[paste0("Class_",s)]] = apollo_ol(ol_settings_1, functionality)

### Take product across observation for same individual
P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)

s=s+1
}

#########################################################################


### 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
```{r, echo=TRUE, eval = TRUE}

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
estimate_settings=list(writeIter=FALSE) # add to debug or remove

```

#### MODEL OUTPUTS
```{r, echo=TRUE, eval = TRUE}
modelOutput_settings <- list(printPVal=TRUE, printT1=FALSE, printClassical=FALSE)
ctable <- apollo_modelOutput(model,modelOutput_settings)

```

Re: OL with latent classes

Posted: 17 Nov 2020, 20:19
by MAF
I think the code I pasted does not make any sense, I have tried so many combinations trying to figure out how to do it that I have lost track of what I am doing.

I am lost regarding how to input the vector Tau and the V for an OL model when using two classes in the "while" loop and also when defining "apollo_lcPars". An example will help a lot.

Re: OL with latent classes

Posted: 17 Nov 2020, 20:44
by stephanehess
Miguel

there are a number of issues here:

1. I believe what you are wanting to do is to use an ordered logit model inside the latent class structure, so in the within class model. That is in apollo_probabilities, so not in apollo_lcPars.

2. In apollo_lcPars, if the thresholds are to vary across classes, then that should be defined there, e.g.

lcPars[["tau_1"]] =c(tau1_a,tau1_b)
lcPars[["tau_2"]] =c(tau2_a,tau2_b)
...

3. You should not be using the OL for the class allocation model, i.e. pi_values. This should be simple multinomial, not ordered

4. But more importantly, it's not clear whether your data is suitable for ordered logit. For that, the dependent variable should be ordinal, not multinomial

Stephane

Re: OL with latent classes

Posted: 17 Nov 2020, 22:04
by MAF
hello David, Stephane,

I tried a simple OL with V function related to class "b" parameters only and then I get this error message:

"Error in apollo_lc(lc_settings, apollo_inputs, functionality): Arguments 'inClassProb' and 'classProb' for model component "Latent class" must have the same length."

Maybe there are two models (combined) and in the apollo_lcPars function only one. Not sure how to solve this issue.

The code is below. Thanks. Miguel




---
title: "Basic LM"
author: "MAF"
date: "11/17/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

### Load Apollo library
library(apollo)

options(digits = 3)
```

#### LOAD LIBRARY AND DEFINE CORE SETTINGS
```{r, echo=TRUE, eval = TRUE}
### Clear memory
rm(list = ls())

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="LCM.4",
modelDescr ="Combined Latent Class model",
indivID ="ID",
nCores = 5,
noDiagnostics = TRUE
)
```

#### LOAD DATA AND APPLY ANY TRANSFORMATIONS
```{r, echo=TRUE, eval = TRUE}
database = read.csv("database.csv",header=TRUE)
ncol(database)
nrow(database)
head(database)

### Add one to prepare for OL analysis
database$atti_avai <- database$atti_avai + 1

ncol(database)
nrow(database)
head(database)
```

#### DEFINE MODEL PARAMETER
```{r, echo=TRUE, eval = TRUE}
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(

# Betas for upper model, choice whether use home deliveries
asc_1 = 0, asc_2 = 0,
beta_Subs_a = 0,
beta_Inc1_a = 0, beta_Inc2_a = 0, beta_Inc4_a = 0, beta_Inc5_a = 0,

delta_b = 0,
beta_Subs_b = 0,
beta_Inc1_b = 0, beta_Inc2_b = 0, beta_Inc4_b = 0, beta_Inc5_b = 0,

# OL Thresholds
tau_avai_1 =1.0, tau_avai_2 =2.0, tau_avai_3 =3.0,tau_avai_4 =4.0, tau_avai_5 =5.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("asc_1","beta_Subs_a")
```

#### DEFINE LATENT CLASS COMPONENTS
```{r, echo=TRUE, eval = TRUE}

apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()

lcpars[["beta_Subs"]] = list(beta_Subs_a, beta_Subs_b)
lcpars[["beta_Inc1"]] = list(beta_Inc1_a, beta_Inc1_b)
lcpars[["beta_Inc2"]] = list(beta_Inc2_a, beta_Inc2_b)
lcpars[["beta_Inc4"]] = list(beta_Inc4_a, beta_Inc4_b)
lcpars[["beta_Inc5"]] = list(beta_Inc5_a, beta_Inc5_b)


V=list()
V[["class_a"]] = beta_Subs_a * Subs + beta_Inc1_a * Inc1 + beta_Inc2_a * Inc2 + beta_Inc4_a * Inc4 + beta_Inc5_a * Inc5 #+


V[["class_b"]] = delta_b + beta_Subs_b * Subs + beta_Inc1_b * Inc1 + beta_Inc2_b * Inc2 + beta_Inc4_b * Inc4 + beta_Inc5_b * Inc5 #+


mnl_settings = list(
alternatives = c(class_a=1, class_b=2),
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)
}

```

#### GROUP AND VALIDATE INPUTS
```{r, echo=TRUE, eval = TRUE}
apollo_inputs = apollo_validateInputs()
```

#### DEFINE MODEL AND LIKELIHOOD FUNCTION
```{r, echo=TRUE, eval = TRUE}
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()


# OL Model using Class "b"
ol_settings_avai = list(outcomeOrdered = atti_avai,
V = beta_Subs_b * Subs + beta_Inc1_b * Inc1 + beta_Inc2_b * Inc2 + beta_Inc4_b * Inc4 + beta_Inc5_b * Inc5,
tau = c(tau_avai_1,tau_avai_2, tau_avai_3, tau_avai_4, tau_avai_5),
rows = (question==7),
componentName = "indic_avai")
P[["indic_avai"]] = apollo_ol(ol_settings_avai, functionality)


### 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),
rows = (question==7),
choiceVar = choice
)

### Loop over classes
s=1
while(s<=2){

### Compute class-specific utilities
V=list()
V[['alt1']] = asc_1
V[['alt2']] = asc_2 + beta_Subs[[s]] * Subs +
beta_Inc1[[s]] * Inc1 + beta_Inc2[[s]] * Inc2 + beta_Inc4[[s]] * Inc4 + beta_Inc5[[s]] * Inc5# +

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)

s=s+1
}

### Compute latent class model probabilities
lc_settings = list(inClassProb = P, classProb=pi_values)

P[["choice"]] = apollo_lc(lc_settings, apollo_inputs, functionality)


### Likelihood of the whole model
P = apollo_combineModels(P, apollo_inputs, functionality)

### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)

return(P)
}
```

#### MODEL ESTIMATION
```{r, echo=TRUE, eval = TRUE}
### Optional: calculate LL before model estimation
apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
estimate_settings=list(writeIter=FALSE) # add to debug or remove

```

#### MODEL OUTPUTS
```{r, echo=TRUE, eval = TRUE}

modelOutput_settings <- list(printPVal=TRUE, printT1=FALSE, printClassical=FALSE)
ctable <- apollo_modelOutput(model,modelOutput_settings)

```

Re: OL with latent classes

Posted: 17 Nov 2020, 22:29
by stephanehess
Hi

there are other things with your model that are not right.

The parameter you are using in your class allocation model seem to be the same as those that vary across classes and are used in the within class model. That is not how a latent class model works.

Please have a look at the examples online. Alternatively, please try and explain your model specification to us in simple terms, not in code, and we will try to help you.

Stephane