View source: R/Westerlund.R View source: R/Westerlund.R
| plot.westerlund_test | R Documentation |
This function provides an S3 method for visualizing objects of class westerlund_test.
It creates a faceted ggplot2 visualization of the bootstrap distributions for
the four Westerlund (2007) panel cointegration statistics: G_t, G_a,
P_t, and P_a. The plot displays the kernel density of bootstrap
replications, the observed test statistic as a solid line, and the left-tail
bootstrap critical value as a dashed line.
## S3 method for class 'westerlund_test'
plot(
x,
title = "Westerlund Test: Bootstrap Distributions",
conf_level = 0.05,
colors = list(
obs = "#D55E00",
crit = "#0072B2",
fill = "grey80",
density = "grey30"
),
lwd = list(obs = 1, crit = 0.8, density = 0.5),
show_grid = TRUE,
...
)
x |
An object of class |
title |
Character. The main title of the plot. |
conf_level |
Numeric in (0,1). Left-tail quantile used as the critical value.
For example, |
colors |
A named list of colors for plot elements. Expected elements include
|
lwd |
A named list of line widths or sizes for the |
show_grid |
Logical. If |
... |
Additional arguments passed to or from other methods. |
The function converts the bootstrap distribution matrix into a long-format data
frame to utilize the faceting capabilities of the ggplot2 package. This
implementation uses ggplot2::geom_density() for empirical distribution
estimation and arranges the four subplots into a 2x2 grid. By using
facet_wrap(~Statistic, scales = "free"), the plots maintain independent
x-axis scales to better visualize the specific range of each statistic.
Text annotations are automatically added to each facet to display the exact
numerical values of the Observed Stat and the Critical Value for quick reference.
The dashed line represents the bootstrap critical value for a left-tailed test.
If the observed statistic, represented by the solid line, is located to the left
of the dashed line, it indicates a rejection of the null hypothesis of no
cointegration at the specified conf_level significance level. This
visual approach allows for an immediate assessment of the test results relative
to the empirically derived distribution.
The function requires the ggplot2, tidyr, and scales packages
to be installed. Because it is an S3 method, it is typically invoked by calling
plot() directly on the result object of a Westerlund test.
Returns a ggplot object. This allows the user to apply further
customizations, such as changing themes with + theme_bw() or adding
additional layers and labels.
Westerlund's pooled statistics can be sensitive to nuisance parameters and cross-sectional dependence in finite samples. Visualizing the bootstrap density provides a more robust reference than asymptotic normal curves, offering a clearer picture of the distribution under the null hypothesis.
Because the function returns a ggplot object, users can modify the
output easily. For example, the theme can be changed by adding a theme
layer, such as plot(res) + ggplot2::theme_dark().
Westerlund, J. (2007). Testing for error correction in panel data. Oxford Bulletin of Economics and Statistics, 69(6), 709–748.
westerlund_test,
WesterlundBootstrap,
DisplayWesterlund
## Example: generating a mock westerlund_test object
set.seed(123)
mock_res <- list(
bootstrap_distributions = cbind(
Gt = rnorm(399, mean = -2, sd = 1),
Ga = rnorm(399, mean = -8, sd = 3),
Pt = rnorm(399, mean = -2, sd = 1),
Pa = rnorm(399, mean = -6, sd = 3)
),
test_stats = list(Gt = -3.1, Ga = -10.2, Pt = -2.7, Pa = -7.8)
)
class(mock_res) <- "westerlund_test"
## Use the S3 plot method
p <- plot(mock_res)
## Display plot
if (interactive()) print(p)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.