Page 1 of 1

Combining output from apollo_prediction lists

Posted: 05 Dec 2023, 10:26
by Bob123
Dear Apollo team,

I am running model predictions / scenario analysis as per the manual and was wondering if there is a way to combine results as I have ~30/40 scenarios to run.

The default “summary” argument/ setting for apollo_prediction saves the mean, SD and 95%CI which is the information from each scenario I would like to combine. I tried to save each new prediction as a different name (e.g. from predictions_new to Scenario1.1, Scenario 1.2... etc.) and combine later, but apollo_prediction saves the output as a list (as opposed to an R object) which I cannot combine into a table.

I appreciate there might be lots of ways to do this (for the programming expert) for example via creating a loop but this is very difficult to specify all the scenarios in a loop. I would prefer to change the output of apollo_prediction (means, SDs and 95%CIs) into an R object for each scenario (named separately) from which I can work out how to combine into a table. Is there a way to change the output of apollo_prediction do this?

Code: Select all

#Example scenarios
#SCENARIO 1.1
predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs,
                                     prediction_settings=list(runs=50))

database_backup=database

database$DrugA_CERT <- 1 #first scenario 

apollo_inputs = apollo_validateInputs()
predictions_new = apollo_prediction(model, apollo_probabilities, apollo_inputs,
                                    prediction_settings=list(runs=50))

database=database_backup
apollo_inputs = apollo_validateInputs()

#SCENARIO 1.2
predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs,
                                     prediction_settings=list(runs=50))

database_backup=database

database$DrugA_CERT <- 1 #second scenario 

apollo_inputs = apollo_validateInputs()
predictions_new = apollo_prediction(model, apollo_probabilities, apollo_inputs,
                                    prediction_settings=list(runs=50))

database=database_backup
apollo_inputs = apollo_validateInputs()

#28 more scenarios etc…
This code from a previous example also doesn’t seem to work with the list outputs of apollo_prediction and would only contain the means:

Code: Select all

predictions_overview=rbind(table(database$choice)/nrow(database),
                          colMeans(predictions_base),
                          colMeans(predictions_new))

rownames(predictions_overview)=c("Data",
                                 "predictions_base",
                                 "predictions_new")
print(round(predictions_overview,4))
Thanks in advance for your help. 


Re: Combining output from apollo_prediction lists

Posted: 07 Dec 2023, 07:25
by stephanehess
something like this should help you

Code: Select all

output=matrix(0,nrow=ncol(predictions$at_estimates)-3,ncol=5)
colnames(output) <- c("at MLE", 'Sampled mean', "Sampled std.dev.", "Quantile 0.025", "Quantile 0.975")
output[,1]=colSums(predictions$at_estimates[,-c(1,2,ncol(predictions$at_estimates))], na.rm=TRUE)
agg <- apply(predictions$draws, MARGIN=c(2,3), sum, na.rm=TRUE)
agg <- agg[-nrow(agg),]
output[,2]=rowMeans(agg)
output[,3]=apply(agg, MARGIN=1, sd, na.rm=TRUE)
output[,4]=apply(agg, MARGIN=1, quantile, probs=0.025, na.rm=TRUE)
output[,5]=apply(agg, MARGIN=1, quantile, probs=0.975, na.rm=TRUE)