Error in apollo_estimate when using MDCNEV
Posted: 16 Sep 2022, 16:55
Dear all,
When using MDCNEV model, I run into this message:
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Preparing user-defined functions.
Error in mdcnev_settings$probs_MDCNEV(mdcnev_settings) :
object 'tmp' not found
Could you please help me with checking what the problem is? Please feel free to let me know if more information is needed.
Thank you in advance!
Best Regards,
Lydia
My original code is as follows:
# ################################################################# #
#### 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 = "Apollo_MDCNEV_base",
modelDescr = "MDCNEV model on time use data, alpha-gamma profile with outside good and socio-demographics",
indivID = "Q144",
nCores = 4,
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)
library(haven)
database <- read_dta("MDCEV_2112.dta")
### for data dictionary, use ?apollo_timeUseData
### Create consumption variables for combined activities
database$t_outside = rowSums(database[,c("other")]) # outside good: time spent at home and travelling
database$t_leisure = rowSums(database[,c("nrfund", "rfund")])
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base = 0,
gamma_nrfund = 1,
gamma_rfund = 1,
delta_nrfund = 0,
delta_rfund = 0,
theta_mandatory = 1,
theta_optional = 1)
### 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("theta_mandatory")
### Read in starting values for at least some parameters from existing model output file
apollo_beta=apollo_readBeta(apollo_beta,apollo_fixed,"Apollo_PensionInvestment_base",overwriteFixed=FALSE)
# ################################################################# #
#### 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 individual alternatives
alternatives = c("outside",
"nrfund",
"rfund")
### Define availabilities
avail = list(outside = 1,
nrfund = 1,
rfund = 1)
### Define continuous consumption for individual alternatives
continuousChoice = list(outside = other,
nrfund = nrfund,
rfund = rfund)
### Define utilities for individual alternatives
V = list()
V[["outside"]] = 0
V[["nrfund"]] = delta_nrfund
V[["rfund"]] = delta_rfund
### Define alpha parameters
alpha = list(outside = 1 /(1 + exp(-alpha_base)),
nrfund = 1 /(1 + exp(-alpha_base)),
rfund = 1 /(1 + exp(-alpha_base)))
### Define gamma parameters
gamma = list(nrfund = gamma_nrfund,
rfund = gamma_rfund)
### Define nesting structure
mdcnevNests = list(mandatory = theta_mandatory,
optional = theta_optional)
mdcnevStructure = matrix(0, nrow=length(mdcnevNests), ncol=length(V))
### outside work school shopping private leisure
mdcnevStructure[1,] = c( 0, 1, 1) # mandatory
mdcnevStructure[2,] = c( 1, 0, 0) # optional
### Define costs for individual alternatives
cost = list(outside = 1,
nrfund = 1,
rfund = 1)
### Define budget
budget = nrfund + rfund + other
### Define settings for MDCNEV model
mdcnev_settings <- list(alternatives = alternatives,
avail = avail,
continuousChoice = continuousChoice,
utilities = V,
alpha = alpha,
gamma = gamma,
cost = cost,
budget = budget,
mdcnevNests = mdcnevNests,
mdcnevStructure = mdcnevStructure)
### Compute probabilities using MDCNEV model
P[["model"]] = apollo_mdcnev(mdcnev_settings, functionality)
### Take product across observation for same individual
P = apollo_panelProd(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)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
# ################################################################# #
##### POST-PROCESSING ####
# ################################################################# #
### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()
# ----------------------------------------------------------------- #
#---- MODEL PREDICTIONS ----
# ----------------------------------------------------------------- #
### Use the estimated model to make predictions
predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs)
colMeans(predictions_base)
# ----------------------------------------------------------------- #
#---- switch off writing to file ----
# ----------------------------------------------------------------- #
apollo_sink()
When using MDCNEV model, I run into this message:
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Preparing user-defined functions.
Error in mdcnev_settings$probs_MDCNEV(mdcnev_settings) :
object 'tmp' not found
Could you please help me with checking what the problem is? Please feel free to let me know if more information is needed.
Thank you in advance!
Best Regards,
Lydia
My original code is as follows:
# ################################################################# #
#### 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 = "Apollo_MDCNEV_base",
modelDescr = "MDCNEV model on time use data, alpha-gamma profile with outside good and socio-demographics",
indivID = "Q144",
nCores = 4,
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)
library(haven)
database <- read_dta("MDCEV_2112.dta")
### for data dictionary, use ?apollo_timeUseData
### Create consumption variables for combined activities
database$t_outside = rowSums(database[,c("other")]) # outside good: time spent at home and travelling
database$t_leisure = rowSums(database[,c("nrfund", "rfund")])
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base = 0,
gamma_nrfund = 1,
gamma_rfund = 1,
delta_nrfund = 0,
delta_rfund = 0,
theta_mandatory = 1,
theta_optional = 1)
### 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("theta_mandatory")
### Read in starting values for at least some parameters from existing model output file
apollo_beta=apollo_readBeta(apollo_beta,apollo_fixed,"Apollo_PensionInvestment_base",overwriteFixed=FALSE)
# ################################################################# #
#### 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 individual alternatives
alternatives = c("outside",
"nrfund",
"rfund")
### Define availabilities
avail = list(outside = 1,
nrfund = 1,
rfund = 1)
### Define continuous consumption for individual alternatives
continuousChoice = list(outside = other,
nrfund = nrfund,
rfund = rfund)
### Define utilities for individual alternatives
V = list()
V[["outside"]] = 0
V[["nrfund"]] = delta_nrfund
V[["rfund"]] = delta_rfund
### Define alpha parameters
alpha = list(outside = 1 /(1 + exp(-alpha_base)),
nrfund = 1 /(1 + exp(-alpha_base)),
rfund = 1 /(1 + exp(-alpha_base)))
### Define gamma parameters
gamma = list(nrfund = gamma_nrfund,
rfund = gamma_rfund)
### Define nesting structure
mdcnevNests = list(mandatory = theta_mandatory,
optional = theta_optional)
mdcnevStructure = matrix(0, nrow=length(mdcnevNests), ncol=length(V))
### outside work school shopping private leisure
mdcnevStructure[1,] = c( 0, 1, 1) # mandatory
mdcnevStructure[2,] = c( 1, 0, 0) # optional
### Define costs for individual alternatives
cost = list(outside = 1,
nrfund = 1,
rfund = 1)
### Define budget
budget = nrfund + rfund + other
### Define settings for MDCNEV model
mdcnev_settings <- list(alternatives = alternatives,
avail = avail,
continuousChoice = continuousChoice,
utilities = V,
alpha = alpha,
gamma = gamma,
cost = cost,
budget = budget,
mdcnevNests = mdcnevNests,
mdcnevStructure = mdcnevStructure)
### Compute probabilities using MDCNEV model
P[["model"]] = apollo_mdcnev(mdcnev_settings, functionality)
### Take product across observation for same individual
P = apollo_panelProd(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)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
# ################################################################# #
##### POST-PROCESSING ####
# ################################################################# #
### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()
# ----------------------------------------------------------------- #
#---- MODEL PREDICTIONS ----
# ----------------------------------------------------------------- #
### Use the estimated model to make predictions
predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs)
colMeans(predictions_base)
# ----------------------------------------------------------------- #
#---- switch off writing to file ----
# ----------------------------------------------------------------- #
apollo_sink()