An error when estimating nonlinear utility specification
Posted: 18 Jul 2020, 13:59
Dear Stephane,
Thank you for inventing the Apollo package and answering my past questions. I have encountered an error when estimating a nonlinear utility specification using MMNL.
Description of my model: I have a health attribute (h) and a probability attribute (p) associated with the health outcome (The two attributes can vary independently in experimental design). Under an expected utility theory assumption, it is expected that the utility of the health attribute is concave, so I want to estimate the concavity of this attribute by estimating an alpha parameter, where E(U)=p*(h^(1-alpha))/(1-alpha). Therefore, the utility function for alternative i under RUM assumption is written as:
U=ASC+beta1*(p*(h^(1-alpha))/(1-alpha))+beta2*other attributes+error term
I want to estimate the alpha in order to capture the degree of nonlinear of the health attribute.
The problem: When the estimation is run using MNL, it is all fine; but an error occurs when it is estimated using MMNL. Please find below for the codes.
Thank you very much.
Best wishes,
Tim
--------------------------
codes
# ################################################################# #
#### 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 ="T2_EUgain",
modelDescr ="T2_EUgain",
indivID ="ID",
nCores = 3,
noDiagnostics = TRUE
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("R_T2.csv",header=TRUE)
##renames##
names(database)[names(database)=="id"] <- "ID"
database= subset(database, database$same_answersG==0&same_answersQ==0&same_answersQ==0)
#rescale
database$p_1_inc=database$p_1_inc/100
database$p_2_inc=database$p_2_inc/100
database$p_1_dec=database$p_1_dec/100
database$p_2_dec=database$p_2_dec/100
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_1 = 0,
asc_3 = 0,
EU_inc_mu = 0,
EU_inc_sig = 0,
h_dec_mu = 0,
h_dec_sig = 0,
p_dec_mu = 0,
p_dec_sig = 0,
v = 0,
alpha =0,
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( "asc_1")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType="mlhs",
interNDraws=500,
interUnifDraws=c(),
interNormDraws=c("draws_v", "draws_h_dec","draws_p_dec","draws_asc_3",
"draws_EU_inc"),
intraDrawsType="mlhs",
intraNDraws=0,
intraUnifDraws=c(),
intraNormDraws=c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["v"]] = v_mu + v_sig*draws_v
randcoeff[["hdec"]] = h_dec_mu + h_dec_sig*draws_h_dec
randcoeff[["pdec"]] = p_dec_mu + p_dec_sig*draws_p_dec
randcoeff[["asc_3"]] = asc_3_mu + asc_3_sig*draws_asc_3
randcoeff[["EU_inc"]] = EU_inc_mu + EU_inc_sig*draws_EU_inc
return(randcoeff)
}
# ################################################################# #
#### 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()
### Compute utilities
V=list()
V[['alt1']] = asc_1 + EU_inc*(p_1_inc*(h_1_inc^(1-alpha))/(1-alpha)) + hdec*h_1_dec + pdec*p_1_dec + v*v_1 + c*c_1
V[['alt2']] = asc_1 + EU_inc*(p_2_inc*(h_2_inc^(1-alpha))/(1-alpha)) + hdec*h_2_dec + pdec*p_2_dec + v*v_2 + c*c_2
V[['alt3']] = asc_3 + v*v_3 + c*c_3
### 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,
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 ####
# ################################################################# #
### 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 = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, estimate_settings=list(bootstrapSE=2))
#apollo_bootstrap(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
Errors
......
......
# ################################################################# #
> #### 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)
Validating inputs of likelihood function (apollo_probabilities)
Error in apollo_probabilities(apollo_beta, apollo_inputs, functionality = "validate") :
object 'EU_inc' not found
Thank you for inventing the Apollo package and answering my past questions. I have encountered an error when estimating a nonlinear utility specification using MMNL.
Description of my model: I have a health attribute (h) and a probability attribute (p) associated with the health outcome (The two attributes can vary independently in experimental design). Under an expected utility theory assumption, it is expected that the utility of the health attribute is concave, so I want to estimate the concavity of this attribute by estimating an alpha parameter, where E(U)=p*(h^(1-alpha))/(1-alpha). Therefore, the utility function for alternative i under RUM assumption is written as:
U=ASC+beta1*(p*(h^(1-alpha))/(1-alpha))+beta2*other attributes+error term
I want to estimate the alpha in order to capture the degree of nonlinear of the health attribute.
The problem: When the estimation is run using MNL, it is all fine; but an error occurs when it is estimated using MMNL. Please find below for the codes.
Thank you very much.
Best wishes,
Tim
--------------------------
codes
# ################################################################# #
#### 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 ="T2_EUgain",
modelDescr ="T2_EUgain",
indivID ="ID",
nCores = 3,
noDiagnostics = TRUE
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("R_T2.csv",header=TRUE)
##renames##
names(database)[names(database)=="id"] <- "ID"
database= subset(database, database$same_answersG==0&same_answersQ==0&same_answersQ==0)
#rescale
database$p_1_inc=database$p_1_inc/100
database$p_2_inc=database$p_2_inc/100
database$p_1_dec=database$p_1_dec/100
database$p_2_dec=database$p_2_dec/100
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_1 = 0,
asc_3 = 0,
EU_inc_mu = 0,
EU_inc_sig = 0,
h_dec_mu = 0,
h_dec_sig = 0,
p_dec_mu = 0,
p_dec_sig = 0,
v = 0,
alpha =0,
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( "asc_1")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType="mlhs",
interNDraws=500,
interUnifDraws=c(),
interNormDraws=c("draws_v", "draws_h_dec","draws_p_dec","draws_asc_3",
"draws_EU_inc"),
intraDrawsType="mlhs",
intraNDraws=0,
intraUnifDraws=c(),
intraNormDraws=c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["v"]] = v_mu + v_sig*draws_v
randcoeff[["hdec"]] = h_dec_mu + h_dec_sig*draws_h_dec
randcoeff[["pdec"]] = p_dec_mu + p_dec_sig*draws_p_dec
randcoeff[["asc_3"]] = asc_3_mu + asc_3_sig*draws_asc_3
randcoeff[["EU_inc"]] = EU_inc_mu + EU_inc_sig*draws_EU_inc
return(randcoeff)
}
# ################################################################# #
#### 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()
### Compute utilities
V=list()
V[['alt1']] = asc_1 + EU_inc*(p_1_inc*(h_1_inc^(1-alpha))/(1-alpha)) + hdec*h_1_dec + pdec*p_1_dec + v*v_1 + c*c_1
V[['alt2']] = asc_1 + EU_inc*(p_2_inc*(h_2_inc^(1-alpha))/(1-alpha)) + hdec*h_2_dec + pdec*p_2_dec + v*v_2 + c*c_2
V[['alt3']] = asc_3 + v*v_3 + c*c_3
### 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,
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 ####
# ################################################################# #
### 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 = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, estimate_settings=list(bootstrapSE=2))
#apollo_bootstrap(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
Errors
......
......
# ################################################################# #
> #### 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)
Validating inputs of likelihood function (apollo_probabilities)
Error in apollo_probabilities(apollo_beta, apollo_inputs, functionality = "validate") :
object 'EU_inc' not found