Thank you for the fast response. In the following, as requested the code of the two MNL models.
Ordinal:
Code: Select all
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
library(alr4) # for deltaMethod
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "flexQgrid_MNL_OL1Ordinal",
modelDescr = "MNL model in utility space (classical estimation)",
indivID ="sys_RespNum",
nCores = 3, # parallel :: detectCores()
panelData = TRUE,
mixing = FALSE
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
input.file <- "./input/CBC_results_merged.wide.rds"
database <- readRDS(input.file)
### Dual response
# database[which(database$CBC_type == "RD" & database$Choice <6), "Choice"] <- 0
# database[which(database$CBC_type == "RD" & database$Choice >=6), "Choice"] <- 1
# database$Choice[database$CBC_type=="RD"] <- scale(database$Choice[database$CBC_type=="RD"], center=TRUE, scale=FALSE)
### Dummy for first row of a respondent
database[, "sys_RespNum.first"] <- rep(0, times=nrow(database))
database[match( unique(database$sys_RespNum), database$sys_RespNum), "sys_RespNum.first"] <- 1
### Mean centering of indicators
NonAttendance_colIndices <- grep("NonAttendance", colnames(database))
database[,NonAttendance_colIndices] <- apply(database[,NonAttendance_colIndices], 2, scale, center=TRUE, scale=FALSE)
### Rescale data
# old <- c(1,2,3,4,5)
# new <- c(30,60,90,120,150)
#
# database$Quota_Compensation.1[database$Quota_Compensation.1 %in% old] <- new[match(database$Quota_Compensation.1, old, nomatch = 0)]
# database$Quota_Compensation.2[database$Quota_Compensation.2 %in% old] <- new[match(database$Quota_Compensation.2, old, nomatch = 0)]
# database$Quota_Compensation.3[database$Quota_Compensation.3 %in% old] <- new[match(database$Quota_Compensation.3, old, nomatch = 0)]
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
## Dummy Coding + Constants
apollo_beta = c(
# ----------------------------------------------------------------- #
#---- Scaling parameter
# ----------------------------------------------------------------- #
mu_CBC = 0,
mu_DR = 0,
# ----------------------------------------------------------------- #
#---- Alternative specific constants (ASC)
# ----------------------------------------------------------------- #
b_asc_1 = 0,
b_asc_2 = 0,
b_asc_3 = 0,
# ----------------------------------------------------------------- #
#---- DR_asc_0
# ----------------------------------------------------------------- #
# ----------------------------------------------------------------- #
#---- DR_asc_1
# ----------------------------------------------------------------- #
### Ordered measurement model
tau_DR_asc_1_1 = -3,
tau_DR_asc_1_2 = -2,
tau_DR_asc_1_3 = -1,
tau_DR_asc_1_4 = 1,
tau_DR_asc_1_5 = 2,
tau_DR_asc_1_6 = 3,
# b_DR_asc_1 = 0,
### Sociodemographics
b_Gender_DR_asc_1 = 0,
b_Age_DR_asc_1 = 0,
b_Education_DR_asc_1 = 0,
b_Income_DR_asc_1 = 0,
### Current conditions
b_ApartmentOwner_DR_asc_1 = 0,
b_ExistingApps_DR_asc_1 = 0,
b_CurrentMix_DR_asc_1 = 0,
# ----------------------------------------------------------------- #
#---- Quota_Count
# ----------------------------------------------------------------- #
b_Quota_Count = 0,
b_Gender_Quota_Count = 0,
b_Age_Quota_Count = 0,
b_Education_Quota_Count = 0,
b_ExistingApps_Quota_Count = 0,
# ----------------------------------------------------------------- #
#---- Quota_Max_Duration
# ----------------------------------------------------------------- #
b_Quota_Max_Duration = 0,
b_Gender_Quota_Duration = 0,
b_Age_Quota_Duration = 0,
b_Education_Quota_Duration = 0,
b_ExistingApps_Quota_Duration = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_morning
# ----------------------------------------------------------------- #
b_Quota_Period_morning = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_midmorning
# ----------------------------------------------------------------- #
b_Quota_Period_midmorning = 0,
b_Gender_Quota_Period_midmorning = 0,
b_Age_Quota_Period_midmorning = 0,
b_Education_Quota_Period_midmorning = 0,
b_ExistingApps_Quota_Period_midmorning = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_afternoon
# ----------------------------------------------------------------- #
b_Quota_Period_afternoon = 0,
b_Gender_Quota_Period_afternoon = 0,
b_Age_Quota_Period_afternoon = 0,
b_Education_Quota_Period_afternoon = 0,
b_ExistingApps_Quota_Period_afternoon = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_evening
# ----------------------------------------------------------------- #
b_Quota_Period_evening = 0,
b_Gender_Quota_Period_evening = 0,
b_Age_Quota_Period_evening = 0,
b_Education_Quota_Period_evening = 0,
b_ExistingApps_Quota_Period_evening = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_night
# ----------------------------------------------------------------- #
b_Quota_Period_night = 0,
b_Gender_Quota_Period_night = 0,
b_Age_Quota_Period_night = 0,
b_Education_Quota_Period_night = 0,
b_ExistingApps_Quota_Period_night = 0,
# ----------------------------------------------------------------- #
#---- Quota_Compensation
# ----------------------------------------------------------------- #
b_Quota_Compensation = -0.1,
b_Gender_Quota_Compensation = 0,
b_Age_Quota_Compensation = 0,
b_Education_Quota_Compensation = 0,
b_Income_Quota_Compensation = 0,
b_CheapTalk_Quota_Compensation = 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("b_asc_3",
"b_Quota_Period_morning",
"mu_CBC")
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities.functionality = "estimate"
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality=apollo_probabilities.functionality) {
### 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()
# ----------------------------------------------------------------- #
#---- Scaling parameters
# ----------------------------------------------------------------- #
# ----------------------------------------------------------------- #
#---- Alternative specific constants (ASC)
# ----------------------------------------------------------------- #
b_asc_1_value = b_asc_1
b_asc_2_value = b_asc_2
b_asc_3_value = b_asc_3
# ----------------------------------------------------------------- #
#---- DR_asc_0
# ----------------------------------------------------------------- #
# ----------------------------------------------------------------- #
#---- DR_asc_1
# ----------------------------------------------------------------- #
b_DR_asc_1_value =
### Sociodemographics
b_Gender_DR_asc_1 * COV_Gender +
b_Age_DR_asc_1 * COV_Age_centered +
b_Education_DR_asc_1 * COV_Education_centered +
(1-IncomeQuan_miss) * b_Income_DR_asc_1 * COV_IncomeClasses_centered +
### Current conditions
b_ApartmentOwner_DR_asc_1 * COV_ApartmentOwner +
b_ExistingApps_DR_asc_1 * COV_ExistingApps +
b_CurrentMix_DR_asc_1 * COV_CurrentMix
# ----------------------------------------------------------------- #
#---- Quota_Count
# ----------------------------------------------------------------- #
b_Quota_Count_value = b_Quota_Count +
b_Gender_Quota_Count * COV_Gender +
b_Age_Quota_Count * COV_Age_centered +
b_Education_Quota_Count * COV_Education_centered +
b_ExistingApps_Quota_Count * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Max_Duration
# ----------------------------------------------------------------- #
b_Quota_Max_Duration_value = b_Quota_Max_Duration +
b_Gender_Quota_Duration * COV_Gender +
b_Age_Quota_Duration * COV_Age_centered +
b_Education_Quota_Duration * COV_Education_centered +
b_ExistingApps_Quota_Duration * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Period_morning
# ----------------------------------------------------------------- #
b_Quota_Period_morning_value = b_Quota_Period_morning
# ----------------------------------------------------------------- #
#---- Quota_Period_midmorning
# ----------------------------------------------------------------- #
b_Quota_Period_midmorning_value = b_Quota_Period_midmorning +
b_Gender_Quota_Period_midmorning * COV_Gender +
b_Age_Quota_Period_midmorning * COV_Age_centered +
b_Education_Quota_Period_midmorning * COV_Education_centered +
b_ExistingApps_Quota_Period_midmorning * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Period_afternoon
# ----------------------------------------------------------------- #
b_Quota_Period_afternoon_value = b_Quota_Period_afternoon +
b_Gender_Quota_Period_afternoon * COV_Gender +
b_Age_Quota_Period_afternoon * COV_Age_centered +
b_Education_Quota_Period_afternoon * COV_Education_centered +
b_ExistingApps_Quota_Period_afternoon * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Period_evening
# ----------------------------------------------------------------- #
b_Quota_Period_evening_value = b_Quota_Period_evening +
b_Gender_Quota_Period_evening * COV_Gender +
b_Age_Quota_Period_evening * COV_Age_centered +
b_Education_Quota_Period_evening * COV_Education_centered +
b_ExistingApps_Quota_Period_evening * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Period_night
# ----------------------------------------------------------------- #
b_Quota_Period_night_value = b_Quota_Period_night +
b_Gender_Quota_Period_night * COV_Gender +
b_Age_Quota_Period_night * COV_Age_centered +
b_Education_Quota_Period_night * COV_Education_centered +
b_ExistingApps_Quota_Period_night * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Compensation
# ----------------------------------------------------------------- #
b_Quota_Compensation_value = b_Quota_Compensation +
### Sociodemographics
b_Gender_Quota_Compensation * COV_Gender +
b_Age_Quota_Compensation * COV_Age_centered +
b_Education_Quota_Compensation * COV_Education_centered +
(1-IncomeQuan_miss) * b_Income_Quota_Compensation * COV_IncomeClasses_centered +
b_CheapTalk_Quota_Compensation * COV_CheapTalk
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
### CBC (Forced Choices)
V[['alt1']] = b_asc_1_value +
b_Quota_Count_value * Quota_Count.1 +
b_Quota_Max_Duration_value * Quota_Max_Duration.1 +
b_Quota_Period_morning_value * 0 + b_Quota_Period_midmorning_value * Quota_Period2.1 + b_Quota_Period_afternoon_value * Quota_Period3.1 + b_Quota_Period_evening_value * Quota_Period4.1 + b_Quota_Period_night_value * Quota_Period5.1 +
b_Quota_Compensation_value * Quota_Compensation.1
V[['alt2']] = b_asc_2_value +
b_Quota_Count_value * Quota_Count.2 +
b_Quota_Max_Duration_value * Quota_Max_Duration.2 +
b_Quota_Period_morning_value * 0 + b_Quota_Period_midmorning_value * Quota_Period2.2 + b_Quota_Period_afternoon_value * Quota_Period3.2 + b_Quota_Period_evening_value * Quota_Period4.2 + b_Quota_Period_night_value * Quota_Period5.2 +
b_Quota_Compensation_value * Quota_Compensation.2
V[['alt3']] = b_asc_3_value +
b_Quota_Count_value * Quota_Count.3 +
b_Quota_Max_Duration_value * Quota_Max_Duration.3 +
b_Quota_Period_morning_value * 0 + b_Quota_Period_midmorning_value * Quota_Period2.3 + b_Quota_Period_afternoon_value * Quota_Period3.3 + b_Quota_Period_evening_value * Quota_Period4.3 + b_Quota_Period_night_value * Quota_Period5.3 +
b_Quota_Compensation_value * Quota_Compensation.3
# Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3),
avail = list(alt1=1, alt2=1, alt3=1),
choiceVar = Choice,
V = lapply(V, "*", (1+mu_CBC)),
rows = (CBC_type=="R"),
componentName = "CBC"
)
# Compute probabilities using MNL model
P[['CBC']] = apollo_mnl(mnl_settings, functionality)
### DR (Free Choices)
ol_settings1 = list(outcomeOrdered=Choice,
V = (1+mu_DR) * (b_Quota_Count_value * Quota_Count.1 +
b_Quota_Max_Duration_value * Quota_Max_Duration.1 +
b_Quota_Period_morning_value * 0 + b_Quota_Period_midmorning_value * Quota_Period2.1 + b_Quota_Period_afternoon_value * Quota_Period3.1 + b_Quota_Period_evening_value * Quota_Period4.1 + b_Quota_Period_night_value * Quota_Period5.1 +
b_Quota_Compensation_value * Quota_Compensation.1 +
b_DR_asc_1_value),
tau=list(tau_DR_asc_1_1, tau_DR_asc_1_2, tau_DR_asc_1_3, tau_DR_asc_1_4, tau_DR_asc_1_5, tau_DR_asc_1_6),
rows=(CBC_type=="RD"),
componentName = "OL_DualResponse")
P[["OL_DualResponse"]] = apollo_ol(ol_settings1, functionality)
### Combined model
P = apollo_combineModels(P, apollo_inputs, 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)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
saveOutput_settings = list(
printClassical = TRUE,
printPVal = TRUE,
printDiagnostics = TRUE,
printCovar = TRUE,
printCorr = TRUE,
printOutliers = 50,
printChange = TRUE,
saveEst = TRUE,
saveCov = TRUE,
saveCorr = TRUE,
saveModelObject = TRUE
)
apollo_saveOutput(model, saveOutput_settings)
Linear:
Code: Select all
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
library(alr4) # for deltaMethod
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "flexQgrid_MNL_OL1",
modelDescr = "MNL model in utility space (classical estimation)",
indivID ="sys_RespNum",
nCores = 1, # parallel :: detectCores()
panelData = TRUE,
mixing = FALSE
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
input.file <- "./input/CBC_results_merged.wide.rds"
database <- readRDS(input.file)
### Dual response
# database[which(database$CBC_type == "RD" & database$Choice <6), "Choice"] <- 0
# database[which(database$CBC_type == "RD" & database$Choice >=6), "Choice"] <- 1
database$Choice[database$CBC_type=="RD"] <- scale(database$Choice[database$CBC_type=="RD"], center=TRUE, scale=FALSE)
### Dummy for first row of a respondent
database[, "sys_RespNum.first"] <- rep(0, times=nrow(database))
database[match( unique(database$sys_RespNum), database$sys_RespNum), "sys_RespNum.first"] <- 1
### Mean centering of indicators
NonAttendance_colIndices <- grep("NonAttendance", colnames(database))
database[,NonAttendance_colIndices] <- apply(database[,NonAttendance_colIndices], 2, scale, center=TRUE, scale=FALSE)
### Rescale data
# old <- c(1,2,3,4,5)
# new <- c(30,60,90,120,150)
#
# database$Quota_Compensation.1[database$Quota_Compensation.1 %in% old] <- new[match(database$Quota_Compensation.1, old, nomatch = 0)]
# database$Quota_Compensation.2[database$Quota_Compensation.2 %in% old] <- new[match(database$Quota_Compensation.2, old, nomatch = 0)]
# database$Quota_Compensation.3[database$Quota_Compensation.3 %in% old] <- new[match(database$Quota_Compensation.3, old, nomatch = 0)]
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
## Dummy Coding + Constants
apollo_beta = c(
# ----------------------------------------------------------------- #
#---- Scaling parameter
# ----------------------------------------------------------------- #
mu_CBC = 0,
mu_DR = 0,
# ----------------------------------------------------------------- #
#---- Alternative specific constants (ASC)
# ----------------------------------------------------------------- #
b_asc_1 = 0,
b_asc_2 = 0,
b_asc_3 = 0,
# ----------------------------------------------------------------- #
#---- DR_asc_0
# ----------------------------------------------------------------- #
# ----------------------------------------------------------------- #
#---- DR_asc_1
# ----------------------------------------------------------------- #
b_DR_asc_1 = 0,
### Continuous measurement model
sigma_DR_asc_1 = 1,
### Sociodemographics
b_Gender_DR_asc_1 = 0,
b_Age_DR_asc_1 = 0,
b_Education_DR_asc_1 = 0,
b_Income_DR_asc_1 = 0,
### Current conditions
b_ApartmentOwner_DR_asc_1 = 0,
b_ExistingApps_DR_asc_1 = 0,
b_CurrentMix_DR_asc_1 = 0,
# b_EnergyDecisionMaker = 0,
# ----------------------------------------------------------------- #
#---- Quota_Count
# ----------------------------------------------------------------- #
b_Quota_Count = 0,
b_Gender_Quota_Count = 0,
b_Age_Quota_Count = 0,
b_Education_Quota_Count = 0,
b_ExistingApps_Quota_Count = 0,
# ----------------------------------------------------------------- #
#---- Quota_Max_Duration
# ----------------------------------------------------------------- #
b_Quota_Max_Duration = 0,
b_Gender_Quota_Duration = 0,
b_Age_Quota_Duration = 0,
b_Education_Quota_Duration = 0,
b_ExistingApps_Quota_Duration = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_morning
# ----------------------------------------------------------------- #
b_Quota_Period_morning = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_midmorning
# ----------------------------------------------------------------- #
b_Quota_Period_midmorning = 0,
b_Gender_Quota_Period_midmorning = 0,
b_Age_Quota_Period_midmorning = 0,
b_Education_Quota_Period_midmorning = 0,
b_ExistingApps_Quota_Period_midmorning = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_afternoon
# ----------------------------------------------------------------- #
b_Quota_Period_afternoon = 0,
b_Gender_Quota_Period_afternoon = 0,
b_Age_Quota_Period_afternoon = 0,
b_Education_Quota_Period_afternoon = 0,
b_ExistingApps_Quota_Period_afternoon = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_evening
# ----------------------------------------------------------------- #
b_Quota_Period_evening = 0,
b_Gender_Quota_Period_evening = 0,
b_Age_Quota_Period_evening = 0,
b_Education_Quota_Period_evening = 0,
b_ExistingApps_Quota_Period_evening = 0,
# ----------------------------------------------------------------- #
#---- Quota_Period_night
# ----------------------------------------------------------------- #
b_Quota_Period_night = 0,
b_Gender_Quota_Period_night = 0,
b_Age_Quota_Period_night = 0,
b_Education_Quota_Period_night = 0,
b_ExistingApps_Quota_Period_night = 0,
# ----------------------------------------------------------------- #
#---- Quota_Compensation
# ----------------------------------------------------------------- #
b_Quota_Compensation = -0.1,
b_Gender_Quota_Compensation = 0,
b_Age_Quota_Compensation = 0,
b_Education_Quota_Compensation = 0,
b_Income_Quota_Compensation = 0,
b_CheapTalk_Quota_Compensation = 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("b_asc_3",
"b_Quota_Period_morning",
"mu_CBC")
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities.functionality = "estimate"
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality=apollo_probabilities.functionality) {
### 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()
# ----------------------------------------------------------------- #
#---- Scaling parameters
# ----------------------------------------------------------------- #
# ----------------------------------------------------------------- #
#---- Alternative specific constants (ASC)
# ----------------------------------------------------------------- #
b_asc_1_value = b_asc_1
b_asc_2_value = b_asc_2
b_asc_3_value = b_asc_3
# ----------------------------------------------------------------- #
#---- DR_asc_0
# ----------------------------------------------------------------- #
# ----------------------------------------------------------------- #
#---- DR_asc_1
# ----------------------------------------------------------------- #
b_DR_asc_1_value = b_DR_asc_1 +
### Sociodemographics
b_Gender_DR_asc_1 * COV_Gender +
b_Age_DR_asc_1 * COV_Age_centered +
b_Education_DR_asc_1 * COV_Education_centered +
(1-IncomeQuan_miss) * b_Income_DR_asc_1 * COV_IncomeClasses_centered +
### Current conditions
b_ApartmentOwner_DR_asc_1 * COV_ApartmentOwner +
b_ExistingApps_DR_asc_1 * COV_ExistingApps +
b_CurrentMix_DR_asc_1 * COV_CurrentMix
# ----------------------------------------------------------------- #
#---- Quota_Count
# ----------------------------------------------------------------- #
b_Quota_Count_value = b_Quota_Count +
b_Gender_Quota_Count * COV_Gender +
b_Age_Quota_Count * COV_Age_centered +
b_Education_Quota_Count * COV_Education_centered +
b_ExistingApps_Quota_Count * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Max_Duration
# ----------------------------------------------------------------- #
b_Quota_Max_Duration_value = b_Quota_Max_Duration +
b_Gender_Quota_Duration * COV_Gender +
b_Age_Quota_Duration * COV_Age_centered +
b_Education_Quota_Duration * COV_Education_centered +
b_ExistingApps_Quota_Duration * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Period_morning
# ----------------------------------------------------------------- #
b_Quota_Period_morning_value = b_Quota_Period_morning
# ----------------------------------------------------------------- #
#---- Quota_Period_midmorning
# ----------------------------------------------------------------- #
b_Quota_Period_midmorning_value = b_Quota_Period_midmorning +
b_Gender_Quota_Period_midmorning * COV_Gender +
b_Age_Quota_Period_midmorning * COV_Age_centered +
b_Education_Quota_Period_midmorning * COV_Education_centered +
b_ExistingApps_Quota_Period_midmorning * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Period_afternoon
# ----------------------------------------------------------------- #
b_Quota_Period_afternoon_value = b_Quota_Period_afternoon +
b_Gender_Quota_Period_afternoon * COV_Gender +
b_Age_Quota_Period_afternoon * COV_Age_centered +
b_Education_Quota_Period_afternoon * COV_Education_centered +
b_ExistingApps_Quota_Period_afternoon * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Period_evening
# ----------------------------------------------------------------- #
b_Quota_Period_evening_value = b_Quota_Period_evening +
b_Gender_Quota_Period_evening * COV_Gender +
b_Age_Quota_Period_evening * COV_Age_centered +
b_Education_Quota_Period_evening * COV_Education_centered +
b_ExistingApps_Quota_Period_evening * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Period_night
# ----------------------------------------------------------------- #
b_Quota_Period_night_value = b_Quota_Period_night +
b_Gender_Quota_Period_night * COV_Gender +
b_Age_Quota_Period_night * COV_Age_centered +
b_Education_Quota_Period_night * COV_Education_centered +
b_ExistingApps_Quota_Period_night * COV_ExistingApps
# ----------------------------------------------------------------- #
#---- Quota_Compensation
# ----------------------------------------------------------------- #
b_Quota_Compensation_value = b_Quota_Compensation +
### Sociodemographics
b_Gender_Quota_Compensation * COV_Gender +
b_Age_Quota_Compensation * COV_Age_centered +
b_Education_Quota_Compensation * COV_Education_centered +
(1-IncomeQuan_miss) * b_Income_Quota_Compensation * COV_IncomeClasses_centered +
b_CheapTalk_Quota_Compensation * COV_CheapTalk
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
### CBC (Forced Choices)
V[['alt1']] = b_asc_1_value +
b_Quota_Count_value * Quota_Count.1 +
b_Quota_Max_Duration_value * Quota_Max_Duration.1 +
b_Quota_Period_morning_value * 0 + b_Quota_Period_midmorning_value * Quota_Period2.1 + b_Quota_Period_afternoon_value * Quota_Period3.1 + b_Quota_Period_evening_value * Quota_Period4.1 + b_Quota_Period_night_value * Quota_Period5.1 +
b_Quota_Compensation_value * Quota_Compensation.1
V[['alt2']] = b_asc_2_value +
b_Quota_Count_value * Quota_Count.2 +
b_Quota_Max_Duration_value * Quota_Max_Duration.2 +
b_Quota_Period_morning_value * 0 + b_Quota_Period_midmorning_value * Quota_Period2.2 + b_Quota_Period_afternoon_value * Quota_Period3.2 + b_Quota_Period_evening_value * Quota_Period4.2 + b_Quota_Period_night_value * Quota_Period5.2 +
b_Quota_Compensation_value * Quota_Compensation.2
V[['alt3']] = b_asc_3_value +
b_Quota_Count_value * Quota_Count.3 +
b_Quota_Max_Duration_value * Quota_Max_Duration.3 +
b_Quota_Period_morning_value * 0 + b_Quota_Period_midmorning_value * Quota_Period2.3 + b_Quota_Period_afternoon_value * Quota_Period3.3 + b_Quota_Period_evening_value * Quota_Period4.3 + b_Quota_Period_night_value * Quota_Period5.3 +
b_Quota_Compensation_value * Quota_Compensation.3
# Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3),
avail = list(alt1=1, alt2=1, alt3=1),
choiceVar = Choice,
V = lapply(V, "*", (1+mu_CBC)),
rows = (CBC_type=="R"),
componentName = "CBC"
)
# Compute probabilities using MNL model
P[['CBC']] = apollo_mnl(mnl_settings, functionality)
### DR (Free Choices)
normalDensity_settings1 = list(outcomeNormal = Choice,
xNormal = (1+mu_DR) * (b_Quota_Count_value * Quota_Count.1 +
b_Quota_Max_Duration_value * Quota_Max_Duration.1 +
b_Quota_Period_morning_value * 0 + b_Quota_Period_midmorning_value * Quota_Period2.1 + b_Quota_Period_afternoon_value * Quota_Period3.1 + b_Quota_Period_evening_value * Quota_Period4.1 + b_Quota_Period_night_value * Quota_Period5.1 +
b_Quota_Compensation_value * Quota_Compensation.1 +
b_DR_asc_1_value),
mu = 0,
sigma = sigma_DR_asc_1,
rows=(CBC_type=="RD"),
componentName = "OL_DualResponse")
P[["OL_DualResponse"]] = apollo_normalDensity(normalDensity_settings1, functionality)
### Combined model
P = apollo_combineModels(P, apollo_inputs, 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)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
saveOutput_settings = list(
printClassical = TRUE,
printPVal = TRUE,
printDiagnostics = TRUE,
printCovar = TRUE,
printCorr = TRUE,
printOutliers = 50,
printChange = TRUE,
saveEst = TRUE,
saveCov = TRUE,
saveCorr = TRUE,
saveModelObject = TRUE
)
apollo_saveOutput(model, saveOutput_settings)