Order of parameters in apollo_beta (LC model)
Posted: 29 Apr 2024, 12:09
Dear Prof. Hess,
I am running a simple LC model (without covariates for now). All attributes are dummy coded.
I am confused because I have run the same model twice with a different order of attributes in the function appolo_beta. In one case attribute by attribute (as in the example)
and in the other case first all attributes of the same class, then the second class and so on (easier to copy paste when I add a class).
With two classes, both types of coding work and give the same results. But with three classes, coding like in the example produces the warning “Estimation failed. No covariance matrix to compute.” Whereas the other type of coding provides results. Isn’t the order of the arguments within apollo_beta normally irrelevant?
Here is my entire Code:
Thank you for your help!
Paul
I am running a simple LC model (without covariates for now). All attributes are dummy coded.
I am confused because I have run the same model twice with a different order of attributes in the function appolo_beta. In one case attribute by attribute (as in the example)
Code: Select all
apollo_beta = c(
beta_totb_keine_a = 0,
beta_totb_keine_b = 0,
beta_totb_keine_c = 0,
beta_totb_hoheStubben_a = 0,
beta_totb_hoheStubben_b = 0,
beta_totb_hoheStubben_c = 0,
beta_totb_gruppen_a = 0,
beta_totb_gruppen_b = 0,
beta_totb_gruppen_c = 0,
beta_totb_ueberall_a =0,
beta_totb_ueberall_b =0,
beta_totb_ueberall_c =0,
beta_boden_gemulcht_a = 0,
beta_boden_gemulcht_b = 0,
beta_boden_gemulcht_c = 0,
beta_boden_ueberall_a = 0,
beta_boden_ueberall_b = 0,
beta_boden_ueberall_c = 0,
beta_boden_keine_a = 0,
beta_boden_keine_b = 0,
beta_boden_keine_c = 0,
beta_zaun_Flaechig_a = 0,
beta_zaun_Flaechig_b = 0,
beta_zaun_Flaechig_c = 0,
beta_zaun_kleingatter_a = 0,
beta_zaun_kleingatter_b = 0,
beta_zaun_kleingatter_c = 0,
beta_zaun_Teilflaechig_a = 0,
beta_zaun_Teilflaechig_b = 0,
beta_zaun_Teilflaechig_c = 0,
beta_zaun_zaun_a = 0,
beta_zaun_zaun_b = 0,
beta_zaun_zaun_c = 0,
delta_a = 0,
delta_b = 0,
delta_c = 0)]Code: Select all
apollo_beta = c(
beta_totb_keine_a = 0,
beta_totb_hoheStubben_a = 0,
beta_totb_gruppen_a = 0,
beta_totb_ueberall_a =0,
beta_boden_gemulcht_a = 0,
beta_boden_ueberall_a = 0,
beta_boden_keine_a = 0,
beta_zaun_Flaechig_a = 0,
beta_zaun_kleingatter_a = 0,
beta_zaun_Teilflaechig_a = 0,
beta_zaun_zaun_a = 0,
delta_a = 0,
beta_totb_keine_b = 0,
beta_totb_hoheStubben_b = 0,
beta_totb_gruppen_b = 0,
beta_totb_ueberall_b =0,
beta_boden_gemulcht_b = 0,
beta_boden_ueberall_b = 0,
beta_boden_keine_b = 0,
beta_zaun_Flaechig_b = 0,
beta_zaun_kleingatter_b = 0,
beta_zaun_Teilflaechig_b = 0,
beta_zaun_zaun_b = 0,
delta_b = 0,
beta_totb_keine_c = 0,
beta_totb_hoheStubben_c = 0,
beta_totb_gruppen_c = 0,
beta_totb_ueberall_c =0,
beta_boden_gemulcht_c = 0,
beta_boden_ueberall_c = 0,
beta_boden_keine_c = 0,
beta_zaun_Flaechig_c = 0,
beta_zaun_kleingatter_c = 0,
beta_zaun_Teilflaechig_c = 0,
beta_zaun_zaun_c = 0,
delta_c = 0)Here is my entire Code:
Code: Select all
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
library(readxl)
setwd("~/R/Default speicherort")
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "LC_no_covariates",
modelDescr = "Simple LC model, using dummy coding, no covariates in class allocation model",
indivID = "id",
nCores = 7,
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
longZ1komplett <- read_excel("longZ1komplett.xlsx")
database = longZ1komplett
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(
beta_totb_keine_a = 0,
beta_totb_hoheStubben_a = 0,
beta_totb_gruppen_a = 0,
beta_totb_ueberall_a =0,
beta_boden_gemulcht_a = 0,
beta_boden_ueberall_a = 0,
beta_boden_keine_a = 0,
beta_zaun_Flaechig_a = 0,
beta_zaun_kleingatter_a = 0,
beta_zaun_Teilflaechig_a = 0,
beta_zaun_zaun_a = 0,
delta_a = 0,
beta_totb_keine_b = 0,
beta_totb_hoheStubben_b = 0,
beta_totb_gruppen_b = 0,
beta_totb_ueberall_b =0,
beta_boden_gemulcht_b = 0,
beta_boden_ueberall_b = 0,
beta_boden_keine_b = 0,
beta_zaun_Flaechig_b = 0,
beta_zaun_kleingatter_b = 0,
beta_zaun_Teilflaechig_b = 0,
beta_zaun_zaun_b = 0,
delta_b = 0,
beta_totb_keine_c = 0,
beta_totb_hoheStubben_c = 0,
beta_totb_gruppen_c = 0,
beta_totb_ueberall_c =0,
beta_boden_gemulcht_c = 0,
beta_boden_ueberall_c = 0,
beta_boden_keine_c = 0,
beta_zaun_Flaechig_c = 0,
beta_zaun_kleingatter_c = 0,
beta_zaun_Teilflaechig_c = 0,
beta_zaun_zaun_c = 0,
delta_c = 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_a", "delta_b",
"beta_totb_keine_a","beta_boden_gemulcht_a","beta_zaun_Flaechig_a",
"beta_totb_keine_b","beta_boden_gemulcht_b","beta_zaun_Flaechig_b",
"beta_totb_keine_c","beta_boden_gemulcht_c","beta_zaun_Flaechig_c"
)
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["beta_totb_keine"]] = list(beta_totb_keine_a, beta_totb_keine_b, beta_totb_keine_c)
lcpars[["beta_totb_hoheStubben"]] = list(beta_totb_hoheStubben_a, beta_totb_hoheStubben_b, beta_totb_hoheStubben_c)
lcpars[["beta_totb_gruppen"]] = list(beta_totb_gruppen_a, beta_totb_gruppen_b, beta_totb_gruppen_c)
lcpars[["beta_totb_ueberall"]] = list(beta_totb_ueberall_a, beta_totb_ueberall_b, beta_totb_ueberall_c)
lcpars[["beta_boden_gemulcht"]] = list(beta_boden_gemulcht_a, beta_boden_gemulcht_b, beta_boden_gemulcht_c)
lcpars[["beta_boden_ueberall"]] = list(beta_boden_ueberall_a, beta_boden_ueberall_b, beta_boden_ueberall_c)
lcpars[["beta_boden_keine"]] = list(beta_boden_keine_a, beta_boden_keine_b, beta_boden_keine_c)
lcpars[["beta_zaun_Flaechig"]] = list(beta_zaun_Flaechig_a, beta_zaun_Flaechig_b, beta_zaun_Flaechig_c)
lcpars[["beta_zaun_kleingatter"]] = list(beta_zaun_kleingatter_a, beta_zaun_kleingatter_b, beta_zaun_kleingatter_c)
lcpars[["beta_zaun_Teilflaechig"]] = list(beta_zaun_Teilflaechig_a, beta_zaun_Teilflaechig_b, beta_zaun_Teilflaechig_c)
lcpars[["beta_zaun_zaun"]] = list(beta_zaun_zaun_a, beta_zaun_zaun_b, beta_zaun_zaun_c)
V=list()
V[["class_a"]] = delta_a
V[["class_b"]] = delta_b
V[["class_c"]] = delta_c
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)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### 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:3){
### Compute class-specific utilities
V = list()
V[['alt1']] =
beta_totb_keine[[s]] * (c1_totb==1) + beta_totb_hoheStubben[[s]] * (c1_totb==2) + beta_totb_gruppen[[s]] * (c1_totb==3) + beta_totb_ueberall[[s]] * (c1_totb==4) +
beta_boden_gemulcht[[s]]* (c1_boden==1) + beta_boden_keine[[s]] * (c1_boden==2) + beta_boden_ueberall[[s]] * (c1_boden==3) +
beta_zaun_Flaechig[[s]] * (c1_zaun==1) + beta_zaun_kleingatter[[s]] * (c1_zaun==2) + beta_zaun_Teilflaechig[[s]] * (c1_zaun==3) + beta_zaun_zaun[[s]] * (c1_zaun==4)
V[['alt2']] =
beta_totb_keine[[s]] * (c2_totb==1) + beta_totb_hoheStubben[[s]] * (c2_totb==2) + beta_totb_gruppen[[s]] * (c2_totb==3) + beta_totb_ueberall[[s]] * (c2_totb==4) +
beta_boden_gemulcht[[s]]* (c2_boden==1) + beta_boden_keine[[s]] * (c2_boden==2) + beta_boden_ueberall[[s]] * (c2_boden==3) +
beta_zaun_Flaechig[[s]] * (c2_zaun==1) + beta_zaun_kleingatter[[s]] * (c2_zaun==2) + beta_zaun_Teilflaechig[[s]] * (c2_zaun==3) + beta_zaun_zaun[[s]] * (c2_zaun==4)
V[['alt3']] =
beta_totb_keine[[s]] * (c3_totb==1) + beta_totb_hoheStubben[[s]] * (c3_totb==2) + beta_totb_gruppen[[s]] * (c3_totb==3) + beta_totb_ueberall[[s]] * (c3_totb==4) +
beta_boden_gemulcht[[s]]* (c3_boden==1) + beta_boden_keine[[s]] * (c3_boden==2) + beta_boden_ueberall[[s]] * (c3_boden==3) +
beta_zaun_Flaechig[[s]] * (c3_zaun==1) + beta_zaun_kleingatter[[s]] * (c3_zaun==2) + beta_zaun_Teilflaechig[[s]] * (c3_zaun==3) + beta_zaun_zaun[[s]] * (c3_zaun==4)
mnl_settings$utilities = 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 ####
# ################################################################# #
### Optional starting values search
# apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model, modelOutput_settings = list(printPVal=1))Paul