plot_ppc: Plot posterior predictions against observed data

View source: R/plot_ppc.R

plot_ppcR Documentation

Plot posterior predictions against observed data

Description

plot_ppc plots posterior predictions in a variety of ways.

Usage

plot_ppc(
  train_indiv = list(),
  train_trials = list(),
  test_perf = list(),
  id = NULL,
  group_title = "",
  legend_pos = "right",
  pal = NULL,
  font = "",
  font_size = 14,
  ...
)

Arguments

train_indiv

List, maximum length 3. The first element should be an individual-level tibble containing summed predictions for each trial and individual (outputted from get_preds_by_chain). The second and third elements should be integers or numeric vectors containing the number of trials to lag for the training plots; and the last n trials to calculate differences in mean observed/predicted densities for.

train_trials

List, maximum length 3. The first element should be a trial-level tibble containing summed posterior draws and their HDIs, both overall and for each block and block group of interest (outputted from get_preds_by_chain). The second and third elements are optional, but enable lagging by a certain number of trials for the cumulative and difference (i.e., observed minus predicted) plots.

test_perf

List, maximum length 3. The first element should be a individual-level tibble containing summed predictions for each trial and individual (outputted from get_preds_by_chain). The second and third lists are optional, and are passed to the plt.test argument of plot_import to plot observed grouped and individual pair accuracy respectively against their posterior predictions (a grouped plot including all pairs is plotted by default).

id

subjID to select if only plots for a single participant are desired. Will also accept a single numeric value i, which will select the ith participant in the output.

group_title

Sets consistent titles for all plots.

legend_pos

Enables the legend positions to be set manually.

pal, font, font_size

Same as plot_import().

...

Other rarely used arguments which set the number of trials/blocks or the name of the predicted variable.

Value

Either a single or named list of ggplot objects.

Examples

## Not run: 
data(example_data)
dir.create("outputs/cmdstan/predictions")

fit <- fit_learning_model(
  example_data$nd,
  model = "2a",
  vb = FALSE,
  exp_part = "training",
  iter_sampling = 2000,
  outputs = c("model_env", "raw_df", "stan_datalist")
)

pred_paths <- generate_posterior_quantities(
  fit_mcmc = fit$fit,
  data_list = fit$stan_datalist,
  return_type = "paths"
)

obs_df_preds <- get_preds_by_chain(
  out_files = pred_paths,
  out_dir = "outputs/cmdstan/predictions",
  obs_df = fit$raw_df,
  n_draws_chain = 2000
)

ind <- readRDS("outputs/cmdstan/predictions/indiv_obs_sum_ppcs_df.RDS")
tri <- readRDS("outputs/cmdstan/predictions/trial_block_avg_hdi_ppcs_df.RDS")

# Cumulative choice probabilities, block-lagged
plot_ppc(
  train_indiv = list(ind, 20),
  group_title = "Cumulative choice probabilities"
)

# Difference between observed and predicted choice, over the whole task
# i.e., 120 trials per type
plot_ppc(
  train_indiv = list(ind, list(), 120),
  group_title = "Difference between observed and predicted choices"
)

# Individual-level predictions and observed choice probabilities, across task

## N.B. partial match can be used to plot the types in names(tri$types)
## e.g., using "block_6" to plot "AB_block_6", "CD_block_6", and "EF_block_6"

plot_ppc(
  train_trials = list(tri, "all_trials"),
  group_title = "Individual-level choices against predictions"
)

# Test plots can be plotted similarly with test_perf - the second and third
# arguments follow the same logic as plot_import(plt.test)

## End(Not run)


qdercon/pstpipeline documentation built on June 1, 2025, 1:11 p.m.