plot_glm: Plot associations between Q-learning model parameters and...

View source: R/plot_glm.R

plot_glmR Documentation

Plot associations between Q-learning model parameters and variables of interest

Description

plot_glm is designed to plot the results of fitting Bayesian GLMs, with QL model parameters as the response variables, via the parameter_glm() function. It plots both a box-and-whisker plot (defaulting to 95% HDIs for the box, and 99% HDIs for the lines), plus the posterior distribution of coefficients (half-violin plots made up of the individual posterior draws).

Usage

plot_glm(
  par_df,
  plot_var,
  id.col = "parameter",
  test = FALSE,
  grp = id.col,
  fclr = id.col,
  axis_fixed = FALSE,
  grp_labs = NULL,
  plot_together = TRUE,
  ovrll_title = NULL,
  title_font_size = 16,
  title_rel_ht = NULL,
  plt_rows = 1,
  plt_rel_widths = 1,
  cred = c(0.95, 0.99),
  coord_flip = TRUE,
  box_alpha = 0.6,
  box_width = 0.125,
  box_nudge = 0.1,
  pal = NULL,
  font_size = 11,
  font = "",
  ...
)

Arguments

par_df

A posterior::draws_df(), likely obtained from running parameter_glm().

plot_var

The variable of interest to plot (e.g., distanced vs non-distanced).

id.col

The column that contains the QL model parameter names.

test

Boolean indicating whether summaries are from the test phase.

grp

Optional group to plot separately on each plot, which should be the interaction variable specified in parameter_glm().

fclr

To what variable should the colour scheme be applied? Defaults to grp = id.col.

axis_fixed

Logical indicating whether the y-axis should be fixed across all plots. Defaults to FALSE.

grp_labs

Optional labels for the groups defined by grp. It is recommended to first run the function with this kept as NULL to make sure you label the correct densities.

plot_together

If TRUE, returns a panel with all plots plotted as defined by subsequent arguments. Otherwise a named list of plots is returned.

ovrll_title

Title to set for the whole plot. Ignored if plot_together == FALSE.

title_font_size, title_rel_ht

Font size, and relative height of the title compared to the main plot, given as a 2 element vector. Defaults to 16pt and c(0.15, 1) respectively. Ignored if plot_together == FALSE.

plt_rows, plt_rel_widths

Number of rows, and relative widths for the plotted grid (passed to cowplot::plot_grid()). Defaults to 1 (i.e. single row, all equal widths). Ignored if !plot_together.

cred

Vector, length 2, which defines the % HDI covered by the boxplot boxes and lines respectively.

coord_flip

Plot horizontal (TRUE) or vertical (FALSE) densities.

box_alpha, box_width, box_nudge

Control the transparency, size, and position of the summary boxplot.

pal, font_size, font

Same as plot_import().

...

Additional arguments such as alpha_par_nms to alter axis titles or control quantile_hdi().

Value

Either a list of ggplots or a cowplot::plot_grid() if plot_together == TRUE.

Examples

## Not run: 
Comparing parameters across groups

data(example_data)

fit_nd <- fit_learning_model(
  example_data$nd,
  model = "2a",
  vb = FALSE,
  exp_part = "training"
)
fit_dis <- fit_learning_model(
  example_data$dis,
  model = "2a",
  vb = FALSE,
  exp_part = "training"
)

distanced <- parameter_glm(
  summary_df = list(fit_nd$summary, fit_dis$summary),
  raw_df = list(fit_nd$raw_df, fit_dis$raw_df),
  var_of_interest = "distanced",
  covariates = c("age", "sex", "digit_span"),
  iter_warmup = 1000, iter_sampling = 1000
)
plot_glm(distanced, plot_var = "distanced")

## End(Not run)


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