Problem of Hessian matrix and missing s.e
Posted: 26 Aug 2020, 09:50
Hi there,
My mixed logit model with price following lognormal distribution failed at starting values. After trying the command "WorkInLogs", it worked with successful convergence but the s.e and t ratio are missing in the output. It warns me that "Some eigenvalues of hessian are positive, indicating potential problems!"
I attach my codes as following. Please help!
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="0825mixed logit(3)",
modelDescr ="Mixed logit model,ttp lognormal",
indivID ="id",
workInLogs = TRUE,
mixing = TRUE,
nCores = 4
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("C:/Users/Administrator/Desktop/zhangxian/0728main_balanced_pool.csv",header=TRUE)
database$ttp_1 <- database$ttp_1 * (-1)
database$ttp_2 <- database$ttp_2 * (-1)
database$ttp_3 <- database$ttp_3 * (-1)
database$ttp_4 <- database$ttp_4 * (-1)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_non_purchase = 0,
mu_b_ttp = -3,
sigma_b_ttp = 0,
mu_b_deal = 0,
sigma_b_deal = 0,
mu_b_star = 0,
sigma_b_star = 0,
mu_b_ave_safe = 0,
sigma_b_ave_safe = 0,
mu_b_very_safe = 0,
sigma_b_very_safe= 0,
b_wellknown = 0,
b_ave_loc = 0,
b_very_loc = 0,
b_ave_clean = 0,
mu_b_very_clean = 0,
sigma_b_very_clean=0,
mu_b_quiet_night = 0,
sigma_b_quiet_night=0,
mu_b_quiet_allday = 0,
sigma_b_quiet_allday=0,
mu_b_rev = 0,
sigma_b_rev = 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()
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 500,
interUnifDraws = c(),
interNormDraws = c("draws_ttp", "draws_deal", "draws_star", "draws_ave_safe", "draws_very_safe", "draws_very_loc", "draws_very_clean", "draws_quiet_night", "draws_quiet_allday", "draws_rev"),
intraDrawsType = "halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_ttp"]] = exp(mu_b_ttp + sigma_b_ttp * draws_ttp)
randcoeff[["b_deal"]] = mu_b_deal + sigma_b_deal * draws_deal
randcoeff[["b_star"]] = mu_b_star + sigma_b_star * draws_star
randcoeff[["b_ave_safe"]] = mu_b_ave_safe + sigma_b_ave_safe * draws_ave_safe
randcoeff[["b_very_safe"]] = mu_b_very_safe + sigma_b_very_safe * draws_very_safe
randcoeff[["b_very_clean"]] = mu_b_very_clean + sigma_b_very_clean * draws_very_clean
randcoeff[["b_quiet_night"]] = mu_b_quiet_night + sigma_b_quiet_night * draws_quiet_night
randcoeff[["b_quiet_allday"]] = mu_b_quiet_allday + sigma_b_quiet_allday * draws_quiet_allday
randcoeff[["b_rev"]] = mu_b_rev + sigma_b_rev * draws_rev
return(randcoeff)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
### Function initialisation: do not change the following three commands
### 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()
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['option_1']] = b_ttp * ttp_1 + b_deal * free_1 + b_star * star_1 + b_ave_safe * (safe_1==1) + b_very_safe * (safe_1==2) + b_wellknown * wellknown_1 + b_ave_loc * (loc_1==1) + b_very_loc * (loc_1==2) + b_ave_clean * (clean_1==1) + b_very_clean * (clean_1==2) + b_quiet_night * (noise_1==1) + b_quiet_allday * (noise_1==2) + b_rev * rev_1
V[['option_2']] = b_ttp * ttp_2 + b_deal * free_2 + b_star * star_2 + b_ave_safe * (safe_2==1) + b_very_safe * (safe_2==2) + b_wellknown * wellknown_2 + b_ave_loc * (loc_2==1) + b_very_loc * (loc_2==2) + b_ave_clean * (clean_2==1) + b_very_clean * (clean_2==2) + b_quiet_night * (noise_2==1) + b_quiet_allday * (noise_2==2) + b_rev * rev_2
V[['option_3']] = b_ttp * ttp_3 + b_deal * free_3 + b_star * star_3 + b_ave_safe * (safe_3==1) + b_very_safe * (safe_3==2) + b_wellknown * wellknown_3 + b_ave_loc * (loc_3==1) + b_very_loc * (loc_3==2) + b_ave_clean * (clean_3==1) + b_very_clean * (clean_3==2) + b_quiet_night * (noise_3==1) + b_quiet_allday * (noise_3==2) + b_rev * rev_3
V[['non-purchase']] = asc_non_purchase
### Define settings for MNL model component
mnl_settings = list(
alternatives = c('option_1'=1, 'option_2'=2, 'option_3'=3, 'non-purchase'=4),
avail = 1,
choiceVar = choice,
V = V
)
### Compute probabilities using MNL model
P[['model']] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P = apollo_panelProd(P, apollo_inputs, functionality)
### Average across inter-individual draws
P = apollo_avgInterDraws(P, 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, estimate_settings=list(hessianRoutine="maxLik"))
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
Best, Xian
My mixed logit model with price following lognormal distribution failed at starting values. After trying the command "WorkInLogs", it worked with successful convergence but the s.e and t ratio are missing in the output. It warns me that "Some eigenvalues of hessian are positive, indicating potential problems!"
I attach my codes as following. Please help!
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="0825mixed logit(3)",
modelDescr ="Mixed logit model,ttp lognormal",
indivID ="id",
workInLogs = TRUE,
mixing = TRUE,
nCores = 4
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("C:/Users/Administrator/Desktop/zhangxian/0728main_balanced_pool.csv",header=TRUE)
database$ttp_1 <- database$ttp_1 * (-1)
database$ttp_2 <- database$ttp_2 * (-1)
database$ttp_3 <- database$ttp_3 * (-1)
database$ttp_4 <- database$ttp_4 * (-1)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_non_purchase = 0,
mu_b_ttp = -3,
sigma_b_ttp = 0,
mu_b_deal = 0,
sigma_b_deal = 0,
mu_b_star = 0,
sigma_b_star = 0,
mu_b_ave_safe = 0,
sigma_b_ave_safe = 0,
mu_b_very_safe = 0,
sigma_b_very_safe= 0,
b_wellknown = 0,
b_ave_loc = 0,
b_very_loc = 0,
b_ave_clean = 0,
mu_b_very_clean = 0,
sigma_b_very_clean=0,
mu_b_quiet_night = 0,
sigma_b_quiet_night=0,
mu_b_quiet_allday = 0,
sigma_b_quiet_allday=0,
mu_b_rev = 0,
sigma_b_rev = 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()
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 500,
interUnifDraws = c(),
interNormDraws = c("draws_ttp", "draws_deal", "draws_star", "draws_ave_safe", "draws_very_safe", "draws_very_loc", "draws_very_clean", "draws_quiet_night", "draws_quiet_allday", "draws_rev"),
intraDrawsType = "halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_ttp"]] = exp(mu_b_ttp + sigma_b_ttp * draws_ttp)
randcoeff[["b_deal"]] = mu_b_deal + sigma_b_deal * draws_deal
randcoeff[["b_star"]] = mu_b_star + sigma_b_star * draws_star
randcoeff[["b_ave_safe"]] = mu_b_ave_safe + sigma_b_ave_safe * draws_ave_safe
randcoeff[["b_very_safe"]] = mu_b_very_safe + sigma_b_very_safe * draws_very_safe
randcoeff[["b_very_clean"]] = mu_b_very_clean + sigma_b_very_clean * draws_very_clean
randcoeff[["b_quiet_night"]] = mu_b_quiet_night + sigma_b_quiet_night * draws_quiet_night
randcoeff[["b_quiet_allday"]] = mu_b_quiet_allday + sigma_b_quiet_allday * draws_quiet_allday
randcoeff[["b_rev"]] = mu_b_rev + sigma_b_rev * draws_rev
return(randcoeff)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
### Function initialisation: do not change the following three commands
### 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()
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['option_1']] = b_ttp * ttp_1 + b_deal * free_1 + b_star * star_1 + b_ave_safe * (safe_1==1) + b_very_safe * (safe_1==2) + b_wellknown * wellknown_1 + b_ave_loc * (loc_1==1) + b_very_loc * (loc_1==2) + b_ave_clean * (clean_1==1) + b_very_clean * (clean_1==2) + b_quiet_night * (noise_1==1) + b_quiet_allday * (noise_1==2) + b_rev * rev_1
V[['option_2']] = b_ttp * ttp_2 + b_deal * free_2 + b_star * star_2 + b_ave_safe * (safe_2==1) + b_very_safe * (safe_2==2) + b_wellknown * wellknown_2 + b_ave_loc * (loc_2==1) + b_very_loc * (loc_2==2) + b_ave_clean * (clean_2==1) + b_very_clean * (clean_2==2) + b_quiet_night * (noise_2==1) + b_quiet_allday * (noise_2==2) + b_rev * rev_2
V[['option_3']] = b_ttp * ttp_3 + b_deal * free_3 + b_star * star_3 + b_ave_safe * (safe_3==1) + b_very_safe * (safe_3==2) + b_wellknown * wellknown_3 + b_ave_loc * (loc_3==1) + b_very_loc * (loc_3==2) + b_ave_clean * (clean_3==1) + b_very_clean * (clean_3==2) + b_quiet_night * (noise_3==1) + b_quiet_allday * (noise_3==2) + b_rev * rev_3
V[['non-purchase']] = asc_non_purchase
### Define settings for MNL model component
mnl_settings = list(
alternatives = c('option_1'=1, 'option_2'=2, 'option_3'=3, 'non-purchase'=4),
avail = 1,
choiceVar = choice,
V = V
)
### Compute probabilities using MNL model
P[['model']] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P = apollo_panelProd(P, apollo_inputs, functionality)
### Average across inter-individual draws
P = apollo_avgInterDraws(P, 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, estimate_settings=list(hessianRoutine="maxLik"))
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
Best, Xian