Click to show/hide R Code
#First, we source the helper functions for the analysis
source("R/functions.R")The code in this script runs the decision tree at the base-case parameter values, draws the tree, and saves the tables and figures shown on the base case results page.
The full R script is available at:
The code sections below are imported directly from that script when this page is rendered.
This R script file is itself reliant on the following helper files:
The script first sources the functions used to evaluate and draw the tree.
#First, we source the helper functions for the analysis
source("R/functions.R")plot_tree_diagram_QC() is called with the name of each parameter in place of its value, and complementary branches are labelled with their formulas. The result is saved as figures/crag_tree_parameters.svg, the conceptual tree on the base case page.
#Create the plot that shows how the parameters appear in the diagram:
plot_parameters_in_tree<-plot_tree_diagram_QC(p_usage="p_usage",
p_donor_cryptococcus="p_donor_cryptococcus",
p_transmission="p_transmission",
p_spont_cryptococcus="p_spont_cryptococcus",
p_sensitivity="p_sensitivity",
p_specificity="p_specificity",
p_cancelled="p_cancelled",
p_prophrate="p_prophrate",
p_prophefficacy="p_prophefficacy",
number_donors="number_donors",
cost_test="cost_test",
cost_disease="cost_disease",
cost_fluconazole="cost_fluconazole",
cost_cancellation="cost_cancellation",
cost_nocryptococcus="cost_nocryptococcus",
cost_nonacceptance="cost_nonacceptance",
q_nocryptococcus='q_nocryptococcus',
q_noacceptance="q_noacceptance",
q_loss_cryptococcus="q_loss_cryptococcus",
circle_diameter=1.4,
box_width=1.9,
box_height=1,
diamond_width=7,
diamond_height=2.5,
p_nonusage="1-p_usage",
p_donor_nocryptococcus="1-p_donor_cryptococcus",
p_nontransmission="1-p_transmission",
p_nospont_cryptococcus="1-p_spont_cryptococcus",
p_falsenegative="1-p_sensitivity",
p_falsepositive="1-p_specificity",
p_nocancelled="1-p_cancelled",
p_noprophrate="1-p_prophrate",
p_noprophefficacy="1-p_prophefficacy",
p_breakthrough_donorpos="(1-p_prophefficacy)*p_transmission",
p_nobreakthrough_donorpos="1-(1-p_prophefficacy)*p_transmission",
p_breakthrough_donorneg="(1-p_prophefficacy)*p_spont_cryptococcus",
p_nobreakthrough_donorneg="1-(1-p_prophefficacy)*p_spont_cryptococcus",
q_cryptococcus="q_nocryptococcus-q_loss_cryptococcus")
export_svg(plot_parameters_in_tree)%>%
writeLines(., "figures/crag_tree_parameters.svg")create_tree_diagram_QC() runs the tree at the base-case values in expected_value. The tree is saved as figures/crag_tree_QC.svg, and a copy annotated with the incremental cost, QALYs and net monetary benefit of screening is saved as figures/crag_tree_QC_annotated.svg.
#Run the base case tree diagram
g_QC<-create_tree_diagram_QC()
#Print plot
g_QC$plot
#Save the tree diagram as svg
svg_QC <- export_svg(g_QC$plot)
writeLines(svg_QC, "figures/crag_tree_QC.svg")
#Annotate figure
svg_doc <- read_xml(svg_QC)
add_svg_annotation(
svg_doc,
c("Screening vs no screening:",
glue("• ΔC = {scales::number(g_QC$cost_difference, accuracy = 0.01)}"),
glue("• ΔQ = {formatC(g_QC$qaly_difference, format = 'f', digits = 4)}"),
glue("• NMB = {scales::number(g_QC$nmb, accuracy = 0.01)}")
)
)
write_xml(svg_doc, "figures/crag_tree_QC_annotated.svg")The pathway table lists every path through the tree with its probability, cost and QALYs. It is saved as figures/output_table_QC.png and tables/output_table_QC.docx.
#Now, we look at the path table
result_tibble_QC<-g_QC$path_table
#Let's look at the tibble as a gt table
crag_table_QC<-result_tibble_QC%>%gt()%>%
cols_label(
strategy = "Strategy",
acceptance = "Acceptance",
donor_dz_status = "Donor status",
donor_test_result = "Test result",
cancellation = "Transplant cancelled",
proph = "Prophylaxis",
outcome = "Outcome",
probability = "Path probability",
cost_total = "Cost if outcome ($)",
cost_expected = "Expected cost ($)",
qaly_total = "QALY if outcome",
qaly_expected = "Expected QALY"
)%>%
tab_style(
style = cell_text(align = "center"),
locations = cells_column_labels(everything())
)%>%
fmt_missing(everything(), missing_text = "")
#Print the table
crag_table_QC
#Save table as figure and docx, after deleting prior docx
crag_table_QC%>%
gtsave("figures/output_table_QC.png",
vwidth = 2000, # try 1600–2400
vheight = 1200,
expand = 10)
if (file.exists("tables/output_table_QC.docx")) {
unlink("tables/output_table_QC.docx")
}
crag_table_QC%>%
gtsave("tables/output_table_QC.docx")The parameter values used in the run are formatted as a table and saved as figures/parameter_table_QC.png and tables/parameter_table_QC.docx.
#Let's create a table from the parameters
parameter_table_QC<-g_QC$parameter_tibble%>%
gt()%>%
cols_label(
parameter = "Parameter",
value = "Value"
)%>%
fmt_number(
columns = value,
rows = parameter %in% c("number_donors"),
decimals = 0
)%>%
fmt_number(
columns = value,
rows = parameter %in% c("cost_test", "cost_disease", "cost_fluconazole", "cost_cancellation",
"cost_cryptococcus", "cost_nonacceptance", "cost_nocryptococcus"),
decimals = 2
)%>%
fmt_number(
columns = value,
rows = parameter %in% c("q_nocryptococcus", "q_noacceptance", "q_loss_cryptococcus"),
decimals = 1
)
parameter_table_QC
parameter_table_QC%>%
gtsave("figures/parameter_table_QC.png")
if (file.exists("tables/parameter_table_QC.docx")) {
unlink("tables/parameter_table_QC.docx")
}
parameter_table_QC%>%
gtsave("tables/parameter_table_QC.docx")The pathway table is summed by strategy to give each strategy’s total probability, expected cost, expected QALYs and net monetary value at the willingness-to-pay threshold. It is saved as figures/summary_table_QC.png and tables/summary_table_QC.docx.
#Let's also look at the summary statistics for this table
summary_tibble_QC<-result_tibble_QC%>%
group_by(strategy)%>%
summarize(total_probability=sum(probability), total_expected_cost=sum(cost_expected),total_expected_qaly=sum(qaly_expected))%>%
mutate(NMV=wtp*total_expected_qaly-total_expected_cost)
summary_gt_QC<-summary_tibble_QC%>%
gt()%>%
cols_label(
strategy = "Strategy",
total_probability = "Total Probability",
total_expected_cost = "Total Expected Cost",
total_expected_qaly = "Total Expected QALY",
NMV = "Net Monetary Value"
)%>%
tab_style(
style = cell_text(align = "center"),
locations = cells_column_labels(everything())
)
summary_gt_QC
summary_gt_QC%>%
gtsave("figures/summary_table_QC.png")
if (file.exists("tables/summary_table_QC.docx")) {
unlink("tables/summary_table_QC.docx")
}
summary_gt_QC%>%
gtsave("tables/summary_table_QC.docx")
#Save the base-case results for the website
write_csv(result_tibble_QC, "output/base_case_path_table.csv")
tibble(
cost_difference = g_QC$cost_difference,
qaly_difference = g_QC$qaly_difference,
nmb = g_QC$nmb
)%>%
write_csv("output/base_case_results.csv")