Page 1 of 1

Error in apollo_modifyUserDefFunc(apollo_beta, apollo_fixed, apollo_probabilities)

Posted: 03 Nov 2022, 15:54
by Shan
Dear Sir,

I hope you are doing well. I would like to estimate income effects in my simple MNL model. However, when running the codes, R reported an error indicating that a parameter (b_fare_bus) from apollo_beta was re-defined inside apollo_probabilities, which is not allowed. I have checked my codes with the example ones, but I could not find out where my problem is. Could you please help me find out? Part of my codes is as follows.

### Vector of parameters:
apollo_beta = c(asc_car = 0,
asc_bus = 0,
asc_metro = 0,
b_fuel_CNG = 0,
b_fuel_electricity = 0,
b_access_bus = 0,
b_access_metro = 0,
b_freq_bus = 0,
b_freq_metro = 0,
b_egress_bus = 0,
b_egress_metro = 0,
b_seat_all_bus = 0,
b_seat_all_metro = 0,
b_seat_half_bus = 0,
b_seat_half_metro = 0,
b_seat_half = 0,
b_seat_no = 0,
b_tt_car = 0,
b_tt_bus = 0,
b_tt_metro = 0,
b_fare_bus = 0,
b_fare_bus_shift_female = 0,
fare_bus_income_elast = 0,
b_fare_metro = 0,
b_fare_metro_shift_female = 0,
fare_metro_income_elast = 0,
b_fuelcost = 0,
b_fuelcost_shift_female = 0,
fuelcost_income_elast = 0,
b_parking = 0)

### Parameters to be kept fixed at their starting value in apollo_beta:
apollo_fixed = c("asc_car", "b_fuel_CNG", "b_seat_no")

# ################################################################# #
#### 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 a list of probabilities P
P = list()

### Create coefficients with interactions with socio-demographics
b_fare_bus= (b_fare_bus + b_fare_bus_shift_female * female) * (income/mean_income) ^ fare_bus_income_elast
b_fare_metro= (b_fare_metro + b_fare_metro_shift_female * female) * (income/mean_income) ^ fare_metro_income_elast
b_fuelcost= (b_fuelcost + b_fuelcost_shift_female * female) * (income/mean_income) ^ fuelcost_income_elast

Re: Error in apollo_modifyUserDefFunc(apollo_beta, apollo_fixed, apollo_probabilities)

Posted: 03 Nov 2022, 18:53
by stephanehess
Hi

you have e.g.

Code: Select all

b_fare_bus= (b_fare_bus + b_fare_bus_shift_female * female) * (income/mean_income) ^ fare_bus_income_elast
so you are redefining b_fare_bus, as it appears on the left and right hand side. The easy solution is to instead use something like:

Code: Select all

b_fare_bus= (b_fare_bus_base + b_fare_bus_shift_female * female) * (income/mean_income) ^ fare_bus_income_elast
so b_fare_bus_base is the estimated parameter, and b_fare_bus is used inside the utilities, and is given by the combination across parameters

Stephane