Input validation code works on other PC, not mine
Posted: 28 Dec 2020, 04:43
Hello professor,
I am now utilizing "example_20" which is Latent class model.
the version of apollo is the lastest one, 0.2.2 and R version also the latest.
However, on the step of "group and validate Inputs", the code makes the error as follows;
------
Several observations per individual detected based on the value of ID. Setting panelData in apollo_control set
to TRUE.
All checks on apollo_control completed.
Error in apollo_validateData(database, apollo_control, silent = silent) :
All rows for the same individual should be next to each other in the data!
-------
I tried the code on other PCs, but some PC, it works and another it didn't.
The difference between executed one and other is, the code executed PC, the R and apollo version is older one.
I cannot find the reason why, and never seen this problem before.
The dataset and code was same between two PCs, so maybe not the problem of dataset or PC.
Here, I will also attach the code that I used.
I also made bold where it makes the error.
-----
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
apollo_control = list(
modelName ="Latent_TSP",
modelDescr ="LC model with class allocation model on iwami mode choice data",
indivID ="ID",
nCores = 3
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("iwami_final_1224.csv",header=TRUE)
colnames(database)[1] <- "ID" #change variable name error #change variable name error
# data cleaning
database <- database[database$area!=99,]
database <- database[database$gender!=99,]
database <- database[database$age!=99,]
database <- database[database$job!=99,]
database <- database[database$choice!=99,]
database$job1 <-ifelse(database$job==1,1,0) #farmer-non
database$job2 <-ifelse(database$job==6|database$job==12,1,0) #job-non
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_1 = 0,
asc_2 = 0,
beta_wt_a = 0,
beta_wt_b = 0,
beta_iv_a = 0,
beta_iv_b = 0,
beta_md_a = 0,
beta_md_b = 0,
beta_bw_a = 0,
beta_bw_b = 0,
beta_tc_a = 0,
beta_tc_b = 0,
delta_a = 0,
gamma_age_a = 0,
gamma_jobless_a = 0,
delta_b = 0,
gamma_age_b = 0,
gamma_jobless_b = 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_2", "delta_b", "gamma_age_b","gamma_jobless_b")
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["beta_wt"]] = list(beta_wt_a, beta_wt_b)
lcpars[["beta_iv"]] = list(beta_iv_a, beta_iv_b)
lcpars[["beta_md"]] = list(beta_md_a, beta_md_b)
lcpars[["beta_bw"]] = list(beta_bw_a, beta_bw_b)
lcpars[["beta_tc"]] = list(beta_tc_a, beta_tc_b)
V=list()
V[["class_a"]] = delta_a + gamma_age_a*age + gamma_jobless_a*job2
V[["class_b"]] = delta_b + gamma_age_b*age + gamma_jobless_b*job2
mnl_settings = list(
alternatives = c(class_a=1, class_b=2),
avail = 1, # all of the alternatives are available for every choice observation in the data
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 ####
# ################################################################# #
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),
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 + beta_wt[[s]]*WT1 + beta_iv[[s]]*IV1 + beta_bw[[s]]*BW1 + beta_hw[[s]]*dt1 + beta_tc[[s]]*TC1
V[['alt2']] = asc_2 + beta_wt[[s]]*WT2 + beta_iv[[s]]*IV2 + beta_bw[[s]]*BW2 + beta_hw[[s]]*dt2 + beta_tc[[s]]*TC2
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[["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 ####
# ################################################################# #
#apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
#apollo_outOfSample(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs,
estimate_settings=list(writeIter=FALSE))
### Show output in screen
apollo_modelOutput(model)
### Save output to file(s)
apollo_saveOutput(model)
-------
Thank you so much.
I am now utilizing "example_20" which is Latent class model.
the version of apollo is the lastest one, 0.2.2 and R version also the latest.
However, on the step of "group and validate Inputs", the code makes the error as follows;
------
Several observations per individual detected based on the value of ID. Setting panelData in apollo_control set
to TRUE.
All checks on apollo_control completed.
Error in apollo_validateData(database, apollo_control, silent = silent) :
All rows for the same individual should be next to each other in the data!
-------
I tried the code on other PCs, but some PC, it works and another it didn't.
The difference between executed one and other is, the code executed PC, the R and apollo version is older one.
I cannot find the reason why, and never seen this problem before.
The dataset and code was same between two PCs, so maybe not the problem of dataset or PC.
Here, I will also attach the code that I used.
I also made bold where it makes the error.
-----
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
apollo_control = list(
modelName ="Latent_TSP",
modelDescr ="LC model with class allocation model on iwami mode choice data",
indivID ="ID",
nCores = 3
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("iwami_final_1224.csv",header=TRUE)
colnames(database)[1] <- "ID" #change variable name error #change variable name error
# data cleaning
database <- database[database$area!=99,]
database <- database[database$gender!=99,]
database <- database[database$age!=99,]
database <- database[database$job!=99,]
database <- database[database$choice!=99,]
database$job1 <-ifelse(database$job==1,1,0) #farmer-non
database$job2 <-ifelse(database$job==6|database$job==12,1,0) #job-non
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_1 = 0,
asc_2 = 0,
beta_wt_a = 0,
beta_wt_b = 0,
beta_iv_a = 0,
beta_iv_b = 0,
beta_md_a = 0,
beta_md_b = 0,
beta_bw_a = 0,
beta_bw_b = 0,
beta_tc_a = 0,
beta_tc_b = 0,
delta_a = 0,
gamma_age_a = 0,
gamma_jobless_a = 0,
delta_b = 0,
gamma_age_b = 0,
gamma_jobless_b = 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_2", "delta_b", "gamma_age_b","gamma_jobless_b")
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["beta_wt"]] = list(beta_wt_a, beta_wt_b)
lcpars[["beta_iv"]] = list(beta_iv_a, beta_iv_b)
lcpars[["beta_md"]] = list(beta_md_a, beta_md_b)
lcpars[["beta_bw"]] = list(beta_bw_a, beta_bw_b)
lcpars[["beta_tc"]] = list(beta_tc_a, beta_tc_b)
V=list()
V[["class_a"]] = delta_a + gamma_age_a*age + gamma_jobless_a*job2
V[["class_b"]] = delta_b + gamma_age_b*age + gamma_jobless_b*job2
mnl_settings = list(
alternatives = c(class_a=1, class_b=2),
avail = 1, # all of the alternatives are available for every choice observation in the data
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 ####
# ################################################################# #
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),
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 + beta_wt[[s]]*WT1 + beta_iv[[s]]*IV1 + beta_bw[[s]]*BW1 + beta_hw[[s]]*dt1 + beta_tc[[s]]*TC1
V[['alt2']] = asc_2 + beta_wt[[s]]*WT2 + beta_iv[[s]]*IV2 + beta_bw[[s]]*BW2 + beta_hw[[s]]*dt2 + beta_tc[[s]]*TC2
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[["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 ####
# ################################################################# #
#apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
#apollo_outOfSample(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs,
estimate_settings=list(writeIter=FALSE))
### Show output in screen
apollo_modelOutput(model)
### Save output to file(s)
apollo_saveOutput(model)
-------
Thank you so much.