blimp.plot: Blimp Trace Plots and Posterior Distribution Plots

View source: R/blimp.plot.R

blimp.plotR Documentation

Blimp Trace Plots and Posterior Distribution Plots

Description

This function reads the posterior distribution including burn-in and post-burn-in phase for all parameters saved in long format in a file called posterior.* by the function blimp.run or blimp when specifying posterior = TRUE to display trace plots and posterior distribution plots.

Usage

blimp.plot(x, plot = c("none", "trace", "post"), param = NULL, labels = TRUE,
           burnin = TRUE, point = c("all", "none", "m", "med", "map"),
           ci = c("none", "eti", "hdi"), conf.level = 0.95, hist = TRUE,
           density = TRUE, area = TRUE, alpha = 0.4, fill = "gray85",
           nrow = NULL, ncol = NULL, scales = c("fixed", "free", "free_x", "free_y"),
           xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL,
           xbreaks = ggplot2::waiver(), ybreaks = ggplot2::waiver(),
           xexpand = ggplot2::waiver(), yexpand = ggplot2::waiver(),
           palette = "Set 2", binwidth = NULL, bins = NULL,
           density.col = "#0072B2", shape = 21,
           point.col = c("#CC79A7", "#D55E00", "#009E73"),
           linewidth = 0.6, linetype = "dashed", line.col = "black",
           plot.margin = NULL, legend.title.size = 10, legend.text.size = 10,
           legend.box.margin = NULL, saveplot = c("all", "none", "trace", "post"),
           file = "Blimp_Plot.pdf", file.plot = c("_TRACE", "_POST"),
           width = NA, height = NA, units = c("in", "cm", "mm", "px"), dpi = 600,
           check = TRUE)

Arguments

x

a character string indicating the name of folder containing the posterior.* file, e.g., "Posterior_Ex4.3" or the name of the posterior.* file with or without any file extension, e.g., "Posterior_ExEx4.3/posterior.csv" or "Posterior_ExEx4.3/posterior". Alternatively, a misty.object of type blimp can be specified, i.e., result object of the blimp.plot() function. Note that if the posterior file is specified without file extension while multiple posterior.* files in different file formats are available, then the file is read in following order: csv,RData, rds, and xlsx.

plot

a character string indicating the type of plot to display, i.e., "none" for not displaying any plot, "trace" (default) for displaying trace plots, and post for displaying posterior distribution plots.

param

a numeric vector indicating which parameters to print for the trace plots or posterior distribution plots. Note that the number of the parameter (Param) and the parameter specification (L1, L2, and L3) are provided in the text file "partable.txt". Note that parameters with zero variance are excluded by default.

labels

