#My estimation reports an error message that "Error in v[r] : object of type 'closure' is not subsettable".
#please find the attached dataset for your reference
# Thanks for your help in advance.
#Here is my entire code
Code: Select all
rm(list = ls())
library(apollo)
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="redtide_uncorrelated",
modelDescr ="Mixed logit model, uncorrelated Lognormals in utility space",
indivID ="id",
mixing = TRUE,
nCores = 4
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
library(readxl)
database<-read.csv("D:\\cleaned_data.csv")
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(mu_log_b_cov =-3,
sigma_log_b_cov = 0,
mu_log_b_acc1 =-3,
sigma_log_b_acc1 = 0,
mu_log_b_acc2 =-3,
sigma_log_b_acc2 = 0,
mu_log_b_bid =-3,
sigma_log_b_bid = 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_cov","draws_acc1","draws_acc2","draws_bid"),
intraDrawsType = "halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_cov"]] = -exp( mu_log_b_cov + sigma_log_b_cov * draws_cov )
randcoeff[["b_acc1"]] = -exp( mu_log_b_acc1 + sigma_log_b_acc1 * draws_acc1 )
randcoeff[["b_acc2"]] = -exp( mu_log_b_acc2 + sigma_log_b_acc2 * draws_acc2 )
randcoeff[["b_bid"]] = -exp( mu_log_b_bid + sigma_log_b_bid * draws_bid )
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[["alt1"]] = b_cov * cov_1 + b_acc1 * acc1_1 + b_acc2 * acc2_1 + b_bid * bid_1
V[["alt2"]] = b_cov * cov_2 + b_acc1 * acc1_2 + b_acc2 * acc2_2 + b_bid * bid_2
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1,alt2=2),
avail = list(alt1=1, alt2=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)