Error in model predictions
Posted: 15 Nov 2022, 12:49
Dear Sir,
I hope that you are doing well. I tried to generate model predictions from a simple MNL model. However, an error was reported indicating a non-numeric argument to the binary operator for the code: change=(predictions_new-predictions_base)/predictions_base. I compared my codes for model predictions with those in the Apollo manual but couldn't figure out what was wrong. Here are the codes for my model predictions:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
### Use the estimated model to make predictions
predictions_base = apollo_prediction(model,apollo_probabilities,apollo_inputs,prediction_settings=list(runs=30))
### Now imagine the fuel cost for car increases by 1%
database$fuelcost_car=1.01*database$fuelcost_car
### Rerun prediction with the new data
apollo_inputs=apollo_validateInputs()
predictions_new= predictions_new = apollo_prediction(model, apollo_probabilities, apollo_inputs)
### Return to original data
database$fuelcost_car = 1/1.01*database$fuelcost_car
apollo_inputs = apollo_validateInputs()
### Work with predictions at estimates
predictions_base=predictions_base[["at_estimates"]]
### Compute the change in probabilities
change=(predictions_new-predictions_base)/predictions_base
### Not interested in chosen alternative now, so drop the last column
change=change[,-ncol(change)]
### First two columns (change in ID and task) are also not needed
change=change[,-c(1,2)]
### Look at the first individual
change[1:9,]
### And for person 9
change[74:82,]
### Summary of changes (possible presence of NAs for unavailable alternatives)
summary(change)
### Look at mean changes for subsets of the data, ignoring NAs
colMeans(change,na.rm=TRUE)
colMeans(subset(change,database$female==1),na.rm=TRUE)
colMeans(subset(change,database$female==0),na.rm=TRUE)
colMeans(subset(change,(database$income<quantile(database$income,0.25))),na.rm=TRUE)
colMeans(subset(change,(database$income>=quantile(database$income,0.25))|(database$income<=quantile(database$income,0.75))),na.rm=TRUE)
colMeans(subset(change,(database$income>quantile(database$income,0.75))),na.rm=TRUE)
### Computing elasticities
### Own elasticity for car(3:alternatives=1;4:alternatives=2;5:alternatives=3):
log(sum(predictions_new[,3])/sum(predictions_base[,3]))/log(1.1)
### Cross-elasticities for other modes
log(sum(predictions_new[,4])/sum(predictions_base[,4]))/log(1.1)
log(sum(predictions_new[,5])/sum(predictions_base[,5]))/log(1.1)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Kind regards,
Shan
I hope that you are doing well. I tried to generate model predictions from a simple MNL model. However, an error was reported indicating a non-numeric argument to the binary operator for the code: change=(predictions_new-predictions_base)/predictions_base. I compared my codes for model predictions with those in the Apollo manual but couldn't figure out what was wrong. Here are the codes for my model predictions:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
### Use the estimated model to make predictions
predictions_base = apollo_prediction(model,apollo_probabilities,apollo_inputs,prediction_settings=list(runs=30))
### Now imagine the fuel cost for car increases by 1%
database$fuelcost_car=1.01*database$fuelcost_car
### Rerun prediction with the new data
apollo_inputs=apollo_validateInputs()
predictions_new= predictions_new = apollo_prediction(model, apollo_probabilities, apollo_inputs)
### Return to original data
database$fuelcost_car = 1/1.01*database$fuelcost_car
apollo_inputs = apollo_validateInputs()
### Work with predictions at estimates
predictions_base=predictions_base[["at_estimates"]]
### Compute the change in probabilities
change=(predictions_new-predictions_base)/predictions_base
### Not interested in chosen alternative now, so drop the last column
change=change[,-ncol(change)]
### First two columns (change in ID and task) are also not needed
change=change[,-c(1,2)]
### Look at the first individual
change[1:9,]
### And for person 9
change[74:82,]
### Summary of changes (possible presence of NAs for unavailable alternatives)
summary(change)
### Look at mean changes for subsets of the data, ignoring NAs
colMeans(change,na.rm=TRUE)
colMeans(subset(change,database$female==1),na.rm=TRUE)
colMeans(subset(change,database$female==0),na.rm=TRUE)
colMeans(subset(change,(database$income<quantile(database$income,0.25))),na.rm=TRUE)
colMeans(subset(change,(database$income>=quantile(database$income,0.25))|(database$income<=quantile(database$income,0.75))),na.rm=TRUE)
colMeans(subset(change,(database$income>quantile(database$income,0.75))),na.rm=TRUE)
### Computing elasticities
### Own elasticity for car(3:alternatives=1;4:alternatives=2;5:alternatives=3):
log(sum(predictions_new[,3])/sum(predictions_base[,3]))/log(1.1)
### Cross-elasticities for other modes
log(sum(predictions_new[,4])/sum(predictions_base[,4]))/log(1.1)
log(sum(predictions_new[,5])/sum(predictions_base[,5]))/log(1.1)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Kind regards,
Shan