logical: if TRUE (default), parameter labels (e.g., y Beta x for the slope of the regression y on x) are shown in the facet labels. If FALSE, the numbers of the parameter (e.g., Parameter 1 are shown in the the facet labels.

burnin

logical: if FALSE, the burn-in iterations are discarded when displaying trace plots. The default setting for plot = "trace" is TRUE. Note that the burn-in iterations are always discarded when displaying posterior distribution plots (plot = "post") regardless of the setting of the argument burnin.

point

a character vector indicating the point estimate(s) to be displayed in the posterior distribution plots, i.e., "all" for all point estimates, "none" for not displaying any point estimates, "m" for the posterior mean estimate, "med" (default) for the posterior median estimate, and "map" for the maximum a posteriori estimate.

ci

a character string indicating the type of credible interval to be displayed in the posterior distribution plots, i.e., "none" for not displaying any credible intervals, "eti" (default) for displaying the equal-tailed intervals and "hdi" for displaying the highest density interval.

conf.level

a numeric value between 0 and 1 indicating the confidence level of the credible interval (default is 0.95).

hist

logical: if TRUE (default), histograms are drawn in the posterior probability plots.

density

logical: if TRUE (default), density curves are drawn in the posterior probability plots.

area

logical: if TRUE (default), statistical not significant and statistical significant area is filled with a different color and vertical lines are drawn.

alpha

a numeric value between 0 and 1 for the alpha argument (default is 0.4) for the annotate, and geom_histogram function.

fill

a character string indicating the color for the "fill" argument (default is "gray85") for the annotate and geom_histogram functions.

nrow

a numeric value indicating the nrow argument (default is NULL) for the facet_wrap function.

ncol

a numeric value indicating the ncol argument (default is 2) for the facet_wrap function.

scales

a character string indicating the scales argument (default is "free") for the facet_wrap function.

xlab

a character string indicating the name argument for the scale_x_continuous function.

ylab

a character string indicating the name argument for the scale_y_continuous function.

xlim

a numeric vector with two elements indicating the limits argument (default it NULL) for the scale_x_continuous function.

ylim

a numeric vector with two elements indicating the limits argument (default it NULL) for the scale_y_continuous function.

xbreaks

a numeric vector indicating the breaks argument (default is ggplot2::waiver()) for the scale_x_continuous function.

ybreaks

a numeric vector indicating the breaks argument (default is ggplot2::waiver()) for the scale_y_continuous function.

xexpand

a numeric vector with two elements indicating the expand argument (default is (0.02, 0)) for the scale_x_continuous function.

yexpand

a numeric vector with two elements indicating the expand argument for the scale_y_continuous function. Note that the default setting depends on the type of plot, e.g., (0.02, 0) for the trace plots and expansion(mult = c(0, 0.05)) for the posterior distribution plots.

palette

a character string indicating the palette name (default is "Set 2") for the hcl.colors function. Note that the character string must be one of hcl.pals().

binwidth

a numeric value indicating the binwidth argument (default is to use the number of bins in bins argument) for the geom_histogram function.

bins

a numeric value indicating the bins argument (default is 30) for the geom_histogram function.

density.col

a character string indicating the color argument (default is "#0072B2") for the geom_density function.

shape

a numeric value indicating the shape argument (default is 21) for the geom_point function.

point.col

a character vector with three elements indicating the values argument (default is c("#CC79A7", "#D55E00", "#009E73")) for the scale_color_manual function.

linewidth

a numeric value indicating the linewidth argument (default is 0.6) for the geom_vline function.

linetype

a numeric value indicating the linetype argument (default is "dashed") for the geom_vline function.

line.col

a character string indicating the color argument (default is "black") for the geom_vline function.

plot.margin

a numeric vector indicating the plot.margin argument for the theme function. Note that the default setting depends on the type of the plot, e.g., c(4, 15, -10, 0) for the trace plots, and c(4, 15, 4, 4) for the autocorrelation plots.

legend.title.size

a numeric value indicating the legend.title argument (default is element_text(size = 10)) for the theme function.

legend.text.size

a numeric value indicating the legend.text argument (default is element_text(size = 10)) for the theme function.

legend.box.margin

a numeric vector indicating the legend.box.margin argument for the theme function. Note that the default setting depends on the type of plot, e.g., c(-16, 6, 6, 6) for the trace plots, and c(-25, 6, 6, 6) for the posterior distribution plots with displaying point estimates.

saveplot

a character vector indicating the plot to be saved, i.e., "all" for saving all plots, "none" (default) for not saving any plots, "trace" for saving the trace plots and post for the saving the posterior distribution plots.

file

a character string indicating the filename argument (default is "Blimp_Plot.pdf") including the file extension for the ggsave function. Note that one of ".eps", ".ps", ".tex", ".pdf" (default), ".jpeg", ".tiff", ".png", ".bmp", ".svg" or ".wmf" needs to be specified as file extension in the file argument.

file.plot

a character vector with two elements for distinguishing different types of plots. By default, the character string specified in the argument "file" ("Blimp_Plot") is concatenated with "_TRACE" ("Blimp_Plot_TRACE") for the trace plots, and "_POST" ("Blimp_Plot_POST") for the posterior distribution plots.

width

a numeric value indicating the width argument (default is the size of the current graphics device) for the ggsave function.

height

a numeric value indicating the height argument (default is the size of the current graphics device) for the ggsave function.

units

a character string indicating the units argument (default is in) for the ggsave function.

dpi

a numeric value indicating the dpi argument (default is 600) for the ggsave function.

check

logical: if TRUE (default), argument specification is checked.

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

x

a character string indicating the name of the posterior.* file

args

specification of function arguments

data

list with posterior distribution of each parameter estimate in long format (plotdat), plot data for the trace plots (trace), and plot data for the posterior distribution plots (post).

plot

list with the trace plots (trace and posterior distribution plots (post)

Author(s)

Takuya Yanagida

References

Keller, B. T., & Enders, C. K. (2023). Blimp user’s guide (Version 3). Retrieved from www.appliedmissingdata.com/blimp

See Also

blimp, blimp.update, blimp.run, blimp.print, blimp.plot, blimp.bayes

Examples

## Not run: 
#----------------------------------------------------------------------------
# Blimp Example 4.3: Linear Regression

#..........
# Trace Plots

# Example 1a: Default setting, specifying name of the folder
blimp.plot("Posterior_Ex4.3")

# Example 1b: Default setting, specifying the posterior file
blimp.plot("Posterior_Ex4.3/posterior.csv")

# Example 1c: Print parameters 2, 3, 4, and 5
blimp.plot("Posterior_Ex4.3", param = 2:5)

# Example 1e: Arrange panels in three columns
blimp.plot("Posterior_Ex4.3", ncol = 3)

# Example 1f: Specify "Pastel 1" palette for the hcl.colors function
blimp.plot("Posterior_Ex4.3", palette = "Pastel 1")

#..........
# Posterior Distribution Plots

# Example 2a: Default setting, i.e., posterior median and equal-tailed interval
blimp.plot("Posterior_Ex4.3", plot = "post")

# Example 2b: Display posterior mean and maximum a posteriori
blimp.plot("Posterior_Ex4.3", plot = "post", point = c("m", "map"))

# Example 2c: Display maximum a posteriori and highest density interval
blimp.plot("Posterior_Ex4.3", plot = "post", point = "map", ci = "hdi")

# Example 2d: Do not display any point estimates and credible interval
blimp.plot("Posterior_Ex4.3", plot = "post", point = "none", ci = "none")

# Example 2d: Do not display histograms
blimp.plot("Posterior_Ex4.3", plot = "post", hist = FALSE)

#..........
# Save Plots

# Example 3a: Save all plots in pdf format
blimp.plot("Posterior_Ex4.3", saveplot = "all")

# Example 3b: Save all plots in png format with 300 dpi
blimp.plot("Posterior_Ex4.3", saveplot = "all", file = "Blimp_Plot.png", dpi = 300)

# Example 3a: Save posterior distribution plot, specify width and height of the plot
blimp.plot("Posterior_Ex4.3", plot = "none", saveplot = "post",
           width = 7.5, height = 7)

#----------------------------------------------------------------------------
# Plot from misty.object

# Create misty.object
object <- blimp.plot("Posterior_Ex4.3", plot = "none")

# Trace plot
blimp.plot(object, plot = "trace")

# Posterior distribution plot
blimp.plot(object, plot = "post")

#----------------------------------------------------------------------------
# Create Plots Manually

# Load ggplot2 package
library(ggplot2)

# Create misty object
object <- blimp.plot("Posterior_Ex4.3", plot = "none")

#..........
# Example 4: Trace Plots

# Extract data
data.trace <- object$data$trace

# Plot
ggplot(data.trace, aes(x = iter, y = value, color = chain)) +
  annotate("rect", xmin = 0, xmax = 1000, ymin = -Inf, ymax = Inf,
           alpha = 0.4, fill = "gray85") +
  geom_line() +
  facet_wrap(~ param, ncol = 2, scales = "free") +
  scale_x_continuous(name = "", expand = c(0.02, 0)) +
  scale_y_continuous(name = "", expand = c(0.02, 0)) +
  scale_colour_manual(name = "Chain",
                      values = hcl.colors(n = 2, palette = "Set 2")) +
  theme_bw() +
  guides(color = guide_legend(nrow = 1, byrow = TRUE)) +
  theme(plot.margin = margin(c(4, 15, -10, 0)),
        legend.position = "bottom",
        legend.title = element_text(size = 10),
        legend.text = element_text(size = 10),
        legend.box.margin = margin(c(-16, 6, 6, 6)),
        legend.background = element_rect(fill = "transparent"))

#..........
# Example 5: Posterior Distribution Plots

# Extract data
data.post <- object$data$post

# Plot
ggplot(data.post, aes(x = value)) +
  geom_histogram(aes(y = after_stat(density)), color = "black", alpha = 0.4,
                 fill = "gray85") +
  geom_density(color = "#0072B2") +
  geom_vline(data = data.frame(param = levels(data.post$param),
                               stat = tapply(data.post$value, data.post$param, median)),
             aes(xintercept = stat, color = "Median"), linewidth = 0.6) +
  geom_vline(data = data.frame(param = levels(data.post$param),
                               low = tapply(data.post$value, data.post$param,
                                            function(y) quantile(y, probs = 0.025))),
             aes(xintercept = low), linetype = "dashed", linewidth = 0.6) +
  geom_vline(data = data.frame(param = levels(data.post$param),
                               upp = tapply(data.post$value, data.post$param,
                                            function(y) quantile(y, probs = 0.975))),
             aes(xintercept = upp), linetype = "dashed", linewidth = 0.6) +
  facet_wrap(~ param, ncol = 2, scales = "free") +
  scale_x_continuous(name = "", expand = c(0.02, 0)) +
  scale_y_continuous(name = "Probability Density, f(x)",
                     expand = expansion(mult = c(0L, 0.05))) +
  scale_color_manual(name = "Point Estimate", values = c(Median = "#D55E00")) +
  labs(caption = "95% Equal-Tailed Interval") +
  theme_bw() +
  theme(plot.margin = margin(c(4, 15, -8, 4)),
        plot.caption = element_text(hjust = 0.5, vjust = 7),
        legend.position = "bottom",
        legend.title = element_text(size = 10),
        legend.text = element_text(size = 10),
        legend.box.margin = margin(c(-30, 6, 6, 6)),
        legend.background = element_rect(fill = "transparent"))

## End(Not run)

misty documentation built on Oct. 24, 2024, 5:10 p.m.

Related to blimp.plot in misty...