Learn R Programming

BNPmix (version 0.2.5)

plot.BNPdens: Density plot for BNPdens class

Description

Extension of the plot method to the BNPdens class. The method plot.BNPdens returns suitable plots for a BNPdens object. See details.

Usage

# S3 method for BNPdens
plot(x, dimension = c(1, 2), col = "#0037c4",
  show_points = F, show_hist = F, show_clust = F, bin_size = NULL,
  wrap_dim = NULL, xlab = "", ylab = "", band = T,
  conf_level = c(0.025, 0.975), ...)

Arguments

x

an object of class BNPdens;

dimension

if x is the output of a model fitted to multivariate data, dimensions specifies the two dimensions for the bivariate contour plot (if they are equal, a marginal univarite plot is returned);

col

the color of the lines;

show_points

if TRUE, the function plots also the observations, default FALSE;

show_hist

if TRUE, and the model is univariate, the function plots also the histogram of the data, default FALSE;

show_clust

if TRUE the function plots also the points colored with respect to the estimated partition, default FALSE;

bin_size

if show_hist = TRUE, it correponds to the size of each bin, default range(data) / 30;

wrap_dim

bivariate vector, if x is the output of DDPdensity, it correponds to the number of rows and columns in the plot. Default c(ngroup, 1);

xlab

label of the horizontal axis;

ylab

label of the vertical axis;

band

if TRUE and x is the output of a univariate model or of DDPdensity, the plot method displays quantile-based posterior credible bands along with estimated densities;

conf_level

bivariate vector, order of the quantiles for the posterior credible bands. Default c(0.025, 0.975);

...

additional arguments to be passed.

Value

A ggplot2 object.

Details

If the BNPdens object is generated by PYdensity, the function returns the univariate or bivariate estimated density plot. If x is a BNPdens object generated by PYregression, the function returns the scatterplot of the data colored as in the estimated partition. If x is a BNPdens object generated by DDPdensity, the function returns a wrapped plot with one density per group. The plots can be enhanced in several ways: for univariate densities, if show_hist = TRUE, the plot shows also the histogram of the data; if show_points = TRUE, the plot shows also the observed points along the x-axis; if show_points = TRUE and show_clust = TRUE, the points are colored according to the partition estimated with the partition function. For multivariate densities: if show_points = TRUE, the plot shows also the scatterplot of the data; if show_points = TRUE and show_clust = TRUE, the points are colored according to the estimated partition.

Examples

Run this code
# NOT RUN {
# PYdensity example
data_toy <- c(rnorm(100, -3, 1), rnorm(100, 3, 1))
grid <- seq(-7, 7, length.out = 50)
est_model <- PYdensity(y = data_toy,
 mcmc = list(niter = 200, nburn = 100, nupd = 100),
 output = list(grid = grid))
class(est_model)
plot(est_model)

# PYregression example
x_toy <- c(rnorm(100, 3, 1), rnorm(100, 3, 1))
y_toy <- c(x_toy[1:100] * 2 + 1, x_toy[101:200] * 6 + 1) + rnorm(200, 0, 1)
grid_x <- c(0, 1, 2, 3, 4, 5)
grid_y <- seq(0, 35, length.out = 50)
est_model <- PYregression(y = y_toy, x = x_toy,
mcmc = list(niter = 200, nburn = 100),
output = list(grid_x = grid_x, grid_y = grid_y))
summary(est_model)
plot(est_model)

# DDPdensity example
data_toy <- c(rnorm(50, -4, 1), rnorm(100, 0, 1), rnorm(50, 4, 1))
group_toy <- c(rep(1,100), rep(2,100))
grid <- seq(-7, 7, length.out = 50)
est_model <- DDPdensity(y = data_toy, group = group_toy,
mcmc = list(niter = 200, nburn = 100, napprox_unif = 50),
output = list(grid = grid))
summary(est_model)
plot(est_model)


# }

Run the code above in your browser using DataLab